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
|
#include "game.h"
#include "card.h"
#include "hand.h"
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
#include <curses.h>
#include <sys/types.h>
#include "ui.h"
#include "global.h"
#include "net/comm.h"
#include "net/client.h"
#include "net/server.h"
static void init_mainstack(card *stack, const uint32_t size)
{
assert(stack != NULL);
// assign card values to main stack
for(uint32_t i=0, val=MIN_CARD; i<size; i++, val++)
stack[i] = val;
// shuffle stack
for(uint32_t i=0; i<3*size; i++)
{
uint32_t x = rand() % size;
uint32_t y = rand() % size;
card tmp = stack[x];
stack[x] = stack[y];
stack[y] = tmp;
}
}
static const card mainstack_remove_card(card *stack, const uint32_t size)
{
for(uint32_t i=0; i<size; i++)
{
card c = stack[i];
if(c == 0)
continue;
stack[i] = 0;
return c;
}
return 0; // stack empty
}
static const uint8_t num_cards_in_stack(const card *stack, const uint32_t size)
{
uint8_t count = 0;
for(int i=0; i<size; i++)
{
if(stack[i] == 0)
continue;
count++;
}
return count;
}
static void main_loop_client(int sock)
{
bool running = true;
gamestate state = STATE_CLIENT_WAIT_FOR_HAND_CARDS;
uint8_t round = 1;
data_store *data = datamodel();
uint8_t open_card_idx;
uint8_t picked_stack_idx;
while(running)
{
switch(state)
{
case STATE_CLIENT_WAIT_FOR_HAND_CARDS:
if(round == 1)
{
// Receive and display table stacks
net_recv(sock, msg_type_initial_stacks);
ui_display_wnd_table_cards(&data->table_stacks, false, 0);
ui_display_wnd_stack_points(&data->table_stacks, false, 0);
}
// Wait for hand cards from server and display them
net_recv(sock, msg_type_deal_hand);
ui_display_wnd_hand_cards(&data->hand, false, 0);
state = STATE_CLIENT_SELECT_OPEN_CARD;
break;
case STATE_CLIENT_SELECT_OPEN_CARD:
// Select open card
open_card_idx = ui_choose_card(&data->hand);
data->selected_card = data->hand.cards[open_card_idx];
// Send open card to server
net_send(sock, msg_type_selected_card, NULL);
// Remove picked open card from hand
hand_remove_card(&data->hand, open_card_idx);
ui_display_wnd_hand_cards(&data->hand, false, 0);
state = STATE_CLIENT_WAIT_FOR_OPEN_CARDS;
break;
case STATE_CLIENT_WAIT_FOR_OPEN_CARDS:
net_recv(sock, msg_type_selected_card_all);
pnoc_sort(data->players.players, data->players.count); // sort in ascending order
ui_display_wnd_current_state(data->players.players, data->players.count, true, 0, 0); // TODO fix parameters
//state = STATE_CLIENT_PLAY_CARDS;
//sleep(2);
//return;
state = STATE_CLIENT_SELECT_OPEN_CARD;; // just for testing
break;
#if 0
case STATE_CLIENT_PLAY_CARDS:
foreach(open_card)
{
play_lowest_open_card
{
determine_stack_for_open_card
{
if (stack_has_to_be_picked)
{
if (we_have_to_pick)
{
pick_stack();
send_stack_to_server();
}
else // another client has to pick
{
receive_stack();
}
clear_stack(stack_id);
}
}
place_card()
{
if(count_stack_cards == 6)
clear_stack(stack_id);
}
}
}
if (we_have_hand_cards)
{
state = STATE_CLIENT_SELECT_OPEN_CARD;
}
else
{
receive_next_server_action();
if (server_action == DEAL_CARDS)
{
round++;
state = STATE_CLIENT_WAIT_CARDS;
}
else if (server_action == GAME_FINISHED)
state = STATE_CLIENT_GAME_FINISHED;
}
break;
#endif
default:
printf("main_loop_client: entered unknown state\n");
exit(EXIT_FAILURE);
}
}
}
static void main_loop_server(socket_list_t* client_socks)
{
bool running = true;
uint8_t round = 1;
int cards = MAX_CARD - MIN_CARD + 1;
card mainstack[cards];
gamestate state = STATE_SERVER_DEAL_HAND_CARDS;
data_store *data = datamodel();
srand(time(0));
init_mainstack(mainstack, cards);
while(running)
{
switch(state)
{
case STATE_SERVER_DEAL_HAND_CARDS:
if(round == 1)
{
// Draw cards for initial stacks and send them to clients
data->table_stacks.stacks[0].cards[0] = mainstack_remove_card(mainstack, cards);
data->table_stacks.stacks[1].cards[0] = mainstack_remove_card(mainstack, cards);
data->table_stacks.stacks[2].cards[0] = mainstack_remove_card(mainstack, cards);
data->table_stacks.stacks[3].cards[0] = mainstack_remove_card(mainstack, cards);
for(int i = 0; i < data->players.count; i++)
{
net_send(client_socks->sockets[i], msg_type_initial_stacks, NULL);
}
}
int num_dealcards = num_cards_in_stack(mainstack, cards) / data->players.count;
if(num_dealcards > 10)
num_dealcards = 10;
// Deal hand cards to clients
for(int i = 0; i < data->players.count; i++)
{
hand_t h;
memset(h.cards, 0, MAX_HAND_CARDS);
for(int j=0; j<num_dealcards; j++)
h.cards[j] = mainstack_remove_card(mainstack, cards);
hand_sort(&h);
net_send(client_socks->sockets[i], msg_type_deal_hand, &h);
}
state = STATE_SERVER_WAIT_FOR_OPEN_CARDS;
break;
case STATE_SERVER_WAIT_FOR_OPEN_CARDS:
// Receive open cards from clients
for(int i = 0; i < data->players.count; i++)
{
pnoc_t* pl = get_pnoc_from_playerid(&data->players, client_socks->player_ids[i]);
assert(pl != NULL);
net_recv(client_socks->sockets[i], msg_type_selected_card);
pl->open_card = data->selected_card;
}
for(int i=0; i<data->players.count; i++)
net_send(client_socks->sockets[i], msg_type_selected_card_all, NULL);
//state = STATE_SERVER_PLAY_CARDS;
state = STATE_SERVER_WAIT_FOR_OPEN_CARDS; // just for testing
break;
#if 0
case STATE_SERVER_PLAY_CARDS:
foreach(open_card)
{
play_lowest_open_card
{
determine_stack_for_open_card
{
if (stack_has_to_be_picked) {
receive_stack_from_client();
send_received_stack_to_other_clients();
clear_stack(stack_id);
}
}
place_card()
{
if(count_stack_cards == 6)
clear_stack(stack_id);
}
}
}
if (clients_have_hand_cards)
{
state = STATE_SERVER_WAIT_FOR_OPEN_CARDS;
}
else
{
if (main_stack_has_enough_cards_for_clients)
{
send_action_to_client(DEAL_HAND_CARDS);
round++;
state = STATE_SERVER_DEAL_HAND_CARDS;
}
else
{
send_action_to_client(GAME_FINISHED);
state = STATE_SERVER_GAME_FINISHED;
}
}
break;
#endif
default:
printf("main_loop_server: entered unknown state\n");
exit(EXIT_FAILURE);
}
}
}
void start_game(const bool servermode, const char* addr, const char* port)
{
assert(addr != NULL && port != NULL);
bool server_process = false;
if(servermode)
{
pid_t child = fork();
server_process = (child > 0);
}
if(server_process) // Start server and connect to localhost
{
int server_sock;
socket_list_t client_socks;
uint8_t num_players = 2;
data_store* data = datamodel();
server_sock = server_start(port);
server_get_players(server_sock, &client_socks, num_players);
data->players.count = num_players;
for(int i=0; i<num_players; i++)
{
net_recv(client_socks.sockets[i], msg_type_hello);
// assign ids (starting with 1; 0 is invalid)
client_socks.player_ids[i] = i+1;
data->players.players[i].player_id = i+1;
printf("Player connected: %s\n", data->players.players[i].player_name);
}
for(int i=0; i<num_players; i++)
{
net_send(client_socks.sockets[i], msg_type_start_game, NULL);
}
main_loop_server(&client_socks);
}
else // Connect to server
{
int sock;
sleep(1); // TODO make sure server process is listening
sock = client_connect_server(addr, port);
data_store* data = datamodel();
strncpy(data->nickname, "nickname", 10);
net_send(sock, msg_type_hello, NULL);
net_recv(sock, msg_type_start_game);
ui_init();
// Display all windows
ui_display_wnd_table_cards(&data->table_stacks, false, 0);
ui_display_wnd_stack_points(&data->table_stacks, false, 0);
ui_display_wnd_current_state(data->players.players, data->players.count, false, 0, 0);
ui_display_wnd_hand_cards(&data->hand, false, 0);
main_loop_client(sock);
ui_fini();
}
}
|