#include "game.h" #include "card.h" #include "hand.h" #include #include #include #include #include #include #include #include #include "ui.h" #include "global.h" #include "net/comm.h" #include "net/client.h" #include "net/server.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, 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; sleep(1); exit(0); break; #if 0 case STATE_CLIENT_SELECT_OPEN_CARD: select_open_card(); send_open_card_to_server(); state = STATE_CLIENT_WAIT_FOR_OPEN_CARDS; break; case STATE_CLIENT_WAIT_FOR_OPEN_CARDS: receive_sorted_list_of_open_cards(); state = STATE_CLIENT_PLAY_CARDS; break; 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; } break; #endif default: printf("main_loop_client: entered unknown state\n"); exit(EXIT_FAILURE); } } } static void main_loop_server(socket_list_t* client_socks) { bool running = true; gamestate state = STATE_SERVER_DEAL_HAND_CARDS; uint8_t round = 1; data_store *data = datamodel(); while(running) { switch(state) { case STATE_SERVER_DEAL_HAND_CARDS: if(round == 1) { // Send table stacks to clients for(int i = 0; i < data->players.count; i++) { net_send(client_socks->sockets[i], msg_type_initial_stacks, NULL); } } // Deal hand cards to clients for(int i = 0; i < data->players.count; i++) { net_send(client_socks->sockets[i], msg_type_deal_hand, &data->hand); } //state = STATE_SERVER_WAIT_FOR_OPEN_CARDS; break; #if 0 case STATE_SERVER_WAIT_FOR_OPEN_CARDS: receive_open_cards_from_clients(); sort_open_card_list(); // in ascending order send_open_card_list_to_clients(); state = STATE_SERVER_PLAY_CARDS; break; 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; } } break; #endif default: printf("main_loop_server: entered unknown state\n"); exit(EXIT_FAILURE); } } } void start_game(const bool servermode, const char* addr, const char* port) { assert(addr != NULL && port != NULL); bool server_process = false; if(servermode) { pid_t child = fork(); server_process = (child > 0); } if(server_process) // Start server and connect to localhost { int server_sock; socket_list_t client_socks; uint8_t num_players = 1; int cards = MAX_CARD - MIN_CARD + 1; card mainstack[cards]; data_store* data = datamodel(); srand(time(0)); init_mainstack(mainstack, cards); // Example data set for table cards window const card_stack_t _cs1 = { { 1, 2, 3, 4, 101 } }; const card_stack_t _cs2 = { { 6, 7, 53, 0, 0 } }; const card_stack_t _cs3 = { { 11, 55, 0, 0, 0 } }; const card_stack_t _cs4 = { { 17, 29, 36, 42, 0 } }; //data->table_stacks = { { _cs1, _cs2, _cs3, _cs4 } }; data->table_stacks.stacks[0] = _cs1; data->table_stacks.stacks[1] = _cs2; data->table_stacks.stacks[2] = _cs3; data->table_stacks.stacks[3] = _cs4; //const table_stacks_t ts = { { _cs1, _cs2, _cs3, _cs4 } }; // The stack points window uses ts, too, so there is no separate data set // Example data set for current state window pnoc_t pnoc[10] = { {0, "$you", 10}, {1, "1234567890", 23}, {2, "baz", 38}, {3, "foo_bar", 14}, {4, "lolcat", 60}, {5, "blablub123", 15}, {6, "abcdefg", 103}, {7, "hello", 98}, {8, "hornoxe", 33}, {9, "1337nick", 74} }; pnoc_sort(pnoc, 10); const uint32_t score = 10; // Example data set for hand cards window const hand_t h = { {22, 0, 12, 85, 27, 69, 78, 0, 77, 0} }; data->hand = h; hand_sort(&data->hand); server_sock = server_start(port); server_get_players(server_sock, &client_socks, num_players); data->players.count = num_players; for(int i=0; iplayers.players[i].player_name); } for(int i=0; inickname, "nickname", 10); net_send(sock, msg_type_hello, NULL); net_recv(sock, msg_type_start_game); ui_init(); // Display all windows ui_display_wnd_table_cards(&data->table_stacks, false, 0); ui_display_wnd_stack_points(&data->table_stacks, false, 0); ui_display_wnd_hand_cards(&data->hand, false, 0); main_loop_client(sock); ui_fini(); } }