Skip to content
Snippets Groups Projects
Commit 343b2aa9 authored by ChouTatsumi's avatar ChouTatsumi
Browse files

end_game quit_game test

parent 41d1a024
No related branches found
No related tags found
No related merge requests found
......@@ -44,6 +44,7 @@ static char const* const DISCARDED_PAGE = "./views/5_discarded.html";
static char const* const ENDGAME_PAGE = "./views/6_endgame.html";
static char const* const GAMEOVER_PAGE = "./views/7_gameover.html";
bool is_end_game, is_quit_game;
bool p1_is_ready, p2_is_ready;
int p1_sockfd, p2_sockfd;
// assume we can save 2000 keywords from each player,
......@@ -62,7 +63,7 @@ bool show_page(int sockfd, const char* htmldir);
bool show_start_page(int sockfd, char* username);
void print_html_request(char* request, int sockfd);
bool keyword_check(int sockfd, char* keyword);
void player_init(int sockfd);
void player_init();
int main(int argc, char** argv) {
int port;
......@@ -98,9 +99,8 @@ void run_server(const char* ip, const int port) {
port);
// Initialise
p1_sockfd = p2_sockfd = -1;
p1_is_ready = p2_is_ready = false;
p1_guess_len = p2_guess_len = 0;
player_init();
is_end_game = is_quit_game = false;
// initialise an active file descriptors set
fd_set masterfds;
......@@ -243,7 +243,8 @@ static bool handle_http_request(int sockfd) {
char* username = strstr(buff, "user=") + 5;
return show_start_page(sockfd, username);
} else if (strstr(buff, "quit=") != NULL) {
player_init(sockfd);
player_init();
is_quit_game = true;
return show_page(sockfd, GAMEOVER_PAGE);
}
} else
......@@ -263,15 +264,20 @@ static bool handle_http_request(int sockfd) {
return show_page(sockfd, FIRST_TURN_PAGE);
} else if (method == POST) {
if (strstr(buff, "quit=") != NULL) {
player_init(sockfd);
player_init();
is_quit_game = true;
return show_page(sockfd, GAMEOVER_PAGE);
} else if (strstr(buff, "keyword=") != NULL) {
char* keyword = strstr(buff, "keyword=") + 8;
if (p1_is_ready && p2_is_ready) {
if (keyword_check(sockfd, keyword)) {
if (is_end_game)
return show_page(sockfd, ENDGAME_PAGE);
else if (is_quit_game)
return show_page(sockfd, GAMEOVER_PAGE);
else if (keyword_check(sockfd, keyword)) {
// initial players setting
p1_is_ready = p2_is_ready = false;
p1_guess_len = p2_guess_len = 0;
player_init();
is_end_game = true;
return show_page(sockfd, ENDGAME_PAGE);
} else
return show_page(sockfd, ACCEPTED_PAGE);
......@@ -405,14 +411,8 @@ bool keyword_check(int sockfd, char* keyword) {
return false;
}
void player_init(int sockfd) {
if (sockfd == p1_sockfd) {
p1_is_ready = false;
p1_sockfd = -1;
p1_guess_len = 0;
} else if (sockfd == p2_sockfd) {
p2_is_ready = false;
p2_sockfd = -1;
p2_guess_len = 0;
}
void player_init() {
p1_is_ready = p2_is_ready = false;
p1_sockfd = p2_sockfd = -1;
p1_guess_len = p2_guess_len = 0;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment