123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- /*
- * File: handler.c
- * Author: Arthur Brandao
- *
- * Created on 23 novembre 2018
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "error.h"
- #include "bomberstudent_server.h"
- #include "client.h"
- #include "game.h"
- #include "player.h"
- #include "handler.h"
- /* --- Extern --- */
- extern int nbGame;
- /* --- Fonctions publiques --- */
- void ini_handler(){
- add_handler("GET", "client/end", handler_client_end);
- add_handler("GET", "game/list", handler_game_list);
- }
- int handler_client_end(int cliId, JsonParser* json){
- printf("Deconnexion du client %d\n", cliId);
- remove_client(cliId);
- return SUCCESS;
- }
- int handler_game_list(int cliId, JsonParser* json){
- JsonEncoder reponse;
- JsonArray* game;
- JsonArray* map;
- //Recup la liste des parties et des cartes
- game = list_game();
- map = list_map();
- //Creation reponse
- ini_encoder(&reponse);
- add_string(&reponse, "action", "game/list");
- add_string(&reponse, "statut", "200");
- add_string(&reponse, "message", "ok");
- if(game == NULL){
- add_integer(&reponse, "numberGameList", 0);
- } else {
- add_integer(&reponse, "numberGameList", nbGame);
- add_array(&reponse, "games", game);
- clean_json_array(game);
- free(game);
- }
- add_array(&reponse, "maps", map);
- //Envoi reponse au client
- if(!send_client(cliId, &reponse)){
- adderror("Impossible de répondre au client");
- return FAIL;
- }
- //Nettoyage
- clean_json_encoder(&reponse);
- clean_json_array(map);
- free(map);
- return SUCCESS;
- }
|