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
|
#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);
}
}
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 public rooms","Create a room","Join a room",
"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("Room: %s\n",list->items[i]);
}
break;
}
case 1: break;
case 2: break;
case 3: return 0;
}
}
}
|