|
@@ -12,6 +12,7 @@
|
|
|
#include "bomberstudent_server.h"
|
|
|
#include "client.h"
|
|
|
#include "player.h"
|
|
|
+#include "delay.h"
|
|
|
#include "handler.h"
|
|
|
|
|
|
/* --- Extern --- */
|
|
@@ -88,6 +89,7 @@ void ini_handler() {
|
|
|
add_handler("POST", "game/join", handler_game_join);
|
|
|
add_handler("POST", "game/quit", handler_game_quit);
|
|
|
add_handler("POST", "player/move", handler_player_move);
|
|
|
+ add_handler("POST", "object/new", handler_object_new);
|
|
|
}
|
|
|
|
|
|
int handler_client_end(int cliId, JsonParser* json) {
|
|
@@ -417,3 +419,115 @@ int handler_player_move(int cliId, JsonParser* json) {
|
|
|
free(move);
|
|
|
return SUCCESS;
|
|
|
}
|
|
|
+
|
|
|
+int handler_object_new(int cliId, JsonParser* json) {
|
|
|
+ char* class;
|
|
|
+ int index, type, length, playerIndex = 0;
|
|
|
+ Player* p = NULL;
|
|
|
+ obj_node* objn;
|
|
|
+ JsonEncoder reponse;
|
|
|
+ //Verification arguments
|
|
|
+ if (get_pos(json, "class") == JSON_ERROR) {
|
|
|
+ send_err_client(cliId, EREQUEST);
|
|
|
+ adderror("Le json du client est incorrect");
|
|
|
+ return FAIL;
|
|
|
+ }
|
|
|
+ //Recup valeur
|
|
|
+ class = get_string(json, "class");
|
|
|
+ //Verif valeur class
|
|
|
+ length = strlen(class);
|
|
|
+ if (strncmp(class, "classic", length) == 0) {
|
|
|
+ type = OBJ_BCLASSIC;
|
|
|
+ } else if (strncmp(class, "mine", length) == 0) {
|
|
|
+ type = OBJ_BMINE;
|
|
|
+ } else if (strncmp(class, "remote", length) == 0) {
|
|
|
+ type = OBJ_BREMOTE;
|
|
|
+ } else if (strncmp(class, "bomb_up", length) == 0) {
|
|
|
+ type = OBJ_BOMBUP;
|
|
|
+ } else if (strncmp(class, "bomb_down", length) == 0) {
|
|
|
+ type = OBJ_BOMBDOWN;
|
|
|
+ } else if (strncmp(class, "fire_power", length) == 0) {
|
|
|
+ type = OBJ_FIREPOWER;
|
|
|
+ } else if (strncmp(class, "scooter", length) == 0) {
|
|
|
+ type = OBJ_SCOOTER;
|
|
|
+ } else if (strncmp(class, "broken_legs", length) == 0) {
|
|
|
+ type = OBJ_BROKENLEG;
|
|
|
+ } else if (strncmp(class, "life_up", length) == 0) {
|
|
|
+ type = OBJ_LIFEUP;
|
|
|
+ } else if (strncmp(class, "life_max", length) == 0) {
|
|
|
+ type = OBJ_LIFEMAX;
|
|
|
+ } else if (strncmp(class, "major", length) == 0) {
|
|
|
+ type = OBJ_MAJOR;
|
|
|
+ } else {
|
|
|
+ free(class);
|
|
|
+ send_err_client(cliId, EREQUEST);
|
|
|
+ adderror("Le json du client est incorrect");
|
|
|
+ return FAIL;
|
|
|
+ }
|
|
|
+ //Verif game existe
|
|
|
+ index = search_client_game(cliId);
|
|
|
+ if (index == ERR) {
|
|
|
+ free(class);
|
|
|
+ send_err_client(cliId, EREQUEST);
|
|
|
+ adderror("La game du client n'existe pas");
|
|
|
+ return FAIL;
|
|
|
+ }
|
|
|
+ //Recup le joueur
|
|
|
+ pthread_mutex_lock(&gameMutex[index]);
|
|
|
+ for (int i = 0; i < MAXPLAYER; i++) {
|
|
|
+ pthread_mutex_lock(&playerMutex[(index * MAXPLAYER) + i]);
|
|
|
+ if (game[index].player[i]->ini && game[index].player[i]->id == cliId) {
|
|
|
+ playerIndex = i;
|
|
|
+ p = game[index].player[i];
|
|
|
+ pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
|
|
|
+ }
|
|
|
+ if (p == NULL) {
|
|
|
+ pthread_mutex_unlock(&gameMutex[index]);
|
|
|
+ free(class);
|
|
|
+ adderror("Aucun joueur associé au client");
|
|
|
+ return FAIL;
|
|
|
+ }
|
|
|
+ //Initialisation json
|
|
|
+ ini_encoder(&reponse);
|
|
|
+ add_string(&reponse, "action", "object/new");
|
|
|
+ //Regarde si un objet correspond
|
|
|
+ objn = object_search(game[index].object, type, p->x, p->y);
|
|
|
+ if (objn == NULL) {
|
|
|
+ add_integer(&reponse, "status", 501);
|
|
|
+ add_string(&reponse, "message", "No object");
|
|
|
+ //Envoi
|
|
|
+ if (!send_client(cliId, &reponse)) {
|
|
|
+ adderror("Impossible de répondre au client");
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
+ free(class);
|
|
|
+ return FAIL;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //Ajoute l'objet au joueur
|
|
|
+ pthread_mutex_lock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
|
|
|
+ add_player_object(p, type);
|
|
|
+ //Creation reponse
|
|
|
+ add_integer(&reponse, "status", 201);
|
|
|
+ describe_player(p, &reponse);
|
|
|
+ pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
|
|
|
+ //Envoi
|
|
|
+ if (!send_client(cliId, &reponse)) {
|
|
|
+ adderror("Impossible de répondre au client");
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
+ free(class);
|
|
|
+ return FAIL;
|
|
|
+ }
|
|
|
+ //Si major lance le timer pour avertir de la fin
|
|
|
+ if(type == OBJ_MAJOR){
|
|
|
+ delay(TIMEMAJOR, index, playerIndex, callback_major_end);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //Nettoyage
|
|
|
+ pthread_mutex_unlock(&gameMutex[index]);
|
|
|
+ free(class);
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
+ return SUCCESS;
|
|
|
+}
|