handler.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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_integer(&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_integer(&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_integer(&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_integer(&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_integer(&reponse, "status", 201);
  188. add_string(&reponse, "message", msg);
  189. add_string(&reponse, "startPos", "1,1");
  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_integer(&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. free(name);
  228. return FAIL;
  229. }
  230. //Ajout du joueur dans la partie
  231. joueur = add_player(&game[index], cliId);
  232. if (joueur == ERR) {
  233. add_integer(&reponse, "status", 501);
  234. add_string(&reponse, "message", "Cannot join the game");
  235. if (!send_client(cliId, &reponse)) {
  236. adderror("Impossible de répondre au client");
  237. }
  238. clean_json_encoder(&reponse);
  239. free(name);
  240. return FAIL;
  241. }
  242. //Recup info
  243. describe_game(&game[index], joueur, &reponse);
  244. //Ajout infos
  245. add_integer(&reponse, "status", 201);
  246. add_string(&reponse, "startPos", "1,1");
  247. //Envoi
  248. if (!send_client(cliId, &reponse)) {
  249. adderror("Impossible de répondre au client");
  250. clean_json_encoder(&reponse);
  251. free(name);
  252. return FAIL;
  253. }
  254. //Nettoyage
  255. clean_json_encoder(&reponse);
  256. free(name);
  257. //Avertit les autres joueurs
  258. add_integer(&reponse, "id", cliId);
  259. add_string(&reponse, "pos", "0,0");
  260. notify_player(&game[index], "POST", "game/newplayer", &reponse, cliId);
  261. //Nettoyage
  262. clean_json_encoder(&reponse);
  263. return SUCCESS;
  264. }
  265. int handler_game_quit(int cliId, JsonParser* json) {
  266. boolean find = false;
  267. JsonEncoder notif;
  268. //Cherche le client dans les parties
  269. for (int i = 0; i < MAXGAME; i++) {
  270. pthread_mutex_lock(&gameMutex[i]);
  271. if (game[i].active) {
  272. for (int j = 0; j < MAXPLAYER; j++) {
  273. pthread_mutex_lock(&playerMutex[(i * MAXPLAYER) + j]);
  274. if (game[i].player[j]->ini && game[i].player[j]->id == cliId) {
  275. pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
  276. pthread_mutex_unlock(&gameMutex[i]);
  277. find = true;
  278. remove_player(&game[i], j);
  279. //Avertit les autres joueurs
  280. ini_encoder(&notif);
  281. add_integer(&notif, "player", cliId);
  282. notify_player(&game[i], "POST", "game/quit", &notif, cliId);
  283. clean_json_encoder(&notif);
  284. break;
  285. }
  286. pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
  287. }
  288. if (find) {
  289. break;
  290. }
  291. }
  292. pthread_mutex_unlock(&gameMutex[i]);
  293. }
  294. return SUCCESS;
  295. }
  296. int handler_player_move(int cliId, JsonParser* json) {
  297. char* move;
  298. int index, x = 0, y = 0;
  299. Player* p = NULL;
  300. boolean ok = false, mine = false;
  301. JsonEncoder reponse;
  302. //Verification arguments
  303. if (get_pos(json, "move") == JSON_ERROR) {
  304. return FAIL;
  305. }
  306. //Recup valeur
  307. move = get_string(json, "move");
  308. //Verif game existe
  309. index = search_client_game(cliId);
  310. if (index == ERR) {
  311. free(move);
  312. adderror("La game du client n'existe pas");
  313. return FAIL;
  314. }
  315. //Recup le joueur
  316. pthread_mutex_lock(&gameMutex[index]);
  317. for (int i = 0; i < MAXPLAYER; i++) {
  318. pthread_mutex_lock(&playerMutex[(index * MAXPLAYER) + i]);
  319. if (game[index].player[i]->ini && game[index].player[i]->id == cliId) {
  320. p = game[index].player[i];
  321. pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
  322. break;
  323. }
  324. pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
  325. }
  326. if (p == NULL) {
  327. pthread_mutex_unlock(&gameMutex[index]);
  328. free(move);
  329. adderror("Aucun joueur associé au client");
  330. return FAIL;
  331. }
  332. //Regarde si le joueur peut bouger
  333. if (strncmp(move, "up", 2) == 0) {
  334. 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*/)) {
  335. ok = true;
  336. //Bouge le joueur sur la carte
  337. p->y--;
  338. //Si marche sur une mine
  339. if (game[index].map[p->x][p->y] == '2') {
  340. game[index].map[p->x][p->y] = '_';
  341. mine = true;
  342. x = p->x;
  343. y = p->y;
  344. }
  345. }
  346. } else if (strncmp(move, "down", 4) == 0) {
  347. 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*/)) {
  348. ok = true;
  349. //Bouge le joueur sur la carte
  350. p->y++;
  351. //Si marche sur une mine
  352. if (game[index].map[p->x][p->y] == '2') {
  353. game[index].map[p->x][p->y] = '_';
  354. mine = true;
  355. x = p->x;
  356. y = p->y;
  357. }
  358. }
  359. } else if (strncmp(move, "left", 4) == 0) {
  360. 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*/)) {
  361. ok = true;
  362. //Bouge le joueur sur la carte
  363. p->x--;
  364. //Si marche sur une mine
  365. if (game[index].map[p->x][p->y] == '2') {
  366. game[index].map[p->x][p->y] = '_';
  367. mine = true;
  368. x = p->x;
  369. y = p->y;
  370. }
  371. }
  372. } else if (strncmp(move, "right", 4) == 0) {
  373. 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*/)) {
  374. ok = true;
  375. //Bouge le joueur sur la carte
  376. p->x++;
  377. //Si marche sur une mine
  378. if (game[index].map[p->x][p->y] == '2') {
  379. game[index].map[p->x][p->y] = '_';
  380. mine = true;
  381. x = p->x;
  382. y = p->y;
  383. }
  384. }
  385. } else {
  386. pthread_mutex_unlock(&gameMutex[index]);
  387. free(move);
  388. adderror("Le json du client est incorrect");
  389. return FAIL;
  390. }
  391. pthread_mutex_unlock(&gameMutex[index]);
  392. //Notifie les joueurs si mouvement ok
  393. if (ok) {
  394. ini_encoder(&reponse);
  395. add_integer(&reponse, "player", cliId);
  396. add_string(&reponse, "move", move);
  397. notify_player(&game[index], "POST", "player/position/update", &reponse, -1);
  398. //Si marche sur une mine
  399. if (mine) {
  400. printf("Mine %d %d", x, y);
  401. }
  402. }
  403. //Nettoyage
  404. clean_json_encoder(&reponse);
  405. free(move);
  406. return SUCCESS;
  407. }