Browse Source

:tada: Ajout fichiers gestion game

Arthur Brandao 6 years ago
parent
commit
1f2896009d
4 changed files with 120 additions and 1 deletions
  1. 1 0
      Serveur/constante.h
  2. 64 0
      Serveur/game.c
  3. 52 0
      Serveur/game.h
  4. 3 1
      Serveur/makefile

+ 1 - 0
Serveur/constante.h

@@ -34,6 +34,7 @@
 #define NBERRORRESET 3 //Nombre d'erreur avant de considérer que la connexion comme fermée 
 #define MAXGAME 5 //Nombre maximum de parti autorisé en simultané
 #define MAXPLAYER 4 //Nombre max de joueur par parti
+#define MAPDIR "map/" //Chemin vers le dossier des maps
 
 /* --- Reseaux --- */
 #define PORT_UDP 18624

+ 64 - 0
Serveur/game.c

@@ -0,0 +1,64 @@
+/* 
+ * File:   game.c
+ * Author: Arthur Brandao
+ *
+ * Created on 28 novembre 2018
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "file.h"
+#include "game.h"
+
+/* --- Extern --- */
+Game game[MAXGAME];
+int nbGame = 0;
+
+/* --- Fonctions publiques --- */
+JsonArray* list_map(){
+    char** result;
+    int nbResult;
+    JsonArray* ja;
+    //Initialisation json
+    ja = malloc(sizeof(JsonArray));
+    ini_array_encoder(ja);
+    //Regarde les fichiers dans le dossier
+    result = file_list(MAPDIR, &nbResult);
+    for(int i = 0; i < nbResult; i++){
+        add_array_string(ja, result[i]);
+        free(result[i]);
+    }
+    return ja;
+}
+
+JsonArray* list_game(){
+    JsonArray* ja;
+    //Si il n' y a aucune game
+    if(nbGame == 0){
+        return NULL;
+    }
+    //Initialisation json
+    ja = malloc(sizeof(JsonArray));
+    ini_array_encoder(ja);
+    //Ajoute chaque game
+    for(int i = 0; i < nbGame; i++){
+        //Creation objet json
+        JsonEncoder je;
+        ini_encoder(&je);
+        add_string(&je, "name", game[i].name);
+        add_integer(&je, "nbPlayer", game[i].nbPlayer);
+        add_string(&je, "map", game[i].mapName);
+        //Ajout dans le tableau
+        add_array_object(ja, &je);
+        //Suppr encoder objet
+        clean_json_encoder(&je);
+    }
+    return ja;
+}
+
+/**
+ * Associe une map à une game
+ * @param Game* La game à associé à la map
+ * @param char* Le nom de la map
+ */
+void get_map(Game*, char*);

+ 52 - 0
Serveur/game.h

@@ -0,0 +1,52 @@
+/* 
+ * File:   game.h
+ * Author: Arthur Brandao
+ *
+ * Created on 28 novembre 2018
+ */
+
+#ifndef GAME_H
+#define GAME_H
+
+/* --- Include --- */
+#include "constante.h"
+#include "player.h"
+#include "json.h"
+
+/* --- Struct --- */
+typedef struct{
+    char* name; //Nom
+    int nbPlayer; //Nombre de joueur
+    char* mapName; //Nom de la map
+    int width; //Largeur de la map
+    int height; //Hauteur de la map
+    char** map; //Map
+    Player player[MAXPLAYER]; //Les joueurs actuels
+}Game;
+
+/* --- Extern --- */
+extern Game game[MAXGAME];
+extern int nbGame;
+
+/* --- Fonctions --- */
+/**
+ * Liste le nom de toutes les maps sous forme de JSON
+ * @return JsonArray* Les maps existantes
+ */
+JsonArray* list_map();
+
+/**
+ * Liste les game en cours en JSON
+ * @return JsonArray* Les games existantes
+ */
+JsonArray* list_game();
+
+/**
+ * Associe une map à une game
+ * @param Game* La game à associé à la map
+ * @param char* Le nom de la map
+ */
+void get_map(Game*, char*);
+
+#endif /* GAME_H */
+

+ 3 - 1
Serveur/makefile

@@ -3,7 +3,7 @@
 #
 
 EXEC = main test
-OBJETS = str.o json_parser.o json_encoder.o json_array.o error.o arraylist.o server_tcp.o server_udp.o bomberstudent_server.o client.o file.o handler.o player.o
+OBJETS = str.o json_parser.o json_encoder.o json_array.o error.o arraylist.o server_tcp.o server_udp.o bomberstudent_server.o client.o file.o handler.o player.o game.o
 NOM_PROJET = Projet Reseau
 
 #
@@ -109,6 +109,8 @@ file.o: file.c error.h str.h file.h constante.h
 handler.o: handler.c bomberstudent_server.h constante.h server.h json.h \
  str.h client.h handler.h
 player.o: player.c player.h constante.h client.h server.h
+game.o: game.c file.h constante.h game.h player.h client.h server.h \
+ json.h str.h
 main.o: main.c error.h bomberstudent_server.h constante.h server.h json.h \
  str.h client.h main.h handler.h
 test.o: test.c json.h str.h constante.h arraylist.h server.h error.h \