Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
N
nirmalathasa_comp30023_2019_project-1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Abhisha Nirmalathas
nirmalathasa_comp30023_2019_project-1
Commits
9d56068d
Commit
9d56068d
authored
Apr 14, 2019
by
Abhisha Nirmalathas
Browse files
Options
Downloads
Patches
Plain Diff
basic playing functionalities with few logic errors
parent
de94b606
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
http-server.c
+31
-21
31 additions, 21 deletions
http-server.c
http-server.o
+0
-0
0 additions, 0 deletions
http-server.o
image_tagger
+0
-0
0 additions, 0 deletions
image_tagger
user.c
+41
-1
41 additions, 1 deletion
user.c
user.h
+12
-3
12 additions, 3 deletions
user.h
user.o
+0
-0
0 additions, 0 deletions
user.o
with
84 additions
and
25 deletions
http-server.c
+
31
−
21
View file @
9d56068d
...
@@ -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
(
player
s_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
;
}
}
This diff is collapsed.
Click to expand it.
http-server.o
+
0
−
0
View file @
9d56068d
No preview for this file type
This diff is collapsed.
Click to expand it.
image_tagger
+
0
−
0
View file @
9d56068d
No preview for this file type
This diff is collapsed.
Click to expand it.
user.c
+
41
−
1
View file @
9d56068d
...
@@ -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
This diff is collapsed.
Click to expand it.
user.h
+
12
−
3
View file @
9d56068d
...
@@ -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
This diff is collapsed.
Click to expand it.
user.o
+
0
−
0
View file @
9d56068d
No preview for this file type
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment