From 5dda478c5106b6c1ad05bd9937fea1092bccf2c3 Mon Sep 17 00:00:00 2001
From: Anqi Chen <a.chen49@student.unimelb.edu.au>
Date: Mon, 29 Apr 2019 15:12:35 +1000
Subject: [PATCH] change the logic

---
 h.c | 117 ++++++++++++++++++++++++++----------------------------------
 1 file changed, 51 insertions(+), 66 deletions(-)

diff --git a/h.c b/h.c
index b63c4c6..73d2f73 100644
--- a/h.c
+++ b/h.c
@@ -1,5 +1,5 @@
 /*
-** code by Angela Chan
+** http-server.c
 */
 
 #include <errno.h>
@@ -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;
 
 
-// 3 kind of methods
+
 typedef enum
 {
     GET,
@@ -80,13 +80,15 @@ struct User* newUser() {
 }
 
 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++)
     {
         user->keywords[i] = NULL;
     }
 }
 
-// abstract the cookie from http response in buff
 int find_cookie(char* buff)
 {
     char* cookie_id=strstr(buff, "cookie_id=");
@@ -98,7 +100,7 @@ int find_cookie(char* buff)
     else
     {
         temp = malloc(sizeof(char)*strlen(cookie_id)+1);
-        strncpy(temp, cookie_id, strlen(cookie_id));
+        strcpy(temp, cookie_id);
         temp+=10;
     }
     printf("find_cookie in buff %s\n", cookie_id);
@@ -110,7 +112,7 @@ int find_cookie(char* buff)
     return  atoi(temp);
 }
 
-//find the current user
+
 struct User* find_curr(struct User* user_arr[], char* buff)
 {
     int cookie_id = find_cookie(buff);
@@ -135,18 +137,17 @@ struct User* find_curr(struct User* user_arr[], char* buff)
         printf(" func curr: cookie_id is %d\n%d\n\n\n", cookie_id,curr_user->userID);
     }
     
-	for (int i=0; i<2; i++) {
-		if (user_arr[i]->userID == cookie_id)
-		{				
+    for (int i=0; i<2; i++) {
+        if (user_arr[i]->userID == cookie_id)
+        {               
             printf("existing curr user %d , with cookie_id %d", i,cookie_id);
-			return user_arr[i];
-			
-		}
-	}
+            return user_arr[i];
+            
+        }
+    }
 
 }
 
-// get the other user
 struct User* the_other_user(struct User* curr_user, struct User* user_arr[]){
     for(int i=0; i<2; i++){
         if (user_arr[i]!=curr_user){
@@ -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){
     struct stat st;
     stat(html, &st);
@@ -180,19 +191,19 @@ void simple_load_html(char* html, int sockfd, char* buff, int n, struct User* cu
             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){
 
-    // split buff according to </body>
+    // slice the html string into 2 parts
     char start[MAX_BUFF];
     char* end = strstr(old_buffer, "</body>");
 
-    // copy and close up
+    // front part
     strncpy(start, old_buffer, strlen(old_buffer)-strlen(end));
     start[strlen(start)] = '\0';
 
-    // merge the buffs
-    strncpy(new_buffer, start, strlen(start));
+    // combine 
+    strcpy(new_buffer, start);
     strcat(new_buffer, newword);
     strcat(new_buffer, end);
 
@@ -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){
 
     // get the size of the file
@@ -240,29 +251,17 @@ void edit_load_html(char* html, int sockfd, char* buff, char* newword,struct Use
     free(new_buffer);
 }
 
-
-// read the new guess word into the user keyword array
 void record_keyword(struct User* curr_user, char *new_guess){
     for (int i=0; i<=curr_user->n_word; i++){
         if(curr_user->keywords[curr_user->n_word]==NULL){
             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++;
             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[])
 {
     // try to read the request
@@ -303,33 +302,32 @@ static bool handle_http_request(int sockfd, struct User* user_arr[])
     printf("buff1: \n");
     printf("%s\n",buff);
 
-    // get current user
     struct User* curr_user=find_curr(user_arr,buff);
 
     // sanitise the URI
     while (*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
     if (*curr == ' ' || *curr == '?')
 
         //Stage1: when user name is not registed
         if (curr_user->stage==0){
-            // the only GET method before registing name is the intro page
             if (method==GET)
             {
                 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);
             }
-            // the only POST method before registing name is the start page
-            
+        
             else if (method == POST)
             {
                 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
-                // 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 username[MAX_BUFF];
-                strncpy(username, "<br>",strlen("<br>"));
+                strcpy(username, "<br>");
                 strcat(username, user);
                 username[strlen(username)] = '\0';
                 edit_load_html("2_start.html", sockfd,buff,username,curr_user);
@@ -347,65 +345,55 @@ static bool handle_http_request(int sockfd, struct User* user_arr[])
             if(strstr(buff, "quit=Quit") != NULL)
             {   curr_user->isOnline=0;
                 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
             else if (method == GET)
             {   curr_user->isOnline=1;
                 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
         else if (curr_user->stage ==2){
-            // ready to guess 
+
             char * new_guess = strstr(buff, "keyword=") + 8;
 
-            // if quit, offline and quit
-            // clear all user info 
             if(strstr(buff, "quit=Quit") != NULL)
             {   curr_user->isOnline=0;
                 simple_load_html("7_gameover.html",sockfd,buff,n,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
-            if (the_other_user(curr_user, user_arr)->isOnline==0){
+            else if(the_other_user(curr_user, user_arr)->isOnline==0){
                 simple_load_html("5_discarded.html",sockfd,buff,n,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);
 
-            }
-            // 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))
-                {         
+            else if(new_guess!=NULL){
+                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);
                     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;
                     printf("curr_user->n_word%d\n",curr_user->n_word );
                     // remember new guess
                     record_keyword(curr_user, new_guess);
                     // display new guess
                     char guess[MAX_BUFF];
-                    strncpy(guess,"<br>",strlen("<br>"));
+                    strcpy(guess,"<br>");
                     if (curr_user->n_word==1){
                         strcat(guess, new_guess);
                         guess[strlen(new_guess)]='\0';
@@ -421,11 +409,8 @@ static bool handle_http_request(int sockfd, struct User* user_arr[])
                     edit_load_html("4_accepted.html", sockfd,buff,guess,curr_user);
                 }
             }
-            
 
 
-            
-            
 
         } 
         else
-- 
GitLab