handler.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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", "client/end", handler_client_end);
  79. add_handler("GET", "game/list", handler_game_list);
  80. //Post
  81. add_handler("POST", "game/create", handler_game_create);
  82. add_handler("POST", "game/join", handler_game_join);
  83. add_handler("POST", "player/move", handler_player_move);
  84. }
  85. int handler_client_end(int cliId, JsonParser* json) {
  86. boolean find = false;
  87. JsonEncoder* notif;
  88. printf("Deconnexion du client %d\n", cliId);
  89. //Cherche le client dans les parties
  90. for (int i = 0; i < MAXGAME; i++) {
  91. pthread_mutex_lock(&gameMutex[i]);
  92. if (game[i].active) {
  93. for (int j = 0; j < MAXPLAYER; j++) {
  94. pthread_mutex_lock(&playerMutex[(i * MAXPLAYER) + j]);
  95. if (game[i].player[j]->ini && game[i].player[j]->id == cliId) {
  96. pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
  97. pthread_mutex_unlock(&gameMutex[i]);
  98. find = true;
  99. remove_player(&game[i], j);
  100. //Avertit les autres joueurs
  101. notif = malloc(sizeof (JsonEncoder));
  102. ini_encoder(notif);
  103. add_integer(notif, "player", cliId);
  104. notify_player(&game[i], "POST", "game/quit", notif, cliId);
  105. clean_json_encoder(notif);
  106. free(notif);
  107. break;
  108. }
  109. pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
  110. }
  111. if (find) {
  112. break;
  113. }
  114. }
  115. pthread_mutex_unlock(&gameMutex[i]);
  116. }
  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. int nb;
  125. //Recup la liste des parties et des cartes
  126. ini_array_encoder(&game);
  127. ini_array_encoder(&map);
  128. nb = list_game(&game);
  129. list_map(&map);
  130. //Creation reponse
  131. ini_encoder(&reponse);
  132. add_string(&reponse, "action", "game/list");
  133. add_string(&reponse, "status", "200");
  134. add_string(&reponse, "message", "ok");
  135. if (nb == 0) {
  136. add_integer(&reponse, "numberGameList", 0);
  137. } else {
  138. add_integer(&reponse, "numberGameList", nb);
  139. add_array(&reponse, "games", &game);
  140. }
  141. add_array(&reponse, "maps", &map);
  142. //Envoi reponse au client
  143. if (!send_client(cliId, &reponse)) {
  144. adderror("Impossible de répondre au client");
  145. return FAIL;
  146. }
  147. //Nettoyage
  148. clean_json_encoder(&reponse);
  149. clean_json_array(&map);
  150. clean_json_array(&game);
  151. return SUCCESS;
  152. }
  153. int handler_game_create(int cliId, JsonParser* json) {
  154. char* map, * name, * msg;
  155. int index, joueur;
  156. JsonEncoder reponse;
  157. //Verification arguments
  158. if (get_pos(json, "name") == JSON_ERROR) {
  159. send_err_client(cliId, EREQUEST);
  160. adderror("Le json du client est incorrect");
  161. return FAIL;
  162. }
  163. if (get_pos(json, "map") == JSON_ERROR) {
  164. send_err_client(cliId, EREQUEST);
  165. adderror("Le json du client est incorrect");
  166. return FAIL;
  167. }
  168. //Recup valeur
  169. map = get_string(json, "map");
  170. name = get_string(json, "name");
  171. //Initialisation reponse JSON
  172. ini_encoder(&reponse);
  173. add_string(&reponse, "action", "game/create");
  174. //Verif nom non existant
  175. if (search_game(name) != ERR) {
  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. return FAIL;
  183. }
  184. //Creation
  185. index = create_game(name, map);
  186. if (index == ERR) {
  187. add_string(&reponse, "status", "501");
  188. add_string(&reponse, "message", "Cannot create game");
  189. if (!send_client(cliId, &reponse)) {
  190. adderror("Impossible de répondre au client");
  191. }
  192. clean_json_encoder(&reponse);
  193. return FAIL;
  194. }
  195. //Ajout du joueur dans la partie
  196. joueur = add_player(&game[index], cliId);
  197. if (joueur == ERR) {
  198. stop_game(&game[index]);
  199. add_string(&reponse, "status", "501");
  200. add_string(&reponse, "message", "Cannot create game");
  201. if (!send_client(cliId, &reponse)) {
  202. adderror("Impossible de répondre au client");
  203. }
  204. clean_json_encoder(&reponse);
  205. return FAIL;
  206. }
  207. //Recup info
  208. describe_game(&game[index], joueur, &reponse);
  209. //Ajout infos
  210. msg = new_string(25 + strlen(map));
  211. sprintf(msg, "Game created with %s", map);
  212. add_string(&reponse, "status", "201");
  213. add_string(&reponse, "message", msg);
  214. add_string(&reponse, "startPos", "0,0");
  215. free(msg);
  216. //Envoi
  217. if (!send_client(cliId, &reponse)) {
  218. adderror("Impossible de répondre au client");
  219. clean_json_encoder(&reponse);
  220. return FAIL;
  221. }
  222. //Nettoyage
  223. clean_json_encoder(&reponse);
  224. free(map);
  225. free(name);
  226. return SUCCESS;
  227. }
  228. int handler_game_join(int cliId, JsonParser* json) {
  229. char* name;
  230. int index, joueur;
  231. JsonEncoder reponse;
  232. //Verification arguments
  233. if (get_pos(json, "name") == JSON_ERROR) {
  234. send_err_client(cliId, EREQUEST);
  235. adderror("Le json du client est incorrect");
  236. return FAIL;
  237. }
  238. //Recup valeur
  239. name = get_string(json, "name");
  240. //Initialisation json reponse
  241. ini_encoder(&reponse);
  242. add_string(&reponse, "action", "game/join");
  243. //Verif game existe
  244. index = search_game(name);
  245. if (index == ERR) {
  246. add_string(&reponse, "status", "501");
  247. add_string(&reponse, "message", "Cannot join the game");
  248. if (!send_client(cliId, &reponse)) {
  249. adderror("Impossible de répondre au client");
  250. }
  251. clean_json_encoder(&reponse);
  252. return FAIL;
  253. }
  254. //Ajout du joueur dans la partie
  255. joueur = add_player(&game[index], cliId);
  256. if (joueur == ERR) {
  257. add_string(&reponse, "status", "501");
  258. add_string(&reponse, "message", "Cannot join the game");
  259. if (!send_client(cliId, &reponse)) {
  260. adderror("Impossible de répondre au client");
  261. }
  262. clean_json_encoder(&reponse);
  263. return FAIL;
  264. }
  265. //Recup info
  266. describe_game(&game[index], joueur, &reponse);
  267. //Ajout infos
  268. add_string(&reponse, "status", "201");
  269. add_string(&reponse, "startPos", "0,0");
  270. //Envoi
  271. if (!send_client(cliId, &reponse)) {
  272. adderror("Impossible de répondre au client");
  273. clean_json_encoder(&reponse);
  274. return FAIL;
  275. }
  276. //Nettoyage
  277. clean_json_encoder(&reponse);
  278. free(name);
  279. //Avertit les autres joueurs
  280. add_integer(&reponse, "id", cliId);
  281. add_string(&reponse, "pos", "0,0");
  282. notify_player(&game[index], "POST", "game/newplayer", &reponse, cliId);
  283. //Nettoyage
  284. clean_json_encoder(&reponse);
  285. return SUCCESS;
  286. }
  287. int handler_player_move(int cliId, JsonParser* json) {
  288. printf("Player Move\n");
  289. char* move;
  290. int index, x = 0, y = 0;
  291. Player* p = NULL;
  292. boolean mine = false;
  293. JsonEncoder reponse;
  294. //Verification arguments
  295. if (get_pos(json, "move") == JSON_ERROR) {
  296. return FAIL;
  297. }
  298. //Recup valeur
  299. move = get_string(json, "move");
  300. //Verif game existe
  301. index = search_client_game(cliId);
  302. if (index == ERR) {
  303. free(move);
  304. clean_json_encoder(&reponse);
  305. return FAIL;
  306. }
  307. //Recup le joueur
  308. pthread_mutex_lock(&gameMutex[index]);
  309. for(int i = 0; i < MAXPLAYER; i++){
  310. pthread_mutex_lock(&playerMutex[(index * MAXPLAYER) + i]);
  311. if(game[index].player[i]->ini && game[index].player[i]->id == cliId){
  312. p = game[index].player[i];
  313. pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
  314. break;
  315. }
  316. pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
  317. }
  318. if(p == NULL){
  319. free(move);
  320. return FAIL;
  321. }
  322. //Regarde si le joueur peut bouger
  323. if (strncmp(move, "up", 2) == 0) {
  324. 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*/ )){
  325. //Bouge le joueur sur la carte
  326. p->y--;
  327. //Si marche sur une mine
  328. if(game[index].map[p->x][p->y] == '2'){
  329. game[index].map[p->x][p->y] = '_';
  330. mine = true;
  331. x = p->x;
  332. y = p->y;
  333. }
  334. }
  335. } else if (strncmp(move, "down", 4) == 0) {
  336. 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*/ )){
  337. //Bouge le joueur sur la carte
  338. p->y++;
  339. //Si marche sur une mine
  340. if(game[index].map[p->x][p->y] == '2'){
  341. game[index].map[p->x][p->y] = '_';
  342. mine = true;
  343. x = p->x;
  344. y = p->y;
  345. }
  346. }
  347. } else if (strncmp(move, "left", 4) == 4) {
  348. 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*/ )){
  349. //Bouge le joueur sur la carte
  350. p->x--;
  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, "right", 4) == 0){
  360. 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*/ )){
  361. //Bouge le joueur sur la carte
  362. p->x++;
  363. //Si marche sur une mine
  364. if(game[index].map[p->x][p->y] == '2'){
  365. game[index].map[p->x][p->y] = '_';
  366. mine = true;
  367. x = p->x;
  368. y = p->y;
  369. }
  370. }
  371. } else {
  372. pthread_mutex_unlock(&gameMutex[index]);
  373. free(move);
  374. send_err_client(cliId, EREQUEST);
  375. adderror("Le json du client est incorrect");
  376. return FAIL;
  377. }
  378. pthread_mutex_unlock(&gameMutex[index]);
  379. //Notifie les joueurs
  380. ini_encoder(&reponse);
  381. add_integer(&reponse, "player", cliId);
  382. add_string(&reponse, "move", move);
  383. notify_player(&game[index], "POST", "player/position/update", &reponse, -1);
  384. //Si marche sur une mine
  385. if(mine){
  386. printf("Mine %d %d", x, y);
  387. }
  388. //Nettoyage
  389. clean_json_encoder(&reponse);
  390. free(move);
  391. return SUCCESS;
  392. }