summaryrefslogtreecommitdiffstats
path: root/src/client/client.cpp
blob: 8ad25d8c00d40108b3f1be2e2e8eabeed2b5673b (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
#include <ncurses.h>
#include <string.h>
#include <unistd.h>

#include "user.hpp"

#define EXIT_BUTTON 4
#define SERVER_IP "127.0.0.1"
static int port = 7777;

int showMainMenu(int max_row, int max_col)
{
	int num_items = 5;
	const char *items[num_items] = {
		"Connect to room",
		"Create room",
		"Help",
		"About WantChat",
		"Exit"
	};

	int height_menu = num_items + 2;
	int width_menu = strlen(items[0]) + 2;

	SelectionMenu main_menu = SelectionMenu("Hello. Welcome to \
		WantChat", items, num_items, height_menu, width_menu,
		(max_row-height_menu)/2, (max_col-width_menu)/2, 0);
	main_menu.Update();

	keypad(main_menu.GetWindow(), true);
	int choice = main_menu.Handling();
	main_menu.Hide();
	return choice;
	// дескриптор будет вызван после выхода из функции неявно
}

int main(int argc, char *argv[])
{
	initscr();
	cbreak();
	noecho();
	// keypad(stdscr, TRUE);
	curs_set(false);

	int rows, columns;
	getmaxyx(stdscr, rows, columns); 
	if(rows != 24 || columns != 80) {
		endwin();
		printf("Please use terminal with size 24x80\n");
		return 1;
	}

	Client *user = Client::Start(SERVER_IP, port);
	if(!user) {
		endwin();
		perror("client");
		return 1;
	}

	int choice;
	while( (choice = showMainMenu(rows, columns)) != EXIT_BUTTON)
	{
		switch(choice)
		{
			case 0: {
				ChatRoom *room = new ChatRoom();
				user->Run(room);

				delete room;
				break;
			}
		}
	}

	endwin();
	return 0;
}

	/*WINDOW *chat = newwin(21, 59, 0, 0);
	box(chat, 0, 0);
	wrefresh(chat);

	WINDOW *players = newwin(21, 20, 0, 60);
	box(players, 0, 0);
	wrefresh(players);

	WINDOW *input = newwin(2, 80, 22, 0);
	//box(input, ' ', ' ');
	wmove(input, 0, 2);
	wrefresh(input);*/