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
|
#include "verification_client_input.h"
#include "card_handling.h"
#include "printing_game_frames.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>
enum {
output_buffer_size = 1024
};
static char output_buffer[output_buffer_size];
/*
* 1 - response received
* 0 - waiting for re-entry of data
*/
int vci_confirmation_waiting(int fd, char *buffer)
{
if(buffer[0] == '\n') {
write(fd, buffer, 1);
return 1;
}
else
printf("please press enter\n");
return 0;
}
/*
* example: A#\A^\Av\'\n'
* or (while tossing): '\n'
*/
int vci_attack_or_tossing(int fd, char *buffer, player_cards deck,
enum client_states state)
{
int rank = 0, i, free_pos = 0;
char * card = NULL;
if(state == tossing && buffer[0] == '\n') { /* cancel card tossing */
write(fd, buffer, 1);
printf("skipping the card toss\n");
return 1;
}
for(i = 0; buffer[i] != '\n'; ++i) {
/* some symbols */
if(state == attack && buffer[1] != '\n') {
card = card_search_by_marked_letter(deck, buffer[i]);
if(card) {
if(!rank)
rank = convert_rank_to_int(card);
if(rank != convert_rank_to_int(card)) {
printf("incorrect input\n> ");
fflush(stdout);
return 0;
}
}
/* one symbol inputed? */
} else if(state == attack && buffer[1] == '\n')
card = card_search_by_unmarked_letter(deck, buffer[i]);
else if(state == tossing)
card = card_search_by_marked_letter(deck, buffer[i]);
if(!card) {
printf("incorrect input\n> ");
fflush(stdout);
return 0;
}
strncpy(output_buffer + free_pos, card, strlen(card));
free_pos += strlen(card);
output_buffer[free_pos] = '\\';
++free_pos;
}
if(state == attack && buffer[0] == '\n') {
printf("incorrect input\n> ");
fflush(stdout);
return 0;
}
output_buffer[free_pos] = '\n';
write(fd, output_buffer, free_pos+1);
return 1;
}
/*
* example: 5v'\n'
* or: '\n'
*/
int vci_defense(int fd, char *buffer, player_cards deck)
{
int last_idx;
char *card = NULL;
if(buffer[0] == '\n') { /* the player does not want to keep the defense */
write(fd, buffer, 1);
printf("accepting cards\n");
return 1;
}
if(buffer[1] != '\n') {
printf("incorrect input\n> ");
fflush(stdout);
return 0;
}
if((card = card_search_by_marked_letter(deck, buffer[0])) != NULL) {
strcpy(output_buffer, card);
last_idx = strlen(card);
output_buffer[last_idx] = '\n';
write(fd, output_buffer, last_idx+1);
return 1;
}
/* card is not found in the deck or cannot be used */
printf("incorrect input\n> ");
fflush(stdout);
return 0;
}
|