Răsfoiți Sursa

Explosion des mines

Arthur Brandao 6 ani în urmă
părinte
comite
5819f10e0f
4 a modificat fișierele cu 46 adăugiri și 20 ștergeri
  1. 35 10
      Serveur/game.c
  2. 9 8
      Serveur/handler.c
  3. 1 1
      Serveur/player.c
  4. 1 1
      Serveur/player.h

+ 35 - 10
Serveur/game.c

@@ -46,6 +46,26 @@ char** parse_map(char* mapContent, int width, int height) {
     return map;
 }
 
+/**
+ * Transforme un tableau 2D en une chaine
+ * @param char** Le contenu du fichier map
+ * @param int Largeur de la map
+ * @param int Hauteur de la map
+ * @return char* La map
+ */
+char* unparse_map(char** map, int width, int height) {
+    int pos = 0;
+    char* mapContent = malloc(sizeof (char) * (width * height + 1));
+    memset(mapContent, 0, width * height + 1);
+    //Remplissage
+    for (int i = 0; i < height; i++) {
+        for (int j = 0; j < width; j++) {
+            mapContent[pos++] = map[j][i];
+        }
+    }
+    return mapContent;
+}
+
 /* --- Fonctions publiques --- */
 boolean ini_games() {
     //Ini les games
@@ -316,6 +336,7 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
     JsonEncoder object, notif;
     JsonArray bomb, bonus, chain;
     int res, index, cBomb = 0, cBonus = 0, cChain = 0;
+    char* map;
     //Inie json
     ini_encoder(&object);
     ini_encoder(&notif);
@@ -324,7 +345,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]);
-    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':
@@ -337,7 +358,7 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
             add_string(json, "type", "remote");
             break;
         default:
-            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;
     }
@@ -346,15 +367,15 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
     memset(pos, 0, 20);
     snprintf(pos, 20, "%d,%d", x, y);
     add_string(json, "pos", pos);
-    //Si le joueur à un bonus
-    if (g->player[playerIndex]->firePower > 0) {
-        add_boolean(json, "bonus", true);
-    } else {
-        add_boolean(json, "bonus", false);
-    }
     //Calcul l'explosion non mine
     if (g->map[x][y] != '2') {
         g->map[x][y] = '_';
+        //Si le joueur à un bonus
+        if (g->player[playerIndex]->firePower > 0) {
+            add_boolean(json, "bonus", true);
+        } else {
+            add_boolean(json, "bonus", false);
+        }
         //Vers le haut
         for (int i = 0; i < 2 + g->player[playerIndex]->firePower; i++) {
             //Si on est dans la map
@@ -519,6 +540,8 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
     //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]);
@@ -536,14 +559,16 @@ boolean bomb_explode(Game* g, int playerIndex, int x, int y, JsonEncoder* json)
         }
     }
     //Affichage de la map
-    
+    map = unparse_map(g->map, g->width, g->height);
+    add_string(json, "map", map);
     //Ajout des json
     add_array(json, "bomb", &bomb);
     add_array(json, "bonusMalus", &bonus);
     add_array(json, "chain", &chain);
     //Nettoyage
-    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]);
+    free(map);
     return true;
 }
 

+ 9 - 8
Serveur/handler.c

@@ -382,7 +382,6 @@ int handler_player_move(int cliId, JsonParser* json) {
             p->y--;
             //Si marche sur une mine
             if (game[index].map[p->x][p->y] == '2') {
-                game[index].map[p->x][p->y] = '_';
                 mine = true;
                 x = p->x;
                 y = p->y;
@@ -395,7 +394,6 @@ int handler_player_move(int cliId, JsonParser* json) {
             p->y++;
             //Si marche sur une mine
             if (game[index].map[p->x][p->y] == '2') {
-                game[index].map[p->x][p->y] = '_';
                 mine = true;
                 x = p->x;
                 y = p->y;
@@ -408,7 +406,6 @@ int handler_player_move(int cliId, JsonParser* json) {
             p->x--;
             //Si marche sur une mine
             if (game[index].map[p->x][p->y] == '2') {
-                game[index].map[p->x][p->y] = '_';
                 mine = true;
                 x = p->x;
                 y = p->y;
@@ -421,7 +418,6 @@ int handler_player_move(int cliId, JsonParser* json) {
             p->x++;
             //Si marche sur une mine
             if (game[index].map[p->x][p->y] == '2') {
-                game[index].map[p->x][p->y] = '_';
                 mine = true;
                 x = p->x;
                 y = p->y;
@@ -444,7 +440,15 @@ int handler_player_move(int cliId, JsonParser* json) {
         notify_player(&game[index], "POST", "player/position/update", &reponse, -1);
         //Si marche sur une mine
         if (mine) {
-            printf("Mine %d %d", x, y);
+            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);
+                }
+            }
+            clean_json_encoder(&notif);
         }
     }
     //Nettoyage
@@ -635,8 +639,6 @@ int handler_attack_bomb(int cliId, JsonParser* json) {
             int* tab = malloc(sizeof(int) * 2);
             tab[0] = x;
             tab[1] = y;
-            //Ajoute bombe
-            object_add(p->bomb, OBJ_BCLASSIC, x, y);
             //Timer
             printf("Delay\n");
             delay_data(TIMEBOMB, index, playerIndex, (void*) tab, callback_bomb_explode);
@@ -647,7 +649,6 @@ int handler_attack_bomb(int cliId, JsonParser* json) {
             p->nbBomb++;
             p->mine--;
             game[index].map[x][y] = '2';
-            //Ajoute la bombe dans la liste du joueur
         }
     } 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);

+ 1 - 1
Serveur/player.c

@@ -21,7 +21,7 @@ void create_player(Player* p, Client* c){
     p->maxLife = 100;
     p->speed = 1;
     p->classicBomb = 1;
-    p->mine = 0;
+    p->mine = 1;
     p->remoteBomb = 0;
     p->maxBomb = 2;
     p->nbBomb = 0;

+ 1 - 1
Serveur/player.h

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