Jelajahi Sumber

Meilleur gestion de la deconnexion

Arthur Brandao 6 tahun lalu
induk
melakukan
5174f0dc87
3 mengubah file dengan 33 tambahan dan 2 penghapusan
  1. 7 2
      Serveur/game.c
  2. 2 0
      Serveur/game.h
  3. 24 0
      Serveur/handler.c

+ 7 - 2
Serveur/game.c

@@ -4,6 +4,7 @@
  *
  * Created on 28 novembre 2018
  */
+#define _XOPEN_SOURCE 500
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -143,12 +144,11 @@ int create_game(char* name, char* map){
     //Recup la map
     length = strlen(MAPDIR) + strlen(map);
     path = new_string(length);
-    sprintf(path, "%s%s", MAPDIR, map);
+    snprintf(path, length + 1, "%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;
@@ -252,6 +252,11 @@ boolean notify_player(Game* g, char* method, char* ressource, JsonEncoder* param
     return res;
 }
 
+void remove_player(Game* g, int playerIndex){
+    g->nbPlayer--;
+    delete_player(g->player[playerIndex]);
+}
+
 void stop_game(Game* g){
     //Indique comme inactive
     g->active = false;

+ 2 - 0
Serveur/game.h

@@ -67,6 +67,8 @@ JsonEncoder* describe_game(Game*, int);
 
 boolean notify_player(Game*, char*, char*, JsonEncoder*, int);
 
+void remove_player(Game*, int);
+
 void stop_game(Game*);
 
 void clean_games();

+ 24 - 0
Serveur/handler.c

@@ -78,7 +78,31 @@ void ini_handler(){
 }
 
 int handler_client_end(int cliId, JsonParser* json){
+    boolean find = false;
+    JsonEncoder* notif;
     printf("Deconnexion du client %d\n", cliId);
+    //Cherche le client dans les parties
+    for(int i = 0; i < MAXGAME; i++){
+        if(game[i].active){
+            for(int j = 0; j < MAXPLAYER; j++){
+                if(game[i].player[j]->ini && game[i].player[j]->id == cliId){
+                    find = true;
+                    remove_player(&game[i], j);
+                    //Avertit les autres joueurs
+                    notif = malloc(sizeof(JsonEncoder));
+                    ini_encoder(notif);
+                    add_integer(notif, "id", cliId);
+                    notify_player(&game[i], "POST", "game/playerquit", notif, cliId);
+                    clean_json_encoder(notif);
+                    free(notif);
+                    break;
+                }
+            }
+            if(find){
+                break;
+            }
+        }
+    }
     remove_client(cliId);
     return SUCCESS;
 }