summaryrefslogtreecommitdiff
path: root/src/server_game_states.c
blob: 1b7a3bafa3a40d27ae065ffba5b1eab1828fc975 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*****************************************************************************
 *                          ___ __  __ ___ _ __                              *
 *                         / _ \\ \/ // _ \ '_ \                             *
 *                        | (_) |>  <|  __/ | | |                            *
 *                         \___//_/\_\\___|_| |_|                            *
 *                                                                           *
 *                             The card game                                 *
 *                                                                           *
 *  Copyright (C) 2011, Reiner Herrmann <reiner@reiner-h.de>                 *
 *                      Mario Kilies <MarioKilies@GMX.net>                   *
 *                                                                           *
 *  This program is free software; you can redistribute it and/or modify     *
 *  it under the terms of the GNU General Public License as published by     *
 *  the Free Software Foundation; either version 3 of the License, or        *
 *  (at your option) any later version.                                      *
 *                                                                           *
 *  This program is distributed in the hope that it will be useful,          *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             *
 *  GNU General Public License for more details.                             *
 *                                                                           *
 *  You should have received a copy of the GNU General Public License        *
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.     *
 *                                                                           *
 *****************************************************************************/

#include "game_states.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "data_store.h"

/**
 * Game state handler. If it is the first round, the server takes cards from the main stack and places them onto the table, as table stacks. The server then sends the table stacks to all players. After this, the hands for the players will be drawn from the main stack and sent to the players. On returning from the function, a state transition occurs.
 * @param[in] client_socks The client sockets used for communication
 * @param[in] round The current round of the game
 * @param[in,out] m The used main stack
 * @return STATE_SERVER_WAIT_FOR_OPEN_CARDS The next state after dealing hand cards
*/
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);
	}

	// set own pseudo hand to keep track of number of available hand cards
	memset(&d->hand.cards, 0, MAX_HAND_CARDS);
	for(int i=0; i<num_dealcards; i++)
		d->hand.cards[i] = 1;

	return STATE_SERVER_WAIT_FOR_OPEN_CARDS;
}

/**
 * Game state handler. In this state, the server waits for all clients to choose their open card. After receiving all open cards, the list of all open cards will be propagated to all players. On returning from the function, a state transition occurs.
 * @param[in] client_socks The client sockets used for communication
 * @return STATE_SERVER_PLAY_CARDS The next state after waiting for all 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);

	// update local hand for tracking number of cards
	for(int i=0; i<MAX_HAND_CARDS; i++)
	{
		if(d->hand.cards[i] == 1)
		{
			d->hand.cards[i] = 0;
			break;
		}
	}
	player_list_sort_by_open_card(&d->player_list, d->player_list.count); // sort in ascending order

	return STATE_SERVER_PLAY_CARDS;
}

/**
 * Game state handler. In this state, the server tries to play all open cards. If a player's card is smaller than all the cards on the stacks, the player has to choose a stack. Receive the player's chosen stack, replace it with his open card and increment the player's score. If the player's card fits on a stack, but the stack is already full, then replace the stack with the card, and add the stack points to the player's points.
 * If the players still have hand cards, then wait for chosen open cards from players. If the players do not have anymore hand cards and the main stack has enough cards to deal every player at least one card, then deal new hand cards. Otherwise the game has finished.
 * @param[in] client_socks The client sockets used for communication
 * @param[in,out] m The used main stack
 * @return STATE_SERVER_WAIT_FOR_OPEN_CARDS The next state, if the players still have hand cards
 * @return STATE_SERVER_DEAL_HAND_CARDS The next state, if the main stack has at least one card for each player left. The server will then deal those cards
 * @return STATE_SERVER_GAME_FINISHED The next state, if the main stack is empty
*/
game_state_t state_server_play_cards(const socket_list_t *client_socks, const main_stack_t *m)
{
	data_store_t *d = data_store();

	for(int i=0; i<d->player_list.count; i++)
	{
		card c = d->player_list.players[i].open_card;
		uint8_t stack_idx = get_stack_idx_for_card(&d->table_stacks, c);

		if(stack_idx >= NUM_TABLE_STACKS) // card does not fit on any stack
		{
			int cur_sock = socket_for_player_id(client_socks, d->player_list.players[i].player_id);
			net_recv(cur_sock, msg_type_selected_stack_c);
			for(int j=0; j<client_socks->count; j++) // send received stack to all clients (including the one who sent it)
				net_send(client_socks->sockets[j], msg_type_selected_stack_s, NULL);

			d->player_list.players[i].score += card_stack_get_points(&d->table_stacks.stacks[d->stack_index]);
			card_stack_replace(&d->table_stacks.stacks[d->stack_index], c);
		}
		else // card fits on a stack -> place it
		{
			card_stack_t* cs = &d->table_stacks.stacks[stack_idx];
			if(cs->cards[MAX_CARD_STACK_SIZE-1] != 0) // stack is full
			{
				d->player_list.players[i].score += card_stack_get_points(cs);
				card_stack_replace(cs, c);
			}
			else
			{
				// put open card on top of stack
				for(int j=0; j<MAX_CARD_STACK_SIZE; j++)
				{
					if(cs->cards[j] != 0)
						continue;
					cs->cards[j] = c;
					break;
				}
			}
		}
	}

	if(hand_count_cards(&d->hand) > 0) // still cards in hand?
	{
		return STATE_SERVER_WAIT_FOR_OPEN_CARDS;
	}
	else
	{
		if(main_stack_size(m) > d->player_list.count) // no more cards -> deal new cards
		{
			d->game_finished = false;
			for(int i=0; i<client_socks->count; i++)
				net_send(client_socks->sockets[i], msg_type_next_action, NULL);

			return STATE_SERVER_DEAL_HAND_CARDS;
		}
		else // empty main stack -> end game
		{
			d->game_finished = true;
			for(int i=0; i<client_socks->count; i++)
				net_send(client_socks->sockets[i], msg_type_next_action, NULL);
			return STATE_SERVER_GAME_FINISHED;
		}
	}

	assert(false);
	return 1337;
}

/**
 * Game state handler. Will be called if the game has finished. As the game has finished, no state transition takes place before returning from this function.
*/
void state_server_game_finished(void)
{
}