瀏覽代碼

:sparkles: Creation et destruction des parties

Arthur Brandao 6 年之前
父節點
當前提交
172132aff7
共有 7 個文件被更改,包括 128 次插入20 次删除
  1. 95 6
      Serveur/game.c
  2. 7 7
      Serveur/game.h
  3. 3 0
      Serveur/main.c
  4. 6 6
      Serveur/makefile
  5. 1 1
      Serveur/player.c
  6. 1 0
      Serveur/player.h
  7. 15 0
      Serveur/test.c

+ 95 - 6
Serveur/game.c

@@ -7,6 +7,8 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include "error.h"
+#include "str.h"
 #include "file.h"
 #include "game.h"
 
@@ -14,6 +16,31 @@
 Game game[MAXGAME];
 int nbGame = 0;
 
+/* --- Fonctions privées --- */
+/**
+ * Transforme le fichier map en un tableau 2D
+ * @param char* Le contenu du fichier map
+ * @param int Largeur de la map
+ * @param int Hauteur de la map
+ * @return char** La map parser
+ */
+char** parse_map(char* mapContent, int width, int height){
+    char** map = malloc(sizeof(char*) * width);
+    //Creation des colonnes
+    for(int i = 0; i < width; i++){
+        map[i] = malloc(sizeof(char) * height);
+    }
+    //Remplissage
+    for(int i = 0; i < height; i++){
+        for(int j = 0; j < width; j++){
+            map[j][i] = *mapContent;
+            mapContent++;
+        }
+        mapContent++;
+    }
+    return map;
+}
+
 /* --- Fonctions publiques --- */
 void ini_games(){
     for(int i = 0; i < MAXGAME; i++){
@@ -95,9 +122,71 @@ int* map_size(char* map){
     return res;
 }
 
-/**
- * Associe une map à une game
- * @param Game* La game à associé à la map
- * @param char* Le nom de la map
- */
-void get_map(Game*, char*);
+int create_game(char* name, char* map){
+    int length, index = 0;
+    char* path;
+    int* size;
+    //Regarde si il reste une game de disponnible
+    if(nbGame == MAXGAME){
+        return ERR;
+    }
+    //Cherche une game libre
+    while(index < MAXGAME){
+        if(game[index].active == false){
+            break;
+        }
+        index++;
+    }
+    //Recup la map
+    length = strlen(MAPDIR) + strlen(map);
+    path = new_string(length);
+    sprintf(path, "%s%s", MAPDIR, map);
+    game[index].mapContent = file_get_content(path);
+    //Calcul taille de la map
+    size = map_size(game[index].mapContent);
+    //Set Up
+    length = strlen(name) + 1;
+    game[index].active = true;
+    game[index].name = string_copy(name);
+    game[index].nbPlayer = 0;
+    game[index].mapName = string_copy(map);
+    game[index].width = size[WIDTH];
+    game[index].height = size[HEIGHT];
+    game[index].map = parse_map(game[index].mapContent, size[WIDTH], size[HEIGHT]);
+    for(int i = 0; i < MAXPLAYER; i++){
+        game[index].player[i] = malloc(sizeof(Player));
+        game[index].player[i]->ini = false;
+    }
+    //Retourne la position de la partie dans le tableau
+    nbGame++;
+    free(path);
+    free(size);
+    return index;
+}
+
+void stop_game(Game* g){
+    //Indique comme inactive
+    g->active = false;
+    //Suppr les joueurs
+    for(int i = 0; i < MAXPLAYER; i++){
+        if(g->player[i]->ini){
+            delete_player(g->player[i]);
+        }
+        free(g->player[i]);
+    }
+    //Libere la memoire
+    for(int i = 0; g->width; i++){
+        free(g->map[i]);
+    }
+    free(g->map);
+    free(g->mapName);
+    free(g->mapContent);
+}
+
+void clean_games(){
+    for(int i = 0; i < MAXGAME; i++){
+        if(game[i].active){
+            stop_game(&game[i]);
+        }
+    }
+}

+ 7 - 7
Serveur/game.h

@@ -23,10 +23,11 @@ typedef struct{
     char* name; //Nom
     int nbPlayer; //Nombre de joueur
     char* mapName; //Nom de la map
+    char* mapContent; //La map en string
     int width; //Largeur de la map
     int height; //Hauteur de la map
     char** map; //Map
-    Player player[MAXPLAYER]; //Les joueurs actuels
+    Player* player[MAXPLAYER]; //Les joueurs actuels
 }Game;
 
 /* --- Extern --- */
@@ -58,12 +59,11 @@ JsonArray* list_game();
  */
 int* map_size(char*);
 
-/**
- * Associe une map à une game
- * @param Game* La game à associé à la map
- * @param char* Le nom de la map
- */
-void get_map(Game*, char*);
+int create_game(char*, char*);
+
+void stop_game(Game*);
+
+void clean_games();
 
 #endif /* GAME_H */
 

+ 3 - 0
Serveur/main.c

@@ -32,6 +32,9 @@ void handler(int sig){
         //Avertit les clients
         printf("Notifie les clients de l'arret du serveur\n");
         notify_close(END_USER);
+        clean_clientlist();
+        //Stop les parties
+        clean_games();
     }
 }
 

+ 6 - 6
Serveur/makefile

@@ -106,12 +106,12 @@ bomberstudent_server.o: bomberstudent_server.c arraylist.h constante.h \
  server.h json.h str.h bomberstudent_server.h client.h error.h
 client.o: client.c client.h constante.h server.h
 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
+handler.o: handler.c error.h bomberstudent_server.h constante.h server.h \
+ json.h str.h client.h game.h player.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
+game.o: game.c error.h str.h file.h constante.h game.h player.h client.h \
+ server.h json.h
 main.o: main.c error.h bomberstudent_server.h constante.h server.h json.h \
- str.h client.h main.h handler.h
+ str.h client.h main.h handler.h game.h player.h
 test.o: test.c json.h str.h constante.h arraylist.h server.h error.h \
- bomberstudent_server.h client.h file.h
+ bomberstudent_server.h client.h file.h game.h player.h

+ 1 - 1
Serveur/player.c

@@ -33,5 +33,5 @@ void create_player(Player* p, Client* c){
 }
 
 void delete_player(Player* p){
-    //Rien a faire actuellement
+    p->cli = NULL;
 }

+ 1 - 0
Serveur/player.h

@@ -14,6 +14,7 @@
 
 /* --- Structure --- */
 typedef struct{
+    boolean ini; //Indique si initalisé
     /* Stats basique */
     int id; //Id du joueur <=> id du Client
     Client* cli; //Client pour communiquer avec le joueur

+ 15 - 0
Serveur/test.c

@@ -234,12 +234,27 @@ int array_parse(){
 int size(){
     char* content;
     int* size;
+    //char** map;
     
     content = file_get_content("map/map1");
     size = map_size(content);
     printf("Width : %d\n", size[WIDTH]);
     printf("Height : %d\n", size[HEIGHT]);
     
+    /*map = parse_map(content, size[WIDTH], size[HEIGHT]);
+    printf("[1, 2] %c\n", map[1][2]);
+    for(int i = 0; i < size[HEIGHT]; i++){
+        for(int j = 0; j < size[WIDTH]; j++){
+            printf("%c", map[j][i]);
+        }
+        
+        printf("\n");
+    }    
+    
+    for(int i = 0; i < size[WIDTH]; i++){
+        free(map[i]);
+    }
+    free(map);*/
     free(content);
     free(size);