|
@@ -86,42 +86,17 @@ void ini_handler() {
|
|
//Post
|
|
//Post
|
|
add_handler("POST", "game/create", handler_game_create);
|
|
add_handler("POST", "game/create", handler_game_create);
|
|
add_handler("POST", "game/join", handler_game_join);
|
|
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", "player/move", handler_player_move);
|
|
}
|
|
}
|
|
|
|
|
|
int handler_client_end(int cliId, JsonParser* json) {
|
|
int handler_client_end(int cliId, JsonParser* json) {
|
|
- boolean find = false;
|
|
|
|
- JsonEncoder* notif;
|
|
|
|
- printf("Deconnexion du client %d\n", cliId);
|
|
|
|
- //Cherche le client dans les parties
|
|
|
|
- for (int i = 0; i < MAXGAME; i++) {
|
|
|
|
- pthread_mutex_lock(&gameMutex[i]);
|
|
|
|
- if (game[i].active) {
|
|
|
|
- for (int j = 0; j < MAXPLAYER; j++) {
|
|
|
|
- pthread_mutex_lock(&playerMutex[(i * MAXPLAYER) + j]);
|
|
|
|
- if (game[i].player[j]->ini && game[i].player[j]->id == cliId) {
|
|
|
|
- pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
|
|
|
|
- pthread_mutex_unlock(&gameMutex[i]);
|
|
|
|
- find = true;
|
|
|
|
- remove_player(&game[i], j);
|
|
|
|
- //Avertit les autres joueurs
|
|
|
|
- notif = malloc(sizeof (JsonEncoder));
|
|
|
|
- ini_encoder(notif);
|
|
|
|
- add_integer(notif, "player", cliId);
|
|
|
|
- notify_player(&game[i], "POST", "game/quit", notif, cliId);
|
|
|
|
- clean_json_encoder(notif);
|
|
|
|
- free(notif);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
|
|
|
|
- }
|
|
|
|
- if (find) {
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- pthread_mutex_unlock(&gameMutex[i]);
|
|
|
|
|
|
+ //Verif que le client est toujours actif
|
|
|
|
+ if (get_client(cliId) != NULL) {
|
|
|
|
+ printf("Deconnexion du client %d\n", cliId);
|
|
|
|
+ handler_game_quit(cliId, json);
|
|
|
|
+ remove_client(cliId);
|
|
}
|
|
}
|
|
- remove_client(cliId);
|
|
|
|
return SUCCESS;
|
|
return SUCCESS;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -295,6 +270,39 @@ int handler_game_join(int cliId, JsonParser* json) {
|
|
return SUCCESS;
|
|
return SUCCESS;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+int handler_game_quit(int cliId, JsonParser* json) {
|
|
|
|
+ printf("Quit\n");
|
|
|
|
+ boolean find = false;
|
|
|
|
+ JsonEncoder notif;
|
|
|
|
+ //Cherche le client dans les parties
|
|
|
|
+ for (int i = 0; i < MAXGAME; i++) {
|
|
|
|
+ pthread_mutex_lock(&gameMutex[i]);
|
|
|
|
+ if (game[i].active) {
|
|
|
|
+ for (int j = 0; j < MAXPLAYER; j++) {
|
|
|
|
+ pthread_mutex_lock(&playerMutex[(i * MAXPLAYER) + j]);
|
|
|
|
+ if (game[i].player[j]->ini && game[i].player[j]->id == cliId) {
|
|
|
|
+ pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
|
|
|
|
+ pthread_mutex_unlock(&gameMutex[i]);
|
|
|
|
+ find = true;
|
|
|
|
+ remove_player(&game[i], j);
|
|
|
|
+ //Avertit les autres joueurs
|
|
|
|
+ ini_encoder(¬if);
|
|
|
|
+ add_integer(¬if, "player", cliId);
|
|
|
|
+ notify_player(&game[i], "POST", "game/quit", ¬if, cliId);
|
|
|
|
+ clean_json_encoder(¬if);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ pthread_mutex_unlock(&playerMutex[(i * MAXPLAYER) + j]);
|
|
|
|
+ }
|
|
|
|
+ if (find) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ pthread_mutex_unlock(&gameMutex[i]);
|
|
|
|
+ }
|
|
|
|
+ return SUCCESS;
|
|
|
|
+}
|
|
|
|
+
|
|
int handler_player_move(int cliId, JsonParser* json) {
|
|
int handler_player_move(int cliId, JsonParser* json) {
|
|
printf("Player Move\n");
|
|
printf("Player Move\n");
|
|
char* move;
|
|
char* move;
|
|
@@ -307,7 +315,7 @@ int handler_player_move(int cliId, JsonParser* json) {
|
|
return FAIL;
|
|
return FAIL;
|
|
}
|
|
}
|
|
//Recup valeur
|
|
//Recup valeur
|
|
- move = get_string(json, "move");
|
|
|
|
|
|
+ move = get_string(json, "move");
|
|
//Verif game existe
|
|
//Verif game existe
|
|
index = search_client_game(cliId);
|
|
index = search_client_game(cliId);
|
|
if (index == ERR) {
|
|
if (index == ERR) {
|
|
@@ -317,26 +325,26 @@ int handler_player_move(int cliId, JsonParser* json) {
|
|
}
|
|
}
|
|
//Recup le joueur
|
|
//Recup le joueur
|
|
pthread_mutex_lock(&gameMutex[index]);
|
|
pthread_mutex_lock(&gameMutex[index]);
|
|
- for(int i = 0; i < MAXPLAYER; i++){
|
|
|
|
|
|
+ for (int i = 0; i < MAXPLAYER; i++) {
|
|
pthread_mutex_lock(&playerMutex[(index * MAXPLAYER) + i]);
|
|
pthread_mutex_lock(&playerMutex[(index * MAXPLAYER) + i]);
|
|
- if(game[index].player[i]->ini && game[index].player[i]->id == cliId){
|
|
|
|
|
|
+ if (game[index].player[i]->ini && game[index].player[i]->id == cliId) {
|
|
p = game[index].player[i];
|
|
p = game[index].player[i];
|
|
pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
|
|
pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
|
|
pthread_mutex_unlock(&playerMutex[(index * MAXPLAYER) + i]);
|
|
}
|
|
}
|
|
- if(p == NULL){
|
|
|
|
|
|
+ if (p == NULL) {
|
|
free(move);
|
|
free(move);
|
|
return FAIL;
|
|
return FAIL;
|
|
}
|
|
}
|
|
//Regarde si le joueur peut bouger
|
|
//Regarde si le joueur peut bouger
|
|
if (strncmp(move, "up", 2) == 0) {
|
|
if (strncmp(move, "up", 2) == 0) {
|
|
- if(p->y > 0 && !player_collision(&game[index], p->x, p->y - 1) && ( game[index].map[p->x][p->y - 1] == '_' /*Vide*/ || game[index].map[p->x][p->y - 1] == '2' /*Mine*/ )){
|
|
|
|
|
|
+ if (p->y > 0 && !player_collision(&game[index], p->x, p->y - 1) && (game[index].map[p->x][p->y - 1] == '_' /*Vide*/ || game[index].map[p->x][p->y - 1] == '2' /*Mine*/)) {
|
|
//Bouge le joueur sur la carte
|
|
//Bouge le joueur sur la carte
|
|
p->y--;
|
|
p->y--;
|
|
//Si marche sur une mine
|
|
//Si marche sur une mine
|
|
- if(game[index].map[p->x][p->y] == '2'){
|
|
|
|
|
|
+ if (game[index].map[p->x][p->y] == '2') {
|
|
game[index].map[p->x][p->y] = '_';
|
|
game[index].map[p->x][p->y] = '_';
|
|
mine = true;
|
|
mine = true;
|
|
x = p->x;
|
|
x = p->x;
|
|
@@ -344,11 +352,11 @@ int handler_player_move(int cliId, JsonParser* json) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else if (strncmp(move, "down", 4) == 0) {
|
|
} else if (strncmp(move, "down", 4) == 0) {
|
|
- if(p->y < game[index].height && !player_collision(&game[index], p->x, p->y + 1) && ( game[index].map[p->x][p->y + 1] == '_' /*Vide*/ || game[index].map[p->x][p->y + 1] == '2' /*Mine*/ )){
|
|
|
|
|
|
+ if (p->y < game[index].height && !player_collision(&game[index], p->x, p->y + 1) && (game[index].map[p->x][p->y + 1] == '_' /*Vide*/ || game[index].map[p->x][p->y + 1] == '2' /*Mine*/)) {
|
|
//Bouge le joueur sur la carte
|
|
//Bouge le joueur sur la carte
|
|
p->y++;
|
|
p->y++;
|
|
//Si marche sur une mine
|
|
//Si marche sur une mine
|
|
- if(game[index].map[p->x][p->y] == '2'){
|
|
|
|
|
|
+ if (game[index].map[p->x][p->y] == '2') {
|
|
game[index].map[p->x][p->y] = '_';
|
|
game[index].map[p->x][p->y] = '_';
|
|
mine = true;
|
|
mine = true;
|
|
x = p->x;
|
|
x = p->x;
|
|
@@ -356,23 +364,23 @@ int handler_player_move(int cliId, JsonParser* json) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else if (strncmp(move, "left", 4) == 4) {
|
|
} else if (strncmp(move, "left", 4) == 4) {
|
|
- if(p->x > 0 && !player_collision(&game[index], p->x - 1, p->y) && ( game[index].map[p->x - 1][p->y] == '_' /*Vide*/ || game[index].map[p->x - 1][p->y] == '2' /*Mine*/ )){
|
|
|
|
|
|
+ if (p->x > 0 && !player_collision(&game[index], p->x - 1, p->y) && (game[index].map[p->x - 1][p->y] == '_' /*Vide*/ || game[index].map[p->x - 1][p->y] == '2' /*Mine*/)) {
|
|
//Bouge le joueur sur la carte
|
|
//Bouge le joueur sur la carte
|
|
p->x--;
|
|
p->x--;
|
|
//Si marche sur une mine
|
|
//Si marche sur une mine
|
|
- if(game[index].map[p->x][p->y] == '2'){
|
|
|
|
|
|
+ if (game[index].map[p->x][p->y] == '2') {
|
|
game[index].map[p->x][p->y] = '_';
|
|
game[index].map[p->x][p->y] = '_';
|
|
mine = true;
|
|
mine = true;
|
|
x = p->x;
|
|
x = p->x;
|
|
y = p->y;
|
|
y = p->y;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- } else if(strncmp(move, "right", 4) == 0){
|
|
|
|
- if(p->x < game[index].width && !player_collision(&game[index], p->x + 1, p->y) && ( game[index].map[p->x + 1][p->y] == '_' /*Vide*/ || game[index].map[p->x + 1][p->y] == '2' /*Mine*/ )){
|
|
|
|
|
|
+ } else if (strncmp(move, "right", 4) == 0) {
|
|
|
|
+ if (p->x < game[index].width && !player_collision(&game[index], p->x + 1, p->y) && (game[index].map[p->x + 1][p->y] == '_' /*Vide*/ || game[index].map[p->x + 1][p->y] == '2' /*Mine*/)) {
|
|
//Bouge le joueur sur la carte
|
|
//Bouge le joueur sur la carte
|
|
p->x++;
|
|
p->x++;
|
|
//Si marche sur une mine
|
|
//Si marche sur une mine
|
|
- if(game[index].map[p->x][p->y] == '2'){
|
|
|
|
|
|
+ if (game[index].map[p->x][p->y] == '2') {
|
|
game[index].map[p->x][p->y] = '_';
|
|
game[index].map[p->x][p->y] = '_';
|
|
mine = true;
|
|
mine = true;
|
|
x = p->x;
|
|
x = p->x;
|
|
@@ -384,7 +392,7 @@ int handler_player_move(int cliId, JsonParser* json) {
|
|
free(move);
|
|
free(move);
|
|
send_err_client(cliId, EREQUEST);
|
|
send_err_client(cliId, EREQUEST);
|
|
adderror("Le json du client est incorrect");
|
|
adderror("Le json du client est incorrect");
|
|
- return FAIL;
|
|
|
|
|
|
+ return FAIL;
|
|
}
|
|
}
|
|
pthread_mutex_unlock(&gameMutex[index]);
|
|
pthread_mutex_unlock(&gameMutex[index]);
|
|
//Notifie les joueurs
|
|
//Notifie les joueurs
|
|
@@ -393,7 +401,7 @@ int handler_player_move(int cliId, JsonParser* json) {
|
|
add_string(&reponse, "move", move);
|
|
add_string(&reponse, "move", move);
|
|
notify_player(&game[index], "POST", "player/position/update", &reponse, -1);
|
|
notify_player(&game[index], "POST", "player/position/update", &reponse, -1);
|
|
//Si marche sur une mine
|
|
//Si marche sur une mine
|
|
- if(mine){
|
|
|
|
|
|
+ if (mine) {
|
|
printf("Mine %d %d", x, y);
|
|
printf("Mine %d %d", x, y);
|
|
}
|
|
}
|
|
//Nettoyage
|
|
//Nettoyage
|