Bladeren bron

Passage de status en int + debug

Arthur Brandao 6 jaren geleden
bovenliggende
commit
f957dde9a3
3 gewijzigde bestanden met toevoegingen van 40 en 28 verwijderingen
  1. 5 5
      Serveur/bomberstudent_server.c
  2. 33 23
      Serveur/handler.c
  3. 2 0
      Serveur/handler.h

+ 5 - 5
Serveur/bomberstudent_server.c

@@ -20,10 +20,10 @@ extern int serrno;
 /* --- Globale --- */
 arraylist get;
 arraylist post;
-char* error_code[] = {
-    "520",
-    "400",
-    "403"
+int error_code[] = {
+    520,
+    400,
+    403
 };
 char* error_message[] = {
     "Unknown error",
@@ -318,7 +318,7 @@ boolean send_client(int cliId, JsonEncoder* je) {
 boolean send_err_client(int cliId, int error) {
     JsonEncoder* je = malloc(sizeof (JsonEncoder));
     //Creation JSON
-    add_string(je, "status", error_code[error]);
+    add_integer(je, "status", error_code[error]);
     add_string(je, "message", error_message[error]);
     //Envoi
     if (!send_client(cliId, je)) {

+ 33 - 23
Serveur/handler.c

@@ -113,7 +113,7 @@ int handler_game_list(int cliId, JsonParser* json) {
     //Creation reponse
     ini_encoder(&reponse);
     add_string(&reponse, "action", "game/list");
-    add_string(&reponse, "status", "200");
+    add_integer(&reponse, "status", 200);
     add_string(&reponse, "message", "ok");
     if (nb == 0) {
         add_integer(&reponse, "numberGameList", 0);
@@ -157,7 +157,7 @@ int handler_game_create(int cliId, JsonParser* json) {
     add_string(&reponse, "action", "game/create");
     //Verif nom non existant
     if (search_game(name) != ERR) {
-        add_string(&reponse, "status", "501");
+        add_integer(&reponse, "status", 501);
         add_string(&reponse, "message", "Cannot create game");
         if (!send_client(cliId, &reponse)) {
             adderror("Impossible de répondre au client");
@@ -168,7 +168,7 @@ int handler_game_create(int cliId, JsonParser* json) {
     //Creation
     index = create_game(name, map);
     if (index == ERR) {
-        add_string(&reponse, "status", "501");
+        add_integer(&reponse, "status", 501);
         add_string(&reponse, "message", "Cannot create game");
         if (!send_client(cliId, &reponse)) {
             adderror("Impossible de répondre au client");
@@ -180,7 +180,7 @@ int handler_game_create(int cliId, JsonParser* json) {
     joueur = add_player(&game[index], cliId);
     if (joueur == ERR) {
         stop_game(&game[index]);
-        add_string(&reponse, "status", "501");
+        add_integer(&reponse, "status", 501);
         add_string(&reponse, "message", "Cannot create game");
         if (!send_client(cliId, &reponse)) {
             adderror("Impossible de répondre au client");
@@ -193,9 +193,9 @@ int handler_game_create(int cliId, JsonParser* json) {
     //Ajout infos
     msg = new_string(25 + strlen(map));
     sprintf(msg, "Game created with %s", map);
-    add_string(&reponse, "status", "201");
+    add_integer(&reponse, "status", 201);
     add_string(&reponse, "message", msg);
-    add_string(&reponse, "startPos", "0,0");
+    add_string(&reponse, "startPos", "1,1");
     free(msg);
     //Envoi
     if (!send_client(cliId, &reponse)) {
@@ -228,34 +228,37 @@ int handler_game_join(int cliId, JsonParser* json) {
     //Verif game existe
     index = search_game(name);
     if (index == ERR) {
-        add_string(&reponse, "status", "501");
+        add_integer(&reponse, "status", 501);
         add_string(&reponse, "message", "Cannot join the game");
         if (!send_client(cliId, &reponse)) {
             adderror("Impossible de répondre au client");
         }
         clean_json_encoder(&reponse);
+        free(name);
         return FAIL;
     }
     //Ajout du joueur dans la partie
     joueur = add_player(&game[index], cliId);
     if (joueur == ERR) {
-        add_string(&reponse, "status", "501");
+        add_integer(&reponse, "status", 501);
         add_string(&reponse, "message", "Cannot join the game");
         if (!send_client(cliId, &reponse)) {
             adderror("Impossible de répondre au client");
         }
         clean_json_encoder(&reponse);
+        free(name);
         return FAIL;
     }
     //Recup info
     describe_game(&game[index], joueur, &reponse);
     //Ajout infos
-    add_string(&reponse, "status", "201");
-    add_string(&reponse, "startPos", "0,0");
+    add_integer(&reponse, "status", 201);
+    add_string(&reponse, "startPos", "1,1");
     //Envoi
     if (!send_client(cliId, &reponse)) {
         adderror("Impossible de répondre au client");
         clean_json_encoder(&reponse);
+        free(name);
         return FAIL;
     }
     //Nettoyage
@@ -306,7 +309,7 @@ int handler_player_move(int cliId, JsonParser* json) {
     char* move;
     int index, x = 0, y = 0;
     Player* p = NULL;
-    boolean mine = false;
+    boolean ok = false, mine = false;
     JsonEncoder reponse;
     //Verification arguments
     if (get_pos(json, "move") == JSON_ERROR) {
@@ -318,7 +321,7 @@ int handler_player_move(int cliId, JsonParser* json) {
     index = search_client_game(cliId);
     if (index == ERR) {
         free(move);
-        clean_json_encoder(&reponse);
+        adderror("La game du client n'existe pas");
         return FAIL;
     }
     //Recup le joueur
@@ -333,12 +336,15 @@ int handler_player_move(int cliId, JsonParser* json) {
         pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
     }
     if (p == NULL) {
+        pthread_mutex_unlock(&gameMutex[index]);
         free(move);
+        adderror("Aucun joueur associé au client");
         return FAIL;
     }
     //Regarde si le joueur peut bouger
     if (strncmp(move, "up", 2) == 0) {
         if (p->y > 0 && !player_collision(&game[index], p->x, p->y - 1) && (game[index].map[p->x][p->y - 1] == '_' /*Vide*/ || game[index].map[p->x][p->y - 1] == '2' /*Mine*/)) {
+            ok = true;
             //Bouge le joueur sur la carte
             p->y--;
             //Si marche sur une mine
@@ -351,6 +357,7 @@ int handler_player_move(int cliId, JsonParser* json) {
         }
     } else if (strncmp(move, "down", 4) == 0) {
         if (p->y < game[index].height && !player_collision(&game[index], p->x, p->y + 1) && (game[index].map[p->x][p->y + 1] == '_' /*Vide*/ || game[index].map[p->x][p->y + 1] == '2' /*Mine*/)) {
+            ok = true;
             //Bouge le joueur sur la carte
             p->y++;
             //Si marche sur une mine
@@ -361,8 +368,9 @@ int handler_player_move(int cliId, JsonParser* json) {
                 y = p->y;
             }
         }
-    } else if (strncmp(move, "left", 4) == 4) {
+    } else if (strncmp(move, "left", 4) == 0) {
         if (p->x > 0 && !player_collision(&game[index], p->x - 1, p->y) && (game[index].map[p->x - 1][p->y] == '_' /*Vide*/ || game[index].map[p->x - 1][p->y] == '2' /*Mine*/)) {
+            ok = true;
             //Bouge le joueur sur la carte
             p->x--;
             //Si marche sur une mine
@@ -375,6 +383,7 @@ int handler_player_move(int cliId, JsonParser* json) {
         }
     } else if (strncmp(move, "right", 4) == 0) {
         if (p->x < game[index].width && !player_collision(&game[index], p->x + 1, p->y) && (game[index].map[p->x + 1][p->y] == '_' /*Vide*/ || game[index].map[p->x + 1][p->y] == '2' /*Mine*/)) {
+            ok = true;
             //Bouge le joueur sur la carte
             p->x++;
             //Si marche sur une mine
@@ -388,22 +397,23 @@ int handler_player_move(int cliId, JsonParser* json) {
     } else {
         pthread_mutex_unlock(&gameMutex[index]);
         free(move);
-        send_err_client(cliId, EREQUEST);
         adderror("Le json du client est incorrect");
         return FAIL;
     }
     pthread_mutex_unlock(&gameMutex[index]);
-    //Notifie les joueurs
-    ini_encoder(&reponse);
-    add_integer(&reponse, "player", cliId);
-    add_string(&reponse, "move", move);
-    notify_player(&game[index], "POST", "player/position/update", &reponse, -1);
-    //Si marche sur une mine
-    if (mine) {
-        printf("Mine %d %d", x, y);
+    //Notifie les joueurs si mouvement ok
+    if (ok) {
+        ini_encoder(&reponse);
+        add_integer(&reponse, "player", cliId);
+        add_string(&reponse, "move", move);
+        notify_player(&game[index], "POST", "player/position/update", &reponse, -1);
+        //Si marche sur une mine
+        if (mine) {
+            printf("Mine %d %d", x, y);
+        }
     }
     //Nettoyage
     clean_json_encoder(&reponse);
     free(move);
     return SUCCESS;
-}
+}

+ 2 - 0
Serveur/handler.h

@@ -43,5 +43,7 @@ int handler_game_quit(int, JsonParser*);
 
 int handler_player_move(int, JsonParser*);
 
+int handler_object_new(int, JsonParser*);
+
 #endif /* HANDLER_H */