From 3fc586325f8fc8fb4f1990bf87ad4c37e722f381 Mon Sep 17 00:00:00 2001 From: Mario Kilies Date: Tue, 25 Jan 2011 11:11:47 +0100 Subject: Created a mainstack datatype. Refactored server game states into separate functions. --- src/game.c | 140 ++++--------------------------------------------------------- 1 file changed, 8 insertions(+), 132 deletions(-) (limited to 'src/game.c') diff --git a/src/game.c b/src/game.c index d70f14c..3f6973d 100644 --- a/src/game.c +++ b/src/game.c @@ -15,50 +15,7 @@ #include "net/client.h" #include "net/server.h" #include "game_states.h" - -static void init_mainstack(card *stack, const uint32_t size) -{ - assert(stack != NULL); - - // assign card values to main stack - for(uint32_t i=0, val=MIN_CARD; itable_stacks.stacks[0].cards[0] = mainstack_remove_card(mainstack, cards); - data->table_stacks.stacks[1].cards[0] = mainstack_remove_card(mainstack, cards); - data->table_stacks.stacks[2].cards[0] = mainstack_remove_card(mainstack, cards); - data->table_stacks.stacks[3].cards[0] = mainstack_remove_card(mainstack, cards); - for(int i = 0; i < data->players.count; i++) - { - net_send(client_socks->sockets[i], msg_type_initial_stacks, NULL); - } - } - - int num_dealcards = num_cards_in_stack(mainstack, cards) / data->players.count; - if(num_dealcards > 10) - num_dealcards = 10; - - // Deal hand cards to clients - for(int i = 0; i < data->players.count; i++) - { - hand_t h; - memset(h.cards, 0, MAX_HAND_CARDS); - for(int j=0; jsockets[i], msg_type_deal_hand, &h); - } - - state = STATE_SERVER_WAIT_FOR_OPEN_CARDS; + state = state_server_deal_hand_cards(client_socks, round, &m); break; case STATE_SERVER_WAIT_FOR_OPEN_CARDS: - // Receive open cards from clients - for(int i = 0; i < data->players.count; i++) - { - pnoc_t* pl = get_pnoc_from_playerid(&data->players, client_socks->player_ids[i]); - assert(pl != NULL); - net_recv(client_socks->sockets[i], msg_type_selected_card); - pl->open_card = data->selected_card; - } - - for(int i=0; iplayers.count; i++) - net_send(client_socks->sockets[i], msg_type_selected_card_all, NULL); - - //state = STATE_SERVER_PLAY_CARDS; - state = STATE_SERVER_WAIT_FOR_OPEN_CARDS; // just for testing + state = state_server_wait_for_open_cards(client_socks); break; #if 0 case STATE_SERVER_PLAY_CARDS: - 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) - { - state = STATE_SERVER_WAIT_FOR_OPEN_CARDS; - } - else - { - if (main_stack_has_enough_cards_for_clients) - { - send_action_to_client(DEAL_HAND_CARDS); - round++; - state = STATE_SERVER_DEAL_HAND_CARDS; - } - else - { - send_action_to_client(GAME_FINISHED); - state = STATE_SERVER_GAME_FINISHED; - } - } + state = state_server_play_cards(client_socks, round); break; #endif default: @@ -226,6 +100,8 @@ void start_game(const bool servermode, const char* addr, const char* port) socket_list_t client_socks; uint8_t num_players = 2; + srand(time(0)); // Initialize RNG + data_store* data = datamodel(); server_sock = server_start(port); -- cgit v1.2.3