summaryrefslogtreecommitdiff
path: root/src/client_game_states.c
blob: 2682d674b97b42629cf1993f5083c2f359b2ee66 (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
#include "game_states.h"
#include "net/comm.h"
#include "ui.h"
#include "data_store.h"

game_state_t state_client_wait_for_hand_cards(const int sock, const uint8_t round)
{
	data_store_t *d = data_store();

	if(round == 1)
	{
		// Receive and display table stacks
		net_recv(sock, msg_type_initial_stacks);
		ui_display_wnd_table_cards(&d->table_stacks, false, 0);
		ui_display_wnd_stack_points(&d->table_stacks, false, 0);
	}

	// Wait for hand cards from server and display them
	net_recv(sock, msg_type_deal_hand);
	ui_display_wnd_hand_cards(&d->hand, false, 0);

	return STATE_CLIENT_SELECT_OPEN_CARD;
}

game_state_t state_client_select_open_card(const int sock)
{
	data_store_t *d = data_store();
	uint8_t open_card_idx;

	// Select open card
	open_card_idx = ui_choose_card(&d->hand);
	d->selected_card = d->hand.cards[open_card_idx];

	// Send open card to server
	net_send(sock, msg_type_selected_card, NULL);

	// Remove picked open card from hand
	hand_remove_card(&d->hand, open_card_idx);
	ui_display_wnd_hand_cards(&d->hand, false, 0);

	return STATE_CLIENT_WAIT_FOR_OPEN_CARDS;
}

game_state_t state_client_wait_for_open_cards(const int sock)
{
	data_store_t *d = data_store();

	net_recv(sock, msg_type_selected_card_all);
	player_list_sort_by_open_card(&d->player_list, d->player_list.count); // sort in ascending order

	ui_display_wnd_current_state(&d->player_list, d->player_list.count, true, 0, 0); // TODO fix parameters

	//return STATE_CLIENT_PLAY_CARDS;
	return STATE_CLIENT_SELECT_OPEN_CARD;; // just for testing
}

game_state_t state_client_play_cards(const int sock, const uint8_t round)
{
	data_store_t *ds = data_store();

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

		if(stack_idx >= NUM_TABLE_STACKS) // card does not fit on any stack
		{
			uint32_t stack_points;

			if(ds->player_list.players[i].player_id == ds->own_player_id) // our turn to select stack
			{
				ds->stack_index = ui_choose_stack(&ds->table_stacks);
				net_send(sock, msg_type_selected_stack_c, NULL);
			}

			net_recv(sock, msg_type_selected_stack_s);
			stack_points = card_stack_get_points(&ds->table_stacks.stacks[ds->stack_index]);
			card_stack_clear(&ds->table_stacks.stacks[ds->stack_index], c);
		}
		else // card fits on a stack -> place it
		{
			card_stack_t* cs = &ds->table_stacks.stacks[stack_idx];
			if(cs->cards[MAX_CARD_STACK_SIZE-1] != 0) // stack is full
			{
				uint32_t stack_points = card_stack_get_points(cs);
				card_stack_clear(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 0
	if (we_have_hand_cards)
	{
		return STATE_CLIENT_SELECT_OPEN_CARD;
	}
	else
	{
		receive_next_server_action();
		if (server_action == DEAL_CARDS)
		{
			round++;
			return STATE_CLIENT_WAIT_FOR_CARDS;
		}
		else if (server_action == GAME_FINISHED)
			return STATE_CLIENT_GAME_FINISHED;
	}
#endif

	return 1337;
}