handler.c 8.1 KB

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