summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 6e68d3505ba3afb360c96c48faff2ad072ff9c49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdlib.h>
#include <curses.h>
#include "ui.h"
#include "game.h"
#include "net.h"
#include "player.h"
#include "hand.h"

int main(int argc, char **argv)
{
	char* port = 0;
	char *addr;
	bool servermode = false;

	if (argc < 2)
	{
		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
	{
		int ssock;
		int* csocks;
		player_list players;
		int opponents = 3;
		const char* nickname = "deki";
		const hand testhand = { 12, 23, 35, 42, 55, 57, 70, 81, 103, 0 };
		servermode = true;
		port = argv[1];

		// start listening
		ssock = server_start(port);

		// accept client connections
		csocks = server_get_players(ssock, opponents);

		players.count = opponents + 1;
		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;
			printf("player connected: len:%d, name:%s\n", strlen(name), name);
		}

		// start game and send player list to clients
		server_start_game(csocks, opponents, &players);

		// send test hand
		for(int i=0; i<opponents; i++)
			server_deal_cards(csocks[i], testhand);

		// cleanup
		for(int i=0; i<players.count; i++)
			free(players.names[i]);
		close(ssock);
		free(csocks);
	}
	else if (argc == 3) // Client mode
	{
		int sock;
		player_list* players;
		const char* nickname = "schnippi";
		hand* testhand;
		addr = argv[1];
		port = argv[2];

		// connect to server
		sock = client_connect_server(addr, port);

		// greet server and tell nickname
		client_hello(sock, nickname);

		// 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]);

		// receive test hand
		testhand = net_recv(sock, msg_type_deal_cards);
		printf("received cards: ");
		for(int i=0; i<MAX_HAND_CARDS; i++)
			printf("%d, ", *testhand[i]);
		printf("\n");

		// cleanup
		cleanup_playerlist(players);
		free(testhand);
		close(sock);
	}

	//ui_init();
	//start_game(servermode, addr, port);
	//ui_fini();
	return EXIT_SUCCESS;
}