-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.lua
734 lines (678 loc) · 13 KB
/
client.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
-- vim:et
require "socket"
local sock
local sendq
local recvq
local seq=0
local allsocks
local insync=false
local timeout=0
love.filesystem.mkdir("replays")
local rep=love.filesystem.newFile("replays/lastreplay")
local function init_vars()
ME=nil
players=ctable()
devices=ctable()
units=ctable()
links=storage()
packets=storage()
shots=storage()
dhash=sphash(200)
uhash=sphash(100)
rq_um=runqueue()
end
function net_conn(addr,nick)
net_err=nil
sendq=squeue()
recvq=rqueue()
init_vars()
sock=socket.udp()
allsocks={sock}
local ts=socket.gettime()
timeout=ts+5
seq=1
insync=false
if not sock:setpeername(addr,6352) then
net_err="host not found"
return
end
sock:send(string.format("PLr:%s:%s",nick,NVER))
rep:open("w")
end
function net_abort()
if sock then
sock:close()
sock=nil
end
end
local lastsend=0
local lastrecv=0
function net_read(ts)
local str=sock:receive()
if not str then
return nil
end
lastrecv=ts+4
timeout=ts+30
if str:find("!",1,true) then
local s=recvq:put(str)
if not s then
console.msg("! malformed packet received")
net_err="malformed packet received"
return nil
end
sock:send(string.format("ACK:%d",s))
str=str_split(str,"!")[2]
rep:write(string.format("%s@%s\n",ts,str))
return nil
end
rep:write(string.format("%s@%s\n",ts,str))
return str
end
function net_sync()
if net_err then
return false
end
if timeout==0 then
return false
end
local ts=socket.gettime()
local ret=socket.select(allsocks,nil,0)
local msg
if ret[sock] then
msg=net_read(ts)
end
if ts>=timeout then
net_err="timeout..."
return false
end
net_parse(msg,ts)
return insync
end
function net_send(fmt,...)
sendq:put(string.format(fmt,...))
end
function net_close()
if not replay and sock then
sock:send("DISCONNECT")
sock:close()
rep:close()
sock=nil
end
end
local cmd={}
cmd["ACK"]=function(a,ts)
if a.n==2 then
sendq:del(tonumber(a[2]))
end
end
cmd["PING"]=function(a,ts)
if a.n<2 then
return
end
sock:send(string.format("PONG:%s",a[2]))
lastsend=ts+5
end
cmd["PINGS"]=function(a,ts)
if a.n<3 then
return
end
local c=(a.n-1)/2
local i,pl
for x=1,c do
i=x*2
pl=players[tonumber(a[i])]
if pl then
pl.showping=a[i+1]
end
end
end
cmd["MSG"]=function(a,ts) -- MSG:nick:~msg
if a.n<3 then
return
end
console.msg(string.format("<%s> %s",a[2],a[3]))
end
cmd["INFO"]=function(a,ts) -- INFO:~msg
if a.n<2 then
return
end
console.msg(string.format("%s",a[2]))
end
cmd["ERR"]=function(a,ts)
net_err=a[2]
end
cmd["DONE"]=function(a,ts)
insync=true
fakets=socket.gettime()
net_send("OK")
end
cmd["ALLY"]=function(a,ts)
if a.n<3 then
return
end
local p1=players[tonumber(a[2])]
local p2=players[tonumber(a[3])]
local b=tonumber(a[4])==1
if ME==p1 then
ally[p2]=b
end
local str=b and "friend" or "enemy"
console.msg(string.format("%s sets relation to %s: %s",p1.name,p2.name,str))
end
cmd["PLa"]=function(a,ts) -- PLa:idx:nick:cash
if a.n<4 then
return
end
local idx=tonumber(a[2])
local cash=tonumber(a[4])
local pl=Player:new(cash)
pl.idx=idx
pl.name=a[3]
pl.ping=0
pl.showping=0
players[idx]=pl
if a[5]=="me" then
ME=pl
end
if insync or replay then
console.msg(string.format("%s has connected.",a[3]))
end
end
cmd["PLd"]=function(a,ts) -- PLa:idx
if a.n<2 then
return
end
local idx=tonumber(a[2])
local pl=players[idx]
pl:disconnect()
players[idx]=nil
ally[pl]=nil
console.msg(string.format("%s disconnected.",pl.name))
end
cmd["PC"]=function(a,ts) -- Cash:idx:cash:maxcash
if a.n<4 then
return
end
local pl=players[tonumber(a[2])]
if pl then
pl.cash=tonumber(a[3])
pl.maxcash=tonumber(a[4])
end
end
cmd["Da"]=function(a,ts) -- Add:pl:cl:idx:health:online:x:y:...
if a.n<8 then
return
end
local pl=players[tonumber(a[2])]
local cl=d_cl[a[3]]
local idx=tonumber(a[4])
local b=tonumber(a[6])==1
local x,y=tonumber(a[7]),tonumber(a[8])
if not cl then
return
end
local o=cl:new(pl,x,y)
if a[3]=="G" then
if a.n<9 then
return
end
o.pwr=tonumber(a[9])
elseif o.ec then
if a.n<10 then
return
end
o.pkt=tonumber(a[9])
o.ec=tonumber(a[10])
elseif o.maxpkt then
if a.n<9 then
return
end
o.pkt=tonumber(a[9])
end
o.idx=idx
o.health=tonumber(a[5])
o:init_gui()
o.online=b
devices[idx]=o
dhash:add(o)
end
cmd["Dn"]=function(a,ts) -- New:pl:cl:idx:x:y:...
if a.n<6 then
return
end
local pl=players[tonumber(a[2])]
local cl=d_cl[a[3]]
local idx=tonumber(a[4])
local x,y=tonumber(a[5]),tonumber(a[6])
if not cl then
return
end
local o=cl:new(pl,x,y)
if a[3]=="G" then
if a.n<7 then
return
end
o.pwr=tonumber(a[7])
end
o.idx=idx
o:init_gui()
devices[idx]=o
dhash:add(o)
if o.cl=="B" and pl==ME then
main_started()
end
end
cmd["Dm"]=function(a,ts) -- Move:idx:x,y
if a.n<4 then
return
end
local o=devices[tonumber(a[2])]
local x,y=tonumber(a[3]),tonumber(a[4])
o:move(x,y)
end
cmd["Ds"]=function(a,ts) -- Switch:idx:online
if a.n<3 then
return
end
local o=devices[tonumber(a[2])]
local b=tonumber(a[3])==1
o:switch(b)
end
cmd["Du"]=function(a,ts) -- Upgrade:idx:v
if a.n<3 then
return
end
local o=devices[tonumber(a[2])]
o.ec=tonumber(a[3])
end
cmd["Dd"]=function(a,ts) -- Del:idx
if a.n<2 then
return
end
local idx=tonumber(a[2])
local o=devices[idx]
o:delete()
devices[idx]=nil
end
cmd["Th"]=function(a,ts) -- Hit:d1:u2:pkt:health
if a.n<5 then
return
end
local d1=devices[tonumber(a[2])]
local u2=units[tonumber(a[3])]
if d1 and u2 then
d1.pkt=tonumber(a[4])
u2.health=tonumber(a[5])
local s=Shot:new(d1,u2)
shots:add(s)
end
end
cmd["Td"]=function(a,ts) -- Destroy:d1:u2:pkt
if a.n<4 then
return
end
local idx=tonumber(a[3])
local d1=devices[tonumber(a[2])]
local u2=units[idx]
d1.pkt=tonumber(a[4])
u2:delete()
units[idx]=nil
local s=Shot:new(d1,u2)
shots:add(s)
end
cmd["TH"]=function(a,ts) -- Hit:d1:d2:pkt:health
if a.n<5 then
return
end
local d1=devices[tonumber(a[2])]
local d2=devices[tonumber(a[3])]
if d1 and d2 then
d1.pkt=tonumber(a[4])
d2.health=tonumber(a[5])
local s=Shot:new(d1,d2)
shots:add(s)
end
end
cmd["TD"]=function(a,ts) -- Destroy:d1:d2:pkt
if a.n<4 then
return
end
local idx=tonumber(a[3])
local d1=devices[tonumber(a[2])]
local d2=devices[idx]
d1.pkt=tonumber(a[4])
d2:delete()
devices[idx]=nil
local s=Shot:new(d1,d2)
shots:add(s)
end
cmd["Lc"]=function(a,ts) -- Link:dev1:dev2
if a.n<3 then
return
end
local d1=devices[tonumber(a[2])]
local d2=devices[tonumber(a[3])]
d1:link(d2)
end
cmd["Lu"]=function(a,ts) -- Unlink:dev1:dev2
if a.n<3 then
return
end
local d1=devices[tonumber(a[2])]
local d2=devices[tonumber(a[3])]
local l=d1:unlink(d2)
if l then
links:del(l)
end
end
cmd["LU"]=function(a,ts) -- UNLINK:dev1
if a.n<2 then
return
end
local o=devices[tonumber(a[2])]
o:unlink_all()
end
cmd["Pr"]=function(a,ts) -- Routed:d1:d2:pkt:pkt
if a.n<5 then
return
end
local d1=devices[tonumber(a[2])]
local d2=devices[tonumber(a[3])]
if d1 and d2 then
d1.pkt=tonumber(a[4]) or 0
d2.pkt=tonumber(a[5])
local p=Packet:new(d1,d2)
packets:add(p)
end
end
cmd["Ph"]=function(a,ts) -- Routed:d1:d2:pkt:health
if a.n<5 then
return
end
local d1=devices[tonumber(a[2])]
local d2=devices[tonumber(a[3])]
if d1 and d2 then
d1.pkt=tonumber(a[4]) or 0
d2.health=tonumber(a[5])
local p=Packet:new(d1,d2)
packets:add(p)
end
end
cmd["Pc"]=function(a,ts) -- Cash:d1:d2:cash:pkt
if a.n<5 then
return
end
local d1=devices[tonumber(a[2])]
local d2=devices[tonumber(a[3])]
local pl=d2 and d2.pl or nil
if pl then
pl.cash=tonumber(a[4])
d1.pkt=tonumber(a[5])
local p=Packet:new(d1,d2)
packets:add(p)
end
end
cmd["Ua"]=function(a,ts) -- Add:pl:cl:idx:health:pkt:x:y
if a.n<8 then
return
end
local pl=players[tonumber(a[2])]
local cl=u_cl[a[3]]
local idx=tonumber(a[4])
local x,y=tonumber(a[7]),tonumber(a[8])
if not cl then
return
end
local o=cl:new(pl,x,y)
o.idx=idx
o.health=tonumber(a[5])
o.pkt=tonumber(a[6])
units[idx]=o
uhash:add(o)
end
cmd["Un"]=function(a,ts) -- New:pl:cl:idx:x:y:...
if a.n<6 then
return
end
local pl=players[tonumber(a[2])]
local cl=u_cl[a[3]]
local idx=tonumber(a[4])
local x,y=tonumber(a[5]),tonumber(a[6])
if not cl then
return
end
local o=cl:new(pl,x,y)
o.idx=idx
units[idx]=o
uhash:add(o)
end
cmd["Um"]=function(a,ts) -- Move:idx:ts:x:y:x:y
if a.n<7 then
return
end
local o=units[tonumber(a[2])]
local dt=tonumber(a[3])
local x,y=tonumber(a[6]),tonumber(a[7])
o.x=tonumber(a[4])
o.y=tonumber(a[5])
o:move(x,y)
rq_um:add(o,fakets,0.01)
o:step(dt)
end
cmd["Up"]=function(a,ts) -- Move:idx:x,y
if a.n<4 then
return
end
local o=units[tonumber(a[2])]
local x,y=tonumber(a[3]),tonumber(a[4])
uhash:del(o)
o.x=tonumber(a[3])
o.y=tonumber(a[4])
uhash:add(o)
o.vx=nil
o.vy=nil
end
cmd["Ps"]=function(a,ts) -- Support:d1:u2:pkt:pkt
if a.n<5 then
return
end
local d1=devices[tonumber(a[2])]
local u2=units[tonumber(a[3])]
if d1 and u2 then
d1.pkt=tonumber(a[4]) or 0
u2.pkt=tonumber(a[5])
local p=Packet:new(d1,u2)
packets:add(p)
end
end
cmd["Pu"]=function(a,ts) -- Health:d1:u2:pkt:health
if a.n<5 then
return
end
local d1=devices[tonumber(a[2])]
local u2=units[tonumber(a[3])]
if d1 and u2 then
d1.pkt=tonumber(a[4]) or 0
u2.health=tonumber(a[5])
local p=Packet:new(d1,u2)
packets:add(p)
end
end
cmd["Sh"]=function(a,ts) -- Hit:u1:u2:pkt:health
if a.n<5 then
return
end
local u1=units[tonumber(a[2])]
local u2=units[tonumber(a[3])]
if u1 and u2 then
u1.pkt=tonumber(a[4])
u2.health=tonumber(a[5])
local s=Shot:new(u1,u2)
shots:add(s)
end
return
end
cmd["SH"]=function(a,ts) -- Hit:u1:d2:pkt:health
if a.n<5 then
return
end
local u1=units[tonumber(a[2])]
local d2=devices[tonumber(a[3])]
if u1 and d2 then
u1.pkt=tonumber(a[4])
d2.health=tonumber(a[5])
local s=Shot:new(u1,d2)
shots:add(s)
end
end
cmd["Sd"]=function(a,ts) -- Destroy:u1:u2:pkt
if a.n<4 then
return
end
local idx=tonumber(a[3])
local u1=units[tonumber(a[2])]
local u2=units[idx]
u1.pkt=tonumber(a[4])
u2:delete()
units[idx]=nil
local s=Shot:new(u1,u2)
shots:add(s)
end
cmd["SD"]=function(a,ts) -- Destroy:u1:d2:pkt
if a.n<4 then
return
end
local idx=tonumber(a[3])
local u1=units[tonumber(a[2])]
local d2=devices[idx]
u1.pkt=tonumber(a[4])
d2:delete()
devices[idx]=nil
local s=Shot:new(u1,d2)
shots:add(s)
end
local function parse_server(msg,ts)
local a=str_split(msg,":")
local chunk=cmd[a[1]]
if chunk then
chunk(a,ts)
end
end
local dummy={
ACK=true;
PING=true;
ERR=true;
DONE=true;
}
local function parse_replay(msg)
local a=str_split(msg,":")
if dummy[a[1]] then
return
end
local chunk=cmd[a[1]]
if chunk then
chunk(a,0)
end
end
function net_parse(msg,ts)
local p=recvq:get(seq)
local mt
if p then
seq=seq+1
mt=str_split(p,"|")
for _,m in ipairs(mt) do
parse_server(m,ts)
end
end
if msg then
mt=str_split(msg,"|")
for _,m in ipairs(mt) do
parse_server(m,ts)
end
end
end
function rep_parse(msg)
mt=str_split(msg,"|")
for _,m in ipairs(mt) do
parse_replay(m)
end
end
function net_proc()
if net_err then
net_abort()
return
end
local ts=socket.gettime()
local ret=socket.select(allsocks,nil,0)
local msg
if ret[sock] then
msg=net_read(ts)
end
if ts>=timeout then
console.msg("! connection timed out")
net_err="timeout"
return
end
net_parse(msg,ts)
for p in sendq:iter(ts,0.5) do
sock:send(p)
end
if sendq.len>0 then
lastsend=ts+5
end
if ts>=lastsend then
if ts>=lastrecv then
sock:send("PING")
else
sock:send("KEEPALIVE")
end
lastsend=ts+5
end
end
local msg_ts
local rep_dt
local rep_m
local repend=false
function rep_init()
local s=replay()
local t=str_split(s,"@")
init_vars()
msg_ts=tonumber(t[1])
rep_parse(t[2])
s=replay()
t=str_split(s,"@")
local ts=tonumber(t[1])
rep_dt=ts-msg_ts
msg_ts=ts
rep_m=t[2]
love.draw=main_draw
love.update=main_update
love.quit=main_quit
love.keypressed=main_keypressed
love.keyreleased=main_keyreleased
love.mousepressed=main_mousepressed
love.mousereleased=main_mousereleased
end
function rep_proc(dt)
if repend then
return false
end
rep_dt=rep_dt-dt
if rep_dt>0 then
return true
end
rep_parse(rep_m)
local s=replay()
if not s then
repend=true
console.msg("replay has ended.")
return false
end
local t=str_split(s,"@")
local ts=tonumber(t[1])
rep_dt=ts-msg_ts
msg_ts=ts
rep_m=t[2]
return true
end