back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/client/data_decryption.c
diff options
context:
space:
mode:
Diffstat (limited to 'client/data_decryption.c')
-rw-r--r--client/data_decryption.c169
1 files changed, 169 insertions, 0 deletions
diff --git a/client/data_decryption.c b/client/data_decryption.c
new file mode 100644
index 0000000..2d10561
--- /dev/null
+++ b/client/data_decryption.c
@@ -0,0 +1,169 @@
+#include "data_decryption.h"
+#include "client.h"
+#include "card_stack.h"
+
+#include <stdlib.h>
+#include <string.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 != '\0'; ++start_p, ++i)
+ cl->trump_card[i] = *start_p;
+ cl->trump_card[i] = '\0';
+ /* move to : */
+ ++start_p;
+ *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 == '0') {
+ /* 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 == '0') {
+ 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;
+ }
+ 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)
+{
+ int i, j;
+
+ for(i = 0, j = 0; *start_p != '\n'; ++start_p) {
+ /* empty queue */
+ if(*start_p == '0') {
+ i = -1;
+ 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;
+}
+
+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;
+}