summaryrefslogtreecommitdiff
path: root/src/server_game_states.c
blob: 746b4be59f601a6aac3e120f236eaf156fe6c2df (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
#include "game_states.h"
#include <stdlib.h>
#include <string.h>
#include "data_store.h"

game_state_t state_server_deal_hand_cards(const socket_list_t *client_socks, const uint8_t round, main_stack_t *m)
{
	data_store_t *d = data_store();

	if(round == 1)
	{
		// Draw cards for initial stacks and send them to clients
		d->table_stacks.stacks[0].cards[0] = main_stack_remove_card(m);
		d->table_stacks.stacks[1].cards[0] = main_stack_remove_card(m);
		d->table_stacks.stacks[2].cards[0] = main_stack_remove_card(m);
		d->table_stacks.stacks[3].cards[0] = main_stack_remove_card(m);

		for(int i = 0; i < d->player_list.count; i++)
		{
			net_send(client_socks->sockets[i], msg_type_initial_stacks, NULL);
		}
	}

	int num_dealcards = main_stack_size(m) / d->player_list.count;
	if(num_dealcards > 10)
		num_dealcards = 10;

	// Deal hand cards to clients
	for(int i = 0; i < d->player_list.count; i++)
	{
		hand_t h;
		memset(h.cards, 0, MAX_HAND_CARDS);

		for(int j = 0; j < num_dealcards; j++)
			h.cards[j] = main_stack_remove_card(m);

		hand_sort(&h);

		net_send(client_socks->sockets[i], msg_type_deal_hand, &h);
	}

	return STATE_SERVER_WAIT_FOR_OPEN_CARDS;
}

game_state_t state_server_wait_for_open_cards(const socket_list_t *client_socks)
{
	data_store_t *d = data_store();

	// Receive open cards from clients
	for(int i = 0; i < d->player_list.count; i++)
	{
		player_list_entry_t *ple = get_player_list_entry_by_player_id(&d->player_list, client_socks->player_ids[i]);
		assert(ple != NULL);
		net_recv(client_socks->sockets[i], msg_type_selected_card);
		ple->open_card = d->selected_card;
	}

	for(int i = 0; i < d->player_list.count; i++)
		net_send(client_socks->sockets[i], msg_type_selected_card_all, NULL);

	//return STATE_SERVER_PLAY_CARDS;
	return STATE_SERVER_WAIT_FOR_OPEN_CARDS; // just for testing
}

game_state_t state_server_play_cards(const socket_list_t *client_socks, const uint8_t round)
{
#if 0
	data_store_t *d = data_store();

	foreach(open_card)
	{
		play_lowest_open_card
		{
			determine_stack_for_open_card
			{
				if (stack_has_to_be_picked) {
					receive_stack_from_client();
					send_received_stack_to_other_clients();
					clear_stack(stack_id);
				}
			}
			place_card()
			{
				if(count_stack_cards == 6)
					clear_stack(stack_id);
			}
		}
	}

	if (clients_have_hand_cards)
	{
		return STATE_SERVER_WAIT_FOR_OPEN_CARDS;
	}
	else
	{
		if (main_stack_has_enough_cards_for_clients)
		{
			send_action_to_client(DEAL_HAND_CARDS);
			round++;
			return STATE_SERVER_DEAL_HAND_CARDS;
		}
		else
		{
			send_action_to_client(GAME_FINISHED);
			return STATE_SERVER_GAME_FINISHED;
		}
	}
#endif
	return 1337;
}