back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorscratko <m@scratko.xyz>2024-08-18 01:33:08 +0300
committerscratko <m@scratko.xyz>2024-08-18 01:33:08 +0300
commit9970a2275a56d7835ba0c12a8586dc25cf7ec1cf (patch)
tree914b9353b2df1ba9dec5481fa3e7e6a8462dea5d /server
parent4b2fdc91d42a438193d15840e10851c3847fbe80 (diff)
downloaddurak-9970a2275a56d7835ba0c12a8586dc25cf7ec1cf.tar.gz
durak-9970a2275a56d7835ba0c12a8586dc25cf7ec1cf.tar.bz2
durak-9970a2275a56d7835ba0c12a8586dc25cf7ec1cf.zip
Global fixes v4.0
Removed unnecessary comments. Added resource cleanup for client and server. Changed queue display. Added player indicator. It's possible to quit the game while typing or waiting for a connection. Fixed a bug with determining the limit of card tossing.
Diffstat (limited to 'server')
-rw-r--r--server/card_queue.c15
-rw-r--r--server/card_queue.h1
-rw-r--r--server/card_stack.c12
-rw-r--r--server/card_stack.h1
-rw-r--r--server/server.c41
-rw-r--r--server/server.h4
-rw-r--r--server/server_data_processing.c38
-rw-r--r--server/server_game_process.c44
-rw-r--r--server/server_game_process.h5
9 files changed, 71 insertions, 90 deletions
diff --git a/server/card_queue.c b/server/card_queue.c
index 194ea0c..c5d59a8 100644
--- a/server/card_queue.c
+++ b/server/card_queue.c
@@ -45,6 +45,8 @@ const char* pop_card_queue(struct card_queue *cq)
cq->first = cq->first->next;
card = tmp->str;
free(tmp);
+ if(!cq->first)
+ cq->last = NULL;
return card;
}
@@ -59,3 +61,16 @@ int find_out_card_quantity_in_cq(const struct card_queue *cq)
}
return counter;
}
+
+void clear_queue(struct card_queue *cq)
+{
+ struct card_queue_item *tmp;
+
+ while(cq->first) {
+ tmp = cq->first;
+ cq->first = cq->first->next;
+ free(tmp);
+ }
+ cq->first = NULL;
+ cq->last = NULL;
+}
diff --git a/server/card_queue.h b/server/card_queue.h
index 778ece4..ebe2bb8 100644
--- a/server/card_queue.h
+++ b/server/card_queue.h
@@ -18,5 +18,6 @@ struct card_queue_item* get_next_card_from_queue(struct card_queue *cq,
int find_out_card_quantity_in_cq(const struct card_queue *cq);
int is_empty_queue(struct card_queue *cq);
const char* pop_card_queue(struct card_queue *cq);
+void clear_queue(struct card_queue *cq);
#endif
diff --git a/server/card_stack.c b/server/card_stack.c
index bce0133..077707e 100644
--- a/server/card_stack.c
+++ b/server/card_stack.c
@@ -86,3 +86,15 @@ int find_card_in_stack(player_cards involved_cards, const char *str)
}
return 0;
}
+
+void clear_stack(player_cards *deck)
+{
+ struct card_stack_item *tmp;
+
+ while(*deck) {
+ tmp = *deck;
+ free(tmp);
+ *deck = (*deck)->next;
+ }
+ *deck = NULL;
+}
diff --git a/server/card_stack.h b/server/card_stack.h
index 8581eec..9b204ab 100644
--- a/server/card_stack.h
+++ b/server/card_stack.h
@@ -18,5 +18,6 @@ struct card_stack_item* get_next_card_from_stack(player_cards deck,
player_cards prev);
int find_card_in_stack(player_cards involved_cards, const char *str);
void pop_stack(player_cards *deck);
+void clear_stack(player_cards *deck);
#endif
diff --git a/server/server.c b/server/server.c
index f842a70..8ebc937 100644
--- a/server/server.c
+++ b/server/server.c
@@ -14,7 +14,6 @@
#include "card_stack.h"
#include "card_queue.h"
-
static void init_session(struct session *new_session, struct sockaddr_in *from,
int fd, int player_position)
{
@@ -62,12 +61,24 @@ static void close_connection(struct server *serv)
if(!serv->sess_arr[i])
continue;
serv->sess_arr[i]->state = disconnect;
- send_disconnect_status(serv->sess_arr[i]->fd);
- close(serv->sess_arr[i]->fd);
+ if(serv->sess_arr[i]->fd != -1) {
+ send_disconnect_status(serv->sess_arr[i]->fd);
+ close(serv->sess_arr[i]->fd);
+ }
+ clear_stack(&serv->sess_arr[i]->deck);
free(serv->sess_arr[i]);
serv->sess_arr[i] = NULL;
}
serv->connected_players_counter = 0;
+ free(serv->cc.number_arr);
+ free(serv->shuffled_deck);
+ free(serv->turn_queue);
+ free(serv->gi);
+ serv->cc.number_arr = NULL;
+ serv->shuffled_deck = NULL;
+ serv->turn_queue = NULL;
+ serv->gi = NULL;
+ clear_queue(&serv->cq);
}
static int check_playable_player_number(struct server *serv)
@@ -93,10 +104,6 @@ static void init_new_game(struct server *serv)
serv->trump_card =
extract_trump_card(serv->shuffled_deck, &serv->shuffled_deck_size);
serv->trump_suit = serv->trump_card + strlen(serv->trump_card) - 1;
-#if 0
- serv->trump_suit = find_trump_suit(serv->shuffled_deck,
- &serv->shuffled_deck_size);
-#endif
serv->turn_queue = malloc(sizeof(struct session*) *
serv->connected_players_counter);
serv->turn_queue_size = serv->connected_players_counter;
@@ -354,7 +361,7 @@ static void define_phase_after_tossing(struct server *serv)
}
else {
put_all_cards_from_queue_to_table(&serv->cot, &serv->cq);
- if(is_receiving_cards_limit(&serv->cot, serv->turn_queue[1]->deck,
+ if(!is_receiving_cards_limit(&serv->cot, serv->turn_queue[1]->deck,
&serv->cq) &&
check_someone_can_toss_card(serv->turn_queue,
serv->turn_queue_size,
@@ -383,7 +390,7 @@ static void define_phase_after_tossing(struct server *serv)
} else {
serv->turn_queue[1]->defense_lost = 1;
put_all_cards_from_queue_to_table(&serv->cot, &serv->cq);
- if(is_receiving_cards_limit(&serv->cot,
+ if(!is_receiving_cards_limit(&serv->cot,
serv->turn_queue[1]->deck,
&serv->cq) &&
check_someone_can_toss_card(serv->turn_queue,
@@ -692,7 +699,7 @@ static int check_server_state_change_conditions(struct server *serv)
static void make_data_transfer(struct server *serv, fd_set *readfds,
fd_set *writefds)
{
- int i, result;
+ int i, result, close_connection_status = 0;
for(i = 0; i < serv->max_sess_arr_size; ++i) {
if(!serv->sess_arr[i])
continue;
@@ -749,12 +756,12 @@ static void make_data_transfer(struct server *serv, fd_set *readfds,
get_cards_from_tossing_player(serv->sess_arr[i],
serv->turn_queue[1]->deck,
&serv->cot, &serv->cq);
- if(result == 1 || result == 2)
+ if(result == answer_got || result == anwer_got_with_limit)
serv->sess_arr[i]->tm = answer_got;
- if(result == 2)
+ if(result == anwer_got_with_limit)
serv->tossing_limit = 1;
- else if(result == 3)
+ else if(result == cancel)
serv->sess_arr[i]->tm = cancel;
}
break;
@@ -765,6 +772,12 @@ static void make_data_transfer(struct server *serv, fd_set *readfds,
print_game_result(serv->sess_arr[i], serv->durak_position);
break;
}
+ if(!result) {
+ close(i);
+ serv->sess_arr[i]->fd = -1;
+ close_connection_status = 1;
+ result = 1; /* next can be reading that's not ready */
+ }
}
if(serv->state == start_game || serv->state == table ||
serv->state == end_game)
@@ -772,7 +785,7 @@ static void make_data_transfer(struct server *serv, fd_set *readfds,
if(check_server_state_change_conditions(serv))
serv->change_server_state = 1;
/* connection is closed */
- if(!result) {
+ if(close_connection_status) {
close_connection(serv);
serv->change_server_state = 0;
serv->state = no_players;
diff --git a/server/server.h b/server/server.h
index 5f7ffcc..552f899 100644
--- a/server/server.h
+++ b/server/server.h
@@ -47,10 +47,11 @@ enum client_game_states {
};
enum tossing_mode {
+ none,
cancel,
answer_wait,
answer_got,
- none
+ anwer_got_with_limit,
};
enum spectator_mode {
@@ -94,7 +95,6 @@ struct server {
struct session **sess_arr;
int max_sess_arr_size;
int connected_players_counter;
- /* TODO: make static allocation memory */
const char **shuffled_deck;
int shuffled_deck_size;
const char *trump_suit;
diff --git a/server/server_data_processing.c b/server/server_data_processing.c
index df95b7f..99ac579 100644
--- a/server/server_data_processing.c
+++ b/server/server_data_processing.c
@@ -177,6 +177,12 @@ int print_game_part(const struct session *client,
/* let the client print card tips and wait for input */
case attack:
case defense:
+ /* other clents will be waiting particular player */
+ case attack_expectation:
+ free_pos += sprintf(output_buffer + free_pos, "%u:",
+ gi->position_whose_turn);
+ *(output_buffer + free_pos-1) = '\n';
+ break;
/*
* who haven't got tossing card (including defender) must wait
* when other players make moves
@@ -186,12 +192,6 @@ int print_game_part(const struct session *client,
/* instead of last ':' */
*(output_buffer + free_pos-1) = '\n';
break;
- /* other clents will be waiting particular player */
- case attack_expectation:
- free_pos += sprintf(output_buffer + free_pos, "%u:",
- gi->position_whose_turn);
- *(output_buffer + free_pos-1) = '\n';
- break;
case defense_expectation:
case spectator:
/* copying whose turn */
@@ -219,18 +219,6 @@ int print_game_part(const struct session *client,
}
return write_to_client(client->fd, free_pos);
}
-#if 0
-static int is_receiving_cards_limit(int total_defense_cards, int total_attack_cards)
-{
- if(total_defense_cards >= start_deck_size &&
- total_attack_cards == start_deck_size) {
- return 1;
- } else if(total_defense_cards == total_attack_cards)
- return 1;
- return 0;
-}
-#endif
-
/*
* example: 7v\2#\A^\'\n'
@@ -313,9 +301,10 @@ int get_card_from_defender(struct session *client, struct cards_on_table *cot,
/*
* 0 - buffer overflow or client closed connection
- * 1 - some number of cards added to card queue
+ * 1 - some number of cards added to card queue (answer_got)
* 2 - card toss limit was reached when accepting cards from this player
- * 3 - player refused to toss the card
+ * (anwer_got_with_limit)
+ * 3 - player refused to toss the card (cancel)
*
* example: 10#\A^\7v\'\n'
* or: '\n'
@@ -341,7 +330,7 @@ int get_cards_from_tossing_player(struct session *client,
/* cancellation */
if(client->buffer[0] == '\n')
- return 3;
+ return cancel;
for(i = 0, j = 0; i < read_result; ++i) {
if(client->buffer[i] == '\\') {
@@ -351,7 +340,7 @@ int get_cards_from_tossing_player(struct session *client,
data_length =
sprintf(output_buffer, "%d\n", tossing_limit_status);
write_to_client(client->fd, data_length);
- return 3;
+ return cancel;
}
given_card[j] = '\0';
j = 0;
@@ -371,14 +360,13 @@ int get_cards_from_tossing_player(struct session *client,
++j;
}
- /* 7 -- state for result of received cards */
data_length = sprintf(output_buffer, "%d:%s\n", card_acceptance_status,
all_cards_received_status ? "all" : "not all");
write_to_client(client->fd, data_length);
if(all_cards_received_status)
- return 1;
+ return answer_got;
else
- return 2;
+ return anwer_got_with_limit;
}
void print_game_result(const struct session *client, int durak_position)
diff --git a/server/server_game_process.c b/server/server_game_process.c
index d6fc4be..369f3d6 100644
--- a/server/server_game_process.c
+++ b/server/server_game_process.c
@@ -115,19 +115,6 @@ const char* extract_trump_card(const char **shuffled_deck, int *cards_left)
}
return trump_card;
}
-#if 0
-const char* find_trump_suit(const char **shuffled_deck, int *cards_left)
-{
- const char *trump_suit = NULL;
- trump_suit = shuffled_deck[0] ? shuffled_deck[0] : NULL;
- if(trump_suit) {
- shuffled_deck[0] = NULL;
- trump_suit += strlen(trump_suit) - 1;
- --*cards_left;
- }
- return trump_suit;
-}
-#endif
int convert_rank_to_int(const char *card)
{
@@ -207,16 +194,6 @@ void update_card_quantity_arr(struct session **turn_queue, struct card_count cc)
cc.number_arr[key] =
find_out_card_quantity_in_deck(turn_queue[i]->deck);
}
-
-
-#if 0
- for(i = 0; i < serv->max_sess_arr_size; ++i)
- if(serv->sess_arr[i]) {
- player_position_idx = serv->sess_arr[i]->player_position - 1;
- serv->cc.number_arr[player_position_idx] =
- find_out_card_quantity_in_deck(serv->sess_arr[i]->deck);
- }
-#endif
}
struct game_info* get_new_game_info(int players_number, int *card_quantity_arr,
@@ -517,14 +494,7 @@ void put_defender_card_on_table(struct cards_on_table *cot, const char *card)
void move_all_cards_to_defender(struct cards_on_table *cot, player_cards *deck)
{
int i;
-#if 0
- const char *card;
- while(!is_empty_queue(cq)) {
- card = pop_card_queue(cq);
- push_stack(deck, card);
- }
-#endif
for(i = cot->card_arr_idx; i >= 0; --i) {
if(cot->card_arr[i][0] == '-' || cot->card_arr[i][0] == '\\')
continue;
@@ -617,17 +587,3 @@ int check_everyone_can_replanish_deck(int shuffled_cards_left,
}
return shuffled_cards_left >= 0;
}
-#if 0
-void find_out_who_dropped_all_cards(struct session **turn_queue,
- int turn_queue_size, int *number_arr)
-{
- int i, key, player_deck_size;
-
- for(i = 0; i < turn_queue_size; ++i) {
- key = turn_queue[i]->player_position - 1;
- player_deck_size = number_arr[key];
- if(!player_deck_size)
- turn_queue[i]->state = discarded_cards;
- }
-}
-#endif
diff --git a/server/server_game_process.h b/server/server_game_process.h
index f384fdc..0259751 100644
--- a/server/server_game_process.h
+++ b/server/server_game_process.h
@@ -29,9 +29,6 @@ void deal_one_card_in_turn(const char **shuffled_deck, int *shuffled_cards_left,
struct session **turn_queue, int turn_queue_size,
struct card_count cc);
const char* extract_trump_card(const char **shuffled_deck, int *cards_left);
-#if 0
-const char* find_trump_suit(const char **shuffled_deck, int *cards_left);
-#endif
int find_lowest_trump(player_cards deck, const char *trump);
void shift_turn_queue_by_one(struct session **turn_queue, int turn_queue_size);
void move_turn_queue_two_players_ahead(struct session **turn_queue,
@@ -84,7 +81,5 @@ void clear_defense_lost_status(struct session *defender);
int check_everyone_can_replanish_deck(int shuffled_cards_left,
struct session **turn_queue,
int turn_queue_size, int *number_arr);
-void find_out_who_dropped_all_cards(struct session **turn_queue,
- int turn_queue_size, int *number_arr);
#endif