handler.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "error.h"
  10. #include "bomberstudent_server.h"
  11. #include "client.h"
  12. #include "game.h"
  13. #include "player.h"
  14. #include "handler.h"
  15. /* --- Extern --- */
  16. extern Game game[MAXGAME];
  17. extern int nbGame;
  18. /* --- Fonctions privées --- */
  19. /**
  20. * Cherche dans quel partie est un client
  21. * @param int L'id du client
  22. * @return int L'index de la partie
  23. */
  24. int search_client_game(int cliId){
  25. int index = ERR;
  26. for(int i = 0; i < MAXGAME; i++){
  27. //Regarde si la game est active et avec des joueurs
  28. if(game[i].active && game[i].nbPlayer > 0){
  29. //Parcours les joueurs
  30. for(int j = 0; j < MAXPLAYER; j++){
  31. //Si le joueur est actif
  32. if(game[i].player[j]->ini){
  33. //Si l'id est le bon
  34. if(game[i].player[j]->id == cliId){
  35. index = i;
  36. break;
  37. }
  38. }
  39. }
  40. }
  41. //Si on a un resultat
  42. if(index != ERR){
  43. break;
  44. }
  45. }
  46. //Retour
  47. return index;
  48. }
  49. /* --- Fonctions publiques --- */
  50. void ini_handler(){
  51. //Get
  52. add_handler("GET", "client/end", handler_client_end);
  53. add_handler("GET", "game/list", handler_game_list);
  54. //Post
  55. add_handler("POST", "game/create", handler_game_create);
  56. }
  57. int handler_client_end(int cliId, JsonParser* json){
  58. printf("Deconnexion du client %d\n", cliId);
  59. remove_client(cliId);
  60. return SUCCESS;
  61. }
  62. int handler_game_list(int cliId, JsonParser* json){
  63. JsonEncoder reponse;
  64. JsonArray* game;
  65. JsonArray* map;
  66. //Recup la liste des parties et des cartes
  67. game = list_game();
  68. map = list_map();
  69. //Creation reponse
  70. ini_encoder(&reponse);
  71. add_string(&reponse, "action", "game/list");
  72. add_string(&reponse, "status", "200");
  73. add_string(&reponse, "message", "ok");
  74. if(game == NULL){
  75. add_integer(&reponse, "numberGameList", 0);
  76. } else {
  77. add_integer(&reponse, "numberGameList", nbGame);
  78. add_array(&reponse, "games", game);
  79. clean_json_array(game);
  80. free(game);
  81. }
  82. add_array(&reponse, "maps", map);
  83. //Envoi reponse au client
  84. if(!send_client(cliId, &reponse)){
  85. adderror("Impossible de répondre au client");
  86. return FAIL;
  87. }
  88. //Nettoyage
  89. clean_json_encoder(&reponse);
  90. clean_json_array(map);
  91. free(map);
  92. return SUCCESS;
  93. }
  94. int handler_game_create(int cliId, JsonParser* json){
  95. char* map, * name, * msg;
  96. int index, joueur;
  97. JsonEncoder* reponse;
  98. //Verification arguments
  99. if(get_pos(json, "name") == JSON_ERROR){
  100. send_err_client(cliId, EREQUEST);
  101. adderror("Le json du client est incorrect");
  102. return FAIL;
  103. }
  104. if(get_pos(json, "map") == JSON_ERROR){
  105. send_err_client(cliId, EREQUEST);
  106. adderror("Le json du client est incorrect");
  107. return FAIL;
  108. }
  109. //Recup valeur
  110. map = get_string(json, "map");
  111. name = get_string(json, "name");
  112. //Creation
  113. index = create_game(name, map);
  114. if(index == ERR){
  115. reponse = malloc(sizeof(JsonEncoder));
  116. ini_encoder(reponse);
  117. add_string(reponse, "status", "501");
  118. add_string(reponse, "message", "Cannot create game");
  119. if(!send_client(cliId, reponse)){
  120. adderror("Impossible de répondre au client");
  121. }
  122. clean_json_encoder(reponse);
  123. free(reponse);
  124. return FAIL;
  125. }
  126. //Ajout du joueur dans la partie
  127. joueur = add_player(&game[index], cliId);
  128. if(joueur == ERR){
  129. stop_game(&game[index]);
  130. reponse = malloc(sizeof(JsonEncoder));
  131. ini_encoder(reponse);
  132. add_string(reponse, "action", "game/create");
  133. add_string(reponse, "status", "501");
  134. add_string(reponse, "message", "Cannot create game");
  135. if(!send_client(cliId, reponse)){
  136. adderror("Impossible de répondre au client");
  137. }
  138. clean_json_encoder(reponse);
  139. free(reponse);
  140. return FAIL;
  141. }
  142. //Recup info
  143. reponse = describe_game(&game[index], joueur);
  144. //Ajout infos
  145. msg = new_string(25 + strlen(map));
  146. sprintf(msg, "Game created with %s", map);
  147. add_string(reponse, "action", "game/create");
  148. add_string(reponse, "status", "201");
  149. add_string(reponse, "message", msg);
  150. add_string(reponse, "startPos", "0,0");
  151. free(msg);
  152. //Envoi
  153. if(!send_client(cliId, reponse)){
  154. adderror("Impossible de répondre au client");
  155. clean_json_encoder(reponse);
  156. free(reponse);
  157. return FAIL;
  158. }
  159. //Nettoyage
  160. clean_json_encoder(reponse);
  161. free(reponse);
  162. free(map);
  163. free(name);
  164. return SUCCESS;
  165. }