Sfoglia il codice sorgente

Gestion des mouvements

Arthur Brandao 6 anni fa
parent
commit
7b0d506f47
4 ha cambiato i file con 158 aggiunte e 40 eliminazioni
  1. 11 0
      Serveur/game.c
  2. 2 0
      Serveur/game.h
  3. 143 40
      Serveur/handler.c
  4. 2 0
      Serveur/handler.h

+ 11 - 0
Serveur/game.c

@@ -238,6 +238,17 @@ boolean notify_player(Game* g, char* method, char* ressource, JsonEncoder* param
     return res;
 }
 
+boolean player_collision(Game* g, int x, int y){
+    for(int i = 0; i < MAXPLAYER; i++){
+        if(g->player[i]->ini){
+            if(g->player[i]->x == x && g->player[i]->y == y){
+                return true;
+            }
+        }
+    }
+    return  false;
+}
+
 void remove_player(Game* g, int playerIndex){
     g->nbPlayer--;
     delete_player(g->player[playerIndex]);

+ 2 - 0
Serveur/game.h

@@ -68,6 +68,8 @@ void describe_game(Game*, int, JsonEncoder*);
 
 boolean notify_player(Game*, char*, char*, JsonEncoder*, int);
 
+boolean player_collision(Game*, int, int);
+
 void remove_player(Game*, int);
 
 void stop_game(Game*);

+ 143 - 40
Serveur/handler.c

@@ -19,22 +19,23 @@ extern Game game[MAXGAME];
 extern int nbGame;
 
 /* --- Fonctions privées --- */
+
 /**
  * Cherche dans quel partie est un client
  * @param int L'id du client
  * @return int L'index de la partie
  */
-int search_client_game(int cliId){
+int search_client_game(int cliId) {
     int index = ERR;
-    for(int i = 0; i < MAXGAME; i++){
+    for (int i = 0; i < MAXGAME; i++) {
         //Regarde si la game est active et avec des joueurs
-        if(game[i].active && game[i].nbPlayer > 0){
+        if (game[i].active && game[i].nbPlayer > 0) {
             //Parcours les joueurs
-            for(int j = 0; j < MAXPLAYER; j++){
+            for (int j = 0; j < MAXPLAYER; j++) {
                 //Si le joueur est actif
-                if(game[i].player[j]->ini){
+                if (game[i].player[j]->ini) {
                     //Si l'id est le bon
-                    if(game[i].player[j]->id == cliId){
+                    if (game[i].player[j]->id == cliId) {
                         index = i;
                         break;
                     }
@@ -42,7 +43,7 @@ int search_client_game(int cliId){
             }
         }
         //Si on a un resultat
-        if(index != ERR){
+        if (index != ERR) {
             break;
         }
     }
@@ -55,11 +56,11 @@ int search_client_game(int cliId){
  * @param char* Le nom de la partie
  * @return int L'index dans le tableau
  */
-int search_game(char* name){
+int search_game(char* name) {
     int index = ERR;
-    for(int i = 0; i < MAXGAME; i++){
+    for (int i = 0; i < MAXGAME; i++) {
         //Regarde si la game est active et le nom
-        if(game[i].active && strncmp(game[i].name, name, strlen(game[i].name)) == 0){
+        if (game[i].active && strncmp(game[i].name, name, strlen(game[i].name)) == 0) {
             index = i;
             break;
         }
@@ -68,28 +69,29 @@ int search_game(char* name){
 }
 
 /* --- Fonctions publiques --- */
-void ini_handler(){
+void ini_handler() {
     //Get
     add_handler("GET", "client/end", handler_client_end);
     add_handler("GET", "game/list", handler_game_list);
     //Post
     add_handler("POST", "game/create", handler_game_create);
     add_handler("POST", "game/join", handler_game_join);
+    add_handler("POST", "player/move", handler_player_move);
 }
 
-int handler_client_end(int cliId, JsonParser* json){
+int handler_client_end(int cliId, JsonParser* json) {
     boolean find = false;
     JsonEncoder* notif;
     printf("Deconnexion du client %d\n", cliId);
     //Cherche le client dans les parties
-    for(int i = 0; i < MAXGAME; i++){
-        if(game[i].active){
-            for(int j = 0; j < MAXPLAYER; j++){
-                if(game[i].player[j]->ini && game[i].player[j]->id == cliId){
+    for (int i = 0; i < MAXGAME; i++) {
+        if (game[i].active) {
+            for (int j = 0; j < MAXPLAYER; j++) {
+                if (game[i].player[j]->ini && game[i].player[j]->id == cliId) {
                     find = true;
                     remove_player(&game[i], j);
                     //Avertit les autres joueurs
-                    notif = malloc(sizeof(JsonEncoder));
+                    notif = malloc(sizeof (JsonEncoder));
                     ini_encoder(notif);
                     add_integer(notif, "player", cliId);
                     notify_player(&game[i], "POST", "game/quit", notif, cliId);
@@ -98,7 +100,7 @@ int handler_client_end(int cliId, JsonParser* json){
                     break;
                 }
             }
-            if(find){
+            if (find) {
                 break;
             }
         }
@@ -107,7 +109,7 @@ int handler_client_end(int cliId, JsonParser* json){
     return SUCCESS;
 }
 
-int handler_game_list(int cliId, JsonParser* json){
+int handler_game_list(int cliId, JsonParser* json) {
     JsonEncoder reponse;
     JsonArray game;
     JsonArray map;
@@ -122,7 +124,7 @@ int handler_game_list(int cliId, JsonParser* json){
     add_string(&reponse, "action", "game/list");
     add_string(&reponse, "status", "200");
     add_string(&reponse, "message", "ok");
-    if(nb == 0){
+    if (nb == 0) {
         add_integer(&reponse, "numberGameList", 0);
     } else {
         add_integer(&reponse, "numberGameList", nb);
@@ -130,7 +132,7 @@ int handler_game_list(int cliId, JsonParser* json){
     }
     add_array(&reponse, "maps", &map);
     //Envoi reponse au client
-    if(!send_client(cliId, &reponse)){
+    if (!send_client(cliId, &reponse)) {
         adderror("Impossible de répondre au client");
         return FAIL;
     }
@@ -141,17 +143,17 @@ int handler_game_list(int cliId, JsonParser* json){
     return SUCCESS;
 }
 
-int handler_game_create(int cliId, JsonParser* json){
+int handler_game_create(int cliId, JsonParser* json) {
     char* map, * name, * msg;
     int index, joueur;
     JsonEncoder reponse;
     //Verification arguments
-    if(get_pos(json, "name") == JSON_ERROR){
+    if (get_pos(json, "name") == JSON_ERROR) {
         send_err_client(cliId, EREQUEST);
         adderror("Le json du client est incorrect");
         return FAIL;
     }
-    if(get_pos(json, "map") == JSON_ERROR){
+    if (get_pos(json, "map") == JSON_ERROR) {
         send_err_client(cliId, EREQUEST);
         adderror("Le json du client est incorrect");
         return FAIL;
@@ -163,10 +165,10 @@ int handler_game_create(int cliId, JsonParser* json){
     ini_encoder(&reponse);
     add_string(&reponse, "action", "game/create");
     //Verif nom non existant
-    if(search_game(name) != ERR){
+    if (search_game(name) != ERR) {
         add_string(&reponse, "status", "501");
         add_string(&reponse, "message", "Cannot create game");
-        if(!send_client(cliId, &reponse)){
+        if (!send_client(cliId, &reponse)) {
             adderror("Impossible de répondre au client");
         }
         clean_json_encoder(&reponse);
@@ -174,10 +176,10 @@ int handler_game_create(int cliId, JsonParser* json){
     }
     //Creation
     index = create_game(name, map);
-    if(index == ERR){
+    if (index == ERR) {
         add_string(&reponse, "status", "501");
         add_string(&reponse, "message", "Cannot create game");
-        if(!send_client(cliId, &reponse)){
+        if (!send_client(cliId, &reponse)) {
             adderror("Impossible de répondre au client");
         }
         clean_json_encoder(&reponse);
@@ -185,11 +187,11 @@ int handler_game_create(int cliId, JsonParser* json){
     }
     //Ajout du joueur dans la partie
     joueur = add_player(&game[index], cliId);
-    if(joueur == ERR){
+    if (joueur == ERR) {
         stop_game(&game[index]);
         add_string(&reponse, "status", "501");
         add_string(&reponse, "message", "Cannot create game");
-        if(!send_client(cliId, &reponse)){
+        if (!send_client(cliId, &reponse)) {
             adderror("Impossible de répondre au client");
         }
         clean_json_encoder(&reponse);
@@ -205,7 +207,7 @@ int handler_game_create(int cliId, JsonParser* json){
     add_string(&reponse, "startPos", "0,0");
     free(msg);
     //Envoi
-    if(!send_client(cliId, &reponse)){
+    if (!send_client(cliId, &reponse)) {
         adderror("Impossible de répondre au client");
         clean_json_encoder(&reponse);
         return FAIL;
@@ -217,12 +219,12 @@ int handler_game_create(int cliId, JsonParser* json){
     return SUCCESS;
 }
 
-int handler_game_join(int cliId, JsonParser* json){
+int handler_game_join(int cliId, JsonParser* json) {
     char* name;
     int index, joueur;
     JsonEncoder reponse;
     //Verification arguments
-    if(get_pos(json, "name") == JSON_ERROR){
+    if (get_pos(json, "name") == JSON_ERROR) {
         send_err_client(cliId, EREQUEST);
         adderror("Le json du client est incorrect");
         return FAIL;
@@ -232,12 +234,12 @@ int handler_game_join(int cliId, JsonParser* json){
     //Initialisation json reponse
     ini_encoder(&reponse);
     add_string(&reponse, "action", "game/join");
-    //Verif nom non existant
+    //Verif game existe
     index = search_game(name);
-    if(index == ERR){        
+    if (index == ERR) {
         add_string(&reponse, "status", "501");
-        add_string(&reponse, "message", "Cannot join the game game");
-        if(!send_client(cliId, &reponse)){
+        add_string(&reponse, "message", "Cannot join the game");
+        if (!send_client(cliId, &reponse)) {
             adderror("Impossible de répondre au client");
         }
         clean_json_encoder(&reponse);
@@ -245,10 +247,10 @@ int handler_game_join(int cliId, JsonParser* json){
     }
     //Ajout du joueur dans la partie
     joueur = add_player(&game[index], cliId);
-    if(joueur == ERR){
+    if (joueur == ERR) {
         add_string(&reponse, "status", "501");
         add_string(&reponse, "message", "Cannot join the game");
-        if(!send_client(cliId, &reponse)){
+        if (!send_client(cliId, &reponse)) {
             adderror("Impossible de répondre au client");
         }
         clean_json_encoder(&reponse);
@@ -260,7 +262,7 @@ int handler_game_join(int cliId, JsonParser* json){
     add_string(&reponse, "status", "201");
     add_string(&reponse, "startPos", "0,0");
     //Envoi
-    if(!send_client(cliId, &reponse)){
+    if (!send_client(cliId, &reponse)) {
         adderror("Impossible de répondre au client");
         clean_json_encoder(&reponse);
         return FAIL;
@@ -275,4 +277,105 @@ int handler_game_join(int cliId, JsonParser* json){
     //Nettoyage
     clean_json_encoder(&reponse);
     return SUCCESS;
+}
+
+int handler_player_move(int cliId, JsonParser* json) {
+    printf("Player Move\n");
+    char* move;
+    int index, x = 0, y = 0;
+    Player* p = NULL;
+    boolean mine = false;
+    JsonEncoder reponse;
+    //Verification arguments
+    if (get_pos(json, "move") == JSON_ERROR) {
+        return FAIL;
+    }
+    //Recup valeur
+    move = get_string(json, "move"); 
+    //Verif game existe
+    index = search_client_game(cliId);
+    if (index == ERR) {
+        free(move);
+        clean_json_encoder(&reponse);
+        return FAIL;
+    }
+    //Recup le joueur
+    for(int i = 0; i < MAXPLAYER; i++){
+        if(game[index].player[i]->ini && game[index].player[i]->id == cliId){
+            p = game[index].player[i];
+            break;
+        }
+    }
+    if(p == NULL){
+        free(move);
+        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*/ )){
+            //Bouge le joueur sur la carte
+            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;
+            }
+        }
+    } 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*/ )){
+            //Bouge le joueur sur la carte
+            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;
+            }
+        }
+    } else if (strncmp(move, "left", 4) == 4) {
+        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*/ )){
+            //Bouge le joueur sur la carte
+            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;
+            }
+        }
+    } 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*/ )){
+            //Bouge le joueur sur la carte
+            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;
+            }
+        }
+    } else {
+        free(move);
+        send_err_client(cliId, EREQUEST);
+        adderror("Le json du client est incorrect");
+        return FAIL; 
+    }
+    //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);
+    }
+    //Nettoyage
+    clean_json_encoder(&reponse);
+    free(move);
+    return SUCCESS;
 }

+ 2 - 0
Serveur/handler.h

@@ -39,5 +39,7 @@ int handler_game_create(int, JsonParser*);
 
 int handler_game_join(int, JsonParser*);
 
+int handler_player_move(int, JsonParser*);
+
 #endif /* HANDLER_H */