|
@@ -109,25 +109,26 @@ int handler_client_end(int cliId, JsonParser* json){
|
|
|
|
|
|
int handler_game_list(int cliId, JsonParser* json){
|
|
|
JsonEncoder reponse;
|
|
|
- JsonArray* game;
|
|
|
- JsonArray* map;
|
|
|
+ JsonArray game;
|
|
|
+ JsonArray map;
|
|
|
+ int nb;
|
|
|
//Recup la liste des parties et des cartes
|
|
|
- game = list_game();
|
|
|
- map = list_map();
|
|
|
+ ini_array_encoder(&game);
|
|
|
+ ini_array_encoder(&map);
|
|
|
+ nb = list_game(&game);
|
|
|
+ list_map(&map);
|
|
|
//Creation reponse
|
|
|
ini_encoder(&reponse);
|
|
|
add_string(&reponse, "action", "game/list");
|
|
|
add_string(&reponse, "status", "200");
|
|
|
add_string(&reponse, "message", "ok");
|
|
|
- if(game == NULL){
|
|
|
+ if(nb == 0){
|
|
|
add_integer(&reponse, "numberGameList", 0);
|
|
|
} else {
|
|
|
- add_integer(&reponse, "numberGameList", nbGame);
|
|
|
- add_array(&reponse, "games", game);
|
|
|
- clean_json_array(game);
|
|
|
- free(game);
|
|
|
+ add_integer(&reponse, "numberGameList", nb);
|
|
|
+ add_array(&reponse, "games", &game);
|
|
|
}
|
|
|
- add_array(&reponse, "maps", map);
|
|
|
+ add_array(&reponse, "maps", &map);
|
|
|
//Envoi reponse au client
|
|
|
if(!send_client(cliId, &reponse)){
|
|
|
adderror("Impossible de répondre au client");
|
|
@@ -135,15 +136,15 @@ int handler_game_list(int cliId, JsonParser* json){
|
|
|
}
|
|
|
//Nettoyage
|
|
|
clean_json_encoder(&reponse);
|
|
|
- clean_json_array(map);
|
|
|
- free(map);
|
|
|
+ clean_json_array(&map);
|
|
|
+ clean_json_array(&game);
|
|
|
return SUCCESS;
|
|
|
}
|
|
|
|
|
|
int handler_game_create(int cliId, JsonParser* json){
|
|
|
char* map, * name, * msg;
|
|
|
int index, joueur;
|
|
|
- JsonEncoder* reponse;
|
|
|
+ JsonEncoder reponse;
|
|
|
//Verification arguments
|
|
|
if(get_pos(json, "name") == JSON_ERROR){
|
|
|
send_err_client(cliId, EREQUEST);
|
|
@@ -158,71 +159,59 @@ int handler_game_create(int cliId, JsonParser* json){
|
|
|
//Recup valeur
|
|
|
map = get_string(json, "map");
|
|
|
name = get_string(json, "name");
|
|
|
+ //Initialisation reponse JSON
|
|
|
+ ini_encoder(&reponse);
|
|
|
+ add_string(&reponse, "action", "game/create");
|
|
|
//Verif nom non existant
|
|
|
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)){
|
|
|
+ add_string(&reponse, "status", "501");
|
|
|
+ add_string(&reponse, "message", "Cannot create game");
|
|
|
+ if(!send_client(cliId, &reponse)){
|
|
|
adderror("Impossible de répondre au client");
|
|
|
}
|
|
|
- clean_json_encoder(reponse);
|
|
|
- free(reponse);
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
return FAIL;
|
|
|
}
|
|
|
//Creation
|
|
|
index = create_game(name, map);
|
|
|
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)){
|
|
|
+ add_string(&reponse, "status", "501");
|
|
|
+ add_string(&reponse, "message", "Cannot create game");
|
|
|
+ if(!send_client(cliId, &reponse)){
|
|
|
adderror("Impossible de répondre au client");
|
|
|
}
|
|
|
- clean_json_encoder(reponse);
|
|
|
- free(reponse);
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
return FAIL;
|
|
|
}
|
|
|
//Ajout du joueur dans la partie
|
|
|
joueur = add_player(&game[index], cliId);
|
|
|
if(joueur == ERR){
|
|
|
stop_game(&game[index]);
|
|
|
- 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)){
|
|
|
+ add_string(&reponse, "status", "501");
|
|
|
+ add_string(&reponse, "message", "Cannot create game");
|
|
|
+ if(!send_client(cliId, &reponse)){
|
|
|
adderror("Impossible de répondre au client");
|
|
|
}
|
|
|
- clean_json_encoder(reponse);
|
|
|
- free(reponse);
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
return FAIL;
|
|
|
}
|
|
|
//Recup info
|
|
|
- reponse = describe_game(&game[index], joueur);
|
|
|
+ describe_game(&game[index], joueur, &reponse);
|
|
|
//Ajout infos
|
|
|
msg = new_string(25 + strlen(map));
|
|
|
sprintf(msg, "Game created with %s", map);
|
|
|
- add_string(reponse, "action", "game/create");
|
|
|
- add_string(reponse, "status", "201");
|
|
|
- add_string(reponse, "message", msg);
|
|
|
- add_string(reponse, "startPos", "0,0");
|
|
|
+ add_string(&reponse, "status", "201");
|
|
|
+ add_string(&reponse, "message", msg);
|
|
|
+ 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);
|
|
|
- free(reponse);
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
return FAIL;
|
|
|
}
|
|
|
//Nettoyage
|
|
|
- clean_json_encoder(reponse);
|
|
|
- free(reponse);
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
free(map);
|
|
|
free(name);
|
|
|
return SUCCESS;
|
|
@@ -231,7 +220,7 @@ int handler_game_create(int cliId, JsonParser* json){
|
|
|
int handler_game_join(int cliId, JsonParser* json){
|
|
|
char* name;
|
|
|
int index, joueur;
|
|
|
- JsonEncoder* reponse;
|
|
|
+ JsonEncoder reponse;
|
|
|
//Verification arguments
|
|
|
if(get_pos(json, "name") == JSON_ERROR){
|
|
|
send_err_client(cliId, EREQUEST);
|
|
@@ -240,61 +229,50 @@ int handler_game_join(int cliId, JsonParser* json){
|
|
|
}
|
|
|
//Recup valeur
|
|
|
name = get_string(json, "name");
|
|
|
+ //Initialisation json reponse
|
|
|
+ ini_encoder(&reponse);
|
|
|
+ add_string(&reponse, "action", "game/join");
|
|
|
//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)){
|
|
|
+ if(index == ERR){
|
|
|
+ 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);
|
|
|
+ clean_json_encoder(&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)){
|
|
|
+ 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);
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
return FAIL;
|
|
|
}
|
|
|
//Recup info
|
|
|
- reponse = describe_game(&game[index], joueur);
|
|
|
+ describe_game(&game[index], joueur, &reponse);
|
|
|
//Ajout infos
|
|
|
- add_string(reponse, "action", "game/join");
|
|
|
- add_string(reponse, "status", "201");
|
|
|
- add_string(reponse, "startPos", "0,0");
|
|
|
+ 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);
|
|
|
- free(reponse);
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
return FAIL;
|
|
|
}
|
|
|
//Nettoyage
|
|
|
- clean_json_encoder(reponse);
|
|
|
- free(reponse);
|
|
|
+ clean_json_encoder(&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, cliId);
|
|
|
+ add_integer(&reponse, "id", cliId);
|
|
|
+ add_string(&reponse, "pos", "0,0");
|
|
|
+ notify_player(&game[index], "POST", "game/newplayer", &reponse, cliId);
|
|
|
//Nettoyage
|
|
|
- clean_json_encoder(reponse);
|
|
|
- free(reponse);
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
return SUCCESS;
|
|
|
}
|