handler.c 1.5 KB

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