浏览代码

Ajout handler join

Arthur Brandao 6 年之前
父节点
当前提交
b09b3a72fd
共有 4 个文件被更改,包括 95 次插入0 次删除
  1. 17 0
      Serveur/game.c
  2. 2 0
      Serveur/game.h
  3. 74 0
      Serveur/handler.c
  4. 2 0
      Serveur/handler.h

+ 17 - 0
Serveur/game.c

@@ -11,6 +11,7 @@
 #include "str.h"
 #include "file.h"
 #include "game.h"
+#include "bomberstudent_server.h"
 
 /* --- Extern --- */
 Game game[MAXGAME];
@@ -221,6 +222,22 @@ JsonEncoder* describe_game(Game* g, int playerIndex){
     return desc;
 }
 
+boolean notify_player(Game* g, char* method, char* ressource, JsonEncoder* param){
+    boolean res = true;
+    //Regarde si la game est active
+    if(!g->active){
+        return false;
+    }
+    //Parcours les joeurs de la game
+    for(int i = 0; i < MAXPLAYER; i++){
+        //Si joueur actif
+        if(g->player[i]->ini){
+            res = res && notify_client(g->player[i]->cli, method, ressource, param);
+        }
+    }
+    return res;
+}
+
 void stop_game(Game* g){
     //Indique comme inactive
     g->active = false;

+ 2 - 0
Serveur/game.h

@@ -65,6 +65,8 @@ int add_player(Game*, int);
 
 JsonEncoder* describe_game(Game*, int);
 
+boolean notify_player(Game*, char*, char*, JsonEncoder*);
+
 void stop_game(Game*);
 
 void clean_games();

+ 74 - 0
Serveur/handler.c

@@ -74,6 +74,7 @@ void ini_handler(){
     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);
 }
 
 int handler_client_end(int cliId, JsonParser* json){
@@ -137,6 +138,7 @@ int handler_game_create(int cliId, JsonParser* json){
     if(search_game(name) != ERR){
         reponse = malloc(sizeof(JsonEncoder));
         ini_encoder(reponse);
+        add_string(reponse, "action", "game/create");
         add_string(reponse, "status", "501");
         add_string(reponse, "message", "Cannot create game");
         if(!send_client(cliId, reponse)){
@@ -151,6 +153,7 @@ int handler_game_create(int cliId, JsonParser* json){
     if(index == ERR){
         reponse = malloc(sizeof(JsonEncoder));
         ini_encoder(reponse);
+        add_string(reponse, "action", "game/create");
         add_string(reponse, "status", "501");
         add_string(reponse, "message", "Cannot create game");
         if(!send_client(cliId, reponse)){
@@ -199,4 +202,75 @@ int handler_game_create(int cliId, JsonParser* json){
     free(map);
     free(name);
     return SUCCESS;
+}
+
+int handler_game_join(int cliId, JsonParser* json){
+    char* name;
+    int index, joueur;
+    JsonEncoder* reponse;
+    //Verification arguments
+    if(get_pos(json, "name") == JSON_ERROR){
+        send_err_client(cliId, EREQUEST);
+        adderror("Le json du client est incorrect");
+        return FAIL;
+    }
+    //Recup valeur
+    name = get_string(json, "name");
+    //Verif nom non existant
+    index = search_game(name);
+    if(index == ERR){
+        reponse = malloc(sizeof(JsonEncoder));
+        ini_encoder(reponse);
+        add_string(reponse, "action", "game/join");
+        add_string(reponse, "status", "501");
+        add_string(reponse, "message", "Cannot join the game game");
+        if(!send_client(cliId, reponse)){
+            adderror("Impossible de répondre au client");
+        }
+        clean_json_encoder(reponse);
+        free(reponse);
+        return FAIL;
+    }
+    //Ajout du joueur dans la partie
+    joueur = add_player(&game[index], cliId);
+    if(joueur == ERR){
+        reponse = malloc(sizeof(JsonEncoder));
+        ini_encoder(reponse);
+        add_string(reponse, "action", "game/join");
+        add_string(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(reponse);
+        return FAIL;
+    }
+    //Recup info
+    reponse = describe_game(&game[index], joueur);
+    //Ajout infos
+    add_string(reponse, "action", "game/join");
+    add_string(reponse, "status", "201");
+    add_string(reponse, "startPos", "0,0");
+    //Envoi
+    if(!send_client(cliId, reponse)){
+        adderror("Impossible de répondre au client");
+        clean_json_encoder(reponse);
+        free(reponse);
+        return FAIL;
+    }
+    //Nettoyage
+    clean_json_encoder(reponse);
+    free(reponse);
+    free(name);
+    //Avertit les autres joueurs
+    reponse = malloc(sizeof(JsonEncoder));
+    ini_encoder(reponse);
+    add_integer(reponse, "id", cliId);
+    add_string(reponse, "pos", "0,0");
+    notify_player(&game[index], "POST", "game/newplayer", reponse);
+    //Nettoyage
+    clean_json_encoder(reponse);
+    free(reponse);
+    return SUCCESS;
 }

+ 2 - 0
Serveur/handler.h

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