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
|
#include <stdlib.h>
#include <curses.h>
#include <string.h>
#include <unistd.h>
#include "ui.h"
#include "game.h"
#include "net.h"
#include "player.h"
#include "hand.h"
int main(int argc, char **argv)
{
char* port = 0;
char *addr;
bool servermode = false;
int opponents = 1;
if (argc < 2)
{
printf("usage: '%s address port' for client mode, or '%s port' for server mode\n", argv[0], argv[0]);
return EXIT_SUCCESS;
}
else if (argc == 2) // Server mode
{
int ssock;
int* csocks;
player_list players;
const char* nickname = "deki";
const hand_t testhand = { { 12, 23, 35, 42, 55, 57, 70, 81, 103, 0 } };
servermode = true;
port = argv[1];
// start listening
ssock = server_start(port);
// accept client connections
csocks = server_get_players(ssock, opponents);
players.count = opponents + 1;
players.names[0] = malloc(strlen(nickname)+1);
strcpy(players.names[0], nickname);
for(int i=0; i<opponents; i++)
{
// wait for greeting from client i and read nick
char* name = net_recv(csocks[i], msg_type_hello);
players.names[i+1] = name;
printf("player connected: len:%d, name:%s\n", strlen(name), name);
}
// start game and send player list to clients
server_start_game(csocks, opponents, &players);
// send test hand
for(int i=0; i<opponents; i++)
server_deal_cards(csocks[i], &testhand);
// receive selected stacks from players
for(int i=0; i<opponents; i++)
{
uint8_t* stack = net_recv(csocks[i], msg_type_selected_stack_c);
printf("received selected stack from player %d: %d\n", i, *stack);
// broadcast it to players
server_send_selected_stack(csocks, opponents, *stack);
free(stack);
}
// cleanup
for(int i=0; i<players.count; i++)
free(players.names[i]);
close(ssock);
free(csocks);
}
else if (argc == 3) // Client mode
{
int sock;
player_list* players;
const char* nickname = "schnippi";
hand_t *testhand;
addr = argv[1];
port = argv[2];
// connect to server
sock = client_connect_server(addr, port);
// greet server and tell nickname
client_hello(sock, nickname);
// retrieve list of all players from server
players = net_recv(sock, msg_type_start_game);
for(int i=0; i<players->count; i++)
printf("Player %d: %s\n", i, players->names[i]);
// receive test hand
testhand = net_recv(sock, msg_type_deal_cards);
printf("received cards: ");
for(int i=0; i<MAX_HAND_CARDS; i++)
printf("%d, ", testhand->cards[i]);
printf("\n");
// send selected stack to server
client_send_selected_stack(sock, 3);
// receive stack broadcast from server
for(int i=0; i<opponents; i++)
{
uint8_t* stack = net_recv(sock, msg_type_selected_stack_s);
printf("received stack selection for current player: %d\n", *stack);
free(stack);
}
// cleanup
cleanup_playerlist(players);
free(testhand);
close(sock);
}
//ui_init();
//start_game(servermode, addr, port);
//ui_fini();
return EXIT_SUCCESS;
}
|