Quellcode durchsuchen

:bug: Correction crash mine

Arthur Brandao vor 6 Jahren
Ursprung
Commit
5c741a3a56
2 geänderte Dateien mit 29 neuen und 33 gelöschten Zeilen
  1. 14 25
      Serveur/game.c
  2. 15 8
      Serveur/handler.c

+ 14 - 25
Serveur/game.c

@@ -589,34 +589,23 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
                 break;
             }
         }
-    }        
-    //Si c'est une mine
+    }
+        //Si c'est une mine
     else {
         g->map[x][y] = '_';
-        //Pas de bonus pour une mine
-        add_boolean(json, "bonus", false);
-        //Recup le joueur
-        if (player_collision_index(g, x, y, &index)) {
-            if (index != playerIndex) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + index]);
-            if (g->player[index]->major == 0) {
-                //Baisse la vie
-                g->player[index]->life = g->player[index]->life - (g->player[index]->maxLife * DAMAGEMINE);
-                //Notification du joueur
-                describe_player(g->player[index], &notif);
-                if (!notify_client(g->player[index]->cli, "POST", "attack/affect", &notif)) {
-                    adderror("Impossible de notifer le client");
-                }
-                clean_json_encoder(&notif);
+        //Calcul dommage
+        if (g->player[playerIndex]->major == 0) {
+            //Baisse la vie
+            g->player[playerIndex]->life = g->player[playerIndex]->life - (g->player[playerIndex]->maxLife * DAMAGEMINE);
+            //Notification du joueur
+            describe_player(g->player[playerIndex], &notif);
+            if (!notify_client(g->player[playerIndex]->cli, "POST", "attack/affect", &notif)) {
+                adderror("Impossible de notifer le client");
             }
-            if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
+            clean_json_encoder(&notif);
         }
-        //Nettoyage
-        if (playerIndex >= 0) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
-        pthread_mutex_unlock(&gameMutex[g->index]);
-        clean_json_array(&bomb);
-        clean_json_array(&bonus);
-        clean_json_array(&chain);
-        return true;
+        //Bas de bonus si mine
+        add_boolean(json, "bonus", false);
     }
     //Affichage de la map
     free(g->mapContent);
@@ -830,7 +819,7 @@ void bomb_chain(Game* g, int playerIndex, int x, int y, JsonArray* chain) {
                 break;
             }
         }
-    }        //Si c'est une mine
+    }//Si c'est une mine
     else {
         g->map[x][y] = '_';
         //Recup le joueur

+ 15 - 8
Serveur/handler.c

@@ -444,8 +444,15 @@ int handler_player_move(int cliId, JsonParser* json) {
         if (mine) {
             JsonEncoder notif;
             ini_encoder(&notif);
-            if(!bomb_explode(&game[index], -1, x, y, &notif)){
-                adderror("Erreur explosion mine");
+            if (bomb_explode(&game[index], playerIndex, x, y, &notif)) {
+                //Notification joueurs
+                if (!notify_player(&game[index], "POST", "attack/explose", &notif, -1)) {
+                    adderror("Impossible de notifier les joueurs");
+                    clean_json_encoder(&notif);
+                    clean_json_encoder(&reponse);
+                    free(move);
+                    return FAIL;
+                }
             }
             clean_json_encoder(&notif);
         }
@@ -635,7 +642,7 @@ int handler_attack_bomb(int cliId, JsonParser* json) {
             p->classicBomb--;
             game[index].map[x][y] = '1';
             //Lance timer
-            int* tab = malloc(sizeof(int) * 2);
+            int* tab = malloc(sizeof (int) * 2);
             tab[0] = x;
             tab[1] = y;
             //Ajout bombe
@@ -719,7 +726,7 @@ int handler_attack_bomb(int cliId, JsonParser* json) {
     return SUCCESS;
 }
 
-int handler_attack_remote_go(int cliId, JsonParser* json){
+int handler_attack_remote_go(int cliId, JsonParser* json) {
     int index, playerIndex;
     JsonEncoder notif;
     Player* p = NULL;
@@ -747,15 +754,15 @@ int handler_attack_remote_go(int cliId, JsonParser* json){
     }
     //Explose toutes les remotes du joueur
     objn = p->bomb->first;
-    while(objn != NULL){
+    while (objn != NULL) {
         tmp = objn->next;
-        if(objn->type == OBJ_BREMOTE){
+        if (objn->type == OBJ_BREMOTE) {
             //Retire mutex gérés dans fonctions
             pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
             pthread_mutex_unlock(&gameMutex[index]);
-            if(bomb_explode(&game[index], playerIndex, objn->x, objn->y, &notif)){
+            if (bomb_explode(&game[index], playerIndex, objn->x, objn->y, &notif)) {
                 //Notification joueurs
-                if(!notify_player(&game[index], "POST", "attack/explose", &notif, -1)){
+                if (!notify_player(&game[index], "POST", "attack/explose", &notif, -1)) {
                     adderror("Impossible de notifier le joueur de l'explosion");
                 }
                 clean_json_encoder(&notif);