handler.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. add_handler("POST", "game/join", handler_game_join);
  73. }
  74. int handler_client_end(int cliId, JsonParser* json){
  75. printf("Deconnexion du client %d\n", cliId);
  76. remove_client(cliId);
  77. return SUCCESS;
  78. }
  79. int handler_game_list(int cliId, JsonParser* json){
  80. JsonEncoder reponse;
  81. JsonArray* game;
  82. JsonArray* map;
  83. //Recup la liste des parties et des cartes
  84. game = list_game();
  85. map = list_map();
  86. //Creation reponse
  87. ini_encoder(&reponse);
  88. add_string(&reponse, "action", "game/list");
  89. add_string(&reponse, "status", "200");
  90. add_string(&reponse, "message", "ok");
  91. if(game == NULL){
  92. add_integer(&reponse, "numberGameList", 0);
  93. } else {
  94. add_integer(&reponse, "numberGameList", nbGame);
  95. add_array(&reponse, "games", game);
  96. clean_json_array(game);
  97. free(game);
  98. }
  99. add_array(&reponse, "maps", map);
  100. //Envoi reponse au client
  101. if(!send_client(cliId, &reponse)){
  102. adderror("Impossible de répondre au client");
  103. return FAIL;
  104. }
  105. //Nettoyage
  106. clean_json_encoder(&reponse);
  107. clean_json_array(map);
  108. free(map);
  109. return SUCCESS;
  110. }
  111. int handler_game_create(int cliId, JsonParser* json){
  112. char* map, * name, * msg;
  113. int index, joueur;
  114. JsonEncoder* reponse;
  115. //Verification arguments
  116. if(get_pos(json, "name") == JSON_ERROR){
  117. send_err_client(cliId, EREQUEST);
  118. adderror("Le json du client est incorrect");
  119. return FAIL;
  120. }
  121. if(get_pos(json, "map") == JSON_ERROR){
  122. send_err_client(cliId, EREQUEST);
  123. adderror("Le json du client est incorrect");
  124. return FAIL;
  125. }
  126. //Recup valeur
  127. map = get_string(json, "map");
  128. name = get_string(json, "name");
  129. //Verif nom non existant
  130. if(search_game(name) != ERR){
  131. reponse = malloc(sizeof(JsonEncoder));
  132. ini_encoder(reponse);
  133. add_string(reponse, "action", "game/create");
  134. add_string(reponse, "status", "501");
  135. add_string(reponse, "message", "Cannot create game");
  136. if(!send_client(cliId, reponse)){
  137. adderror("Impossible de répondre au client");
  138. }
  139. clean_json_encoder(reponse);
  140. free(reponse);
  141. return FAIL;
  142. }
  143. //Creation
  144. index = create_game(name, map);
  145. if(index == ERR){
  146. reponse = malloc(sizeof(JsonEncoder));
  147. ini_encoder(reponse);
  148. add_string(reponse, "action", "game/create");
  149. add_string(reponse, "status", "501");
  150. add_string(reponse, "message", "Cannot create game");
  151. if(!send_client(cliId, reponse)){
  152. adderror("Impossible de répondre au client");
  153. }
  154. clean_json_encoder(reponse);
  155. free(reponse);
  156. return FAIL;
  157. }
  158. //Ajout du joueur dans la partie
  159. joueur = add_player(&game[index], cliId);
  160. if(joueur == ERR){
  161. stop_game(&game[index]);
  162. reponse = malloc(sizeof(JsonEncoder));
  163. ini_encoder(reponse);
  164. add_string(reponse, "action", "game/create");
  165. add_string(reponse, "status", "501");
  166. add_string(reponse, "message", "Cannot create game");
  167. if(!send_client(cliId, reponse)){
  168. adderror("Impossible de répondre au client");
  169. }
  170. clean_json_encoder(reponse);
  171. free(reponse);
  172. return FAIL;
  173. }
  174. //Recup info
  175. reponse = describe_game(&game[index], joueur);
  176. //Ajout infos
  177. msg = new_string(25 + strlen(map));
  178. sprintf(msg, "Game created with %s", map);
  179. add_string(reponse, "action", "game/create");
  180. add_string(reponse, "status", "201");
  181. add_string(reponse, "message", msg);
  182. add_string(reponse, "startPos", "0,0");
  183. free(msg);
  184. //Envoi
  185. if(!send_client(cliId, reponse)){
  186. adderror("Impossible de répondre au client");
  187. clean_json_encoder(reponse);
  188. free(reponse);
  189. return FAIL;
  190. }
  191. //Nettoyage
  192. clean_json_encoder(reponse);
  193. free(reponse);
  194. free(map);
  195. free(name);
  196. return SUCCESS;
  197. }
  198. int handler_game_join(int cliId, JsonParser* json){
  199. char* name;
  200. int index, joueur;
  201. JsonEncoder* reponse;
  202. //Verification arguments
  203. if(get_pos(json, "name") == JSON_ERROR){
  204. send_err_client(cliId, EREQUEST);
  205. adderror("Le json du client est incorrect");
  206. return FAIL;
  207. }
  208. //Recup valeur
  209. name = get_string(json, "name");
  210. //Verif nom non existant
  211. index = search_game(name);
  212. if(index == ERR){
  213. reponse = malloc(sizeof(JsonEncoder));
  214. ini_encoder(reponse);
  215. add_string(reponse, "action", "game/join");
  216. add_string(reponse, "status", "501");
  217. add_string(reponse, "message", "Cannot join the game game");
  218. if(!send_client(cliId, reponse)){
  219. adderror("Impossible de répondre au client");
  220. }
  221. clean_json_encoder(reponse);
  222. free(reponse);
  223. return FAIL;
  224. }
  225. //Ajout du joueur dans la partie
  226. joueur = add_player(&game[index], cliId);
  227. if(joueur == ERR){
  228. reponse = malloc(sizeof(JsonEncoder));
  229. ini_encoder(reponse);
  230. add_string(reponse, "action", "game/join");
  231. add_string(reponse, "status", "501");
  232. add_string(reponse, "message", "Cannot join the game");
  233. if(!send_client(cliId, reponse)){
  234. adderror("Impossible de répondre au client");
  235. }
  236. clean_json_encoder(reponse);
  237. free(reponse);
  238. return FAIL;
  239. }
  240. //Recup info
  241. reponse = describe_game(&game[index], joueur);
  242. //Ajout infos
  243. add_string(reponse, "action", "game/join");
  244. add_string(reponse, "status", "201");
  245. add_string(reponse, "startPos", "0,0");
  246. //Envoi
  247. if(!send_client(cliId, reponse)){
  248. adderror("Impossible de répondre au client");
  249. clean_json_encoder(reponse);
  250. free(reponse);
  251. return FAIL;
  252. }
  253. //Nettoyage
  254. clean_json_encoder(reponse);
  255. free(reponse);
  256. free(name);
  257. //Avertit les autres joueurs
  258. reponse = malloc(sizeof(JsonEncoder));
  259. ini_encoder(reponse);
  260. add_integer(reponse, "id", cliId);
  261. add_string(reponse, "pos", "0,0");
  262. notify_player(&game[index], "POST", "game/newplayer", reponse);
  263. //Nettoyage
  264. clean_json_encoder(reponse);
  265. free(reponse);
  266. return SUCCESS;
  267. }