handler.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * File: handler.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 23 novembre 2018
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include "error.h"
  11. #include "bomberstudent_server.h"
  12. #include "client.h"
  13. #include "player.h"
  14. #include "handler.h"
  15. /* --- Extern --- */
  16. extern Game game[MAXGAME];
  17. extern int nbGame;
  18. /* --- Fonctions privées --- */
  19. /**
  20. * Cherche dans quel partie est un client
  21. * @param int L'id du client
  22. * @return int L'index de la partie
  23. */
  24. int search_client_game(int cliId){
  25. int index = ERR;
  26. for(int i = 0; i < MAXGAME; i++){
  27. //Regarde si la game est active et avec des joueurs
  28. if(game[i].active && game[i].nbPlayer > 0){
  29. //Parcours les joueurs
  30. for(int j = 0; j < MAXPLAYER; j++){
  31. //Si le joueur est actif
  32. if(game[i].player[j]->ini){
  33. //Si l'id est le bon
  34. if(game[i].player[j]->id == cliId){
  35. index = i;
  36. break;
  37. }
  38. }
  39. }
  40. }
  41. //Si on a un resultat
  42. if(index != ERR){
  43. break;
  44. }
  45. }
  46. //Retour
  47. return index;
  48. }
  49. /**
  50. * Cherche une partie par son nom
  51. * @param char* Le nom de la partie
  52. * @return int L'index dans le tableau
  53. */
  54. int search_game(char* name){
  55. int index = ERR;
  56. for(int i = 0; i < MAXGAME; i++){
  57. //Regarde si la game est active et le nom
  58. if(game[i].active && strncmp(game[i].name, name, strlen(game[i].name)) == 0){
  59. index = i;
  60. break;
  61. }
  62. }
  63. return index;
  64. }
  65. /* --- Fonctions publiques --- */
  66. void ini_handler(){
  67. //Get
  68. add_handler("GET", "client/end", handler_client_end);
  69. add_handler("GET", "game/list", handler_game_list);
  70. //Post
  71. add_handler("POST", "game/create", handler_game_create);
  72. }
  73. int handler_client_end(int cliId, JsonParser* json){
  74. printf("Deconnexion du client %d\n", cliId);
  75. remove_client(cliId);
  76. return SUCCESS;
  77. }
  78. int handler_game_list(int cliId, JsonParser* json){
  79. JsonEncoder reponse;
  80. JsonArray* game;
  81. JsonArray* map;
  82. //Recup la liste des parties et des cartes
  83. game = list_game();
  84. map = list_map();
  85. //Creation reponse
  86. ini_encoder(&reponse);
  87. add_string(&reponse, "action", "game/list");
  88. add_string(&reponse, "status", "200");
  89. add_string(&reponse, "message", "ok");
  90. if(game == NULL){
  91. add_integer(&reponse, "numberGameList", 0);
  92. } else {
  93. add_integer(&reponse, "numberGameList", nbGame);
  94. add_array(&reponse, "games", game);
  95. clean_json_array(game);
  96. free(game);
  97. }
  98. add_array(&reponse, "maps", map);
  99. //Envoi reponse au client
  100. if(!send_client(cliId, &reponse)){
  101. adderror("Impossible de répondre au client");
  102. return FAIL;
  103. }
  104. //Nettoyage
  105. clean_json_encoder(&reponse);
  106. clean_json_array(map);
  107. free(map);
  108. return SUCCESS;
  109. }
  110. int handler_game_create(int cliId, JsonParser* json){
  111. char* map, * name, * msg;
  112. int index, joueur;
  113. JsonEncoder* reponse;
  114. //Verification arguments
  115. if(get_pos(json, "name") == JSON_ERROR){
  116. send_err_client(cliId, EREQUEST);
  117. adderror("Le json du client est incorrect");
  118. return FAIL;
  119. }
  120. if(get_pos(json, "map") == JSON_ERROR){
  121. send_err_client(cliId, EREQUEST);
  122. adderror("Le json du client est incorrect");
  123. return FAIL;
  124. }
  125. //Recup valeur
  126. map = get_string(json, "map");
  127. name = get_string(json, "name");
  128. //Verif nom non existant
  129. if(search_game(name) != ERR){
  130. reponse = malloc(sizeof(JsonEncoder));
  131. ini_encoder(reponse);
  132. add_string(reponse, "status", "501");
  133. add_string(reponse, "message", "Cannot create game");
  134. if(!send_client(cliId, reponse)){
  135. adderror("Impossible de répondre au client");
  136. }
  137. clean_json_encoder(reponse);
  138. free(reponse);
  139. return FAIL;
  140. }
  141. //Creation
  142. index = create_game(name, map);
  143. if(index == ERR){
  144. reponse = malloc(sizeof(JsonEncoder));
  145. ini_encoder(reponse);
  146. add_string(reponse, "status", "501");
  147. add_string(reponse, "message", "Cannot create game");
  148. if(!send_client(cliId, reponse)){
  149. adderror("Impossible de répondre au client");
  150. }
  151. clean_json_encoder(reponse);
  152. free(reponse);
  153. return FAIL;
  154. }
  155. //Ajout du joueur dans la partie
  156. joueur = add_player(&game[index], cliId);
  157. if(joueur == ERR){
  158. stop_game(&game[index]);
  159. reponse = malloc(sizeof(JsonEncoder));
  160. ini_encoder(reponse);
  161. add_string(reponse, "action", "game/create");
  162. add_string(reponse, "status", "501");
  163. add_string(reponse, "message", "Cannot create game");
  164. if(!send_client(cliId, reponse)){
  165. adderror("Impossible de répondre au client");
  166. }
  167. clean_json_encoder(reponse);
  168. free(reponse);
  169. return FAIL;
  170. }
  171. //Recup info
  172. reponse = describe_game(&game[index], joueur);
  173. //Ajout infos
  174. msg = new_string(25 + strlen(map));
  175. sprintf(msg, "Game created with %s", map);
  176. add_string(reponse, "action", "game/create");
  177. add_string(reponse, "status", "201");
  178. add_string(reponse, "message", msg);
  179. add_string(reponse, "startPos", "0,0");
  180. free(msg);
  181. //Envoi
  182. if(!send_client(cliId, reponse)){
  183. adderror("Impossible de répondre au client");
  184. clean_json_encoder(reponse);
  185. free(reponse);
  186. return FAIL;
  187. }
  188. //Nettoyage
  189. clean_json_encoder(reponse);
  190. free(reponse);
  191. free(map);
  192. free(name);
  193. return SUCCESS;
  194. }