瀏覽代碼

Ajout explosion remote

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

+ 0 - 1
Serveur/game.c

@@ -332,7 +332,6 @@ boolean player_collision_index(Game* g, int x, int y, int* index) {
 }
 
 boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json) {
-    printf("Explode\n");
     JsonEncoder object, notif;
     JsonArray bomb, bonus, chain;
     int res, index, cBomb = 0, cBonus = 0, cChain = 0;

+ 57 - 2
Serveur/handler.c

@@ -121,6 +121,7 @@ void ini_handler() {
     add_handler("POST", "player/move", handler_player_move);
     add_handler("POST", "object/new", handler_object_new);
     add_handler("POST", "attack/bomb", handler_attack_bomb);
+    add_handler("POST", "attack/remote/go", handler_attack_remote_go);
 }
 
 int handler_client_end(int cliId, JsonParser* json) {
@@ -635,12 +636,10 @@ int handler_attack_bomb(int cliId, JsonParser* json) {
             p->classicBomb--;
             game[index].map[x][y] = '1';
             //Lance timer
-            printf("Ici\n");
             int* tab = malloc(sizeof(int) * 2);
             tab[0] = x;
             tab[1] = y;
             //Timer
-            printf("Delay\n");
             delay_data(TIMEBOMB, index, playerIndex, (void*) tab, callback_bomb_explode);
         }
     } else if (strncmp(class, "mine", length) == 0) {
@@ -657,6 +656,7 @@ int handler_attack_bomb(int cliId, JsonParser* json) {
             p->remoteBomb--;
             game[index].map[x][y] = '3';
             //Ajoute la bombe dans la liste du joueur
+            object_add(p->bomb, OBJ_BREMOTE, x, y);
         }
     } else {
         pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
@@ -712,4 +712,59 @@ int handler_attack_bomb(int cliId, JsonParser* json) {
     free(pos);
     free(class);
     return SUCCESS;
+}
+
+int handler_attack_remote_go(int cliId, JsonParser* json){
+    int index, playerIndex;
+    JsonEncoder notif;
+    Player* p = NULL;
+    obj_node* objn, *tmp;
+    //Charche la game
+    index = search_client_game(cliId);
+    //Ini json
+    ini_encoder(&notif);
+    //Recup le joueur
+    pthread_mutex_lock(&gameMutex[index]);
+    for (int i = 0; i < MAXPLAYER; i++) {
+        pthread_mutex_lock(&playerMutex[(index * MAXPLAYER) + i]);
+        if (game[index].player[i]->ini && game[index].player[i]->id == cliId) {
+            playerIndex = i;
+            p = game[index].player[i];
+            break;
+        }
+        pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
+    }
+    if (p == NULL) {
+        pthread_mutex_unlock(&gameMutex[index]);
+        send_err_client(cliId, EREQUEST);
+        adderror("Aucun joueur associé au client");
+        return FAIL;
+    }
+    //Explose toutes les remotes du joueur
+    objn = p->bomb->first;
+    while(objn != NULL){
+        tmp = objn->next;
+        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)){
+                //Notification joueurs
+                if(!notify_player(&game[index], "POST", "attack/explose", &notif, -1)){
+                    adderror("Impossible de notifier le joueur de l'explosion");
+                }
+                clean_json_encoder(&notif);
+            }
+            //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;
+        }
+    }
+    //Nettoyage
+    pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
+    pthread_mutex_unlock(&gameMutex[index]);
+    return SUCCESS;
 }

+ 2 - 0
Serveur/handler.h

@@ -47,5 +47,7 @@ int handler_object_new(int, JsonParser*);
 
 int handler_attack_bomb(int, JsonParser*);
 
+int handler_attack_remote_go(int, JsonParser*);
+
 #endif /* HANDLER_H */