handler.c 6.4 KB

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