-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
734 lines (683 loc) · 21.5 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include "networking.h"
#include "pending.h"
void validate_arguments(int, char**);
void read_deck_file(char*, Server*);
void wait_for_players(int);
void add_player_to_game(Player*, char*);
void check_for_full_games(void);
void* start_game(void*);
void send_welcome_message(FILE*);
void deal_cards(Game*);
void increment_game_deck(Game*);
void initiate_bidding(Game*);
int check_for_eligibility(Game*);
int calculate_contract_points(Card*);
int get_winning_bidder_index(Game*);
void print_teams(Game*);
void send_to_players(Game*, char, char*, int);
int play_trick(Game*, int);
int get_trick_winner(Card**, Game*, char);
void set_points(Game*);
int check_points(Game*);
int check_for_empty_hand(Game*);
void reorder_players(Game*);
void create_player(int);
void check_for_eof(Game*, char*, int);
void get_players_bid(Game*, Card*, Card*, int, int);
// Global instance of the server
Server* server;
// Stores all of the pending games before they are started
PendingGame* pendingGameList;
int main(int argc, char *argv[]) {
signal(SIGPIPE, SIG_IGN);
// Validate that all of the input arguments are valid
validate_arguments(argc, argv);
// Allocate the global server struct instance
server = malloc(sizeof(Server));
// Store the server greeting message
server->greeting = strdup(argv[2]);
// Initialise the pending game list to zero
pendingGameList = malloc(sizeof(PendingGame));
pendingGameList = NULL;
// Check for a valid deck file and read in the contents
read_deck_file(argv[3], server);
// Open a listening socket on the supplied port.
int fdServer = open_listen(atoi(argv[1]));
// Wait for incoming client connections
wait_for_players(fdServer);
}
/*
* Validate the command line input arguments.
*/
void validate_arguments(int argc, char** argv) {
if (argc != 4) {
// Throw usage error (exit(1))
fprintf(stderr, "Usage: serv499 port greeting deck\n");
exit(1);
}
char** remainder = malloc(sizeof(char**));
int port = strtol(argv[1], remainder, 10);
if (strcmp(*remainder, "") || port < 1 || port > 65535) {
fprintf(stderr, "Invalid Port\n");
exit(4);
}
free(remainder);
}
/*
* Reads multiple decks from a file and stores them in the server.
*/
void read_deck_file(char* deckFile, Server* server) {
char** decks = NULL;
int deckCount = 0;
FILE* fp = fopen(deckFile, "r");
// Cannot open deck file
if (fp == NULL) {
fprintf(stderr, "Deck Error\n");
exit(6);
}
// Loop through each line (deck) until there is no more
while (1) {
// Add enough memory for one whole deck + null terminator
char* deck = malloc(sizeof(char) * 105);
if (fgets(deck, 105, fp) == NULL) {
free(deck);
break;
}
// Check if there are 52 cards in the deck
if (strlen(deck) != 104) {
fprintf(stderr, "Deck Error\n");
exit(6);
}
if (!validate_deck(deck)) {
fprintf(stderr, "Deck Error\n");
exit(6);
}
decks = realloc(decks, sizeof(decks) + strlen(deck) + 1);
decks[deckCount] = malloc(strlen(deck) + 1);
memcpy(decks[deckCount++], deck, strlen(deck) + 1);
free(deck);
fgetc(fp);
}
server->decks = decks;
server->deckCount = deckCount;
}
/*
* Wait for players (clients) to connect and forwards them through to games.
*/
void wait_for_players(int fdServer) {
int maxFD = fdServer;
int newFD;
fd_set baseSet, readSet;
struct sockaddr_in fromAddr;
socklen_t fromAddrSize;
char hostname[124];
FD_ZERO(&baseSet);
FD_SET(fdServer, &baseSet);
FD_SET(STDIN_FILENO, &baseSet);
while (1) {
readSet = baseSet;
/* Block until any activity happens on the file descriptors */
if (select(maxFD + 1, &readSet, NULL, NULL, NULL) < 0) {
//exit(5);
}
// If we get here something happened
for (int fd = maxFD; fd >= 0; fd--) {
if (FD_ISSET(fd, &readSet)) {
if (fd == fdServer) {
// New connect request
fromAddrSize = sizeof(struct sockaddr_in);
newFD = accept(fdServer, (struct sockaddr*)&fromAddr,
&fromAddrSize);
if (newFD < 0) {
exit(5);
}
// Add new file descriptor to our master set
FD_SET(newFD, &baseSet);
// Update max if necessary
if (newFD > maxFD) {
maxFD = newFD;
}
int error = getnameinfo((struct sockaddr*)&fromAddr,
fromAddrSize, hostname, 124, NULL, 0, 0);
if (error) {
exit(5);
} else {
create_player(newFD);
}
}
}
}
}
}
/*
* Creates a new player to add to a game.
*/
void create_player(int newFD) {
Player* player = malloc(sizeof(Player));
player->writeFD = fdopen(newFD, "w");
player->readFD = fdopen(newFD, "r");
player->name = read_socket_message(player->readFD);
char* gameName = read_socket_message(player->readFD);
send_welcome_message(player->writeFD);
add_player_to_game(player, gameName);
}
/*
* Adds a connected player (client) to a game if it exists, otherwise
* creates a new game.
*/
void add_player_to_game(Player* player, char* gameName) {
PendingGame* pg;
if ((pg = search_game_in_list(gameName, pendingGameList)) != NULL) {
// The game exists so append player to game
pg->game->playerCount++;
player->id = pg->game->playerCount;
pg->game->players[(pg->game->playerCount) - 1] = *player;
} else {
// The game does not exist, so create it and add to list
Game* game = malloc(sizeof(Game));
game->players = malloc(sizeof(Player) * 4);
game->name = gameName;
game->playerCount = 1;
game->players[0] = *player;
player->id = 1;
if (NULL == pendingGameList) {
// If this is the first game created, make it the head of the
// linked list
pendingGameList = add_to_list(game, NULL);
} else {
// Otherwise we don't care so add it on the end
add_to_list(game, pendingGameList);
}
}
check_for_full_games();
}
/*
* Iterates through the pending games and checks if any of them have
* the full 4 players needed to start the game.
*/
void check_for_full_games(void) {
pthread_t threadId;
PendingGame* pg;
// Gets a completed game. Because this is called after every player
// addition, there can only be one game to possible fill at a time.
if ((pg = check_if_complete(pendingGameList)) != NULL) {
Game* game = pg->game;
pendingGameList = delete_game_from_list(game->name, pendingGameList);
pthread_create(&threadId, NULL, start_game, (void*)(Game*)game);
pthread_detach(threadId);
}
}
/*
* The main game thread.
*/
void* start_game(void* arg) {
Game* game = (Game*)arg;
int currentPlayer;
// Initialise some stuff
game->currentDeck = 0;
game->team1Points = 0;
game->team2Points = 0;
// Print the informational team message
reorder_players(game);
print_teams(game);
// Main Game Loop
while (!check_points(game)) {
game->team1Wins = 0;
game->team2Wins = 0;
// Deal cards
deal_cards(game);
// Initiate bidding
initiate_bidding(game);
// Select the player who won bidding to start first
currentPlayer = get_winning_bidder_index(game);
// Main hand loop
while (1) {
if (check_for_empty_hand(game)) {
break;
}
int winner = play_trick(game, currentPlayer);
currentPlayer = winner;
}
set_points(game);
char pointsMsg[1028];
sprintf(pointsMsg, "Team 1=%d, Team 2=%d", game->team1Points,
game->team2Points);
send_to_players(game, 'M', pointsMsg, -1);
}
send_to_players(game, 'O', "", -1);
for (int i = 0; i < 4; i++) {
fclose(game->players[i].readFD);
fclose(game->players[i].writeFD);
}
pthread_exit(NULL);
}
/*
* Check if any team has surpassed 499.
*/
int check_points(Game* game) {
if (game->contractTeam == 1) {
if (game->team1Points > 499) {
send_to_players(game, 'M', "Winner is Team 1", -1);
return 1;
} else if (game->team1Points < -499) {
send_to_players(game, 'M', "Winner is Team 2", -1);
return 1;
}
} else {
if (game->team2Points > 499) {
send_to_players(game, 'M', "Winner is Team 2", -1);
return 1;
} else if (game->team2Points < -499) {
send_to_players(game, 'M', "Winner is Team 1", -1);
return 1;
}
}
return 0;
}
/*
* Alters the appropriate team's points depending on the game result.
*/
void set_points(Game* game) {
if (game->contractTeam == 1) {
if (game->contractGoal > game->team1Wins) {
game->team1Points -= game->contractPoints;
} else {
game->team1Points += game->contractPoints;
}
} else if (game->contractTeam == 2) {
if (game->contractGoal > game->team2Wins) {
game->team2Points -= game->contractPoints;
} else {
game->team2Points += game->contractPoints;
}
}
}
/*
* Plays a trick, being one card from each player.
*/
int play_trick(Game* game, int startingPlayer) {
// Because starting player is an index we want to add 1
int p = startingPlayer + 1;
Card** cards = malloc(sizeof(Card*) * 4);
char leadSuit;
for (int i = 0; i < 4; i++) {
if (startingPlayer > 3) {
p = ((startingPlayer++) % 4);
} else {
p = startingPlayer++;
}
cards[p] = malloc(sizeof(Card));
if (i == 0) {
send_socket_message(game->players[p].writeFD, "L");
char* response = read_socket_message(game->players[p].readFD);
check_for_eof(game, response, p);
read_card_input_from_string(cards[p], response);
if (!is_valid_card(cards[p])) {
fprintf(stderr, "server: bad card from client\n");
} else {
leadSuit = cards[p]->suit;
}
} else {
char c[2];
sprintf(c, "%c", leadSuit);
char* msg = create_message('P', c);
send_socket_message(game->players[p].writeFD, msg);
free(msg);
char* response = read_socket_message(game->players[p].readFD);
check_for_eof(game, response, p);
read_card_input_from_string(cards[p], response);
if (!is_valid_card(cards[p])) {
fprintf(stderr, "server: bad card from client\n");
}
}
send_socket_message(game->players[p].writeFD, "A");
game->players[p].cardCount--;
char play[1024];
sprintf(play, "%s plays %s", game->players[p].name,
card_to_string(cards[p]));
send_to_players(game, 'M', play, p);
}
return get_trick_winner(cards, game, leadSuit);
}
/*
* Checks for eof given by a client.
*/
void check_for_eof(Game* game, char* response, int p) {
if (!strcmp(response, "EOF")) {
char errMsg[1028];
sprintf(errMsg, "%s disconnected early",
game->players[p].name);
send_to_players(game, 'M', errMsg, -1);
}
}
/*
* Check if all the players have empty hands.
*/
int check_for_empty_hand(Game* game) {
if (game->players[0].cardCount == 0 &&
game->players[1].cardCount == 0 &&
game->players[2].cardCount == 0 &&
game->players[3].cardCount == 0) {
return 1;
} else {
return 0;
}
}
/*
* Returns the player index of the winner of the last trick.
*/
int get_trick_winner(Card** cards, Game* game, char leadSuit) {
int currentWinner = 0;
Card* currentCard = NULL;
for (int i = 0; i < 4; i++) {
if (cards[i]->suit == leadSuit) {
if (is_higher(currentCard, cards[i])) {
if (currentCard == NULL) {
currentCard = malloc(sizeof(Card));
}
currentCard = cards[i];
currentWinner = i;
}
}
}
for (int i = 0; i < 4; i++) {
if (cards[i]->suit == game->trumps) {
if ((currentCard->suit == leadSuit) &&
(leadSuit != game->trumps)) {
currentCard = cards[i];
currentWinner = i;
} else if (is_higher(currentCard, cards[i])) {
currentCard = cards[i];
currentWinner = i;
}
}
}
free(currentCard);
char msg[1028];
sprintf(msg, "%s won", game->players[currentWinner].name);
send_to_players(game, 'M', msg, -1);
if (currentWinner == 0 || currentWinner == 2) {
game->team1Wins++;
} else {
game->team2Wins++;
}
return currentWinner;
}
/*
* Reorders players in lexographical order.
*/
void reorder_players(Game* game) {
char* players[4];
int playerOrders[4];
int k = 0;
players[0] = game->players[0].name;
players[1] = game->players[1].name;
players[2] = game->players[2].name;
players[3] = game->players[3].name;
qsort(players, 4, sizeof(players[0]), &compare_players);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (players[i] == game->players[j].name) {
playerOrders[k++] = j;
}
}
}
Player* tempPlayers = malloc(sizeof(Player) * 4);
tempPlayers[0] = game->players[playerOrders[0]];
tempPlayers[1] = game->players[playerOrders[1]];
tempPlayers[2] = game->players[playerOrders[2]];
tempPlayers[3] = game->players[playerOrders[3]];
game->players[0] = tempPlayers[0];
game->players[1] = tempPlayers[1];
game->players[2] = tempPlayers[2];
game->players[3] = tempPlayers[3];
}
/*
* Prints the team names to all players.
*/
void print_teams(Game* game) {
char team1[128];
char team2[128];
sprintf(team1, "Team1: %s, %s", game->players[0].name,
game->players[2].name);
sprintf(team2, "Team2: %s, %s", game->players[1].name,
game->players[3].name);
char* msg1 = create_message('M', team1);
char* msg2 = create_message('M', team2);
for (int i = 0; i < 4; i++) {
send_socket_message(game->players[i].writeFD, msg1);
send_socket_message(game->players[i].writeFD, msg2);
}
}
/*
* Sends a welcome message to all players.
*/
void send_welcome_message(FILE* fd) {
char* welcome = create_message('M', server->greeting);
send_socket_message(fd, welcome);
free(welcome);
}
/*
* Deals out a single hand to each player in the game.
*/
void deal_cards(Game* game) {
char* deck = server->decks[game->currentDeck];
char* p1 = malloc(sizeof(char) * 28);
char* p2 = malloc(sizeof(char) * 28);
char* p3 = malloc(sizeof(char) * 28);
char* p4 = malloc(sizeof(char) * 28);
int a = 0;
int b = 0;
int c = 0;
int d = 0;
for(int i = 0; i < 104; i++) {
p1[a++] = deck[i++];
p1[a++] = deck[i++];
p2[b++] = deck[i++];
p2[b++] = deck[i++];
p3[c++] = deck[i++];
p3[c++] = deck[i++];
p4[d++] = deck[i++];
p4[d++] = deck[i];
}
send_socket_message(game->players[0].writeFD,
create_message('H', p1));
send_socket_message(game->players[1].writeFD,
create_message('H', p2));
send_socket_message(game->players[2].writeFD,
create_message('H', p3));
send_socket_message(game->players[3].writeFD,
create_message('H', p4));
store_hand(p1, &game->players[0]);
store_hand(p2, &game->players[1]);
store_hand(p3, &game->players[2]);
store_hand(p4, &game->players[3]);
free(p1);
free(p2);
free(p3);
free(p4);
increment_game_deck(game);
}
/*
* Increments the current game deck so that the next deck in the deckfile is
* used.
*/
void increment_game_deck(Game* game) {
game->currentDeck = (game->currentDeck + 1) % server->deckCount;
}
/*
* Initiaes the bidding phase of the game and sets the trumps.
*/
void initiate_bidding(Game* game) {
int notMax = 1;
int go = 1;
int first = 1;
Card* currentBid = malloc(sizeof(Card));
Card* sentBid = malloc(sizeof(Card));
for (int i = 0; i < 4; i++) {
game->players[i].eligible = 1;
}
while (go && notMax) {
for (int i = 0; i < 4; i++) {
go = check_for_eligibility(game);
if (game->players[i].eligible && go) {
get_players_bid(game, currentBid, sentBid, i, first);
first = 0;
}
if (currentBid->suit == 'H' &&
currentBid->rank == '9') {
for (int j = 0; j < 4; j++) {
if (j != i) {
game->players[j].eligible = 0;
}
game->players[i].eligible = 1;
}
notMax = 0;
break;
}
}
}
game->trumps = currentBid->suit;
char goal[2];
goal[0] = currentBid->rank;
goal[1] = '\0';
game->contractGoal = atoi(goal);
game->contractPoints = calculate_contract_points(currentBid);
int team = get_winning_bidder_index(game);
if (team == 0 || team == 2) {
game->contractTeam = 1;
} else {
game->contractTeam = 2;
}
char* msg = card_to_string(currentBid);
send_to_players(game, 'T', msg, -1);
free(msg);
free(currentBid);
free(sentBid);
}
/*
* Gets an individual player's bid and parses it.
*/
void get_players_bid(Game* game, Card* currentBid, Card* sentBid, int i,
int first) {
char buff[1028];
if (first) {
first = 0;
// First bid
send_socket_message(game->players[i].writeFD, "B");
char* response = read_socket_message(
game->players[i].readFD);
check_for_eof(game, response, i);
read_card_input_from_string(sentBid, response);
if (!is_valid_bid(sentBid)) {
if (sentBid->rank == 'P' && sentBid->suit == 'P') {
game->players[i].eligible = 0;
sprintf(buff, "%s passes",
game->players[i].name);
send_to_players(game, 'M', buff, i);
} else {
fprintf(stderr, "server: bad bid\n");
}
} else {
memcpy(currentBid, sentBid, sizeof(Card));
sprintf(buff, "%s bids %s", game->players[i].name,
card_to_string(currentBid));
send_to_players(game, 'M', buff, i);
}
} else {
send_socket_message(game->players[i].writeFD,
create_message('B', card_to_string(currentBid)));
char* response = read_socket_message(
game->players[i].readFD);
check_for_eof(game, response, i);
read_card_input_from_string(sentBid, response);
if (!is_valid_bid(sentBid) ||
!is_higher_bid(currentBid, sentBid)) {
if (sentBid->rank == 'P' && sentBid->suit == 'P') {
game->players[i].eligible = 0;
sprintf(buff, "%s passes",
game->players[i].name);
send_to_players(game, 'M', buff, i);
} else {
fprintf(stderr, "server: bad bid\n");
}
} else {
memcpy(currentBid, sentBid, sizeof(Card));
sprintf(buff, "%s bids %s", game->players[i].name,
card_to_string(currentBid));
send_to_players(game, 'M', buff, i);
}
}
}
/*
* Checks if there is more than one player eligible for bidding.
*/
int check_for_eligibility(Game* game) {
int p = 0;
for (int i = 0; i < 4; i++) {
if (game->players[i].eligible) {
p++;
}
}
if (p == 1) {
return 0;
} else {
return 1;
}
}
/*
* Returns the player index of the winning bidder.
*/
int get_winning_bidder_index(Game* game) {
for (int i = 0; i < 4; i++) {
if (game->players[i].eligible) {
return i;
}
}
return -1;
}
/*
* Calculates the amount of points a winning bid is worth.
*/
int calculate_contract_points(Card* bid) {
int points;
int rank;
char* buff = malloc(2);
buff[0] = bid->rank;
buff[1] = '\0';
rank = atoi(buff);
switch (bid->suit) {
case 'S':
points = (rank - 4) * 50 + 20;
break;
case 'C':
points = (rank - 4) * 50 + 30;
break;
case 'D':
points = (rank - 4) * 50 + 40;
break;
case 'H':
points = (rank - 4) * 50 + 50;
break;
}
free(buff);
return points;
}
/*
* Sends a structured message to all players, excluding the player index
* given as the exclude parameter.
*/
void send_to_players(Game* game, char type, char* message, int exclude) {
for (int i = 0; i < 4; i++) {
if (i != exclude) {
char* msg = create_message(type, message);
send_socket_message(game->players[i].writeFD, msg);
free(msg);
}
}
}