-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.lua
371 lines (345 loc) · 9.35 KB
/
server.lua
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
require("socket")
require("class")
json = require("dkjson")
require("stridx")
require("gen_panels")
local byte = string.byte
local char = string.char
local pairs = pairs
local ipairs = ipairs
local random = math.random
local lobby_changed = false
local time = os.time
local floor = math.floor
local TIMEOUT = 10
local VERSION = "021"
local type_to_length = {H=4, E=4, F=4, P=8, I=2, L=2, Q=8}
local INDEX = 1
local connections = {}
local name_to_idx = {}
local socket_to_idx = {}
local proposals = {}
function lobby_state()
local names = {}
for _,v in pairs(connections) do
if v.state == "lobby" then
names[#names+1] = v.name
end
end
return {unpaired = names}
end
function propose_game(sender, receiver, message)
local s_c, r_c = name_to_idx[sender], name_to_idx[receiver]
if s_c then s_c = connections[s_c] end
if r_c then r_c = connections[r_c] end
if s_c and s_c.state == "lobby" and r_c and r_c.state == "lobby" then
proposals[sender] = proposals[sender] or {}
proposals[receiver] = proposals[receiver] or {}
if proposals[sender][receiver] then
if proposals[sender][receiver][receiver] then
create_room(s_c, r_c)
end
else
r_c:send(message)
local prop = {[sender]=true}
proposals[sender][receiver] = prop
proposals[receiver][sender] = prop
end
end
end
function clear_proposals(name)
if proposals[name] then
for othername,_ in pairs(proposals[name]) do
proposals[name][othername] = nil
proposals[othername][name] = nil
end
proposals[name] = nil
end
end
function create_room(a, b)
lobby_changed = true
clear_proposals(a.name)
clear_proposals(b.name)
a.state = "room"
b.state = "room"
a.cursor = "level"
b.cursor = "level"
a.ready = false
b.ready = false
local a_msg, b_msg = {create_room = true}, {create_room = true}
a_msg.opponent = b.name
a_msg.menu_state = b:menu_state()
b_msg.opponent = a.name
b_msg.menu_state = a:menu_state()
a.opponent = b
b.opponent = a
a:send(a_msg)
b:send(b_msg)
end
function start_match(a, b)
local msg = {match_start = true,
player_settings = {character = a.character, level = a.level},
opponent_settings = {character = b.character, level = b.level}}
a:send(msg)
msg.player_settings, msg.opponent_settings = msg.opponent_settings, msg.player_settings
b:send(msg)
a:setup_game()
b:setup_game()
end
Connection = class(function(s, socket)
s.index = INDEX
INDEX = INDEX + 1
connections[s.index] = s
socket_to_idx[socket] = s.index
s.socket = socket
socket:settimeout(0)
s.leftovers = ""
s.state = "needs_name"
s.last_read = time()
end)
function Connection.menu_state(self)
return {cursor=self.cursor, ready=self.ready, character=self.character, level=self.level}
end
function Connection.send(self, stuff)
if type(stuff) == "table" then
local json = json.encode(stuff)
local len = json:len()
local prefix = "J"..char(floor(len/65536))..char(floor((len/256)%256))..char(len%256)
print(byte(prefix[1]), byte(prefix[2]), byte(prefix[3]), byte(prefix[4]))
print("sending json "..json)
stuff = prefix..json
else
if stuff[1] ~= "I" then
print("sending non-json "..stuff)
end
end
local foo = {self.socket:send(stuff)}
if stuff[1] ~= "I" then
print(unpack(foo))
end
if not foo[1] then
self:close()
end
end
function Connection.opponent_disconnected(self)
self.opponent = nil
self.state = "lobby"
lobby_changed = true
local msg = lobby_state()
msg.leave_room = true
self:send(msg)
end
function Connection.setup_game(self)
self.state = "playing"
self.vs_mode = true
self.metal = false
self.rows_left = 14+random(1,8)
self.prev_metal_col = nil
self.metal_col = nil
self.first_seven = nil
end
function Connection.close(self)
if self.state == "lobby" then
lobby_changed = true
end
clear_proposals(self.name)
if self.opponent then
self.opponent:opponent_disconnected()
end
if self.name then
name_to_idx[self.name] = nil
end
socket_to_idx[self.socket] = nil
connections[self.index] = nil
self.socket:close()
end
function Connection.H(self, version)
if version ~= VERSION then
self:send("N")
else
self:send("H")
end
end
function Connection.I(self, message)
if self.opponent then
self.opponent:send("I"..message)
end
end
-- got pong
function Connection.F(self, message)
end
local ok_ncolors = {}
for i=2,7 do
ok_ncolors[i..""] = true
end
function Connection.P(self, message)
if not ok_ncolors[message[1]] then return end
local ncolors = 0 + message[1]
local ret = make_panels(ncolors, string.sub(message, 2, 7), self)
if self.first_seven and self.opponent and
((self.level < 9 and self.opponent.level < 9) or
(self.level >= 9 and self.opponent.level >= 9)) then
self.opponent.first_seven = self.first_seven
end
self:send("P"..ret)
if self.opponent then
self.opponent:send("O"..ret)
end
end
function Connection.Q(self, message)
if not ok_ncolors[message[1]] then return end
local ncolors = 0 + message[1]
local ret = make_gpanels(ncolors, string.sub(message, 2, 7))
self:send("Q"..ret)
if self.opponent then
self.opponent:send("R"..ret)
end
end
function Connection.J(self, message)
message = json.decode(message)
local response
if self.state == "needs_name" and message.name then
if name_to_idx[message.name] then
local names = {}
for _,v in pairs(connections) do
names[#names+1] = v.name -- fine if name is nil :o
end
response = {choose_another_name = {used_names = names}}
self:send(response)
else
self.name = message.name
self.character = message.character
self.level = message.level
lobby_changed = true
self.state = "lobby"
name_to_idx[self.name] = self.index
end
elseif self.state == "lobby" and message.game_request then
if message.game_request.sender == self.name then
propose_game(message.game_request.sender, message.game_request.receiver, message)
end
elseif self.state == "room" and message.menu_state then
self.level = message.menu_state.level
self.character = message.menu_state.character
self.ready = message.menu_state.ready
self.cursor = message.menu_state.cursor
if self.ready and self.opponent.ready then
start_match(self, self.opponent)
else
self.opponent:send(message)
end
elseif self.state == "playing" and message.game_over then
if self.opponent.game_over then
create_room(self, self.opponent)
else
self.game_over = true
end
elseif (self.state == "playing" or self.state == "room") and message.leave_room then
local op = self.opponent
self:opponent_disconnected()
op:opponent_disconnected()
end
end
-- TODO: this should not be O(n^2) lol
function Connection.data_received(self, data)
self.last_read = time()
if data:len() ~= 2 then
print("got raw data "..data)
end
data = self.leftovers .. data
local idx = 1
while data:len() > 0 do
--assert(type(data) == "string")
local msg_type = data[1]
--assert(type(msg_type) == "string")
if msg_type == "J" then
if data:len() < 4 then
break
end
local msg_len = byte(data[2])*65536 + byte(data[3])*256 + byte(data[4])
if data:len() < 4 + msg_len then
break
end
local jmsg = data:sub(5, msg_len+4)
print("got JSON message "..jmsg)
print("Pcall results for json: ", pcall(function()
self:J(jmsg)
end))
data = data:sub(msg_len+5)
else
if msg_type ~= "I" then
print("using non-J type "..msg_type)
end
total_len = type_to_length[msg_type]
if not total_len then
print("closing because len did not exist")
self:close()
return
end
if data:len() < total_len then
print("breaking because len was too small")
break
end
res = {pcall(function()
self[msg_type](self, data:sub(2,total_len))
end)}
if msg_type ~= "I" or not res[1] then
print("got message "..msg_type.." "..data:sub(2,total_len))
print("Pcall results for "..msg_type..": ", unpack(res))
end
data = data:sub(total_len+1)
end
end
self.leftovers = data
end
function Connection.read(self)
local junk, err, data = self.socket:receive("*a")
if not err then
error("shitfuck")
end
if data and data:len() > 0 then
self:data_received(data)
end
end
function broadcast_lobby()
if lobby_changed then
for _,v in pairs(connections) do
if v.state == "lobby" then
v:send(lobby_state())
end
end
lobby_changed = false
end
end
local server_socket = socket.bind("localhost", 49569)
local prev_now = time()
while true do
server_socket:settimeout(0)
local new_conn = server_socket:accept()
if new_conn then
Connection(new_conn)
end
local recvt = {server_socket}
for _,v in pairs(connections) do
recvt[#recvt+1] = v.socket
end
local ready = socket.select(recvt, nil, 1)
assert(type(ready) == "table")
for _,v in ipairs(ready) do
if socket_to_idx[v] then
connections[socket_to_idx[v]]:read()
end
end
local now = time()
if now ~= prev_now then
for _,v in pairs(connections) do
if now - v.last_read > 10 then
v:close()
elseif now - v.last_read > 1 then
v:send("ELOL")
end
end
prev_now = now
end
broadcast_lobby()
end