diff options
Diffstat (limited to 'windows_client/verification_client_input.c')
-rw-r--r-- | windows_client/verification_client_input.c | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/windows_client/verification_client_input.c b/windows_client/verification_client_input.c new file mode 100644 index 0000000..08b00fc --- /dev/null +++ b/windows_client/verification_client_input.c @@ -0,0 +1,116 @@ +#include "verification_client_input.h" +#include "card_handling.h" +#include "printing_game_frames.h" + +#include <winsock2.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') { + send(fd, buffer, 1, 0); + 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 */ + send(fd, buffer, 1, 0); + 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'; + send(fd, output_buffer, free_pos+1, 0); + 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 */ + send(fd, buffer, 1, 0); + 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'; + send(fd, output_buffer, last_idx+1, 0); + return 1; + } + /* card is not found in the deck or cannot be used */ + printf("incorrect input\n> "); + fflush(stdout); + return 0; +} |