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
|
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h> /* for debug */
#include "client.h"
#include "data_decryption.h"
#include "printing_game_frames.h"
#include "verification_client_input.h"
static const char *ip = "109.107.161.30";
static const int port = 1025;
static void clean_up_resources(struct client *cl)
{
free(cl->cc.number_arr);
clear_stack(&cl->deck);
}
static void get_data_from_server(struct client *cl, fd_set *readfds)
{
int i;
int update_info = 0;
/* pointer to delimeter (:) that is behind extract data */
char *end_p;
if(FD_ISSET(cl->fd, readfds)) {
if(!cl->data_left_in_buffer) {
cl->data_left_in_buffer = read(cl->fd, cl->buffer, max_buffer_size);
/* end of file -- closed connection (from server) */
if(!cl->data_left_in_buffer) {
clean_up_resources(cl);
exit(0);
}
#ifdef DEBUG
printf("%d\n", cl->data_left_in_buffer);
for(i = 0; i < cl->data_left_in_buffer; ++i)
putchar(cl->buffer[i]);
putchar('\n');
#endif
}
decrypt_set_state(cl, &end_p);
update_info = 1;
cl->display_new_frame = 1;
cl->pending_server_response = 0;
}
if(update_info) {
switch(cl->state) {
case first_player:
break;
case confirmation_waiting:
cl->total_players = decrypt_get_number(end_p+1, &end_p);
break;
case display_only_table:
case tossing_expectation:
case tossing:
/* last arg will contain pointer to '\n' */
decrypt_set_base_info(cl, end_p+1, &end_p);
break;
case attack:
case defense:
case attack_expectation:
decrypt_set_base_info(cl, end_p+1, &end_p);
decrypt_set_position_whose_turn(cl, end_p+1, &end_p);
break;
case defense_expectation:
case spectator:
decrypt_set_base_info(cl, end_p+1, &end_p);
decrypt_set_position_whose_turn(cl, end_p+1, &end_p);
decrypt_set_card_queue(cl, end_p+1, &end_p);
if(cl->state == spectator)
decrypt_set_spectator_mode(cl, end_p+1);
break;
case card_acceptance_status:
decrypt_set_card_acceptance_status(cl, end_p+1);
break;
case result:
decrypt_set_durak_position(cl, end_p+1);
break;
/* no data to extract */
case tossing_limit_status:
case disconnect:
default:
{}
}
for(i = 0; cl->buffer[i] != '\n'; ++i)
{}
if((cl->buffer+i - cl->buffer + 1) != cl->data_left_in_buffer) {
cl->data_left_in_buffer -= i + 1;
memmove(cl->buffer, cl->buffer+i+1, cl->data_left_in_buffer);
} else
cl->data_left_in_buffer = 0;
}
}
static void prepare_tips_for_client(struct client *cl)
{
char *trump_suit = NULL;
if(!is_empty_stack(cl->deck))
add_hint_letters_stack(cl->deck);
switch(cl->state) {
case attack:
mark_card_for_attackers_stack(cl->deck);
break;
case defense:
trump_suit = cl->trump_card + strlen(cl->trump_card) - 1;
mark_card_for_defenders_stack(cl->deck, &cl->cot, trump_suit);
break;
case tossing:
mark_card_for_tossing_stack(cl->deck, &cl->cot);
break;
default:
{}
}
}
static void change_client_frame(struct client *cl)
{
if(cl->display_new_frame) {
pgf_new_frame();
switch(cl->state) {
case first_player:
pgf_first_player();
break;
case confirmation_waiting:
pgf_confirmation_waiting(cl->total_players);
break;
case display_only_table:
case attack_expectation:
case defense_expectation:
case tossing_expectation:
pgf_table(cl);
if(cl->state == attack_expectation ||
cl->state == defense_expectation ||
cl->state == tossing_expectation)
{
pgf_select_idle_mode_message(cl->state);
}
break;
case spectator:
pgf_table(cl);
pgf_spectator_mode(cl->sp_mode);
break;
case attack:
case defense:
case tossing:
pgf_table(cl);
pgf_suggestions(cl);
break;
case card_acceptance_status:
pgf_card_acceptance_status(cl->all_input_cards_accepted);
break;
case tossing_limit_status:
pgf_tossing_limit_status();
break;
case result:
pgf_game_result(cl->durak_position);
break;
case disconnect:
pgf_disconnect();
break;
case none:
{}
}
cl->display_new_frame = 0;
}
}
static int check_users_exit(enum client_states state, const char *buffer,
int size)
{
if((state == first_player || state == confirmation_waiting) &&
size == 2 && (buffer[0] == 'q' && buffer[1] == '\n'))
return 1;
return size == 3 && !strncmp(buffer, "qq", 2) && buffer[2] == '\n';
}
static void send_data_to_server(struct client *cl, fd_set *readfds)
{
int input_result = 0, data_size;
if(FD_ISSET(0, readfds)) { /* 0 - stdin */
data_size = read(0, cl->buffer, max_buffer_size);
if(check_users_exit(cl->state, cl->buffer, data_size)) {
close(cl->fd);
clean_up_resources(cl);
pgf_disconnect();
exit(0);
}
switch(cl->state) {
case confirmation_waiting:
input_result = vci_confirmation_waiting(cl->fd, cl->buffer);
break;
case attack:
case tossing:
input_result = vci_attack_or_tossing(cl->fd, cl->buffer, cl->deck,
cl->state);
break;
case defense:
input_result = vci_defense(cl->fd, cl->buffer, cl->deck);
break;
default:
{}
}
/* if 0, then re-tracking the input client */
if(input_result)
cl->pending_server_response = 1;
}
}
static int check_tracking_client_input(enum client_states state,
int pending_server_response)
{
if(pending_server_response)
return 0;
switch(state) {
case first_player:
case confirmation_waiting:
case attack:
case defense:
case tossing:
return 1;
default:
return 0;
}
return 0;
}
int main_loop(struct client *cl)
{
int select_result;
fd_set readfds;
for(;;) {
FD_ZERO(&readfds);
FD_SET(cl->fd, &readfds);
if(check_tracking_client_input(cl->state, cl->pending_server_response))
FD_SET(0, &readfds);
if(!cl->data_left_in_buffer) {
select_result = select(cl->fd + 1, &readfds, NULL, NULL, NULL);
if(select_result == -1)
return 2;
}
get_data_from_server(cl, &readfds);
prepare_tips_for_client(cl);
if(cl->display_new_frame)
change_client_frame(cl);
send_data_to_server(cl, &readfds);
}
}
static void init_client(struct client *cl)
{
cl->fd = -1;
cl->state = none;
cl->total_players = 0;
cl->total_cards_left = 0;
cl->player_position = 0;
cl->cc.number_arr = NULL;
cl->cc.number_arr_idx = -1;
cl->cot.card_arr_idx = -1;
cl->deck = NULL;
cl->pending_server_response = 0;
cl->cq.card_arr_idx = -1;
cl->position_whose_turn = 0;
cl->display_new_frame = 0;
cl->all_input_cards_accepted = 0;
cl->data_left_in_buffer = 0;
cl->sp_mode = spectator_table;
cl->durak_position = 0;
}
/*
* 0 - failure
* 1 - success
*/
static int connect_to_server(struct client *cl)
{
int connect_result;
struct sockaddr_in addr;
cl->fd = socket(AF_INET, SOCK_STREAM, 0);
if(cl->fd == -1)
return 0;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(ip);
addr.sin_port = htons(port);
connect_result = connect(cl->fd, (struct sockaddr*) &addr, sizeof(addr));
if(connect_result == -1)
return 0;
return 1;
}
int main()
{
struct client cl;
pgf_new_frame();
pgf_welcome();
sleep(2);
init_client(&cl);
if(!connect_to_server(&cl)) {
pgf_new_frame();
pgf_connection(0);
return 1;
}
pgf_new_frame();
pgf_connection(1);
return main_loop(&cl);
}
|