back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
Diffstat (limited to 'client')
-rw-r--r--client/card_stack.c39
-rw-r--r--client/card_stack.h2
-rw-r--r--client/client.c34
-rw-r--r--client/client.h1
-rw-r--r--client/data_decryption.c12
-rw-r--r--client/printing_game_frames.c18
-rw-r--r--client/verification_client_input.c67
7 files changed, 135 insertions, 38 deletions
diff --git a/client/card_stack.c b/client/card_stack.c
index 5d0700b..d9dc273 100644
--- a/client/card_stack.c
+++ b/client/card_stack.c
@@ -81,11 +81,32 @@ void add_hint_letters_stack(player_cards deck)
}
}
+static int check_no_attackers_cards_marked(player_cards deck)
+{
+ while(deck) {
+ if(deck->is_usable)
+ return 0;
+ deck = deck->next;
+ }
+ return 1;
+}
+
+static void mark_all_card_stack(player_cards deck)
+{
+ player_cards tmp_deck = deck;
+
+ while(tmp_deck) {
+ tmp_deck->is_usable = 1;
+ tmp_deck = tmp_deck->next;
+ }
+}
+
void mark_card_for_attackers_stack(player_cards deck)
{
char *card = NULL, *found_card;
struct card_stack_item *search_deck = NULL;
int target_rank, found_rank;
+ player_cards tmp_begin_deck = deck;
while(deck) {
/* is the card already marked? */
@@ -103,12 +124,16 @@ void mark_card_for_attackers_stack(player_cards deck)
}
found_card = search_deck->str;
found_rank = convert_rank_to_int(found_card);
- if(found_rank == target_rank)
+ if(found_rank == target_rank) {
+ deck->is_usable = 1;
search_deck->is_usable = 1;
+ }
search_deck = search_deck->next;
}
deck = deck->next;
}
+ if(check_no_attackers_cards_marked(tmp_begin_deck))
+ mark_all_card_stack(tmp_begin_deck);
}
#if 0
static int is_card_beaten(const char *attack_card, const char *defend_card,
@@ -142,8 +167,8 @@ void mark_card_for_defenders_stack(player_cards deck,
while(deck) {
for(i = 0; i <= cot->card_arr_idx; ++i)
- if((i == 0 || !(i % 3)) && is_card_beaten(cot->card_arr[i],
- deck->str, trump_suit))
+ if(((i == 0 || !(i % 3)) && cot->card_arr[i+2][0] == '-') &&
+ is_card_beaten(cot->card_arr[i], deck->str, trump_suit))
{
deck->is_usable = 1;
break;
@@ -179,15 +204,15 @@ void mark_card_for_tossing_stack(player_cards deck, struct cards_on_table *cot)
deck = deck->next;
}
}
-
-int card_search_by_letter(player_cards deck, int letter)
+/* rename: and_unmarked */
+char* card_search_by_letter(player_cards deck, int letter)
{
while(deck) {
if(deck->tip == letter && deck->is_usable) {
deck->is_usable = 0;
- return 1;
+ return deck->str;
}
deck = deck->next;
}
- return 0;
+ return NULL;
}
diff --git a/client/card_stack.h b/client/card_stack.h
index 7cb5798..ba973f4 100644
--- a/client/card_stack.h
+++ b/client/card_stack.h
@@ -24,6 +24,6 @@ void mark_card_for_defenders_stack(player_cards deck,
struct cards_on_table *cot,
char* trump_suit);
void mark_card_for_tossing_stack(player_cards deck, struct cards_on_table *cot);
-int card_search_by_letter(player_cards deck, int letter);
+char* card_search_by_letter(player_cards deck, int letter);
#endif
diff --git a/client/client.c b/client/client.c
index d932a51..4abcf7d 100644
--- a/client/client.c
+++ b/client/client.c
@@ -4,6 +4,7 @@
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
+#include <stdio.h> /* test */
#include "client.h"
#include "data_decryption.h"
@@ -15,15 +16,23 @@ static const int port = 8082;
static void get_data_from_server(struct client *cl, fd_set *readfds)
{
+ int i;
int update_info = 0;
/* pointer to delimeter (:) that is behind extract data */
char *end_p;
if(FD_ISSET(cl->fd, readfds)) {
- read(cl->fd, cl->buffer, max_buffer_size);
+ if(!cl->data_left_in_buffer) {
+ cl->data_left_in_buffer = read(cl->fd, cl->buffer, max_buffer_size);
+ printf("%d\n", cl->data_left_in_buffer);
+ for(i = 0; i < cl->data_left_in_buffer; ++i)
+ putchar(cl->buffer[i]);
+ putchar('\n');
+ }
decrypt_set_state(cl, &end_p);
update_info = 1;
cl->display_new_frame = 1;
+ cl->pending_server_response = 0;
}
if(update_info) {
switch(cl->state) {
@@ -50,13 +59,20 @@ static void get_data_from_server(struct client *cl, fd_set *readfds)
decrypt_set_card_queue(cl, end_p+1);
break;
case card_acceptance_status:
- decrypt_set_card_acceptance_status(cl, cl->buffer);
+ decrypt_set_card_acceptance_status(cl, end_p+1);
break;
/* no data to extract */
case tossing_limit_status:
default:
{}
}
+ for(i = 0; cl->buffer[i] != '\n'; ++i)
+ {}
+ if((cl->buffer+i - cl->buffer + 1) != cl->data_left_in_buffer) {
+ cl->data_left_in_buffer -= i + 1;
+ memmove(cl->buffer, cl->buffer+i+1, cl->data_left_in_buffer);
+ } else
+ cl->data_left_in_buffer = 0;
}
}
@@ -66,7 +82,7 @@ static void prepare_tips_for_client(struct client *cl)
if(!is_empty_stack(cl->deck))
add_hint_letters_stack(cl->deck);
-
+/* TODO: clear marked cards ??? */
switch(cl->state) {
case attack:
mark_card_for_attackers_stack(cl->deck);
@@ -126,7 +142,7 @@ static void change_client_frame(struct client *cl)
static void send_data_to_server(struct client *cl, fd_set *readfds)
{
- int input_result;
+ int input_result = 0;
if(FD_ISSET(0, readfds)) { /* 0 - stdin */
read(0, cl->buffer, max_buffer_size);
@@ -181,9 +197,11 @@ int main_loop(struct client *cl)
if(check_tracking_client_input(cl->state, cl->pending_server_response))
FD_SET(0, &readfds);
- select_result = select(cl->fd + 1, &readfds, NULL, NULL, NULL);
- if(select_result == -1)
- return 2;
+ if(!cl->data_left_in_buffer) {
+ select_result = select(cl->fd + 1, &readfds, NULL, NULL, NULL);
+ if(select_result == -1)
+ return 2;
+ }
get_data_from_server(cl, &readfds);
prepare_tips_for_client(cl);
if(cl->display_new_frame)
@@ -210,6 +228,7 @@ static void init_client(struct client *cl)
cl->position_whose_turn = 0;
cl->display_new_frame = 0;
cl->all_input_cards_accepted = 0;
+ cl->data_left_in_buffer = 0;
}
/*
@@ -222,6 +241,7 @@ static int connect_to_server(struct client *cl)
struct sockaddr_in addr;
cl->fd = socket(AF_INET, SOCK_STREAM, 0);
+
if(cl->fd == -1)
return 0;
addr.sin_family = AF_INET;
diff --git a/client/client.h b/client/client.h
index f73cdce..9bdd8cc 100644
--- a/client/client.h
+++ b/client/client.h
@@ -66,6 +66,7 @@ struct client {
int position_whose_turn;
int display_new_frame;
int all_input_cards_accepted;
+ int data_left_in_buffer;
};
#endif
diff --git a/client/data_decryption.c b/client/data_decryption.c
index 2d10561..b445454 100644
--- a/client/data_decryption.c
+++ b/client/data_decryption.c
@@ -4,6 +4,7 @@
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
void decrypt_set_state(struct client *cl, char **end_p)
{
@@ -24,11 +25,9 @@ static void decrypt_set_trump_card(struct client *cl, char *start_p,
{
int i;
- for(i = 0; *start_p != '\0'; ++start_p, ++i)
+ for(i = 0; *start_p != ':'; ++start_p, ++i)
cl->trump_card[i] = *start_p;
cl->trump_card[i] = '\0';
- /* move to : */
- ++start_p;
*end_p = start_p;
}
@@ -65,7 +64,7 @@ void decrypt_set_base_info(struct client *cl, char *start_p, char **end_p)
i = 0, j = 0;
for(start_p = *end_p + 1; *start_p != ':'; ++start_p) {
/* empty table */
- if(*start_p == '0') {
+ if(*start_p == '=') {
/* to delimiter : */
++start_p;
i = -1;
@@ -94,7 +93,7 @@ void decrypt_set_base_info(struct client *cl, char *start_p, char **end_p)
for(++start_p, i = 0; *start_p != ':' && *start_p != '\n'; ++start_p) {
/* empty deck */
- if(*start_p == '0') {
+ if(*start_p == '=') {
i = -1;
/* to delimiter ':' or '\n' */
++start_p;
@@ -107,6 +106,7 @@ void decrypt_set_base_info(struct client *cl, char *start_p, char **end_p)
strcpy(card, tmp_card);
push_stack(&cl->deck, card);
i = 0;
+ continue;
}
tmp_card[i] = *start_p;
++i;
@@ -132,7 +132,7 @@ void decrypt_set_card_queue(struct client *cl, char *start_p)
for(i = 0, j = 0; *start_p != '\n'; ++start_p) {
/* empty queue */
- if(*start_p == '0') {
+ if(*start_p == '=') {
i = -1;
break;
}
diff --git a/client/printing_game_frames.c b/client/printing_game_frames.c
index 38f1a78..f75cec5 100644
--- a/client/printing_game_frames.c
+++ b/client/printing_game_frames.c
@@ -5,17 +5,17 @@
void pgf_first_player()
{
- printf("======================================="
- " You're the first player"
- "Waiting for other players to connect..."
+ printf("=======================================\n"
+ " You're the first player\n"
+ "Waiting for other players to connect...\n"
"=======================================\n");
}
void pgf_confirmation_waiting(int total_players)
{
- printf("======================================="
- " To start game press <Enter> key "
- "Connected players: %u/8 "
+ printf("=======================================\n"
+ " To start game press <Enter> key \n"
+ "Connected players: %u/8 \n"
"=======================================\n", total_players);
}
@@ -24,7 +24,7 @@ void pgf_table(struct client *cl)
struct card_stack_item *deck = NULL;
int i;
- for(i = 0; i < cl->cc.number_arr_idx; ++i)
+ for(i = 0; i <= cl->cc.number_arr_idx; ++i)
/* printing who will move */
if(cl->state == attack_expectation || cl->state == defense_expectation)
printf("<%c%u > ", cl->position_whose_turn-1 == i ? '*' : ' ',
@@ -41,7 +41,8 @@ void pgf_table(struct client *cl)
if(cl->state == defense_expectation)
for(i = 0; i <= cl->cq.card_arr_idx; ++i)
printf(" %s \\ -\n", cl->cq.card_arr[i]);
- printf("\n\n");
+ if(cl->cot.card_arr_idx != -1)
+ printf("\n\n");
/* ================ player's deck ================ */
deck = cl->deck;
i = 0;
@@ -70,6 +71,7 @@ void pgf_suggestions(struct client *cl)
deck = deck->next;
}
printf(" > ");
+ fflush(stdout);
}
void pgf_select_idle_mode_message(enum client_states state)
diff --git a/client/verification_client_input.c b/client/verification_client_input.c
index 2134f30..1530387 100644
--- a/client/verification_client_input.c
+++ b/client/verification_client_input.c
@@ -1,7 +1,15 @@
#include "verification_client_input.h"
+#include "card_handling.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
@@ -18,29 +26,70 @@ int vci_confirmation_waiting(int fd, char *buffer)
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 i;
+ int i, free_pos = 0;
+ char * card = NULL;
- if(state == tossing && buffer[0] == '\n') /* cancel card tossing */
+ if(state == tossing && buffer[0] == '\n') { /* cancel card tossing */
+ write(fd, buffer, 1);
return 1;
+ }
for(i = 0; buffer[i] != '\n'; ++i)
- if(!card_search_by_letter(deck, buffer[i])) {
+ if((card = card_search_by_letter(deck, buffer[i])) != NULL) {
+#if 0
+ if(!rank)
+ rank = convert_rank_to_int(card);
+ if(rank == convert_rank_to_int(card)) {
+#endif
+ strncpy(output_buffer + free_pos, card, strlen(card));
+ free_pos += strlen(card);
+ output_buffer[free_pos] = '\\';
+ ++free_pos;
+#if 0
+ } else {
+ printf("incorrect input\n");
+ return 0;
+ }
+#endif
+ } else {
printf("incorrect input\n");
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)
{
- if(buffer[0] == '\n') /* the player does not want to keep the defense */
+ int last_idx;
+ char *card = NULL;
+
+ if(buffer[0] == '\n') { /* the player does not want to keep the defense */
+ write(fd, buffer, 1);
return 1;
- if(!card_search_by_letter(deck, buffer[0]))
- return 0;
- if(buffer[1] != '\n')
+ }
+ if(buffer[1] != '\n') {
+ printf("incorrect input\n");
return 0;
- return 1;
+ }
+ if((card = card_search_by_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 */
+ return 0;
}