back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/linux_client/data_decryption.c
blob: 1054c453caf8e35d3884d15001920c947ea56807 (plain)
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
#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);
}