Skip to content
Snippets Groups Projects
Commit 5dda478c authored by Anqi Chen's avatar Anqi Chen
Browse files

change the logic

parent a2f6ca27
No related branches found
No related tags found
No related merge requests found
Pipeline #1779 canceled
/* /*
** code by Angela Chan ** http-server.c
*/ */
#include <errno.h> #include <errno.h>
...@@ -41,7 +41,7 @@ static char const * const HTTP_404 = "HTTP/1.1 404 Not Found\r\nContent-Length: ...@@ -41,7 +41,7 @@ static char const * const HTTP_404 = "HTTP/1.1 404 Not Found\r\nContent-Length:
static int const HTTP_404_LENGTH = 45; static int const HTTP_404_LENGTH = 45;
// 3 kind of methods
typedef enum typedef enum
{ {
GET, GET,
...@@ -80,13 +80,15 @@ struct User* newUser() { ...@@ -80,13 +80,15 @@ struct User* newUser() {
} }
void clear_user_keyword(struct User* user){ void clear_user_keyword(struct User* user){
user->stage=0;
user->isOnline=0;
user->n_word=0;
for (int i = 0; i < MAX_BUFF; i++) for (int i = 0; i < MAX_BUFF; i++)
{ {
user->keywords[i] = NULL; user->keywords[i] = NULL;
} }
} }
// abstract the cookie from http response in buff
int find_cookie(char* buff) int find_cookie(char* buff)
{ {
char* cookie_id=strstr(buff, "cookie_id="); char* cookie_id=strstr(buff, "cookie_id=");
...@@ -98,7 +100,7 @@ int find_cookie(char* buff) ...@@ -98,7 +100,7 @@ int find_cookie(char* buff)
else else
{ {
temp = malloc(sizeof(char)*strlen(cookie_id)+1); temp = malloc(sizeof(char)*strlen(cookie_id)+1);
strncpy(temp, cookie_id, strlen(cookie_id)); strcpy(temp, cookie_id);
temp+=10; temp+=10;
} }
printf("find_cookie in buff %s\n", cookie_id); printf("find_cookie in buff %s\n", cookie_id);
...@@ -110,7 +112,7 @@ int find_cookie(char* buff) ...@@ -110,7 +112,7 @@ int find_cookie(char* buff)
return atoi(temp); return atoi(temp);
} }
//find the current user
struct User* find_curr(struct User* user_arr[], char* buff) struct User* find_curr(struct User* user_arr[], char* buff)
{ {
int cookie_id = find_cookie(buff); int cookie_id = find_cookie(buff);
...@@ -146,7 +148,6 @@ struct User* find_curr(struct User* user_arr[], char* buff) ...@@ -146,7 +148,6 @@ struct User* find_curr(struct User* user_arr[], char* buff)
} }
// get the other user
struct User* the_other_user(struct User* curr_user, struct User* user_arr[]){ struct User* the_other_user(struct User* curr_user, struct User* user_arr[]){
for(int i=0; i<2; i++){ for(int i=0; i<2; i++){
if (user_arr[i]!=curr_user){ if (user_arr[i]!=curr_user){
...@@ -155,7 +156,17 @@ struct User* the_other_user(struct User* curr_user, struct User* user_arr[]){ ...@@ -155,7 +156,17 @@ struct User* the_other_user(struct User* curr_user, struct User* user_arr[]){
} }
} }
// simply load the original html
bool match(char* w1, char** w2, int n_word){
for (int i=0; i<n_word; i++){
if (w2[i]==w1){
return true;
}
}
return false;
}
void simple_load_html(char* html, int sockfd, char* buff, int n, struct User* curr_user){ void simple_load_html(char* html, int sockfd, char* buff, int n, struct User* curr_user){
struct stat st; struct stat st;
stat(html, &st); stat(html, &st);
...@@ -180,19 +191,19 @@ void simple_load_html(char* html, int sockfd, char* buff, int n, struct User* cu ...@@ -180,19 +191,19 @@ void simple_load_html(char* html, int sockfd, char* buff, int n, struct User* cu
close(filefd); close(filefd);
} }
// plug in the new words in new buff to the old buff /* adds the new word in the html (between the <body> */
void plug_in_html(char* newword, char* old_buffer, char* new_buffer){ void plug_in_html(char* newword, char* old_buffer, char* new_buffer){
// split buff according to </body> // slice the html string into 2 parts
char start[MAX_BUFF]; char start[MAX_BUFF];
char* end = strstr(old_buffer, "</body>"); char* end = strstr(old_buffer, "</body>");
// copy and close up // front part
strncpy(start, old_buffer, strlen(old_buffer)-strlen(end)); strncpy(start, old_buffer, strlen(old_buffer)-strlen(end));
start[strlen(start)] = '\0'; start[strlen(start)] = '\0';
// merge the buffs // combine
strncpy(new_buffer, start, strlen(start)); strcpy(new_buffer, start);
strcat(new_buffer, newword); strcat(new_buffer, newword);
strcat(new_buffer, end); strcat(new_buffer, end);
...@@ -200,7 +211,7 @@ void plug_in_html(char* newword, char* old_buffer, char* new_buffer){ ...@@ -200,7 +211,7 @@ void plug_in_html(char* newword, char* old_buffer, char* new_buffer){
} }
// if the html is rearranged, load it with the new content
void edit_load_html(char* html, int sockfd, char* buff, char* newword,struct User* curr_user){ void edit_load_html(char* html, int sockfd, char* buff, char* newword,struct User* curr_user){
// get the size of the file // get the size of the file
...@@ -240,29 +251,17 @@ void edit_load_html(char* html, int sockfd, char* buff, char* newword,struct Use ...@@ -240,29 +251,17 @@ void edit_load_html(char* html, int sockfd, char* buff, char* newword,struct Use
free(new_buffer); free(new_buffer);
} }
// read the new guess word into the user keyword array
void record_keyword(struct User* curr_user, char *new_guess){ void record_keyword(struct User* curr_user, char *new_guess){
for (int i=0; i<=curr_user->n_word; i++){ for (int i=0; i<=curr_user->n_word; i++){
if(curr_user->keywords[curr_user->n_word]==NULL){ if(curr_user->keywords[curr_user->n_word]==NULL){
curr_user->keywords[curr_user->n_word] = malloc((1 + strlen(new_guess)) * sizeof(char*)); curr_user->keywords[curr_user->n_word] = malloc((1 + strlen(new_guess)) * sizeof(char*));
strncpy(curr_user->keywords[curr_user->n_word],new_guess, strlen(new_guess)); strcpy(curr_user->keywords[curr_user->n_word],new_guess);
curr_user->n_word++; curr_user->n_word++;
break; break;
} }
} }
} }
// find if word is in array, knowing the length of the array
bool match(char* w1, char* w2[], int n_word){
for (int i=0; i<n_word; i++){
if (w2[i]==w1){
return true;
}
}
return false;
}
static bool handle_http_request(int sockfd, struct User* user_arr[]) static bool handle_http_request(int sockfd, struct User* user_arr[])
{ {
// try to read the request // try to read the request
...@@ -303,33 +302,32 @@ static bool handle_http_request(int sockfd, struct User* user_arr[]) ...@@ -303,33 +302,32 @@ static bool handle_http_request(int sockfd, struct User* user_arr[])
printf("buff1: \n"); printf("buff1: \n");
printf("%s\n",buff); printf("%s\n",buff);
// get current user
struct User* curr_user=find_curr(user_arr,buff); struct User* curr_user=find_curr(user_arr,buff);
// sanitise the URI // sanitise the URI
while (*curr == '.' || *curr == '/') while (*curr == '.' || *curr == '/')
++curr; ++curr;
printf("stage%d\n",curr_user->stage );
// assume the only valid request URI is "/" but it can be modified to accept more files // assume the only valid request URI is "/" but it can be modified to accept more files
if (*curr == ' ' || *curr == '?') if (*curr == ' ' || *curr == '?')
//Stage1: when user name is not registed //Stage1: when user name is not registed
if (curr_user->stage==0){ if (curr_user->stage==0){
// the only GET method before registing name is the intro page
if (method==GET) if (method==GET)
{ {
printf("curr_user->username%s == %d\n", curr_user->username,curr_user->username==NULL); printf("curr_user->username%s == %d\n", curr_user->username,curr_user->username==NULL);
simple_load_html("1_intro.html",sockfd,buff,n,curr_user); simple_load_html("1_intro.html",sockfd,buff,n,curr_user);
} }
// the only POST method before registing name is the start page
else if (method == POST) else if (method == POST)
{ {
printf("curr_user->username%s == %d\n", curr_user->username,curr_user->username==NULL); printf("curr_user->username%s == %d\n", curr_user->username,curr_user->username==NULL);
// locate the username, it is safe to do so in this sample code, but usually the result is expected to be // locate the username, it is safe to do so in this sample code, but usually the result is expected to be
// copied to another buffer using strncpy or strncpy to ensure that it will not be overwritten. // copied to another buffer using strcpy or strncpy to ensure that it will not be overwritten.
char * user = strstr(buff, "user=") + 5; char * user = strstr(buff, "user=") + 5;
char username[MAX_BUFF]; char username[MAX_BUFF];
strncpy(username, "<br>",strlen("<br>")); strcpy(username, "<br>");
strcat(username, user); strcat(username, user);
username[strlen(username)] = '\0'; username[strlen(username)] = '\0';
edit_load_html("2_start.html", sockfd,buff,username,curr_user); edit_load_html("2_start.html", sockfd,buff,username,curr_user);
...@@ -347,6 +345,7 @@ static bool handle_http_request(int sockfd, struct User* user_arr[]) ...@@ -347,6 +345,7 @@ static bool handle_http_request(int sockfd, struct User* user_arr[])
if(strstr(buff, "quit=Quit") != NULL) if(strstr(buff, "quit=Quit") != NULL)
{ curr_user->isOnline=0; { curr_user->isOnline=0;
simple_load_html("7_gameover.html",sockfd,buff,n,curr_user); simple_load_html("7_gameover.html",sockfd,buff,n,curr_user);
curr_user->stage=3;
} }
// start is the only GET mathod for ther stage 1 // start is the only GET mathod for ther stage 1
...@@ -354,58 +353,47 @@ static bool handle_http_request(int sockfd, struct User* user_arr[]) ...@@ -354,58 +353,47 @@ static bool handle_http_request(int sockfd, struct User* user_arr[])
{ curr_user->isOnline=1; { curr_user->isOnline=1;
simple_load_html("3_first_turn.html",sockfd,buff,n,curr_user); simple_load_html("3_first_turn.html",sockfd,buff,n,curr_user);
curr_user->stage =2; curr_user->stage =2;
curr_user->isOnline=1;
} }
} }
// Stage 2: in the process of guessing // Stage 2: in the process of guessing
else if (curr_user->stage ==2){ else if (curr_user->stage ==2){
// ready to guess
char * new_guess = strstr(buff, "keyword=") + 8; char * new_guess = strstr(buff, "keyword=") + 8;
// if quit, offline and quit
// clear all user info
if(strstr(buff, "quit=Quit") != NULL) if(strstr(buff, "quit=Quit") != NULL)
{ curr_user->isOnline=0; { curr_user->isOnline=0;
simple_load_html("7_gameover.html",sockfd,buff,n,curr_user); simple_load_html("7_gameover.html",sockfd,buff,n,curr_user);
clear_user_keyword(curr_user); clear_user_keyword(curr_user);
}
else if (the_other_user(curr_user, user_arr)->stage==3){
simple_load_html("7_gameover.html",sockfd,buff,n,curr_user);
clear_user_keyword(curr_user);
} }
// other user offline, discard else if(the_other_user(curr_user, user_arr)->isOnline==0){
if (the_other_user(curr_user, user_arr)->isOnline==0){
simple_load_html("5_discarded.html",sockfd,buff,n,curr_user); simple_load_html("5_discarded.html",sockfd,buff,n,curr_user);
clear_user_keyword(curr_user); clear_user_keyword(curr_user);
} }
// GET method can only do start in stage2
// clear the user keyword array
if (method = GET) {
clear_user_keyword(curr_user);
simple_load_html("3_first_turn.html",sockfd,buff,n,curr_user);
} else if(new_guess!=NULL){
// two players guess match
if (new_guess != NULL)
{
//if match game end
if(match(new_guess, the_other_user(curr_user, user_arr)->keywords, the_other_user(curr_user, user_arr)->n_word)) if(match(new_guess, the_other_user(curr_user, user_arr)->keywords, the_other_user(curr_user, user_arr)->n_word))
{ {
simple_load_html("6_endgame.html",sockfd,buff,n,curr_user); simple_load_html("6_endgame.html",sockfd,buff,n,curr_user);
clear_user_keyword(curr_user); clear_user_keyword(curr_user);
} }
// if a new guess is take, not match, add it to array and display it else{
else
{
curr_user->stage =2; curr_user->stage =2;
printf("curr_user->n_word%d\n",curr_user->n_word ); printf("curr_user->n_word%d\n",curr_user->n_word );
// remember new guess // remember new guess
record_keyword(curr_user, new_guess); record_keyword(curr_user, new_guess);
// display new guess // display new guess
char guess[MAX_BUFF]; char guess[MAX_BUFF];
strncpy(guess,"<br>",strlen("<br>")); strcpy(guess,"<br>");
if (curr_user->n_word==1){ if (curr_user->n_word==1){
strcat(guess, new_guess); strcat(guess, new_guess);
guess[strlen(new_guess)]='\0'; guess[strlen(new_guess)]='\0';
...@@ -424,9 +412,6 @@ static bool handle_http_request(int sockfd, struct User* user_arr[]) ...@@ -424,9 +412,6 @@ static bool handle_http_request(int sockfd, struct User* user_arr[])
} }
else else
// never used, just for completeness // never used, just for completeness
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment