handler.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 <pthread.h>
  11. #include "error.h"
  12. #include "bomberstudent_server.h"
  13. #include "client.h"
  14. #include "player.h"
  15. #include "handler.h"
  16. /* --- Extern --- */
  17. extern Game game[MAXGAME];
  18. extern int nbGame;
  19. /* --- Fonctions privées --- */
  20. /**
  21. * Thread d'attente de la fonction timer
  22. * @param void* La structure timer_data cast en void*
  23. * @return NULL
  24. */
  25. void* timer_thread(void* data){
  26. timer_data* t;
  27. //Recup données
  28. t = (timer_data*) data;
  29. //Detache le thread
  30. if (pthread_detach(pthread_self()) != 0) {
  31. adderror("Impossible de détacher le thread Timer");
  32. return NULL;
  33. }
  34. //Attend le temps indiqué
  35. sleep(t->second);
  36. //Appel callback
  37. if(t->callback(&game[t->game], t->player) != SUCCESS){
  38. adderror("Erreur callback Timer");
  39. }
  40. return NULL;
  41. }
  42. /**
  43. * Attend X second (sans bloquer l'execution) avant d'executer le callback
  44. * @param int Nombre de second en attente
  45. * @param int Index de la game
  46. * @param int Index du joueur dans la game
  47. * @param int(*)(Game*, int) Le callback
  48. */
  49. void timer(int second, int game, int player, int(*callback)(Game*, int)){
  50. pthread_t thread;
  51. timer_data* t = malloc(sizeof(timer_t));
  52. t->second = second;
  53. t->game = game;
  54. t->player = player;
  55. t->callback = callback;
  56. //Creation thread
  57. if (pthread_create(&thread, NULL, timer_thread, t) != 0) {
  58. adderror("Impossible de créer le thread Timer");
  59. }
  60. }
  61. /**
  62. * Cherche dans quel partie est un client
  63. * @param int L'id du client
  64. * @return int L'index de la partie
  65. */
  66. int search_client_game(int cliId){
  67. int index = ERR;
  68. for(int i = 0; i < MAXGAME; i++){
  69. //Regarde si la game est active et avec des joueurs
  70. if(game[i].active && game[i].nbPlayer > 0){
  71. //Parcours les joueurs
  72. for(int j = 0; j < MAXPLAYER; j++){
  73. //Si le joueur est actif
  74. if(game[i].player[j]->ini){
  75. //Si l'id est le bon
  76. if(game[i].player[j]->id == cliId){
  77. index = i;
  78. break;
  79. }
  80. }
  81. }
  82. }
  83. //Si on a un resultat
  84. if(index != ERR){
  85. break;
  86. }
  87. }
  88. //Retour
  89. return index;
  90. }
  91. /**
  92. * Cherche une partie par son nom
  93. * @param char* Le nom de la partie
  94. * @return int L'index dans le tableau
  95. */
  96. int search_game(char* name){
  97. int index = ERR;
  98. for(int i = 0; i < MAXGAME; i++){
  99. //Regarde si la game est active et le nom
  100. if(game[i].active && strncmp(game[i].name, name, strlen(game[i].name)) == 0){
  101. index = i;
  102. break;
  103. }
  104. }
  105. return index;
  106. }
  107. /* --- Fonctions publiques --- */
  108. void ini_handler(){
  109. //Get
  110. add_handler("GET", "client/end", handler_client_end);
  111. add_handler("GET", "game/list", handler_game_list);
  112. //Post
  113. add_handler("POST", "game/create", handler_game_create);
  114. }
  115. int handler_client_end(int cliId, JsonParser* json){
  116. printf("Deconnexion du client %d\n", cliId);
  117. remove_client(cliId);
  118. return SUCCESS;
  119. }
  120. int handler_game_list(int cliId, JsonParser* json){
  121. JsonEncoder reponse;
  122. JsonArray* game;
  123. JsonArray* map;
  124. //Recup la liste des parties et des cartes
  125. game = list_game();
  126. map = list_map();
  127. //Creation reponse
  128. ini_encoder(&reponse);
  129. add_string(&reponse, "action", "game/list");
  130. add_string(&reponse, "status", "200");
  131. add_string(&reponse, "message", "ok");
  132. if(game == NULL){
  133. add_integer(&reponse, "numberGameList", 0);
  134. } else {
  135. add_integer(&reponse, "numberGameList", nbGame);
  136. add_array(&reponse, "games", game);
  137. clean_json_array(game);
  138. free(game);
  139. }
  140. add_array(&reponse, "maps", map);
  141. //Envoi reponse au client
  142. if(!send_client(cliId, &reponse)){
  143. adderror("Impossible de répondre au client");
  144. return FAIL;
  145. }
  146. //Nettoyage
  147. clean_json_encoder(&reponse);
  148. clean_json_array(map);
  149. free(map);
  150. return SUCCESS;
  151. }
  152. int handler_game_create(int cliId, JsonParser* json){
  153. char* map, * name, * msg;
  154. int index, joueur;
  155. JsonEncoder* reponse;
  156. //Verification arguments
  157. if(get_pos(json, "name") == JSON_ERROR){
  158. send_err_client(cliId, EREQUEST);
  159. adderror("Le json du client est incorrect");
  160. return FAIL;
  161. }
  162. if(get_pos(json, "map") == JSON_ERROR){
  163. send_err_client(cliId, EREQUEST);
  164. adderror("Le json du client est incorrect");
  165. return FAIL;
  166. }
  167. //Recup valeur
  168. map = get_string(json, "map");
  169. name = get_string(json, "name");
  170. //Verif nom non existant
  171. if(search_game(name) != ERR){
  172. reponse = malloc(sizeof(JsonEncoder));
  173. ini_encoder(reponse);
  174. add_string(reponse, "status", "501");
  175. add_string(reponse, "message", "Cannot create game");
  176. if(!send_client(cliId, reponse)){
  177. adderror("Impossible de répondre au client");
  178. }
  179. clean_json_encoder(reponse);
  180. free(reponse);
  181. return FAIL;
  182. }
  183. //Creation
  184. index = create_game(name, map);
  185. if(index == ERR){
  186. reponse = malloc(sizeof(JsonEncoder));
  187. ini_encoder(reponse);
  188. add_string(reponse, "status", "501");
  189. add_string(reponse, "message", "Cannot create game");
  190. if(!send_client(cliId, reponse)){
  191. adderror("Impossible de répondre au client");
  192. }
  193. clean_json_encoder(reponse);
  194. free(reponse);
  195. return FAIL;
  196. }
  197. //Ajout du joueur dans la partie
  198. joueur = add_player(&game[index], cliId);
  199. if(joueur == ERR){
  200. stop_game(&game[index]);
  201. reponse = malloc(sizeof(JsonEncoder));
  202. ini_encoder(reponse);
  203. add_string(reponse, "action", "game/create");
  204. add_string(reponse, "status", "501");
  205. add_string(reponse, "message", "Cannot create game");
  206. if(!send_client(cliId, reponse)){
  207. adderror("Impossible de répondre au client");
  208. }
  209. clean_json_encoder(reponse);
  210. free(reponse);
  211. return FAIL;
  212. }
  213. //Recup info
  214. reponse = describe_game(&game[index], joueur);
  215. //Ajout infos
  216. msg = new_string(25 + strlen(map));
  217. sprintf(msg, "Game created with %s", map);
  218. add_string(reponse, "action", "game/create");
  219. add_string(reponse, "status", "201");
  220. add_string(reponse, "message", msg);
  221. add_string(reponse, "startPos", "0,0");
  222. free(msg);
  223. //Envoi
  224. if(!send_client(cliId, reponse)){
  225. adderror("Impossible de répondre au client");
  226. clean_json_encoder(reponse);
  227. free(reponse);
  228. return FAIL;
  229. }
  230. //Nettoyage
  231. clean_json_encoder(reponse);
  232. free(reponse);
  233. free(map);
  234. free(name);
  235. return SUCCESS;
  236. }