-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
225 lines (207 loc) · 7.69 KB
/
game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "server.h"
typedef enum {EVEN, ODD, DOUB, CON} move_type;
void roll_dice() {
game->die1 = 1 + (rand() % 6);
game->die2 = 1 + (rand() % 6);
printf("(Dice rolled %d and %d - Total %d)\n", game->die1, game->die2, game->die1+game->die2);
}
response game_handler(move_type type, int index, int val) {
bool win = false;
switch (type) {
case EVEN: {
int sum = game->die1 + game->die2;
if (sum % 2 == 0 && game->die1 != game->die2) {
win = true;
break;
}
} case ODD: {
int sum = game->die1 + game->die2;
if (sum % 2 != 0 && sum > 5) {
win = true;
break;
}
} case DOUB: {
if (game->die1 == game->die2) {
win = true;
break;
}
} case CON: {
if (game->die1 == val || game->die2 == val) {
win = true;
break;
}
}
}
if (win) {
return PASS;
}
return FAIL;
}
response message_handler(char message[], int index) {
int len = strlen(message);
// Check for invalid message lengths
if (len < 11 || len > 13) {
printf("Invalid message length from client %s\n", clients[index].client_id);
return ELIM;
}
// Check for tailing ',' which is not taken into account using strtok
if (message[strlen(message)-1] == ',') {
printf("Invalid message format from client %s\n", clients[index].client_id);
return ELIM;
}
// Tokenise the message with the ',' delimiter
char temp[PACKET_SIZE];
strcpy(temp, message);
char *token = strtok(temp, ",");
int sections = 1;
while (token != NULL) {
switch (sections) {
case 1: // Client ID
if (strcmp(token, clients[index].client_id) != 0) { // Checks if the client_id is spoofed
printf("Cheating detected from client %s (sent a packet with another ID %s)\n", clients[index].client_id, token);
return ELIM;
}
break;
case 2: // Asset equals to MOV
if (strcmp(token, "MOV") != 0) {
printf("Invalid MOV syntax from client %s\n", clients[index].client_id);
return ELIM;
}
break;
case 3: // Move type
if (strcmp(token, "EVEN") == 0) {
return game_handler(EVEN, index, 0);
} else if (strcmp(token, "DOUB") == 0) {
return game_handler(DOUB, index, 0);
} else if (strcmp(token, "ODD") == 0 && len == 11) {
return game_handler(ODD, index, 0);
} else if (strcmp(token, "CON") == 0 && len == 13) {
token = strtok(NULL, ",");
if (token == NULL) { // Missing the contains value
printf("Missing CON value from client %s\n", clients[index].client_id);
return ELIM;
}
int val = atoi(token);
if (val < 1 || val > 6) {
printf("Invalid CON value from client %s\n", clients[index].client_id);
return ELIM;
}
return game_handler(CON, index, val);
} else { // Invalid type, does not match required enums
printf("Invalid move type from client %s\n", clients[index].client_id);
return ELIM;
}
}
sections++;
token = strtok(NULL, ",");
}
printf("Invalid message format from client %s\n", clients[index].client_id);
return ELIM;
}
void new_round() {
int ready = 0;
int ready_required = game->players;
int time = 0;
// Check whether all the clients have sent their packet
while (time < POLLING_RATE * MOVE_TIMEOUT) {
for (int i = 0; i < game->max_players; i++) {
// Check non-eliminated clients, with no or timeout results
if (clients[i].client_fd != -1 && (clients[i].result == NONE || clients[i].result == TIMEOUT)) {
if (clients[i].rec[0] == '\0') {
clients[i].result = TIMEOUT; // Indicate those who have not sent
} else {
clients[i].result = message_handler(clients[i].rec, i);
ready++;
}
}
}
if (ready == ready_required) {
break;
}
time++;
usleep((int) (1E6 / POLLING_RATE));
}
// Elimination & player count handler
for (int i = 0; i < game->max_players; i++) {
if (clients[i].client_fd != -1) {
if (clients[i].result == TIMEOUT) {
clients[i].lives--;
printf("Client %s failed to send a move and lost a life (%d lives)\n", clients[i].client_id, clients[i].lives);
clients[i].result = FAIL; // No longer eliminated as per spec changes
if (clients[i].lives == 0) {
clients[i].result = ELIM;
game->players--;
}
} else if (clients[i].result == ELIM) {
game->players--;
} else if (clients[i].result == FAIL) {
clients[i].lives--;
printf("Client %s lost a life (%d lives)\n", clients[i].client_id, clients[i].lives);
if (clients[i].lives == 0) {
clients[i].result = ELIM;
game->players--;
}
} else if (clients[i].result == PASS) {
printf("Client %s made it through (%d lives)\n", clients[i].client_id, clients[i].lives);
}
}
memset(clients[i].rec, '\0', PACKET_SIZE);
}
// Send packets accordingly
for (int i = 0; i < game->max_players; i++) {
if (clients[i].client_fd != -1) {
if (game->players == 1 && clients[i].result != ELIM) {
continue;
} else if (game->players == 0) { // Draw case
send_packet(VICT, i); // Send a victory packet instead of ELIM
} else {
if (clients[i].result == ELIM) {
eliminate_client(i);
}
send_packet(clients[i].result, i);
}
clients[i].result = NONE;
}
}
printf("%d players remaining\n", game->players);
if (game->players == 0) {
game->status = FINISHED;
int draw_players = 0;
printf("\nDraw!");
for (int i = 0; i < game->max_players; i++ ) {
if (clients[i].client_fd != -1) {
printf(" Client %s,", clients[i].client_id);
draw_players++;
}
}
if (draw_players == 0) { // Unusual case, remaining players have all left
printf(" There are no winners\n");
} else {
printf("\b have won!\n");
}
} else if (game->players == 1) {
game->status = FINISHED;
for (int i = 0; i < game->max_players; i++) {
if (clients[i].client_fd != -1) {
printf("\nWinner! Client %s has won!\n", clients[i].client_id);
return;
}
}
}
}
void init_game() {
printf("Starting game\n");
usleep((int) (1E6 / POLLING_RATE));
srand(time(NULL) * getpid()); // Seeds rand
for (int i = 0; i < game->max_players; i++) {
if (clients[i].client_fd != -1) {
send_packet(START, i);
}
}
int round = 1;
while (game->status != FINISHED) {
printf("\nRound %d ", round++);
roll_dice();
new_round();
}
}