|
@@ -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;
|
|
|
}
|