Skip to content
Snippets Groups Projects
Commit 3d2172cc authored by Terence Denning's avatar Terence Denning
Browse files

fixed user bug

parent 66c02bd0
Branches
No related tags found
No related merge requests found
...@@ -29,6 +29,7 @@ void reset_game(); ...@@ -29,6 +29,7 @@ void reset_game();
void reset_user(int sockfd); void reset_user(int sockfd);
void set_user(int sockfd); void set_user(int sockfd);
void user_ready(int sockfd); void user_ready(int sockfd);
void print_details();
// constants // constants
static char const * const HTTP_200_FORMAT = "HTTP/1.1 200 OK\r\n\ static char const * const HTTP_200_FORMAT = "HTTP/1.1 200 OK\r\n\
...@@ -114,7 +115,11 @@ static bool handle_http_request(int sockfd) ...@@ -114,7 +115,11 @@ static bool handle_http_request(int sockfd)
if (method == GET) if (method == GET)
{ {
if( strstr(buff, "?start=Start") != NULL ){ if( strstr(buff, "start=Start") != NULL ){
if( strcmp(webpage, "html/2_start.html") ){
user_ready(sockfd);
}
/* Handle resetting users when starting a new game */ /* Handle resetting users when starting a new game */
printf("gameover: %d\n", gameover); printf("gameover: %d\n", gameover);
if(gameover == 0){ if(gameover == 0){
...@@ -123,8 +128,6 @@ static bool handle_http_request(int sockfd) ...@@ -123,8 +128,6 @@ static bool handle_http_request(int sockfd)
printf("Restarting game, user1: %d, user2: %d\n", user1, user2); printf("Restarting game, user1: %d, user2: %d\n", user1, user2);
gameover = 0; gameover = 0;
} }
user_ready(sockfd);
webpage = "html/3_first_turn.html"; webpage = "html/3_first_turn.html";
} else if( strstr(buff, "Cookie:") != NULL ) { } else if( strstr(buff, "Cookie:") != NULL ) {
webpage = "html/1_intro.html"; webpage = "html/1_intro.html";
...@@ -161,6 +164,7 @@ static bool handle_http_request(int sockfd) ...@@ -161,6 +164,7 @@ static bool handle_http_request(int sockfd)
return false; return false;
} }
close(filefd); close(filefd);
} }
else if (method == POST) else if (method == POST)
{ {
...@@ -207,7 +211,7 @@ static bool handle_http_request(int sockfd) ...@@ -207,7 +211,7 @@ static bool handle_http_request(int sockfd)
stat(webpage, &st); stat(webpage, &st);
username = strstr(buff, "user=") + 5; username = strstr(buff, "user=") + 5;
set_user(sockfd); //set_user(sockfd);
username_length = strlen(username); username_length = strlen(username);
added_length = username_length + 2; added_length = username_length + 2;
...@@ -215,12 +219,11 @@ static bool handle_http_request(int sockfd) ...@@ -215,12 +219,11 @@ static bool handle_http_request(int sockfd)
n = sprintf(buff, HTTP_200_FORMAT_WITH_COOKIE, username, size); n = sprintf(buff, HTTP_200_FORMAT_WITH_COOKIE, username, size);
} }
else if(strstr(buff, "guess=Guess") != NULL) { else if(strstr(buff, "guess=Guess") != NULL) {
char *keyword = strstr(buff, "keyword=")+8; char *keyword = strstr(buff, "keyword=")+8;
int keyword_length = strlen(keyword); int keyword_length = strlen(keyword);
keyword[keyword_length-12] = '\0'; keyword[keyword_length-12] = '\0';
if (sockfd == user1){ if (sockfd == user1){
/* if other player is ready then accept guesses */ /* if other player is ready then accept guesses */
if( user2_start == 1 ){ if( user2_start == 1 ){
...@@ -268,6 +271,13 @@ static bool handle_http_request(int sockfd) ...@@ -268,6 +271,13 @@ static bool handle_http_request(int sockfd)
stat(webpage, &st); stat(webpage, &st);
n = sprintf(buff, HTTP_200_FORMAT, st.st_size); n = sprintf(buff, HTTP_200_FORMAT, st.st_size);
if(sockfd == user1){
printf("I AM USER1");
} else if( sockfd == user2 ){
printf("I AM USER2");
}
print_details();
} else { } else {
printf("\n\n\nerror reading html...\n\n\n"); printf("\n\n\nerror reading html...\n\n\n");
} }
...@@ -299,6 +309,7 @@ static bool handle_http_request(int sockfd) ...@@ -299,6 +309,7 @@ static bool handle_http_request(int sockfd)
// put the separator // put the separator
buff[p2++] = ' '; buff[p2++] = ' ';
buff[p2++] = ' '; buff[p2++] = ' ';
// copy the username // copy the username
strncpy(buff + p2, username, username_length); strncpy(buff + p2, username, username_length);
if (write(sockfd, buff, size) < 0) if (write(sockfd, buff, size) < 0)
...@@ -434,6 +445,8 @@ int main(int argc, char * argv[]) ...@@ -434,6 +445,8 @@ int main(int argc, char * argv[])
void reset_game(){ void reset_game(){
gameover = 1; gameover = 1;
user1 = -1;
user2 = -1;
user1_guess_number = 0; user1_guess_number = 0;
user2_guess_number = 0; user2_guess_number = 0;
user1_start = 0; user1_start = 0;
...@@ -486,3 +499,6 @@ void user_ready(int sockfd){ ...@@ -486,3 +499,6 @@ void user_ready(int sockfd){
} }
} }
void print_details(){
printf("User1: %d, User2: %d, User1_start: %d, User2_start: %d\n\n", user1, user2, user1_start, user2_start);
}
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment