|
@@ -79,6 +79,36 @@ int search_game(char* name) {
|
|
|
return index;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Parse une postion en 2 entier X et Y
|
|
|
+ * @param char* La position
|
|
|
+ * @param int* Le resultat en X
|
|
|
+ * @param int Le resultat en Y
|
|
|
+ * @return Reussite
|
|
|
+ */
|
|
|
+boolean parse_pos(char* pos, int* x, int* y) {
|
|
|
+ int index = 0;
|
|
|
+ char* cy, * cx;
|
|
|
+ //Cherche la separation
|
|
|
+ while (pos[index] != '\0' && pos[index] != ',') {
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ if (pos[index] == '\0') {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //Separation chaine
|
|
|
+ cx = malloc(sizeof (char) * index + 1);
|
|
|
+ memset(cx, 0, index + 1);
|
|
|
+ strncpy(cx, pos, index);
|
|
|
+ cy = pos + index + 1;
|
|
|
+ //Parse
|
|
|
+ *x = atoi(cx);
|
|
|
+ *y = atoi(cy);
|
|
|
+ //Nettoyage
|
|
|
+ free(cx);
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
/* --- Fonctions publiques --- */
|
|
|
void ini_handler() {
|
|
|
//Get
|
|
@@ -90,6 +120,7 @@ void ini_handler() {
|
|
|
add_handler("POST", "game/quit", handler_game_quit);
|
|
|
add_handler("POST", "player/move", handler_player_move);
|
|
|
add_handler("POST", "object/new", handler_object_new);
|
|
|
+ add_handler("POST", "attack/bomb", handler_attack_bomb);
|
|
|
}
|
|
|
|
|
|
int handler_client_end(int cliId, JsonParser* json) {
|
|
@@ -268,7 +299,7 @@ int handler_game_join(int cliId, JsonParser* json) {
|
|
|
free(name);
|
|
|
//Avertit les autres joueurs
|
|
|
add_integer(&reponse, "id", cliId);
|
|
|
- add_string(&reponse, "pos", "0,0");
|
|
|
+ add_string(&reponse, "pos", "1,1");
|
|
|
notify_player(&game[index], "POST", "game/newplayer", &reponse, cliId);
|
|
|
//Nettoyage
|
|
|
clean_json_encoder(&reponse);
|
|
@@ -309,7 +340,7 @@ int handler_game_quit(int cliId, JsonParser* json) {
|
|
|
|
|
|
int handler_player_move(int cliId, JsonParser* json) {
|
|
|
char* move;
|
|
|
- int index, x = 0, y = 0;
|
|
|
+ int index, playerIndex = 0, x = 0, y = 0;
|
|
|
Player* p = NULL;
|
|
|
boolean ok = false, mine = false;
|
|
|
JsonEncoder reponse;
|
|
@@ -331,8 +362,8 @@ int handler_player_move(int cliId, JsonParser* json) {
|
|
|
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]);
|
|
@@ -397,11 +428,13 @@ int handler_player_move(int cliId, JsonParser* json) {
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
+ pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
|
|
|
pthread_mutex_unlock(&gameMutex[index]);
|
|
|
free(move);
|
|
|
adderror("Le json du client est incorrect");
|
|
|
return FAIL;
|
|
|
}
|
|
|
+ pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
|
|
|
pthread_mutex_unlock(&gameMutex[index]);
|
|
|
//Notifie les joueurs si mouvement ok
|
|
|
if (ok) {
|
|
@@ -487,6 +520,7 @@ int handler_object_new(int cliId, JsonParser* json) {
|
|
|
if (p == NULL) {
|
|
|
pthread_mutex_unlock(&gameMutex[index]);
|
|
|
free(class);
|
|
|
+ send_err_client(cliId, EREQUEST);
|
|
|
adderror("Aucun joueur associé au client");
|
|
|
return FAIL;
|
|
|
}
|
|
@@ -521,7 +555,7 @@ int handler_object_new(int cliId, JsonParser* json) {
|
|
|
return FAIL;
|
|
|
}
|
|
|
//Si major lance le timer pour avertir de la fin
|
|
|
- if(type == OBJ_MAJOR){
|
|
|
+ if (type == OBJ_MAJOR) {
|
|
|
delay(TIMEMAJOR, index, playerIndex, callback_major_end);
|
|
|
}
|
|
|
}
|
|
@@ -530,4 +564,139 @@ int handler_object_new(int cliId, JsonParser* json) {
|
|
|
free(class);
|
|
|
clean_json_encoder(&reponse);
|
|
|
return SUCCESS;
|
|
|
+}
|
|
|
+
|
|
|
+int handler_attack_bomb(int cliId, JsonParser* json) {
|
|
|
+ char* pos, * class;
|
|
|
+ int x, y, length, index, playerIndex = 0;
|
|
|
+ Player* p = NULL;
|
|
|
+ boolean ok = false;
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ if (get_pos(json, "pos") == JSON_ERROR) {
|
|
|
+ send_err_client(cliId, EREQUEST);
|
|
|
+ adderror("Le json du client est incorrect");
|
|
|
+ return FAIL;
|
|
|
+ }
|
|
|
+ //Recup valeur
|
|
|
+ class = get_string(json, "class");
|
|
|
+ pos = get_string(json, "pos");
|
|
|
+ //Parse les valeurs de pos
|
|
|
+ if (!parse_pos(pos, &x, &y)) {
|
|
|
+ free(class);
|
|
|
+ free(pos);
|
|
|
+ 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);
|
|
|
+ free(pos);
|
|
|
+ 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];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
|
|
|
+ }
|
|
|
+ if (p == NULL) {
|
|
|
+ pthread_mutex_unlock(&gameMutex[index]);
|
|
|
+ free(class);
|
|
|
+ free(pos);
|
|
|
+ send_err_client(cliId, EREQUEST);
|
|
|
+ adderror("Aucun joueur associé au client");
|
|
|
+ return FAIL;
|
|
|
+ }
|
|
|
+ //Verif si la bombe peut être posé et pose
|
|
|
+ length = strlen(class);
|
|
|
+ if (strncmp(class, "classic", length) == 0) {
|
|
|
+ ok = p->classicBomb > 0 && p->nbBomb > p->maxBomb && game[index].map[x][y] == '_' && !player_collision(&game[index], x, y);
|
|
|
+ if (ok) {
|
|
|
+ p->nbBomb++;
|
|
|
+ p->classicBomb--;
|
|
|
+ game[index].map[x][y] = '1';
|
|
|
+ }
|
|
|
+ } else if (strncmp(class, "mine", length) == 0) {
|
|
|
+ ok = p->mine > 0 && p->nbBomb > p->maxBomb && game[index].map[x][y] == '_' && !player_collision(&game[index], x, y);
|
|
|
+ if (ok) {
|
|
|
+ p->nbBomb++;
|
|
|
+ p->mine--;
|
|
|
+ game[index].map[x][y] = '2';
|
|
|
+ }
|
|
|
+ } else if (strncmp(class, "remote", length) == 0) {
|
|
|
+ ok = p->remoteBomb > 0 && p->nbBomb > p->maxBomb && game[index].map[x][y] == '_' && !player_collision(&game[index], x, y);
|
|
|
+ if (ok) {
|
|
|
+ p->nbBomb++;
|
|
|
+ p->remoteBomb--;
|
|
|
+ game[index].map[x][y] = '3';
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
|
|
|
+ pthread_mutex_unlock(&gameMutex[index]);
|
|
|
+ free(pos);
|
|
|
+ free(class);
|
|
|
+ send_err_client(cliId, EREQUEST);
|
|
|
+ adderror("Le json du client est incorrect");
|
|
|
+ return FAIL;
|
|
|
+ }
|
|
|
+ //Initialisation json
|
|
|
+ ini_encoder(&reponse);
|
|
|
+ add_string(&reponse, "action", "attack/bomb");
|
|
|
+ //Reponse
|
|
|
+ if (ok) {
|
|
|
+ add_integer(&reponse, "status", 201);
|
|
|
+ describe_player(p, &reponse);
|
|
|
+ //Envoi
|
|
|
+ if (!send_client(cliId, &reponse)) {
|
|
|
+ adderror("Impossible de répondre au client");
|
|
|
+ pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
|
|
|
+ pthread_mutex_unlock(&gameMutex[index]);
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
+ free(pos);
|
|
|
+ free(class);
|
|
|
+ return FAIL;
|
|
|
+ }
|
|
|
+ //Notification des autre joeurs
|
|
|
+ pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
|
|
|
+ pthread_mutex_unlock(&gameMutex[index]);
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
+ add_string(&reponse, "pos", pos);
|
|
|
+ add_string(&reponse, "class", class);
|
|
|
+ notify_player(&game[index], "POST", "attack/newbomb", &reponse, cliId);
|
|
|
+ } else {
|
|
|
+ add_integer(&reponse, "status", 403);
|
|
|
+ add_string(&reponse, "message", "Forbidden action");
|
|
|
+ //Envoi
|
|
|
+ if (!send_client(cliId, &reponse)) {
|
|
|
+ adderror("Impossible de répondre au client");
|
|
|
+ pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
|
|
|
+ pthread_mutex_unlock(&gameMutex[index]);
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
+ free(pos);
|
|
|
+ free(class);
|
|
|
+ return FAIL;
|
|
|
+ }
|
|
|
+ pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + playerIndex]);
|
|
|
+ pthread_mutex_unlock(&gameMutex[index]);
|
|
|
+ }
|
|
|
+ //Nettoyage
|
|
|
+ clean_json_encoder(&reponse);
|
|
|
+ free(pos);
|
|
|
+ free(class);
|
|
|
+ return SUCCESS;
|
|
|
}
|