summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/game.c259
-rw-r--r--src/game.h4
-rw-r--r--src/global.c1
-rw-r--r--src/main.c38
-rw-r--r--src/net_client.c4
-rw-r--r--src/net_server.c8
-rw-r--r--src/player.c4
-rw-r--r--src/player.h4
-rw-r--r--src/pnoc.h4
9 files changed, 204 insertions, 122 deletions
diff --git a/src/game.c b/src/game.c
index 7bca6e6..7d4f666 100644
--- a/src/game.c
+++ b/src/game.c
@@ -7,9 +7,12 @@
#include <unistd.h>
#include <assert.h>
#include <curses.h>
+#include <sys/types.h>
#include "ui.h"
+#include "global.h"
+#include "net.h"
-void init_mainstack(card *stack, const uint32_t size)
+static void init_mainstack(card *stack, const uint32_t size)
{
assert(stack != NULL);
@@ -28,56 +31,11 @@ void init_mainstack(card *stack, const uint32_t size)
}
}
-void start_game(const bool servermode, const char *addr, const uint16_t port)
+static void main_loop_client()
{
- assert(addr != NULL);
-
bool running = true;
- int cards = MAX_CARD - MIN_CARD + 1;
- card mainstack[cards];
- enum gamestate state = STATE_DEALCARDS;
-
- 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 } };
- 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] = {
- {"$you", 10},
- {"1234567890", 23},
- {"baz", 38},
- {"foo_bar", 14},
- {"lolcat", 60},
- {"blablub123", 15},
- {"abcdefg", 103},
- {"hello", 98},
- {"hornoxe", 33},
- {"1337nick", 74}
- };
- pnoc_sort(pnoc, 10);
- const uint8_t num_players = 10;
- const uint32_t score = 10;
-
- // Example data set for hand cards window
- hand_t h = { {22, 0, 12, 85, 27, 69, 78, 0, 77, 0} };
- hand_sort(&h);
-
- // Display all windows
- ui_display_wnd_table_cards(&ts, false, 0);
- ui_display_wnd_stack_points(&ts, false, 0);
- ui_display_wnd_current_state(pnoc, num_players, 2, score);
- ui_display_wnd_hand_cards(&h, false, 0);
+ gamestate state = STATE_CLIENT_WAIT_CARDS;
- // main game loop
while(running)
{
/*
@@ -90,41 +48,36 @@ void start_game(const bool servermode, const char *addr, const uint16_t port)
state = STATE_CLIENT_SELECT_OPEN_CARD;
break;
- case STATE_SERVER_DEAL_CARDS:
- if(round == 1)
- send_and_place_table_stacks();
- deal_cards_to_clients();
- state = STATE_SERVER_WAIT_OPEN_CARDS;
- break;
-
case STATE_CLIENT_SELECT_OPEN_CARD:
select_open_card();
send_open_card_to_server();
state = STATE_CLIENT_WAIT_FOR_OPEN_CARDS;
break;
- 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_CLIENT_WAIT_FOR_OPEN_CARDS:
receive_sorted_list_of_open_cards();
state = STATE_CLIENT_PLAY_CARDS;
break;
- case STATE_SERVER_PLAY_CARDS:
+ case STATE_CLIENT_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();
+ 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);
}
}
@@ -136,44 +89,64 @@ void start_game(const bool servermode, const char *addr, const uint16_t port)
}
}
- if (clients_have_hand_cards)
+ if (we_have_hand_cards)
{
- state = STATE_SERVER_WAIT_FOR_OPEN_CARDS;
+ state = STATE_CLIENT_SELECT_OPEN_CARD;
}
else
{
- if (main_stack_has_enough_cards_for_clients)
- {
- send_action_to_client(DEAL_CARDS);
- state = STATE_SERVER_DEAL_CARDS;
- }
- else
+ receive_next_server_action();
+ if (server_action == DEAL_CARDS)
{
- send_action_to_client(GAME_FINISHED);
- state = STATE_SERVER_GAME_FINISHED;
+ state = STATE_CLIENT_WAIT_CARDS;
}
+ else if (server_action == GAME_FINISHED)
+ state = STATE_CLIENT_GAME_FINISHED;
}
break;
- case STATE_CLIENT_PLAY_CARDS:
+ default:
+ assert(false); // should never happen
+ }
+ */
+ }
+
+}
+
+static void main_loop_server()
+{
+ bool running = true;
+ gamestate state = STATE_SERVER_DEAL_CARDS;
+
+ while(running)
+ {
+ /*
+ switch(state)
+ {
+ case STATE_SERVER_DEAL_CARDS:
+ if(round == 1)
+ send_and_place_table_stacks();
+ deal_cards_to_clients();
+ state = STATE_SERVER_WAIT_OPEN_CARDS;
+ break;
+
+ 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)
- {
- if (we_have_to_pick)
- {
- pick_stack();
- send_stack_to_server();
- }
- else // another client has to pick
- {
- receive_stack();
- }
-
+ if (stack_has_to_be_picked) {
+ receive_stack_from_client();
+ send_received_stack_to_other_clients();
clear_stack(stack_id);
}
}
@@ -185,19 +158,22 @@ void start_game(const bool servermode, const char *addr, const uint16_t port)
}
}
- if (we_have_hand_cards)
+ if (clients_have_hand_cards)
{
- state = STATE_CLIENT_SELECT_OPEN_CARD;
+ state = STATE_SERVER_WAIT_FOR_OPEN_CARDS;
}
else
{
- receive_next_server_action();
- if (server_action == DEAL_CARDS)
+ if (main_stack_has_enough_cards_for_clients)
{
- state = STATE_CLIENT_WAIT_CARDS;
+ send_action_to_client(DEAL_CARDS);
+ state = STATE_SERVER_DEAL_CARDS;
+ }
+ else
+ {
+ send_action_to_client(GAME_FINISHED);
+ state = STATE_SERVER_GAME_FINISHED;
}
- else if (server_action == GAME_FINISHED)
- state = STATE_CLIENT_GAME_FINISHED;
}
break;
@@ -206,4 +182,95 @@ void start_game(const bool servermode, const char *addr, const uint16_t port)
}
*/
}
+
}
+
+void start_game(const bool servermode, const char* addr, const char* port)
+{
+ assert(addr != NULL && port != NULL);
+
+ bool server_process = false;
+ 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 uint8_t num_players = 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);
+
+ if(servermode)
+ {
+ pid_t child = fork();
+ server_process = (child > 0);
+ }
+
+ if(server_process) // Start server and connect to localhost
+ {
+ int ssock;
+ uint8_t num_opponents = 1;
+
+ ssock = server_start(port);
+ server_get_players(ssock, num_opponents);
+
+ main_loop_server();
+ }
+ else // Connect to server
+ {
+ int sock;
+
+ sleep(1); // TODO make sure server process is listening
+ sock = client_connect_server(addr, port);
+ client_hello(sock, "nickname_client");
+ net_recv(sock, msg_type_start_game);
+ net_recv(sock, msg_type_deal_cards);
+
+ 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(pnoc, num_players, 2, score);
+ ui_display_wnd_hand_cards(&data->hand, false, 0);
+
+ main_loop_client();
+
+ ui_fini();
+ }
+}
+
diff --git a/src/game.h b/src/game.h
index 4675e68..4eaa786 100644
--- a/src/game.h
+++ b/src/game.h
@@ -7,8 +7,8 @@
#define MIN_CARD 1
#define MAX_CARD 104
-enum gamestate { STATE_DEALCARDS, STATE_WAIT_CARDS, STATE_SELECTCARD, STATE_WAIT_OPPONENTCARDS, STATE_WAIT_OPPONENTSTACK, STATE_SELECTSTACK };
+typedef enum { STATE_CLIENT_WAIT_CARDS, STATE_CLIENT_SELECT_OPEN_CARD, STATE_CLIENT_WAIT_FOR_OPEN_CARDS, STATE_CLIENT_PLAY_CARDS, STATE_SERVER_DEAL_CARDS, STATE_SERVER_WAIT_FOR_OPEN_CARDS, STATE_SERVER_PLAY_CARDS } gamestate;
-void start_game(const bool servermode, const char *addr, const uint16_t port);
+void start_game(const bool servermode, const char* addr, const char* port);
#endif // OXEN_GAME_H
diff --git a/src/global.c b/src/global.c
index d9dc3bf..d2c0804 100644
--- a/src/global.c
+++ b/src/global.c
@@ -1,5 +1,6 @@
#include <stdlib.h>
#include <string.h>
+#include <assert.h>
#include "global.h"
static data_store* data = NULL;
diff --git a/src/main.c b/src/main.c
index d48a3e6..bde5bf1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -11,7 +11,7 @@
int main(int argc, char **argv)
{
char* port = 0;
- char *addr;
+ char* addr = NULL;
bool servermode = false;
int opponents = 1;
@@ -21,7 +21,21 @@ int main(int argc, char **argv)
printf("usage: '%s address port' for client mode, or '%s port' for server mode\n", argv[0], argv[0]);
return EXIT_SUCCESS;
}
- else if (argc == 2) // Server mode
+ else if(argc == 2) // Server mode
+ {
+ addr = "localhost";
+ port = argv[1];
+ }
+ else if(argc == 3)
+ {
+ addr = argv[1];
+ port = argv[2];
+
+ }
+
+ start_game(servermode, addr, port);
+
+ /*else if (argc == 2) // Server mode
{
int ssock;
int* csocks;
@@ -38,14 +52,14 @@ int main(int argc, char **argv)
csocks = server_get_players(ssock, opponents);
players.count = opponents + 1;
- players.names[0] = malloc(strlen(nickname)+1);
- strcpy(players.names[0], nickname);
+ //players.names[0] = malloc(strlen(nickname)+1);
+ //strcpy(players.names[0], nickname);
for(int i=0; i<opponents; i++)
{
// wait for greeting from client i and read nick
char* name = net_recv(csocks[i], msg_type_hello);
- players.names[i+1] = name;
+ //players.names[i+1] = name;
printf("player connected: len:%d, name:%s\n", strlen(name), name);
}
@@ -67,8 +81,8 @@ int main(int argc, char **argv)
}
// cleanup
- for(int i=0; i<players.count; i++)
- free(players.names[i]);
+ //for(int i=0; i<players.count; i++)
+ // free(players.names[i]);
close(ssock);
free(csocks);
}
@@ -89,8 +103,8 @@ int main(int argc, char **argv)
// retrieve list of all players from server
players = net_recv(sock, msg_type_start_game);
- for(int i=0; i<players->count; i++)
- printf("Player %d: %s\n", i, players->names[i]);
+ //for(int i=0; i<players->count; i++)
+ // printf("Player %d: %s\n", i, players->names[i]);
// receive test hand
testhand = net_recv(sock, msg_type_deal_cards);
@@ -114,10 +128,8 @@ int main(int argc, char **argv)
cleanup_playerlist(players);
free(testhand);
close(sock);
- }
+ }*/
+
- //ui_init();
- //start_game(servermode, addr, port);
- //ui_fini();
return EXIT_SUCCESS;
}
diff --git a/src/net_client.c b/src/net_client.c
index 9454649..e90e583 100644
--- a/src/net_client.c
+++ b/src/net_client.c
@@ -144,11 +144,11 @@ player_list* client_recv_player_list(const uint8_t* payload, const uint8_t data_
// read usernames from buffer
for(int i=0; i<players->count; i++)
{
- uint8_t namelen = payload[pos++];
+ /*uint8_t namelen = payload[pos++];
players->names[i] = malloc(namelen+1);
memcpy(players->names[i], payload+pos, namelen);
players->names[i][namelen] = '\0';
- pos += namelen;
+ pos += namelen;*/
}
return players;
diff --git a/src/net_server.c b/src/net_server.c
index 731e232..e8ecb4f 100644
--- a/src/net_server.c
+++ b/src/net_server.c
@@ -129,8 +129,8 @@ void server_start_game(int* clients, const uint8_t clientcount, const player_lis
uint32_t pos;
uint32_t buflen = 3 + usercount; // type + packetlen + usercount + (usercount * len)
- for(int i=0; i<usercount; i++)
- buflen += strlen(players->names[i]);
+ //for(int i=0; i<usercount; i++)
+ // buflen += strlen(players->names[i]);
buf = malloc(buflen);
if(buf == NULL)
@@ -145,10 +145,10 @@ void server_start_game(int* clients, const uint8_t clientcount, const player_lis
// copy usernames with length to buffer
for(int i=0; i<usercount; i++)
{
- uint8_t len = strlen(players->names[i]);
+ /*uint8_t len = strlen(players->names[i]);
buf[pos++] = len;
memcpy(buf+pos, players->names[i], len);
- pos += len;
+ pos += len;*/
}
// send to all users
diff --git a/src/player.c b/src/player.c
index 28914d4..f4b6490 100644
--- a/src/player.c
+++ b/src/player.c
@@ -15,7 +15,7 @@ void cleanup_playerlist(player_list* pl)
{
assert(pl != NULL);
- for(int i=0; i<MAX_PLAYERS; i++)
- free(pl->names[i]);
+ //for(int i=0; i<MAX_PLAYERS; i++)
+ // free(pl->names[i]);
free(pl);
}
diff --git a/src/player.h b/src/player.h
index e1311cd..aad8fcf 100644
--- a/src/player.h
+++ b/src/player.h
@@ -1,15 +1,15 @@
#ifndef OXEN_PLAYER_H
#define OXEN_PLAYER_H
-#define MAX_PLAYER_NAME_LENGTH 10
#define MAX_PLAYERS 10
#include <stdint.h>
+#include "pnoc.h"
typedef struct
{
uint8_t count;
- char* names[MAX_PLAYERS];
+ pnoc_t players[MAX_PLAYERS];
} player_list;
player_list* create_playerlist(void);
diff --git a/src/pnoc.h b/src/pnoc.h
index bfff947..8ef8606 100644
--- a/src/pnoc.h
+++ b/src/pnoc.h
@@ -1,11 +1,13 @@
#ifndef OXEN_PNOC_H
#define OXEN_PNOC_H
-#include "player.h"
#include "card.h"
+#define MAX_PLAYER_NAME_LENGTH 10
+
typedef struct
{
+ uint8_t player_id;
char player_name[MAX_PLAYER_NAME_LENGTH+1]; // Player name length + 1 for string termination
card open_card;
} pnoc_t;