Ver código fonte

Ajout pose bombe

Arthur Brandao 6 anos atrás
pai
commit
c6719feddd
7 arquivos alterados com 210 adições e 13 exclusões
  1. 5 5
      Serveur/bomberstudent_server.c
  2. 0 3
      Serveur/game.c
  3. 173 4
      Serveur/handler.c
  4. 2 0
      Serveur/handler.h
  5. 1 0
      Serveur/player.c
  6. 1 0
      Serveur/player.h
  7. 28 1
      Serveur/test.c

+ 5 - 5
Serveur/bomberstudent_server.c

@@ -316,16 +316,16 @@ boolean send_client(int cliId, JsonEncoder* je) {
 }
 
 boolean send_err_client(int cliId, int error) {
-    JsonEncoder* je = malloc(sizeof (JsonEncoder));
+    JsonEncoder je;
     //Creation JSON
-    add_integer(je, "status", error_code[error]);
-    add_string(je, "message", error_message[error]);
+    ini_encoder(&je);
+    add_integer(&je, "status", error_code[error]);
+    add_string(&je, "message", error_message[error]);
     //Envoi
-    if (!send_client(cliId, je)) {
+    if (!send_client(cliId, &je)) {
         adderror("Impossible d'avertir le client de l'erreur");
         return false;
     }
-    free(je);
     return true;
 }
 

+ 0 - 3
Serveur/game.c

@@ -290,14 +290,11 @@ boolean notify_player(Game* g, char* method, char* ressource, JsonEncoder* param
 
 boolean player_collision(Game* g, int x, int y) {
     for (int i = 0; i < MAXPLAYER; i++) {
-        pthread_mutex_lock(&playerMutex[(g->index * MAXPLAYER) + i]);
         if (g->player[i]->ini) {
             if (g->player[i]->x == x && g->player[i]->y == y) {
-                pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + i]);
                 return true;
             }
         }
-        pthread_mutex_unlock(&playerMutex[(g->index * MAXPLAYER) + i]);
     }
     return false;
 }

+ 173 - 4
Serveur/handler.c

@@ -79,6 +79,36 @@ int search_game(char* name) {
     return index;
 }
 
