-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.c
386 lines (360 loc) · 15.4 KB
/
server.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include "networking.c"
int maf=0;
int nur=0;
int det=0;
int mafiaNum(int users) {
int mafia = 0;
if (users > 8) {
mafia = 3;
} else if (users > 6) {
mafia = 2;
} else {
mafia = 1;
}
maf = mafia;
printf("Total Mafia Members: %d\n", mafia);
return mafia;
}
//number of nurse based off of users (number of players)
int nurseNum(int users) {
int nurse = 0;
if (users > 7) {
nurse = 2;
} else if (users > 2) {
nurse = 1;
} else {
nurse = 0;
}
nur = nurse;
printf("Total Number of Nurse(s): %d\n", nurse);
return nurse;
}
//number of dectecive based off of users (number of players)
int detectiveNum(int users) {
int detective;
if (users > 10) {
detective = 2;
} else {
detective = 1;
}
det = detective;
printf("Total Detective(s): %d\n", detective);
return detective;
}
//need to initialize turns to 0
char *genRoles(int total) {
int players = total;
char *string = malloc(sizeof(char) * 1000);
char *assign = malloc(total * sizeof(char));
for (size_t i = 0; i < maf; i++) {
assign[i] = '1';
}
for (size_t i = 0; i < det; i++) {
assign[maf + i] = '2';
}
for (size_t i = 0; i < nur; i++) {
assign[maf + det + i] = '3';
}
for (size_t i = maf + nur + det; i < total; i++) {
assign[i] = '0';
}
for (size_t i = 0; i < players; i++) {
int index = rand() % total;
strncat(string, &assign[index], 1);
strcat(string, ",");
total--;
char temp = assign[total];
assign[total] = assign[index];
assign[index] = temp;
}
free(assign);
return string;
}
int main() {
int inplay = 1;
int game_start = 0;
int sd, f, client, sub_num = 0;
int taken[13]; //boolean of numbers saying which fd's are taken already;
taken_setup(taken);
int fd1[13][2]; // host reading from subserver
int fd2[13][2]; // host writing to subserver
char *victim;
char *saved;
char **players;
srand(time(NULL));
victim = malloc(sizeof(char) * 1000);
saved = malloc(sizeof(char) * 1000);
players = calloc(12, sizeof(char*));
for (size_t i = 0; i < 12; i++) {
players[i] = malloc(sizeof(char) * 1000);
strcpy(players[i], "\0");
}
//parent -> use fd2[i][1] write to subserver write function
//forked ->fd2[sub_num][0] (subserver reads from server read function
//parent fd1[i][0] server read from subserver read function
//forked fd1[sub_num][1] subserver writes to server write function
//sever writes to all subserver using fd2[i][1] "N*num*" server.c
//subserver in while loop checks to see servers, check if buffer: buffer[0]=='N' server.c
//send buffer to clients use write (client, buffer,buffer) mafia.c
//if buffer[0]=='N' set num_players=parsed number at the end mafia.c
sd = server_setup();
int checkin = 3; // when to prompt host // chenge to 6
char *buffer_p = 0; // buffer pointer
// someone has run server.c !!!!!!!!
while (inplay) {
int client = server_connect(sd); // server receives client !!!!
sub_num = lowest_available(taken);
taken[sub_num] = 1;
char buffer[BUFFER_SIZE] = "not yet";
pipe(fd1[sub_num]); // pipes created woohoo !!!
pipe(fd2[sub_num]);
f = fork();
if (f) { // parent ==> SERVER
close(client); // closing client connection
sleep(1); // give time for creation of pipe
if (sub_num >= checkin) { // if it's time to start
printf("%d players in the game. Ready to start? (yes/no) ", sub_num);
buffer_p = fgets(buffer, sizeof(buffer), stdin);
buffer_p[strlen(buffer_p) - 1] = '\0';
printf("\n");
if (strcmp(buffer_p, "no") == 1) { // if no, give them more time
checkin += 3;
} else if (strcmp(buffer_p, "yes") == 0) { // if yes, let's start
strcpy(buffer, "Start\n");
printf("Game begins!\n");
game_start = 1; // game has started
int i = 0;
for (; i <= sub_num; i++) {
write(fd2[i][1], buffer, sizeof(buffer));
// host writes Start to subserver
}
for (i = 1; i <= sub_num; i++) {
char numP = sub_num + 96;
strcpy(buffer, "Num");
strncat(buffer, &numP, 1);
write(fd2[i][1], buffer, sizeof(buffer));
// host writes number of players to subserver
}
mafiaNum(sub_num);
nurseNum(sub_num);
detectiveNum(sub_num);
strcpy(buffer, "\0");
strcpy(buffer, "R");
char *temp = genRoles(sub_num);
strcat(buffer, temp);
for (i = 1; i <= sub_num; i++) {
write(fd2[i][1], buffer, sizeof(buffer));
// host writes number of players to subserver
}
free(temp);
strcpy(buffer,"\0");
int j = 0;
sleep(1);
for (i = 1; i <= sub_num; i++) { // i is the subserver number
read(fd1[i][0], buffer, sizeof(buffer));
// host reads username from EACH subserver
if (buffer[0] == 'U') {
int k = 0;
for (k = 1; k < strlen(buffer); k++) { // k is the character number
players[i - 1][k - 1] = buffer[k];
}
}
}
strcpy(buffer, "\0");
strcpy(buffer, "U");
int a = 0;// which username slot we are on
for (a = 0; a <= sub_num; a++) {
strcat(players[a], ",");
strcat(buffer, players[a]);
}
for (i = 1; i <= sub_num; i++) {
write(fd2[i][1], buffer, sizeof(buffer));
// host writes list of players to EACH subserver
}
int continue1 = 1;
while (continue1) {
printf("Waiting for Mafia...\n");
for (i = 1; i <= sub_num; i++) { //host reads what the victim is
read(fd1[i][0], buffer, sizeof(buffer));
if (strcmp(buffer, "done") != 0) {
strcpy(victim, buffer); //sent by mafia
printf("%s\n", victim);
}
}
for (i = 1; i <= sub_num; i++) { //host writes the signal to each subserver
strcpy(buffer, "mafia done");
write(fd2[i][1], buffer, sizeof(buffer));
}
printf("Mafia chose: %s\n", victim);
printf("Waiting for Detective...\n");
for (i = 1; i <= sub_num; i++) {
read(fd1[i][0], buffer, sizeof(buffer));
}
for (i = 1; i <= sub_num; i++) {
strcpy(buffer, "detective done");
write(fd2[i][1], buffer, sizeof(buffer));
}
printf("Waiting for Nurse...\n");
for (i = 1; i <= sub_num; i++) {
read(fd1[i][0], buffer, sizeof(buffer));
if (strcmp(buffer, "done") != 0) {
strcpy(saved, buffer);
}
}
printf("Nurse chose: %s\n", saved);
for (i = 1; i <= sub_num; i++) {
strcpy(buffer, "nurse done");
write(fd2[i][1], buffer, sizeof(buffer));
}
if (strcmp(victim, saved) == 0) { //if victim and saved are the same, then no one dies
strcpy(buffer, "");
for (i = 1; i <= sub_num; i++) {
write(fd2[i][1], buffer, sizeof(buffer));
}
printf("Nobody Died!\n");
} else {
for (i = 1; i <= sub_num; i++) {
write(fd2[i][1], victim, sizeof(victim));
}
printf("%s Died\n", victim);
}
char *statements = malloc(sizeof(char) * 12000);
strcpy(statements, "\\Mafia$ Here are the statements:\n\n");
for (i = 1; i <= sub_num; i++) {
read(fd1[i][0], buffer, sizeof(buffer));
if (strcmp(buffer, "dead") != 0) {
strcat(statements, buffer);
strcat(statements, "\n");
}
}
printf("%s\n", statements);
for (i = 1; i <= sub_num; i++) {
write(fd2[i][1], statements, 12000);
}
free(statements);
char *vote = malloc(sizeof(char) * 12000);
strcpy(vote, "V");
for (i = 1; i <= sub_num; i++) {
read(fd1[i][0], buffer, sizeof(buffer));
if (strcmp(buffer, "dead") != 0) {
strcat(vote, buffer);
strcat(vote, ",");
}
}
for (i = 1; i <= sub_num; i++) {
write(fd2[i][1], vote, sizeof(vote));
}
continue1 = 0;
//checking to see if game over
/*for (i = 1; i <= sub_num; i++) {
read(fd1[i][0], buffer, sizeof(buffer));
if (strcmp(buffer, "over") == 0) {
continue1 = 0;
close(client);
close(fd2[i][1]);
close(fd1[i][0]);
}
}*/
}
for (i = 1; i < sub_num; i++) {
close(client);
close(fd2[i][1]);
close(fd1[i][0]);
}
inplay = 0;
}
}
} else { // child ==> SUBSERVER
int mode = -3;
printf("Waiting for players to join...\n");
while (mode == -3 && read(fd2[sub_num][0], buffer, sizeof(buffer))) {
// subserver reads Start from server
if (strcmp(buffer, "Start\n") == 0) {
write(client, buffer, sizeof(buffer));
// subserver writes Start to client
strcpy(buffer, "Game Started");
} else if ((strlen(buffer) == 4) && ('N' == buffer[0])) {
write(client, buffer, sizeof(buffer));
// subserver writes number of players to client
mode = -2;
} else if (strcmp(buffer, "done")) {
write(client, buffer, sizeof(buffer));
}
}
while (mode == -2 && read(client, buffer, sizeof(buffer))) {
if (buffer[0] == 'U') {
// subserver reads username from client
write(fd1[sub_num][1], buffer, sizeof(buffer));
// subserver writes username to host
mode = -1;
}
}
while (mode == -1 && read(fd2[sub_num][0], buffer, sizeof(buffer))) {
// subserver reads the list of usernames
if (buffer[0] == 'U') {
write(client, buffer, sizeof(buffer));
// subserver writes the list of usernames to client
mode = 0;
} else if (buffer[0] == 'R') {
write(client, buffer, sizeof(buffer));
}
}
int continue1 = 1;
while (continue1) {
strcpy(buffer, "\0");
while (mode == 0) {
read(client, buffer, sizeof(buffer)); //subserver sending victim/saved/done
write(fd1[sub_num][1], buffer, sizeof(buffer));
read(fd2[sub_num][0], buffer, sizeof(buffer)); // subserver receives signal
write(client, buffer, sizeof(buffer)); // subserver sends signal to client
read(client, buffer, sizeof(buffer)); //subserver does detective signalling
write(fd1[sub_num][1], buffer, sizeof(buffer));
read(fd2[sub_num][0], buffer, sizeof(buffer));
write(client, buffer, sizeof(buffer));
read(client, buffer, sizeof(buffer)); //nurse signalling
write(fd1[sub_num][1], buffer, sizeof(buffer));
read(fd2[sub_num][0], buffer, sizeof(buffer));
write(client, buffer, sizeof(buffer));
read(fd2[sub_num][0], buffer, sizeof(buffer)); //server sending who the dead person is
write(client, buffer, sizeof(buffer));
mode = 1;
}
while (mode == 1) {
read(client, buffer, sizeof(buffer));
write(fd1[sub_num][1], buffer, sizeof(buffer));
char spec_buffer[12000] = "";
read(fd2[sub_num][0], spec_buffer, sizeof(spec_buffer));
write(client, spec_buffer, sizeof(spec_buffer));
strcpy(buffer,"\0");
read(client, buffer, sizeof(buffer));
// printf("Subserver3 read %s from client\n", buffer);
write(fd1[sub_num][1], buffer, sizeof(buffer));
//printf("subserver3.5 did this\n");
read(fd2[sub_num][0], buffer, sizeof(buffer));
// printf("Subserver4 read %s statements\n", buffer);
write(client, buffer, sizeof(buffer));
mode = 0;
}
//continue1 = 0;
//checking to see if game is over at the end of the day
/*read(client, buffer, sizeof(buffer));
write(fd1[sub_num][1], buffer, sizeof(buffer));
if (strcmp(buffer, "over") == 0) {
continue1 = 0;
}*/
}
close(client);
close(fd1[sub_num][1]);
close(fd2[sub_num][0]);
inplay = 0;
}
}
for (int i = 0; i < 12; i++) {
free(players[i]);
}
free(players);
free(victim);
free(saved);
close(sd);
return 0;
}