Bläddra i källkod

Ajout handler game/quit

Arthur Brandao 6 år sedan
förälder
incheckning
a231df34d2
3 ändrade filer med 83 tillägg och 76 borttagningar
  1. 27 30
      Serveur/bomberstudent_server.c
  2. 54 46
      Serveur/handler.c
  3. 2 0
      Serveur/handler.h

+ 27 - 30
Serveur/bomberstudent_server.c

@@ -25,13 +25,14 @@ char* error_code[] = {
     "400",
     "403"
 };
-char* error_message[] = { 
+char* error_message[] = {
     "Unknown error",
     "Bad Request",
     "Forbidden action"
 };
 
 /* --- Fonctions privées --- */
+
 /**
  * Thread de gestion d'un client
  * @param data
@@ -72,7 +73,7 @@ void* client_thread(void* data) {
             break;
         }
         //Regarde si le client existe toujours
-        if(get_client(cliId) == NULL){
+        if (get_client(cliId) == NULL) {
             break;
         }
     }
@@ -216,7 +217,7 @@ boolean receive_client(Client* cli) {
     //Attente reception
     if (cli->main->server_receive(cli->main, buffer, BUFFER_SIZE) == ERR) {
         //Si la conneion est coupée
-        if(serrno == SEABORT){
+        if (serrno == SEABORT) {
             //On ferme le client
             printf("Connexion perdu avec le client %d\n", cli->id);
             handler_client_end(cli->id, NULL);
@@ -252,21 +253,17 @@ boolean receive_client(Client* cli) {
     ressource = malloc(sizeof (char) * (compteur + 1));
     memset(ressource, 0, compteur + 1);
     strncpy(ressource, reader, compteur);
-    fflush(stdout);
     //Recup param JSON
     if (method == POST) {
-        if (buffer[pos] == '\0') {
-            //Pas de parametre avec le post
-            adderror("Absence de parametre en POST");
-            send_err_client(cli->id, EREQUEST);
-            return false;
-        }
-        json = reader + compteur + 1;
-        jp = malloc(sizeof (JsonParser));
-        if (json_parse(jp, json) != JSON_OK) {
-            adderror("Impossible de parser le JSON");
-            send_err_client(cli->id, EREQUEST);
-            return false;
+        //Si un parametre est present
+        if (buffer[pos] != '\0') {
+            json = reader + compteur + 1;
+            jp = malloc(sizeof (JsonParser));
+            if (json_parse(jp, json) != JSON_OK) {
+                adderror("Impossible de parser le JSON");
+                send_err_client(cli->id, EREQUEST);
+                return false;
+            }
         }
         //Change la liste à utiliser
         al = &post;
@@ -279,13 +276,13 @@ boolean receive_client(Client* cli) {
     }
     //Nettoyage
     free(ressource);
-    if(jp != NULL){
+    if (jp != NULL) {
         free(jp);
     }
     return true;
 }
 
-boolean send_client(int cliId, JsonEncoder* je){
+boolean send_client(int cliId, JsonEncoder* je) {
     Client* cli;
     char* answer, * msg;
     //Recup client
@@ -296,13 +293,13 @@ boolean send_client(int cliId, JsonEncoder* je){
     }
     //Preparation message
     answer = json_encode(je);
-    msg = malloc(sizeof(char) + (strlen(answer) + 2));
+    msg = malloc(sizeof (char) + (strlen(answer) + 2));
     memset(msg, 0, strlen(answer) + 2);
     sprintf(msg, "%s\n", answer);
     //Envoi la reponse
-    if(!cli->main->server_send(cli->main, msg)){
+    if (!cli->main->server_send(cli->main, msg)) {
         //Si la conneion est coupée
-        if(serrno == SEABORT){
+        if (serrno == SEABORT) {
             //On ferme le client
             printf("Connexion perdu avec le client %d\n", cliId);
             handler_client_end(cliId, NULL);
@@ -318,13 +315,13 @@ boolean send_client(int cliId, JsonEncoder* je){
     return true;
 }
 
-boolean send_err_client(int cliId, int error){
-    JsonEncoder* je = malloc(sizeof(JsonEncoder));
+boolean send_err_client(int cliId, int error) {
+    JsonEncoder* je = malloc(sizeof (JsonEncoder));
     //Creation JSON
     add_string(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;
     }
@@ -332,17 +329,17 @@ boolean send_err_client(int cliId, int error){
     return true;
 }
 
-boolean notify_client(Client* cli, char* method, char* ressource, JsonEncoder* param){
+boolean notify_client(Client* cli, char* method, char* ressource, JsonEncoder* param) {
     int length;
     char* answer, * msg;
     //Creation message
     answer = json_encode(param);
     length = strlen(method) + 1 + strlen(ressource) + 1 + strlen(answer) + 2;
-    msg = malloc(sizeof(char) + length);
+    msg = malloc(sizeof (char) +length);
     memset(msg, 0, length);
     sprintf(msg, "%s %s\n%s\n", method, ressource, answer);
     //Envoi la reponse
-    if(!cli->notify->server_send(cli->notify, msg)){
+    if (!cli->notify->server_send(cli->notify, msg)) {
         adderror("Impossible de notifier le client");
         free(answer);
         free(msg);
@@ -354,15 +351,15 @@ boolean notify_client(Client* cli, char* method, char* ressource, JsonEncoder* p
     return true;
 }
 
-boolean notify_all(char* method, char* ressource, JsonEncoder* param){
+boolean notify_all(char* method, char* ressource, JsonEncoder* param) {
     Client* cli;
     boolean res = true;
     //Parcours tous les clients
     int nbClient = get_number_client();
-    for(int i = 0; i < nbClient; i++){
+    for (int i = 0; i < nbClient; i++) {
         cli = get_client(i);
         //Si le client existe toujours
-        if(cli == NULL){
+        if (cli == NULL) {
             continue;
         }
         //Lui envoi le message

+ 54 - 46
Serveur/handler.c

@@ -86,42 +86,17 @@ void ini_handler() {
     //Post
     add_handler("POST", "game/create", handler_game_create);
     add_handler("POST", "game/join", handler_game_join);
+    add_handler("POST", "game/quit", handler_game_quit);
     add_handler("POST", "player/move", handler_player_move);
 }
 
 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++) {
-        pthread_mutex_lock(&gameMutex[i]);
-        if (game[i].active) {
-            for (int j = 0; j < MAXPLAYER; j++) {
-                pthread_mutex_lock(&playerMutex[(i * MAXPLAYER) + j]);
-                if (game[i].player[j]->ini && game[i].player[j]->id == cliId) {
-                    pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
-                    pthread_mutex_unlock(&gameMutex[i]);
-                    find = true;
-                    remove_player(&game[i], j);
-                    //Avertit les autres joueurs
-                    notif = malloc(sizeof (JsonEncoder));
-                    ini_encoder(notif);
-                    add_integer(notif, "player", cliId);
-                    notify_player(&game[i], "POST", "game/quit", notif, cliId);
-                    clean_json_encoder(notif);
-                    free(notif);
-                    break;
-                }
-                pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
-            }
-            if (find) {
-                break;
-            }
-        }
-        pthread_mutex_unlock(&gameMutex[i]);
+    //Verif que le client est toujours actif
+    if (get_client(cliId) != NULL) {
+        printf("Deconnexion du client %d\n", cliId);
+        handler_game_quit(cliId, json);
+        remove_client(cliId);
     }
-    remove_client(cliId);
     return SUCCESS;
 }
 
@@ -295,6 +270,39 @@ int handler_game_join(int cliId, JsonParser* json) {
     return SUCCESS;
 }
 
+int handler_game_quit(int cliId, JsonParser* json) {
+    printf("Quit\n");
+    boolean find = false;
+    JsonEncoder notif;
+    //Cherche le client dans les parties
+    for (int i = 0; i < MAXGAME; i++) {
+        pthread_mutex_lock(&gameMutex[i]);
+        if (game[i].active) {
+            for (int j = 0; j < MAXPLAYER; j++) {
+                pthread_mutex_lock(&playerMutex[(i * MAXPLAYER) + j]);
+                if (game[i].player[j]->ini && game[i].player[j]->id == cliId) {
+                    pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
+                    pthread_mutex_unlock(&gameMutex[i]);
+                    find = true;
+                    remove_player(&game[i], j);
+                    //Avertit les autres joueurs
+                    ini_encoder(&notif);
+                    add_integer(&notif, "player", cliId);
+                    notify_player(&game[i], "POST", "game/quit", &notif, cliId);
+                    clean_json_encoder(&notif);
+                    break;
+                }
+                pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
+            }
+            if (find) {
+                break;
+            }
+        }
+        pthread_mutex_unlock(&gameMutex[i]);
+    }
+    return SUCCESS;
+}
+
 int handler_player_move(int cliId, JsonParser* json) {
     printf("Player Move\n");
     char* move;
@@ -307,7 +315,7 @@ int handler_player_move(int cliId, JsonParser* json) {
         return FAIL;
     }
     //Recup valeur
-    move = get_string(json, "move"); 
+    move = get_string(json, "move");
     //Verif game existe
     index = search_client_game(cliId);
     if (index == ERR) {
@@ -317,26 +325,26 @@ int handler_player_move(int cliId, JsonParser* json) {
     }
     //Recup le joueur
     pthread_mutex_lock(&gameMutex[index]);
-    for(int i = 0; i < MAXPLAYER; i++){
+    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){
+        if (game[index].player[i]->ini && game[index].player[i]->id == cliId) {
             p = game[index].player[i];
             pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
             break;
         }
         pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
     }
-    if(p == NULL){
+    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*/ )){
+        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'){
+            if (game[index].map[p->x][p->y] == '2') {
                 game[index].map[p->x][p->y] = '_';
                 mine = true;
                 x = p->x;
@@ -344,11 +352,11 @@ 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*/ )){
+        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'){
+            if (game[index].map[p->x][p->y] == '2') {
                 game[index].map[p->x][p->y] = '_';
                 mine = true;
                 x = p->x;
@@ -356,23 +364,23 @@ int handler_player_move(int cliId, JsonParser* json) {
             }
         }
     } 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*/ )){
+        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'){
+            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*/ )){
+    } 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'){
+            if (game[index].map[p->x][p->y] == '2') {
                 game[index].map[p->x][p->y] = '_';
                 mine = true;
                 x = p->x;
@@ -384,7 +392,7 @@ int handler_player_move(int cliId, JsonParser* json) {
         free(move);
         send_err_client(cliId, EREQUEST);
         adderror("Le json du client est incorrect");
-        return FAIL; 
+        return FAIL;
     }
     pthread_mutex_unlock(&gameMutex[index]);
     //Notifie les joueurs
@@ -393,7 +401,7 @@ int handler_player_move(int cliId, JsonParser* json) {
     add_string(&reponse, "move", move);
     notify_player(&game[index], "POST", "player/position/update", &reponse, -1);
     //Si marche sur une mine
-    if(mine){
+    if (mine) {
         printf("Mine %d %d", x, y);
     }
     //Nettoyage

+ 2 - 0
Serveur/handler.h

@@ -39,6 +39,8 @@ int handler_game_create(int, JsonParser*);
 
 int handler_game_join(int, JsonParser*);
 
+int handler_game_quit(int, JsonParser*);
+
 int handler_player_move(int, JsonParser*);
 
 #endif /* HANDLER_H */