handler.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 int nbGame;
  17. /* --- Fonctions publiques --- */
  18. void ini_handler(){
  19. add_handler("GET", "client/end", handler_client_end);
  20. add_handler("GET", "game/list", handler_game_list);
  21. }
  22. int handler_client_end(int cliId, JsonParser* json){
  23. printf("Deconnexion du client %d\n", cliId);
  24. remove_client(cliId);
  25. return SUCCESS;
  26. }
  27. int handler_game_list(int cliId, JsonParser* json){
  28. JsonEncoder reponse;
  29. JsonArray* game;
  30. JsonArray* map;
  31. //Recup la liste des parties et des cartes
  32. game = list_game();
  33. map = list_map();
  34. //Creation reponse
  35. ini_encoder(&reponse);
  36. add_string(&reponse, "action", "game/list");
  37. add_string(&reponse, "statut", "200");
  38. add_string(&reponse, "message", "ok");
  39. if(game == NULL){
  40. add_integer(&reponse, "numberGameList", 0);
  41. } else {
  42. add_integer(&reponse, "numberGameList", nbGame);
  43. add_array(&reponse, "games", game);
  44. clean_json_array(game);
  45. free(game);
  46. }
  47. add_array(&reponse, "maps", map);
  48. //Envoi reponse au client
  49. if(!send_client(cliId, &reponse)){
  50. adderror("Impossible de répondre au client");
  51. return FAIL;
  52. }
  53. //Nettoyage
  54. clean_json_encoder(&reponse);
  55. clean_json_array(map);
  56. free(map);
  57. return SUCCESS;
  58. }