diff options
Diffstat (limited to 'linux_client/data_decryption.c')
-rw-r--r-- | linux_client/data_decryption.c | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/linux_client/data_decryption.c b/linux_client/data_decryption.c new file mode 100644 index 0000000..1054c45 --- /dev/null +++ b/linux_client/data_decryption.c @@ -0,0 +1,181 @@ +#include "data_decryption.h" +#include "client.h" +#include "card_stack.h" + +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +void decrypt_set_state(struct client *cl, char **end_p) +{ + enum client_states found_state; + + found_state = (enum client_states) strtol(cl->buffer, end_p, 10); + cl->state = found_state; +} + +/* return convert number from str */ +int decrypt_get_number(char *start_p, char **end_p) +{ + return strtol(start_p, end_p, 10); +} + +static void decrypt_set_trump_card(struct client *cl, char *start_p, + char **end_p) +{ + int i; + + for(i = 0; *start_p != ':'; ++start_p, ++i) + cl->trump_card[i] = *start_p; + cl->trump_card[i] = '\0'; + *end_p = start_p; +} + +void decrypt_set_base_info(struct client *cl, char *start_p, char **end_p) +{ + int i, j; + char tmp_card[4]; + char *card = NULL; + + cl->total_players = decrypt_get_number(start_p, end_p); + cl->total_cards_left = decrypt_get_number(*end_p+1, end_p); + cl->player_position = decrypt_get_number(*end_p+1, end_p); + decrypt_set_trump_card(cl, *end_p+1, end_p); + + if(!cl->cc.number_arr) { + cl->cc.number_arr = malloc(cl->total_players * sizeof(int)); + /* + * memorize the size of the array, + * because later the number of players may change (client disconnection) + */ + cl->cc.number_arr_idx = cl->total_players-1; + } + + /* ============== extraction quantity of cards =============== */ + + /* numbers are separated by '\' */ + i = 0; + do { + cl->cc.number_arr[i] = decrypt_get_number(*end_p+1, end_p); + ++i; + } while(**end_p != ':'); + + /* ============== extraction cards on table =============== */ + i = 0, j = 0; + for(start_p = *end_p + 1; *start_p != ':'; ++start_p) { + /* empty table */ + if(*start_p == '=') { + /* to delimiter : */ + ++start_p; + i = -1; + break; + } + /* delimiter between cards on table */ + if(*start_p == '+') { + cl->cot.card_arr[i][j] = '\0'; + ++i; + j = 0; + continue; + } + cl->cot.card_arr[i][j] = *start_p; + ++j; + } + /* for the last card we need to add a null character */ + if(i != -1) + cl->cot.card_arr[i][j] = '\0'; + cl->cot.card_arr_idx = i; + + /* ================= extraction player's deck ================*/ + /* clear previous stack */ + if(cl->deck) + clear_stack(&cl->deck); + init_stack(&cl->deck); + + for(++start_p, i = 0; *start_p != ':' && *start_p != '\n'; ++start_p) { + /* empty deck */ + if(*start_p == '=') { + i = -1; + /* to delimiter ':' or '\n' */ + ++start_p; + break; + } + /* delimiter */ + if(*start_p == '\\') { + tmp_card[i] = '\0'; + card = malloc(strlen(tmp_card) + 1); + strcpy(card, tmp_card); + push_stack(&cl->deck, card); + i = 0; + continue; + } + tmp_card[i] = *start_p; + ++i; + } + if(i != -1) { + tmp_card[i] = '\0'; + card = malloc(strlen(tmp_card) + 1); + strcpy(card, tmp_card); + push_stack(&cl->deck, card); + } + *end_p = start_p; +} + +void decrypt_set_position_whose_turn(struct client *cl, char *start_p, + char **end_p) +{ + cl->position_whose_turn = decrypt_get_number(start_p, end_p); +} + +void decrypt_set_card_queue(struct client *cl, char *start_p, char **end_p) +{ + int i, j; + + for(i = 0, j = 0; *start_p != ':' && *start_p != '\n'; ++start_p) { + /* empty queue */ + if(*start_p == '=') { + i = -1; + ++start_p; + break; + } + /* delimiter '\' */ + if(*start_p == '\\') { + cl->cq.card_arr[i][j] = '\0'; + j = 0; + ++i; + continue; + } + cl->cq.card_arr[i][j] = *start_p; + ++j; + } + /* last element */ + if(i != -1) + cl->cq.card_arr[i][j] = '\0'; + cl->cq.card_arr_idx = i; + *end_p = start_p; +} + +void decrypt_set_card_acceptance_status(struct client *cl, char *start_p) +{ + int i; + char tmp_buffer[8]; + + for(i = 0; *start_p != '\n'; ++start_p, ++i) + tmp_buffer[i] = *start_p; + tmp_buffer[i] = '\0'; + if(!strcmp(tmp_buffer, "all")) { + cl->all_input_cards_accepted = 1; + return; + } + if(!strcmp(tmp_buffer, "not all")) + cl->all_input_cards_accepted = 0; +} + +void decrypt_set_spectator_mode(struct client *cl, char *start_p) +{ + cl->sp_mode = (enum spectator_mode) decrypt_get_number(start_p, NULL); +} + +void decrypt_set_durak_position(struct client *cl, char *start_p) +{ + cl->durak_position = decrypt_get_number(start_p, NULL); +} |