Skip to content
Snippets Groups Projects
Commit 9d56068d authored by Abhisha Nirmalathas's avatar Abhisha Nirmalathas
Browse files

basic playing functionalities with few logic errors

parent de94b606
Branches
No related tags found
No related merge requests found
...@@ -180,27 +180,34 @@ static bool handle_http_request(int sockfd, User_list* users) ...@@ -180,27 +180,34 @@ static bool handle_http_request(int sockfd, User_list* users)
if(strncmp(req->body, "keyword=", 8) == 0){ if(strncmp(req->body, "keyword=", 8) == 0){
printf("strncmp with keywoprds is successful"); printf("strncmp with keywoprds is successful");
printf("the numer of users is %d\n", users->n_users); printf("the numer of users is %d\n", users->n_users);
// for(int i=0; i < users->n_users; i++){ for(int i=0; i < users->n_users; i++){
// printf("USER ID %d", users->users[i]->id); printf("USER ID %d", users->users[i]->id);
// if(users->users[i]->status == READY){ if(users->users[i]->status == READY){
// printf("is ready\n"); printf("is ready\n");
// } }
// if(users->users[i]->status == WAIT){ if(users->users[i]->status == WAIT){
// printf("is wait\n"); printf("is wait\n");
// } }
// if(users->users[i]->status == QUIT){ if(users->users[i]->status == QUIT){
// printf("is quit\n"); printf("is quit\n");
// } }
// } }
if(players_ready(users)){ if(player_won(users)){
post_request(buff,sockfd, "5_discarded.html"); post_request(buff,sockfd, "6_endgame.html");
} }
else if(should_player_quit(users)){ else if(should_player_quit(users)){
change_player_status(sockfd,users, QUIT); change_player_status(sockfd,users, QUIT);
post_request(buff,sockfd, "7_gameover.html"); post_request(buff,sockfd, "7_gameover.html");
} }
else if(!players_ready(users)){
post_request(buff,sockfd, "5_discarded.html");
}
else{ else{
add_keyword(sockfd, users, req->body); char* keyword = add_keyword(sockfd, users, req->body);
if(has_match_ended(users, keyword)){
change_player_status(sockfd, users, COMPLETE);
post_request(buff,sockfd, "6_endgame.html");
}
post_request(buff,sockfd, "4_accepted.html"); post_request(buff,sockfd, "4_accepted.html");
} }
} }
...@@ -234,6 +241,8 @@ static bool handle_http_request(int sockfd, User_list* users) ...@@ -234,6 +241,8 @@ static bool handle_http_request(int sockfd, User_list* users)
// player_session(buff, sockfd, "1_welcome.html", resp_string); // player_session(buff, sockfd, "1_welcome.html", resp_string);
// free(resp_string); // free(resp_string);
// free(resp); // free(resp);
User* new_player = new_user(sockfd);
add_user(new_player, users);
get_request(buff,sockfd, "1_welcome.html"); get_request(buff,sockfd, "1_welcome.html");
} }
else else
...@@ -298,6 +307,7 @@ int main(int argc, char * argv[]) ...@@ -298,6 +307,7 @@ int main(int argc, char * argv[])
FD_SET(sockfd, &masterfds); FD_SET(sockfd, &masterfds);
// record the maximum socket number // record the maximum socket number
int maxfd = sockfd; int maxfd = sockfd;
User_list* users = initialise_player_list();
while (1) while (1)
{ {
// monitor file descriptors // monitor file descriptors
...@@ -307,7 +317,6 @@ int main(int argc, char * argv[]) ...@@ -307,7 +317,6 @@ int main(int argc, char * argv[])
perror("select"); perror("select");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
User_list* users = initialise_player_list();
// loop all possible descriptor // loop all possible descriptor
for (int i = 0; i <= maxfd; ++i){ for (int i = 0; i <= maxfd; ++i){
// determine if the current file descriptor is active // determine if the current file descriptor is active
...@@ -331,9 +340,10 @@ int main(int argc, char * argv[]) ...@@ -331,9 +340,10 @@ int main(int argc, char * argv[])
// print out the IP and the socket number // print out the IP and the socket number
char ip[INET_ADDRSTRLEN]; char ip[INET_ADDRSTRLEN];
User* new_player = new_user(sockfd); // User* new_player = new_user(sockfd);
add_user(new_player, users); // add_user(new_player, users);
printf("THE NUMBER OF USERS IS %d\n", users->n_users); //printf(" %d \n", users->n_users);
// printf("THE NUMBER OF USERS IS %d\n", users->n_users);
printf( printf(
"new connection from %s on socket %d\n", "new connection from %s on socket %d\n",
// convert to human readable string // convert to human readable string
...@@ -350,7 +360,7 @@ int main(int argc, char * argv[]) ...@@ -350,7 +360,7 @@ int main(int argc, char * argv[])
} }
} }
} }
free(users);
} }
free(users);
return 0; return 0;
} }
No preview for this file type
No preview for this file type
...@@ -38,7 +38,7 @@ void resize_keywords(User* user, char* keyword){ ...@@ -38,7 +38,7 @@ void resize_keywords(User* user, char* keyword){
} }
void add_keyword(int id, User_list* users, char* query){ char* add_keyword(int id, User_list* users, char* query){
const char s[2] = "&"; const char s[2] = "&";
char * curr = query; char * curr = query;
char * token; char * token;
...@@ -50,6 +50,7 @@ void add_keyword(int id, User_list* users, char* query){ ...@@ -50,6 +50,7 @@ void add_keyword(int id, User_list* users, char* query){
resize_keywords(users->users[i], token); resize_keywords(users->users[i], token);
} }
} }
return token;
} }
void add_user(User* user, User_list* users){ void add_user(User* user, User_list* users){
...@@ -98,3 +99,42 @@ void change_player_status(int user_id, User_list* users, STATUS status){ ...@@ -98,3 +99,42 @@ void change_player_status(int user_id, User_list* users, STATUS status){
} }
} }
} }
bool keyword_match(User* user, char* keyword){
for(int i=0; user->n_keywords;i++){
if(strcmp(keyword, user->keywords[i]) == 0){
return true;
}
}
return false;
}
void reset_players(User_list *users){
for(int i=0; i< users->n_users;i++){
users->users[i]->n_keywords = 0;
users->users[i]->n_capacity = 5;
free(users->users[i]->keywords);
users->users[i]->keywords = malloc(sizeof(char*)*users->users[i]->n_capacity);
}
}
bool has_match_ended(User_list* users, char* keyword){
for(int i=0; i< users->n_users;i++){
if(users->users[i]->status == READY){
if(keyword_match(users->users[i], keyword)){
reset_players(users);
return true;
}
}
}
return false;
}
bool player_won(User_list *users){
for(int i=0; i < users->n_users; i++){
if (users->users[i]->status == COMPLETE){
return true;
}
}
return false;
}
\ No newline at end of file
...@@ -3,7 +3,8 @@ typedef enum ...@@ -3,7 +3,8 @@ typedef enum
{ {
READY, READY,
WAIT, WAIT,
QUIT QUIT,
COMPLETE
} STATUS; } STATUS;
...@@ -27,7 +28,7 @@ User_list* initialise_player_list(); ...@@ -27,7 +28,7 @@ User_list* initialise_player_list();
void resize_keywords(User* user, char* keyword); void resize_keywords(User* user, char* keyword);
void add_keyword(int id, User_list* users, char* query); char* add_keyword(int id, User_list* users, char* query);
void add_user(User* user, User_list* users); void add_user(User* user, User_list* users);
...@@ -40,3 +41,11 @@ bool should_player_quit(User_list* users); ...@@ -40,3 +41,11 @@ bool should_player_quit(User_list* users);
bool players_ready(User_list* users); bool players_ready(User_list* users);
void change_player_status(int user_id, User_list* users, STATUS status); void change_player_status(int user_id, User_list* users, STATUS status);
bool keyword_match(User* user, char* keyword);
void reset_players(User_list *users);
bool has_match_ended(User_list* users, char* keyword);
bool player_won(User_list *users);
\ No newline at end of file
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