+/**
+ * Parse une postion en 2 entier X et Y
+ * @param char* La position
+ * @param int* Le resultat en X
+ * @param int Le resultat en Y
+ * @return Reussite
+ */
+boolean parse_pos(char* pos, int* x, int* y) {
+    int index = 0;
+    char* cy, * cx;
+    //Cherche la separation
+    while (pos[index] != '\0' && pos[index] != ',') {
+        index++;
+    }
+    if (pos[index] == '\0') {
+        return false;
+    }
+    //Separation chaine
+    cx = malloc(sizeof (char) * index + 1);
+    memset(cx, 0, index + 1);
+    strncpy(cx, pos, index);
+    cy = pos + index + 1;
+    //Parse
+    *x = atoi(cx);
+    *y = atoi(cy);
+    //Nettoyage
+    free(cx);
+    return true;
+}
+
 /* --- Fonctions publiques --- */
 void ini_handler() {
     //Get
@@ -90,6 +120,7 @@ void ini_handler() {
     add_handler("POST", "game/quit", handler_game_quit);
     add_handler("POST", "player/move", handler_player_move);
     add_handler("POST", "object/new", handler_object_new);
+    add_handler("POST", "attack/bomb", handler_attack_bomb);
 }
 
 int handler_client_end(int cliId, JsonParser* json) {
@@ -268,7 +299,7 @@ int handler_game_join(int cliId, JsonParser* json) {
     free(name);
     //Avertit les autres joueurs
     add_integer(&reponse, "id", cliId);
-    add_string(&reponse, "pos", "0,0");
+    add_string(&reponse, "pos", "1,1");
     notify_player(&game[index], "POST", "game/newplayer", &reponse, cliId);
     //Nettoyage
     clean_json_encoder(&reponse);
@@ -309,7 +340,7 @@ int handler_game_quit(int cliId, JsonParser* json) {
 
 int handler_player_move(int cliId, JsonParser* json) {
     char* move;
-    int index, x = 0, y = 0;
+    int index, playerIndex = 0, x = 0, y = 0;
     Player* p = NULL;
     boolean ok = false, mine = false;
     JsonEncoder reponse;
@@ -331,8 +362,8 @@ int handler_player_move(int cliId, JsonParser* json) {
     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];
-            pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
             break;
         }
         pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
@@ -397,11 +428,13 @@ int handler_player_move(int cliId, JsonParser* json) {
             }
         }
     } else {
+        pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
         pthread_mutex_unlock(&gameMutex[index]);
         free(move);
         adderror("Le json du client est incorrect");
         return FAIL;
     }
+    pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
     pthread_mutex_unlock(&gameMutex[index]);
     //Notifie les joueurs si mouvement ok
     if (ok) {
@@ -487,6 +520,7 @@ int handler_object_new(int cliId, JsonParser* json) {
     if (p == NULL) {
         pthread_mutex_unlock(&gameMutex[index]);
         free(class);
+        send_err_client(cliId, EREQUEST);
         adderror("Aucun joueur associé au client");
         return FAIL;
     }
@@ -521,7 +555,7 @@ int handler_object_new(int cliId, JsonParser* json) {
             return FAIL;
         }
         //Si major lance le timer pour avertir de la fin
-        if(type == OBJ_MAJOR){
+        if (type == OBJ_MAJOR) {
             delay(TIMEMAJOR, index, playerIndex, callback_major_end);
         }
     }
@@ -530,4 +564,139 @@ int handler_object_new(int cliId, JsonParser* json) {
     free(class);
     clean_json_encoder(&reponse);
     return SUCCESS;
+}
+
+int handler_attack_bomb(int cliId, JsonParser* json) {
+    char* pos, * class;
+    int x, y, length, index, playerIndex = 0;
+    Player* p = NULL;
+    boolean ok = false;
+    JsonEncoder reponse;
+    //Verification arguments
+    if (get_pos(json, "class") == JSON_ERROR) {
+        send_err_client(cliId, EREQUEST);
+        adderror("Le json du client est incorrect");
+        return FAIL;
+    }
+    if (get_pos(json, "pos") == JSON_ERROR) {
+        send_err_client(cliId, EREQUEST);
+        adderror("Le json du client est incorrect");
+        return FAIL;
+    }
+    //Recup valeur
+    class = get_string(json, "class");
+    pos = get_string(json, "pos");
+    //Parse les valeurs de pos
+    if (!parse_pos(pos, &x, &y)) {
+        free(class);
+        free(pos);
+        send_err_client(cliId, EREQUEST);
+        adderror("Le json du client est incorrect");
+        return FAIL;
+    }
+    //Verif game existe
+    index = search_client_game(cliId);
+    if (index == ERR) {
+        free(class);
+        free(pos);
+        send_err_client(cliId, EREQUEST);
+        adderror("La game du client n'existe pas");
+        return FAIL;
+    }
+    //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]);
+        free(class);
+        free(pos);
+        send_err_client(cliId, EREQUEST);
+        adderror("Aucun joueur associé au client");
+        return FAIL;
+    }
+    //Verif si la bombe peut être posé et pose
+    length = strlen(class);
+    if (strncmp(class, "classic", length) == 0) {
+        ok = p->classicBomb > 0 && p->nbBomb > p->maxBomb && game[index].map[x][y] == '_' && !player_collision(&game[index], x, y);
+        if (ok) {
+            p->nbBomb++;
+            p->classicBomb--;
+            game[index].map[x][y] = '1';
+        }
+    } else if (strncmp(class, "mine", length) == 0) {
+        ok = p->mine > 0 && p->nbBomb > p->maxBomb && game[index].map[x][y] == '_' && !player_collision(&game[index], x, y);
+        if (ok) {
+            p->nbBomb++;
+            p->mine--;
+            game[index].map[x][y] = '2';
+        }
+    } 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);
+        if (ok) {
+            p->nbBomb++;
+            p->remoteBomb--;
+            game[index].map[x][y] = '3';
+        }
+    } else {
+        pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
+        pthread_mutex_unlock(&gameMutex[index]);
+        free(pos);
+        free(class);
+        send_err_client(cliId, EREQUEST);
+        adderror("Le json du client est incorrect");
+        return FAIL;
+    }
+    //Initialisation json
+    ini_encoder(&reponse);
+    add_string(&reponse, "action", "attack/bomb");
+    //Reponse
+    if (ok) {
+        add_integer(&reponse, "status", 201);
+        describe_player(p, &reponse);
+        //Envoi
+        if (!send_client(cliId, &reponse)) {
+            adderror("Impossible de répondre au client");
+            pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
+            pthread_mutex_unlock(&gameMutex[index]);
+            clean_json_encoder(&reponse);
+            free(pos);
+            free(class);
+            return FAIL;
+        }
+        //Notification des autre joeurs
+        pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
+        pthread_mutex_unlock(&gameMutex[index]);
+        clean_json_encoder(&reponse);
+        add_string(&reponse, "pos", pos);
+        add_string(&reponse, "class", class);
+        notify_player(&game[index], "POST", "attack/newbomb", &reponse, cliId);
+    } else {
+        add_integer(&reponse, "status", 403);
+        add_string(&reponse, "message", "Forbidden action");
+        //Envoi
+        if (!send_client(cliId, &reponse)) {
+            adderror("Impossible de répondre au client");
+            pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
+            pthread_mutex_unlock(&gameMutex[index]);
+            clean_json_encoder(&reponse);
+            free(pos);
+            free(class);
+            return FAIL;
+        }
+        pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
+        pthread_mutex_unlock(&gameMutex[index]);
+    }
+    //Nettoyage
+    clean_json_encoder(&reponse);
+    free(pos);
+    free(class);
+    return SUCCESS;
 }

+ 2 - 0
Serveur/handler.h

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

+ 1 - 0
Serveur/player.c

@@ -24,6 +24,7 @@ void create_player(Player* p, Client* c){
     p->mine = 0;
     p->remoteBomb = 0;
     p->maxBomb = 2;
+    p->nbBomb = 0;
     p->bombUp = 0;
     p->bombDown = 0;
     p->firePower = 0;

+ 1 - 0
Serveur/player.h

@@ -29,6 +29,7 @@ typedef struct{
     int mine; //Nombre de mine
     int remoteBomb; //Nombre de bombe télécommandée
     int maxBomb; //Nombre max de bombe simultané sur le terrain
+    int nbBomb; //Nombre de bombe actuellement sur le terrain
     /* Bonus (Nombre de fois ou le bonus est pris). Les modifications sont toujours calculées par le serveur */
     int bombUp;
     int bombDown;

+ 28 - 1
Serveur/test.c

@@ -261,6 +261,25 @@ int size(){
     return 1;
 }
 
+/*boolean parse_pos(char* pos, int* x, int* y){
+    int index = 0;
+    char* cy, * cx;
+    while(pos[index] != '\0' && pos[index] != ','){
+        index++;
+    }
+    if(pos[index] == '\0'){
+        return false;
+    }
+    cx = malloc(sizeof(char) * index + 1);
+    memset(cx, 0, index + 1);
+    strncpy(cx, pos, index);
+    cy = pos + index + 1;
+    *x = atoi(cx);
+    *y = atoi(cy);
+    free(cx);
+    return true;
+}*/
+
 int main(){
     //return parse();
     //return encode();
@@ -289,5 +308,13 @@ int main(){
     //return files();
     //return array_encode();
     //return array_parse();
-    return size();
+    //return size();
+    
+    /*
+    int x, y;
+    printf("Res %d\n", parse_pos("5890,84", &x, &y));
+    printf("%d, %d\n", x, y);
+    //*/
+    
+    return 0;
 }