소스 검색

Tentative correction bug mine

Arthur Brandao 6 년 전
부모
커밋
cace1e25fa
2개의 변경된 파일31개의 추가작업 그리고 25개의 파일을 삭제
  1. 29 20
      Serveur/game.c
  2. 2 5
      Serveur/handler.c

+ 29 - 20
Serveur/game.c

@@ -74,12 +74,12 @@ char* unparse_map(char** map, int width, int height) {
  * @param int Y de l'objet
  * @return Player* Le joueur trouvé ou NULL
  */
-Player* search_player_object(Game* g, int type, int x, int y){
+Player* search_player_object(Game* g, int type, int x, int y) {
     obj_node* objn;
-    for(int i = 0; i < MAXPLAYER; i++){
-        if(g->player[i]->ini){
+    for (int i = 0; i < MAXPLAYER; i++) {
+        if (g->player[i]->ini) {
             objn = object_search(g->player[i]->bomb, type, x, y);
-            if(objn != NULL){
+            if (objn != NULL) {
                 return g->player[i];
             }
         }
@@ -364,7 +364,7 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
     ini_array_encoder(&chain);
     //Mutex
     pthread_mutex_lock(&gameMutex[g->index]);
-    if(playerIndex >= 0) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
+    if (playerIndex >= 0) pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
     //Determine le type de la bombe
     switch (g->map[x][y]) {
         case '1':
@@ -383,7 +383,7 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
             g->player[playerIndex]->nbBomb--;
             break;
         default:
-            if(playerIndex >= 0) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
+            if (playerIndex >= 0) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
             pthread_mutex_unlock(&gameMutex[g->index]);
             return false;
     }
@@ -441,7 +441,7 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
                     bomb_chain(g, playerIndex, x, y - 1 - i, &chain);
                 }
                 //Si un mur solide
-                if(g->map[x][y - 1 - i] == '-'){
+                if (g->map[x][y - 1 - i] == '-') {
                     break;
                 }
             } else {
@@ -488,7 +488,7 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
                     bomb_chain(g, playerIndex, x, y + 1 + i, &chain);
                 }
                 //Si un mur solide
-                if(g->map[x][y + 1 + i] == '-'){
+                if (g->map[x][y + 1 + i] == '-') {
                     break;
                 }
             } else {
@@ -535,7 +535,7 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
                     bomb_chain(g, playerIndex, x - 1 - i, y, &chain);
                 }
                 //Si un mur solide
-                if(g->map[x - 1 - i][y] == '-'){
+                if (g->map[x - 1 - i][y] == '-') {
                     break;
                 }
             } else {
@@ -582,7 +582,7 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
                     bomb_chain(g, playerIndex, x + 1 + i, y, &chain);
                 }
                 //Si un mur solide
-                if(g->map[x + 1 + i][y] == '-'){
+                if (g->map[x + 1 + i][y] == '-') {
                     break;
                 }
             } else {
@@ -610,6 +610,13 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
             }
             if (index != playerIndex) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + index]);
         }
+        //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;
     }
     //Affichage de la map
     free(g->mapContent);
@@ -620,8 +627,11 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
     add_array(json, "bonusMalus", &bonus);
     add_array(json, "chain", &chain);
     //Nettoyage
-    if(playerIndex >= 0) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
+    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;
 }
 
@@ -638,7 +648,7 @@ void bomb_chain(Game* g, int playerIndex, int x, int y, JsonArray* chain) {
         case '1':
             add_string(&json, "type", "classic");
             p = search_player_object(g, OBJ_BCLASSIC, x, y);
-            if(p != NULL){
+            if (p != NULL) {
                 p->classicBomb++;
                 p->nbBomb--;
                 objn = object_search(p->bomb, OBJ_BCLASSIC, x, y);
@@ -648,7 +658,7 @@ void bomb_chain(Game* g, int playerIndex, int x, int y, JsonArray* chain) {
         case '2':
             add_string(&json, "type", "mine");
             p = search_player_object(g, OBJ_BMINE, x, y);
-            if(p != NULL){
+            if (p != NULL) {
                 p->mine++;
                 p->nbBomb--;
                 objn = object_search(p->bomb, OBJ_BMINE, x, y);
@@ -658,7 +668,7 @@ void bomb_chain(Game* g, int playerIndex, int x, int y, JsonArray* chain) {
         case '3':
             add_string(&json, "type", "remote");
             p = search_player_object(g, OBJ_BREMOTE, x, y);
-            if(p != NULL){
+            if (p != NULL) {
                 p->remoteBomb++;
                 p->nbBomb--;
                 objn = object_search(p->bomb, OBJ_BREMOTE, x, y);
@@ -705,7 +715,7 @@ void bomb_chain(Game* g, int playerIndex, int x, int y, JsonArray* chain) {
                     bomb_chain(g, playerIndex, x, y - 1 - i, chain);
                 }
                 //Si un mur solide
-                if(g->map[x][y - 1 - i] == '-'){
+                if (g->map[x][y - 1 - i] == '-') {
                     break;
                 }
             } else {
@@ -741,7 +751,7 @@ void bomb_chain(Game* g, int playerIndex, int x, int y, JsonArray* chain) {
                     bomb_chain(g, playerIndex, x, y + 1 + i, chain);
                 }
                 //Si un mur solide
-                if(g->map[x][y + 1 + i] == '-'){
+                if (g->map[x][y + 1 + i] == '-') {
                     break;
                 }
             } else {
@@ -777,7 +787,7 @@ void bomb_chain(Game* g, int playerIndex, int x, int y, JsonArray* chain) {
                     bomb_chain(g, playerIndex, x - 1 - i, y, chain);
                 }
                 //Si un mur solide
-                if(g->map[x - 1 - i][y] == '-'){
+                if (g->map[x - 1 - i][y] == '-') {
                     break;
                 }
             } else {
@@ -813,15 +823,14 @@ void bomb_chain(Game* g, int playerIndex, int x, int y, JsonArray* chain) {
                     bomb_chain(g, playerIndex, x + 1 + i, y, chain);
                 }
                 //Si un mur solide
-                if(g->map[x + 1 + i][y] == '-'){
+                if (g->map[x + 1 + i][y] == '-') {
                     break;
                 }
             } else {
                 break;
             }
         }
-    }        
-    //Si c'est une mine
+    }        //Si c'est une mine
     else {
         g->map[x][y] = '_';
         //Recup le joueur

+ 2 - 5
Serveur/handler.c

@@ -444,11 +444,8 @@ int handler_player_move(int cliId, JsonParser* json) {
         if (mine) {
             JsonEncoder notif;
             ini_encoder(&notif);
-            if(bomb_explode(&game[index], -1, x, y, &notif)){
-                //Notification joueurs
-                if(!notify_player(&game[index], "POST", "attack/explose", &notif, -1)){
-                    clean_json_encoder(&notif);
-                }
+            if(!bomb_explode(&game[index], -1, x, y, &notif)){
+                adderror("Erreur explosion mine");
             }
             clean_json_encoder(&notif);
         }