summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMario Kilies <MarioKilies@GMX.net>2011-01-25 10:16:53 +0100
committerMario Kilies <MarioKilies@GMX.net>2011-01-25 10:16:53 +0100
commit6dad43a768646ef0d8f06958446761eb8a1b3f93 (patch)
tree444e202b636e5f119dacb81dfcbcb3060ae5aae4
parent2d8d54e988ced2aaf485c9640ef0a38526a4eb44 (diff)
Refactored client game states into separate functions instead of handling them in a huge switch() statement.
-rw-r--r--src/client_game_states.c108
-rw-r--r--src/game.c86
-rw-r--r--src/game_states.h14
3 files changed, 126 insertions, 82 deletions
diff --git a/src/client_game_states.c b/src/client_game_states.c
new file mode 100644
index 0000000..651c06d
--- /dev/null
+++ b/src/client_game_states.c
@@ -0,0 +1,108 @@
+#include "game_states.h"
+#include "net/comm.h"
+#include "ui.h"
+#include "global.h"
+
+game_state_t state_client_wait_for_hand_cards(const int sock, const uint8_t round)
+{
+ data_store *d = datamodel();
+
+ 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 *d = datamodel();
+ 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 *d = datamodel();
+
+ net_recv(sock, msg_type_selected_card_all);
+ pnoc_sort(d->players.players, d->players.count); // sort in ascending order
+
+ ui_display_wnd_current_state(d->players.players, d->players.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)
+{
+#if 0
+ data_store *d = datamodel();
+ uint8_t stack_idx;
+
+ foreach(open_card)
+ {
+ play_lowest_open_card
+ {
+ determine_stack_for_open_card
+ {
+ if (stack_has_to_be_picked)
+ {
+ if (we_have_to_pick)
+ {
+ pick_stack();
+ send_stack_to_server();
+ }
+ else // another client has to pick
+ {
+ receive_stack();
+ }
+
+ clear_stack(stack_id);
+ }
+ }
+ place_card()
+ {
+ if(count_stack_cards == 6)
+ clear_stack(stack_id);
+ }
+ }
+ }
+ 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;
+}
diff --git a/src/game.c b/src/game.c
index 621eb8a..d70f14c 100644
--- a/src/game.c
+++ b/src/game.c
@@ -65,103 +65,25 @@ static void main_loop_client(int sock)
bool running = true;
game_state_t state = STATE_CLIENT_WAIT_FOR_HAND_CARDS;
uint8_t round = 1;
- data_store *data = datamodel();
- uint8_t open_card_idx;
- uint8_t picked_stack_idx;
while(running)
{
switch(state)
{
case STATE_CLIENT_WAIT_FOR_HAND_CARDS:
- if(round == 1)
- {
- // Receive and display table stacks
- net_recv(sock, msg_type_initial_stacks);
- ui_display_wnd_table_cards(&data->table_stacks, false, 0);
- ui_display_wnd_stack_points(&data->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(&data->hand, false, 0);
-
- state = STATE_CLIENT_SELECT_OPEN_CARD;
+ state = state_client_wait_for_hand_cards(sock, round);
break;
case STATE_CLIENT_SELECT_OPEN_CARD:
- // Select open card
- open_card_idx = ui_choose_card(&data->hand);
- data->selected_card = data->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(&data->hand, open_card_idx);
- ui_display_wnd_hand_cards(&data->hand, false, 0);
-
- state = STATE_CLIENT_WAIT_FOR_OPEN_CARDS;
+ state = state_client_select_open_card(sock);
break;
case STATE_CLIENT_WAIT_FOR_OPEN_CARDS:
- net_recv(sock, msg_type_selected_card_all);
- pnoc_sort(data->players.players, data->players.count); // sort in ascending order
-
- ui_display_wnd_current_state(data->players.players, data->players.count, true, 0, 0); // TODO fix parameters
-
- //state = STATE_CLIENT_PLAY_CARDS;
- //sleep(2);
- //return;
- state = STATE_CLIENT_SELECT_OPEN_CARD;; // just for testing
+ state = state_client_wait_for_open_cards(sock);
break;
-
#if 0
case STATE_CLIENT_PLAY_CARDS:
- foreach(open_card)
- {
- play_lowest_open_card
- {
- determine_stack_for_open_card
- {
- if (stack_has_to_be_picked)
- {
- if (we_have_to_pick)
- {
- pick_stack();
- send_stack_to_server();
- }
- else // another client has to pick
- {
- receive_stack();
- }
-
- clear_stack(stack_id);
- }
- }
- place_card()
- {
- if(count_stack_cards == 6)
- clear_stack(stack_id);
- }
- }
-
- }
- if (we_have_hand_cards)
- {
- state = STATE_CLIENT_SELECT_OPEN_CARD;
- }
- else
- {
- receive_next_server_action();
- if (server_action == DEAL_CARDS)
- {
- round++;
- state = STATE_CLIENT_WAIT_CARDS;
- }
- else if (server_action == GAME_FINISHED)
- state = STATE_CLIENT_GAME_FINISHED;
- }
+ state = state_client_play_cards(sock);
break;
#endif
default:
diff --git a/src/game_states.h b/src/game_states.h
index 8a868f4..2c09f76 100644
--- a/src/game_states.h
+++ b/src/game_states.h
@@ -1,14 +1,28 @@
#ifndef OXEN_GAME_STATES_H
#define OXEN_GAME_STATES_H
+#include <stdint.h>
+
typedef enum {
+ // Client states
STATE_CLIENT_WAIT_FOR_HAND_CARDS,
STATE_CLIENT_SELECT_OPEN_CARD,
STATE_CLIENT_WAIT_FOR_OPEN_CARDS,
STATE_CLIENT_PLAY_CARDS,
+
+ // Server states
STATE_SERVER_DEAL_HAND_CARDS,
STATE_SERVER_WAIT_FOR_OPEN_CARDS,
STATE_SERVER_PLAY_CARDS
} game_state_t;
+game_state_t state_client_wait_for_hand_cards(const int sock, const uint8_t round);
+game_state_t state_client_select_open_card(const int sock);
+game_state_t state_client_wait_for_open_cards(const int sock);
+game_state_t state_client_play_cards(const int sock);
+
+game_state_t state_server_deal_hand_cards();
+game_state_t state_server_wait_for_open_cards();
+game_state_t state_server_play_cards();
+
#endif // OXEN_GAME_STATES_H