瀏覽代碼

:bug: Debug gestion des compteurs de bombes des joueurs

Arthur Brandao 6 年之前
父節點
當前提交
0f4a04f3f8
共有 3 個文件被更改,包括 53 次插入4 次删除
  1. 47 0
      Serveur/game.c
  2. 5 3
      Serveur/handler.c
  3. 1 1
      Serveur/player.h

+ 47 - 0
Serveur/game.c

@@ -66,6 +66,27 @@ char* unparse_map(char** map, int width, int height) {
     return mapContent;
 }
 
+/**
+ * Cherche le joueur à qui appartient un objet
+ * @param Game* La game du joueur
+ * @param int Le type de l'objet
+ * @param int X de l'objet
+ * @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){
+    obj_node* objn;
+    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){
+                return g->player[i];
+            }
+        }
+    }
+    return NULL;
+}
+
 /* --- Fonctions publiques --- */
 boolean ini_games() {
     //Ini les games
@@ -350,14 +371,17 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
         case '1':
             add_string(json, "type", "classic");
             g->player[playerIndex]->classicBomb++;
+            g->player[playerIndex]->nbBomb--;
             break;
         case '2':
             add_string(json, "type", "mine");
             g->player[playerIndex]->mine++;
+            g->player[playerIndex]->nbBomb--;
             break;
         case '3':
             add_string(json, "type", "remote");
             g->player[playerIndex]->remoteBomb++;
+            g->player[playerIndex]->nbBomb--;
             break;
         default:
             if(playerIndex >= 0) pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + playerIndex]);
@@ -605,6 +629,8 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
 void bomb_chain(Game* g, int playerIndex, int x, int y, JsonArray* chain) {
     JsonEncoder json, notif;
     int index;
+    Player* p;
+    obj_node* objn = NULL;
     //Inie json
     ini_encoder(&json);
     ini_encoder(&notif);
@@ -612,12 +638,33 @@ void bomb_chain(Game* g, int playerIndex, int x, int y, JsonArray* chain) {
     switch (g->map[x][y]) {
         case '1':
             add_string(&json, "type", "classic");
+            p = search_player_object(g, OBJ_BCLASSIC, x, y);
+            if(p != NULL){
+                p->classicBomb++;
+                p->nbBomb--;
+                objn = object_search(p->bomb, OBJ_BCLASSIC, x, y);
+                object_delete(p->bomb, objn);
+            }
             break;
         case '2':
             add_string(&json, "type", "mine");
+            p = search_player_object(g, OBJ_BMINE, x, y);
+            if(p != NULL){
+                p->mine++;
+                p->nbBomb--;
+                objn = object_search(p->bomb, OBJ_BMINE, x, y);
+                object_delete(p->bomb, objn);
+            }
             break;
         case '3':
             add_string(&json, "type", "remote");
+            p = search_player_object(g, OBJ_BREMOTE, x, y);
+            if(p != NULL){
+                p->remoteBomb++;
+                p->nbBomb--;
+                objn = object_search(p->bomb, OBJ_BREMOTE, x, y);
+                object_delete(p->bomb, objn);
+            }
             break;
         default:
             return;

+ 5 - 3
Serveur/handler.c

@@ -639,6 +639,8 @@ int handler_attack_bomb(int cliId, JsonParser* json) {
             int* tab = malloc(sizeof(int) * 2);
             tab[0] = x;
             tab[1] = y;
+            //Ajout bombe
+            object_add(p->bomb, OBJ_BCLASSIC, x, y);
             //Timer
             delay_data(TIMEBOMB, index, playerIndex, (void*) tab, callback_bomb_explode);
         }
@@ -648,6 +650,8 @@ int handler_attack_bomb(int cliId, JsonParser* json) {
             p->nbBomb++;
             p->mine--;
             game[index].map[x][y] = '2';
+            //Ajout bombe
+            object_add(p->bomb, OBJ_BMINE, x, y);
         }
     } else if (strncmp(class, "remote", length) == 0) {
         ok = p->remoteBomb > 0 && p->nbBomb < p->maxBomb && game[index].map[x][y] == '_' && !player_collision(&game[index], x, y);
@@ -758,10 +762,8 @@ int handler_attack_remote_go(int cliId, JsonParser* json){
             //Remet mutex
             pthread_mutex_lock(&gameMutex[index]);
             pthread_mutex_lock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
-            //Supprime la bombe de la liste
-            object_delete(p->bomb, objn);
-            objn = tmp;
         }
+        objn = tmp;
     }
     //Nettoyage
     pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);

+ 1 - 1
Serveur/player.h

@@ -39,7 +39,7 @@ typedef struct{
     int lifeMax;
     int lifeUp;
     int major;
-    /* Les remote bomb posées par le joueur */
+    /* Les bombes posées par le joueur */
     Object* bomb;
 }Player;