/* * File: handler.c * Author: Arthur Brandao * * Created on 23 novembre 2018 */ #include #include #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){ 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; }