summaryrefslogtreecommitdiff
path: root/main.c
blob: 0b7a639c0859de672b3b9cb87415e65ee47aa8ea (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#define _GNU_SOURCE
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <termio.h>
#include "memory.h"
#include "showmenu.h"
#include "tcp.h"

#define HOSTNAME "localhost"
#define PORT 1423

#define GAMENAME "regexbattle"


static char *die_message=NULL;

__attribute__((noreturn, format (printf,1,2)))
static void die(const char *format,...){
	va_list ap;
	va_start(ap,format);
	char *buf;
	int ret=vasprintf(&buf,format,ap);
	va_end(ap);
	if(ret<0){
		die_message=strdup("Double error roll");
	} else {
		die_message=buf;
	}
	exit(1);
}

static void exit_cleanup(void){
	endkeyboard();
	endscreen();
	if(die_message!=NULL){
		printf("%s\n",die_message);
	}
}

char* show_prompt(int x,int y,int w,const char *title){
	Promptwidget *prw=prw_make(x,y,w,title);
	char *line=NULL;
	while(line==NULL){
		redraw();
		int key=tgetkey();
		if(key==KEY_ESC)break;
		line=prw_handlekey(prw,key);
	}
	prw_destroy(prw);
	return line;
}

static void room_create(const char *name){}
static void room_join(const char *name){}

int main(void){
	int sock=tcp_connect(HOSTNAME,PORT);
	if(sock==-1){
		printf("Could not connect to %s:%d\n",HOSTNAME,PORT);
		return 1;
	}
	char *recvbuf=NULL;
	i64 recvbufsz=0;
	tcp_send_line(sock,"ping");
	tcp_read_line(sock,&recvbuf,&recvbufsz);
	if(strcmp(recvbuf,"pong")!=0){
		printf("Protocol error; different server running on %s:%d?\n",HOSTNAME,PORT);
		return 1;
	}

	initscreen();
	initkeyboard(false);
	installCLhandler(true);
	atexit(exit_cleanup);

	clearscreen();

	while(true){
		i64 sel=showmenu("REGEXBATTLE",
			"List open public games","Create a new game","Join an open game",
			"Quit",NULL);
		switch(sel){
			case 0: {
				tcp_send_line(sock,"room_list " GAMENAME);
				TcpList *list=tcp_read_list(sock,"room_list");
				if(list==NULL)die("Protocol error: receiving list");
				Size termsize=gettermsize();
				fillrect(0,7,termsize.w,termsize.h-7,' ');
				moveto(0,7);
				for(i64 i=0;i<list->nitems&&i+7<termsize.h;i++){
					tprintf("Public games: %s\n",list->items[i]);
				}
				tcp_list_destroy(list);
				break;
			}

			case 1: {
				char *line=show_prompt(2,8,20,"Game name to create");
				Size termsize=gettermsize();
				fillrect(0,7,termsize.w,termsize.h-7,' ');
				if(line!=NULL){
					room_create(line);
					free(line);
				}
				break;
			}

			case 2: {
				char *line=show_prompt(2,8,20,"Game name to join");
				Size termsize=gettermsize();
				fillrect(0,7,termsize.w,termsize.h-7,' ');
				if(line!=NULL){
					room_join(line);
					free(line);
				}
				break;
			}

			case 3: return 0;
		}
	}
}