#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" #include "game_states.h" #include "main_stack.h" static void main_loop_client(int sock) { bool running = true; game_state_t state = STATE_CLIENT_WAIT_FOR_HAND_CARDS; uint8_t round = 1; while(running) { switch(state) { case STATE_CLIENT_WAIT_FOR_HAND_CARDS: state = state_client_wait_for_hand_cards(sock, round); break; case STATE_CLIENT_SELECT_OPEN_CARD: state = state_client_select_open_card(sock); break; case STATE_CLIENT_WAIT_FOR_OPEN_CARDS: state = state_client_wait_for_open_cards(sock); break; #if 0 case STATE_CLIENT_PLAY_CARDS: state = state_client_play_cards(sock); 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; uint8_t round = 1; game_state_t state = STATE_SERVER_DEAL_HAND_CARDS; main_stack_t m; main_stack_init(&m); while(running) { switch(state) { case STATE_SERVER_DEAL_HAND_CARDS: state = state_server_deal_hand_cards(client_socks, round, &m); break; case STATE_SERVER_WAIT_FOR_OPEN_CARDS: state = state_server_wait_for_open_cards(client_socks); break; #if 0 case STATE_SERVER_PLAY_CARDS: state = state_server_play_cards(client_socks, round); 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 = 2; srand(time(0)); // Initialize RNG data_store* data = datamodel(); 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_id = i+1; printf("Player connected: %s\n", data->players.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_current_state(data->players.players, data->players.count, false, 0, 0); ui_display_wnd_hand_cards(&data->hand, false, 0); main_loop_client(sock); ui_fini(); } }