handler.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. pthread_mutex_t gameMutex[MAXGAME];
  19. pthread_mutex_t playerMutex[MAXGAME * MAXPLAYER];
  20. /* --- Fonctions privées --- */
  21. /**
  22. * Cherche dans quel partie est un client
  23. * @param int L'id du client
  24. * @return int L'index de la partie
  25. */
  26. int search_client_game(int cliId) {
  27. int index = ERR;
  28. for (int i = 0; i < MAXGAME; i++) {
  29. pthread_mutex_lock(&gameMutex[i]);
  30. //Regarde si la game est active et avec des joueurs
  31. if (game[i].active && game[i].nbPlayer > 0) {
  32. //Parcours les joueurs
  33. for (int j = 0; j < MAXPLAYER; j++) {
  34. pthread_mutex_lock(&playerMutex[(i * MAXPLAYER) + j]);
  35. //Si le joueur est actif
  36. if (game[i].player[j]->ini) {
  37. //Si l'id est le bon
  38. if (game[i].player[j]->id == cliId) {
  39. index = i;
  40. pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
  41. break;
  42. }
  43. }
  44. pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
  45. }
  46. }
  47. pthread_mutex_unlock(&gameMutex[i]);
  48. //Si on a un resultat
  49. if (index != ERR) {
  50. break;
  51. }
  52. }
  53. //Retour
  54. return index;
  55. }
  56. /**
  57. * Cherche une partie par son nom
  58. * @param char* Le nom de la partie
  59. * @return int L'index dans le tableau
  60. */
  61. int search_game(char* name) {
  62. int index = ERR;
  63. for (int i = 0; i < MAXGAME; i++) {
  64. pthread_mutex_lock(&gameMutex[i]);
  65. //Regarde si la game est active et le nom
  66. if (game[i].active && strncmp(game[i].name, name, strlen(game[i].name)) == 0) {
  67. index = i;
  68. pthread_mutex_unlock(&gameMutex[i]);
  69. break;
  70. }
  71. pthread_mutex_unlock(&gameMutex[i]);
  72. }
  73. return index;
  74. }
  75. /* --- Fonctions publiques --- */
  76. void ini_handler() {
  77. //Get
  78. add_handler("GET", "game/list", handler_game_list);
  79. //Post
  80. add_handler("POST", "client/end", handler_client_end);
  81. add_handler("POST", "game/create", handler_game_create);
  82. add_handler("POST", "game/join", handler_game_join);
  83. add_handler("POST", "game/quit", handler_game_quit);
  84. add_handler("POST", "player/move", handler_player_move);
  85. }
  86. int handler_client_end(int cliId, JsonParser* json) {
  87. //Verif que le client est toujours actif
  88. if (get_client(cliId) != NULL) {
  89. printf("Deconnexion du client %d\n", cliId);
  90. handler_game_quit(cliId, json);
  91. remove_client(cliId);
  92. }
  93. return SUCCESS;
  94. }
  95. int handler_game_list(int cliId, JsonParser* json) {
  96. JsonEncoder reponse;
  97. JsonArray game;
  98. JsonArray map;
  99. int nb;
  100. //Recup la liste des parties et des cartes
  101. ini_array_encoder(&game);
  102. ini_array_encoder(&map);
  103. nb = list_game(&game);
  104. list_map(&map);
  105. //Creation reponse
  106. ini_encoder(&reponse);
  107. add_string(&reponse, "action", "game/list");
  108. add_string(&reponse, "status", "200");
  109. add_string(&reponse, "message", "ok");
  110. if (nb == 0) {
  111. add_integer(&reponse, "numberGameList", 0);
  112. } else {
  113. add_integer(&reponse, "numberGameList", nb);
  114. add_array(&reponse, "games", &game);
  115. }
  116. add_array(&reponse, "maps", &map);
  117. //Envoi reponse au client
  118. if (!send_client(cliId, &reponse)) {
  119. adderror("Impossible de répondre au client");
  120. return FAIL;
  121. }
  122. //Nettoyage
  123. clean_json_encoder(&reponse);
  124. clean_json_array(&map);
  125. clean_json_array(&game);
  126. return SUCCESS;
  127. }
  128. int handler_game_create(int cliId, JsonParser* json) {
  129. char* map, * name, * msg;
  130. int index, joueur;
  131. JsonEncoder reponse;
  132. //Verification arguments
  133. if (get_pos(json, "name") == JSON_ERROR) {
  134. send_err_client(cliId, EREQUEST);
  135. adderror("Le json du client est incorrect");
  136. return FAIL;
  137. }
  138. if (get_pos(json, "map") == JSON_ERROR) {
  139. send_err_client(cliId, EREQUEST);
  140. adderror("Le json du client est incorrect");
  141. return FAIL;
  142. }
  143. //Recup valeur
  144. map = get_string(json, "map");
  145. name = get_string(json, "name");
  146. //Initialisation reponse JSON
  147. ini_encoder(&reponse);
  148. add_string(&reponse, "action", "game/create");
  149. //Verif nom non existant
  150. if (search_game(name) != ERR) {
  151. add_string(&reponse, "status", "501");
  152. add_string(&reponse, "message", "Cannot create game");
  153. if (!send_client(cliId, &reponse)) {
  154. adderror("Impossible de répondre au client");
  155. }
  156. clean_json_encoder(&reponse);
  157. return FAIL;
  158. }
  159. //Creation
  160. index = create_game(name, map);
  161. if (index == ERR) {
  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. return FAIL;
  169. }
  170. //Ajout du joueur dans la partie
  171. joueur = add_player(&game[index], cliId);
  172. if (joueur == ERR) {
  173. stop_game(&game[index]);
  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. return FAIL;
  181. }
  182. //Recup info
  183. describe_game(&game[index], joueur, &reponse);
  184. //Ajout infos
  185. msg = new_string(25 + strlen(map));
  186. sprintf(msg, "Game created with %s", map);
  187. add_string(&reponse, "status", "201");
  188. add_string(&reponse, "message", msg);
  189. add_string(&reponse, "startPos", "0,0");
  190. free(msg);
  191. //Envoi
  192. if (!send_client(cliId, &reponse)) {
  193. adderror("Impossible de répondre au client");
  194. clean_json_encoder(&reponse);
  195. return FAIL;
  196. }
  197. //Nettoyage
  198. clean_json_encoder(&reponse);
  199. free(map);
  200. free(name);
  201. return SUCCESS;
  202. }
  203. int handler_game_join(int cliId, JsonParser* json) {
  204. char* name;
  205. int index, joueur;
  206. JsonEncoder reponse;
  207. //Verification arguments
  208. if (get_pos(json, "name") == JSON_ERROR) {
  209. send_err_client(cliId, EREQUEST);
  210. adderror("Le json du client est incorrect");
  211. return FAIL;
  212. }
  213. //Recup valeur
  214. name = get_string(json, "name");
  215. //Initialisation json reponse
  216. ini_encoder(&reponse);
  217. add_string(&reponse, "action", "game/join");
  218. //Verif game existe
  219. index = search_game(name);
  220. if (index == ERR) {
  221. add_string(&reponse, "status", "501");
  222. add_string(&reponse, "message", "Cannot join the game");
  223. if (!send_client(cliId, &reponse)) {
  224. adderror("Impossible de répondre au client");
  225. }
  226. clean_json_encoder(&reponse);
  227. return FAIL;
  228. }
  229. //Ajout du joueur dans la partie
  230. joueur = add_player(&game[index], cliId);
  231. if (joueur == ERR) {
  232. add_string(&reponse, "status", "501");
  233. add_string(&reponse, "message", "Cannot join the game");
  234. if (!send_client(cliId, &reponse)) {
  235. adderror("Impossible de répondre au client");
  236. }
  237. clean_json_encoder(&reponse);
  238. return FAIL;
  239. }
  240. //Recup info
  241. describe_game(&game[index], joueur, &reponse);
  242. //Ajout infos
  243. add_string(&reponse, "status", "201");
  244. add_string(&reponse, "startPos", "0,0");
  245. //Envoi
  246. if (!send_client(cliId, &reponse)) {
  247. adderror("Impossible de répondre au client");
  248. clean_json_encoder(&reponse);
  249. return FAIL;
  250. }
  251. //Nettoyage
  252. clean_json_encoder(&reponse);
  253. free(name);
  254. //Avertit les autres joueurs
  255. add_integer(&reponse, "id", cliId);
  256. add_string(&reponse, "pos", "0,0");
  257. notify_player(&game[index], "POST", "game/newplayer", &reponse, cliId);
  258. //Nettoyage
  259. clean_json_encoder(&reponse);
  260. return SUCCESS;
  261. }
  262. int handler_game_quit(int cliId, JsonParser* json) {
  263. boolean find = false;
  264. JsonEncoder notif;
  265. //Cherche le client dans les parties
  266. for (int i = 0; i < MAXGAME; i++) {
  267. pthread_mutex_lock(&gameMutex[i]);
  268. if (game[i].active) {
  269. for (int j = 0; j < MAXPLAYER; j++) {
  270. pthread_mutex_lock(&playerMutex[(i * MAXPLAYER) + j]);
  271. if (game[i].player[j]->ini && game[i].player[j]->id == cliId) {
  272. pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
  273. pthread_mutex_unlock(&gameMutex[i]);
  274. find = true;
  275. remove_player(&game[i], j);
  276. //Avertit les autres joueurs
  277. ini_encoder(&notif);
  278. add_integer(&notif, "player", cliId);
  279. notify_player(&game[i], "POST", "game/quit", &notif, cliId);
  280. clean_json_encoder(&notif);
  281. break;
  282. }
  283. pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
  284. }
  285. if (find) {
  286. break;
  287. }
  288. }
  289. pthread_mutex_unlock(&gameMutex[i]);
  290. }
  291. return SUCCESS;
  292. }
  293. int handler_player_move(int cliId, JsonParser* json) {
  294. char* move;
  295. int index, x = 0, y = 0;
  296. Player* p = NULL;
  297. boolean mine = false;
  298. JsonEncoder reponse;
  299. //Verification arguments
  300. if (get_pos(json, "move") == JSON_ERROR) {
  301. return FAIL;
  302. }
  303. //Recup valeur
  304. move = get_string(json, "move");
  305. //Verif game existe
  306. index = search_client_game(cliId);
  307. if (index == ERR) {
  308. free(move);
  309. clean_json_encoder(&reponse);
  310. return FAIL;
  311. }
  312. //Recup le joueur
  313. pthread_mutex_lock(&gameMutex[index]);
  314. for (int i = 0; i < MAXPLAYER; i++) {
  315. pthread_mutex_lock(&playerMutex[(index * MAXPLAYER) + i]);
  316. if (game[index].player[i]->ini && game[index].player[i]->id == cliId) {
  317. p = game[index].player[i];
  318. pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
  319. break;
  320. }
  321. pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
  322. }
  323. if (p == NULL) {
  324. free(move);
  325. return FAIL;
  326. }
  327. //Regarde si le joueur peut bouger
  328. if (strncmp(move, "up", 2) == 0) {
  329. if (p->y > 0 && !player_collision(&game[index], p->x, p->y - 1) && (game[index].map[p->x][p->y - 1] == '_' /*Vide*/ || game[index].map[p->x][p->y - 1] == '2' /*Mine*/)) {
  330. //Bouge le joueur sur la carte
  331. p->y--;
  332. //Si marche sur une mine
  333. if (game[index].map[p->x][p->y] == '2') {
  334. game[index].map[p->x][p->y] = '_';
  335. mine = true;
  336. x = p->x;
  337. y = p->y;
  338. }
  339. }
  340. } else if (strncmp(move, "down", 4) == 0) {
  341. if (p->y < game[index].height && !player_collision(&game[index], p->x, p->y + 1) && (game[index].map[p->x][p->y + 1] == '_' /*Vide*/ || game[index].map[p->x][p->y + 1] == '2' /*Mine*/)) {
  342. //Bouge le joueur sur la carte
  343. p->y++;
  344. //Si marche sur une mine
  345. if (game[index].map[p->x][p->y] == '2') {
  346. game[index].map[p->x][p->y] = '_';
  347. mine = true;
  348. x = p->x;
  349. y = p->y;
  350. }
  351. }
  352. } else if (strncmp(move, "left", 4) == 4) {
  353. if (p->x > 0 && !player_collision(&game[index], p->x - 1, p->y) && (game[index].map[p->x - 1][p->y] == '_' /*Vide*/ || game[index].map[p->x - 1][p->y] == '2' /*Mine*/)) {
  354. //Bouge le joueur sur la carte
  355. p->x--;
  356. //Si marche sur une mine
  357. if (game[index].map[p->x][p->y] == '2') {
  358. game[index].map[p->x][p->y] = '_';
  359. mine = true;
  360. x = p->x;
  361. y = p->y;
  362. }
  363. }
  364. } else if (strncmp(move, "right", 4) == 0) {
  365. if (p->x < game[index].width && !player_collision(&game[index], p->x + 1, p->y) && (game[index].map[p->x + 1][p->y] == '_' /*Vide*/ || game[index].map[p->x + 1][p->y] == '2' /*Mine*/)) {
  366. //Bouge le joueur sur la carte
  367. p->x++;
  368. //Si marche sur une mine
  369. if (game[index].map[p->x][p->y] == '2') {
  370. game[index].map[p->x][p->y] = '_';
  371. mine = true;
  372. x = p->x;
  373. y = p->y;
  374. }
  375. }
  376. } else {
  377. pthread_mutex_unlock(&gameMutex[index]);
  378. free(move);
  379. send_err_client(cliId, EREQUEST);
  380. adderror("Le json du client est incorrect");
  381. return FAIL;
  382. }
  383. pthread_mutex_unlock(&gameMutex[index]);
  384. //Notifie les joueurs
  385. ini_encoder(&reponse);
  386. add_integer(&reponse, "player", cliId);
  387. add_string(&reponse, "move", move);
  388. notify_player(&game[index], "POST", "player/position/update", &reponse, -1);
  389. //Si marche sur une mine
  390. if (mine) {
  391. printf("Mine %d %d", x, y);
  392. }
  393. //Nettoyage
  394. clean_json_encoder(&reponse);
  395. free(move);
  396. return SUCCESS;
  397. }