forked from klusark/PacMacro
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPacMacro.cpp
142 lines (117 loc) · 3.14 KB
/
PacMacro.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <libwebsockets.h>
#include <jansson.h>
#include "Game.hpp"
#include "Connection.hpp"
enum protocols {
PROTOCOL_PACMACRO,
};
static int nextid = 0;
static int
callback_pacmacro(libwebsocket_context *context,
libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user, void *in, size_t len)
{
Connection *conn = (Connection *)user;
const char *recv = (const char *)in;
switch (reason) {
case LWS_CALLBACK_ESTABLISHED:
conn->id = nextid;
++nextid;
fprintf(stderr, "New Connection\n");
break;
case LWS_CALLBACK_RECEIVE:
{
fprintf(stderr, "rx %d %s\n", (int)len, (const char *)in);
json_error_t error;
json_t *json = json_loads(recv, 0, &error);
if (json == nullptr) {
break;
}
const char *type = json_string_value(json_object_get(json, "type"));
if (strcmp(type, "login") == 0) {
PlayerType type = getPlayerType(json_string_value(json_object_get(json, "role")));
if (type == InvalidType) {
break;
}
conn->wsi = wsi;
conn->_type = type;
g_game->addConnection(conn);
const std::string &data = g_game->getGameState(conn->_type);
conn->send(data);
} else if (strcmp(type, "moveto") == 0) {
int tile = json_integer_value(json_object_get(json, "tile"));
g_game->moveTo(conn->_type, tile);
} else if (strcmp(type, "power") == 0) {
int tile = json_integer_value(json_object_get(json, "tile"));
g_game->power(tile);
} else if (strcmp(type, "restart") == 0) {
int gameLength = json_integer_value(json_object_get(json, "gameLength"));
int pillLength = json_integer_value(json_object_get(json, "pillLength"));
g_game->setGameLength(gameLength);
g_game->setPillLength(pillLength);
g_game->restart();
} else if (strcmp(type, "getconn") == 0) {
g_game->sendConnList(conn);
} else if (strcmp(type, "setconn") == 0) {
int conn = json_integer_value(json_object_get(json, "conn"));
const char *type = json_string_value(json_object_get(json, "newtype"));
PlayerType t = getPlayerType(type);
if (t == InvalidType) {
break;
}
g_game->setConnType(conn, t);
}
}
break;
case LWS_CALLBACK_CLOSED:
g_game->removeConnection(conn);
break;
default:
printf("ASDF %d\n", reason);
break;
}
return 0;
}
/* list of supported protocols and callbacks */
static struct libwebsocket_protocols protocols[] = {
{
"pacmacro",
callback_pacmacro,
sizeof(Connection),
},
{
NULL, NULL, 0 /* End of list */
}
};
int main(int argc, char **argv)
{
int n = 0;
int port = 37645;
struct libwebsocket_context *context;
int opts = 0;
const char *interface = NULL;
g_game = new Game();
lws_context_creation_info info;
memset(&info, 0, sizeof(info));
info.port = port;
info.iface = interface;
info.protocols = protocols;
info.extensions = libwebsocket_get_internal_extensions();
info.gid = -1;
info.uid = -1;
info.options = opts;
context = libwebsocket_create_context(&info);
if (context == NULL) {
fprintf(stderr, "libwebsocket init failed\n");
return -1;
}
while (n >= 0) {
n = libwebsocket_service(context, 5000);
}
libwebsocket_context_destroy(context);
return 0;
}