整理代码

This commit is contained in:
MrZ626
2021-06-09 09:02:11 +08:00
parent 21834a357d
commit 6bd4895255
3 changed files with 44 additions and 31 deletions

View File

@@ -1,4 +1,4 @@
local data=love.data
local loveCompress=love.data.compress
local int=math.floor
local char,byte=string.char,string.byte
@@ -371,7 +371,7 @@ do--function DATA.saveRecording()
JSON.encode(getModList()).."\n"..
DATA.dumpRecording(GAME.rep)
love.filesystem.write(fileName,fileHead.."\n"..data.compress('string','zlib',fileBody))
love.filesystem.write(fileName,fileHead.."\n"..loveCompress('string','zlib',fileBody))
ins(REPLAY,fileName)
FILE.save(REPLAY,'conf/replay')
return true

View File

@@ -1,4 +1,4 @@
local data=love.data
local loveEncode,loveDecode=love.data.encode,love.data.decode
local rem=table.remove
local WS,TIME=WS,TIME
@@ -66,6 +66,11 @@ function NET.getlock(name)
return TIME()<locks[name]
end
--Pong back
local function _pong(wsName,message)
WS.send(wsName,message or"",'pong')
end
--Parse json message
local function _parse(res)
res=JSON.decode(res)
@@ -74,10 +79,12 @@ local function _parse(res)
return res
else
LOG.print(
"WS error:"..(
"Error ws-mes type:"..(
res.type and(
res.reason and res.type..": "..res.reason or
res.type
res.reason and
res.type..": "..res.reason
or
res.type
)or
"[NO Message]"
),
@@ -87,7 +94,7 @@ local function _parse(res)
end
--WS close message
local function wsCloseMessage(message)
local function _closeMessage(message)
local mes=JSON.decode(message:sub(3))
if mes then
LOG.print(("%s [%s] %s"):format(text.wsClose,mes.type or"unknown type",mes.reason or""),'error')
@@ -111,7 +118,7 @@ local function pumpStream(d)
if d.uid~=USER.uid then
for _,P in next,PLAYERS do
if P.uid==d.uid then
local res,stream=pcall(love.data.decode,'string','base64',d.stream)
local res,stream=pcall(loveDecode,'string','base64',d.stream)
if res then
DATA.pumpRecording(stream,P.stream)
else
@@ -215,9 +222,6 @@ function NET.tryLogin(ifAuto)
SFX.play('finesseError')
end
end
function NET.pong(wsName,message)
WS.send(wsName,type(message)=='string'and message or"",'pong')
end
function NET.getAccessToken()
if NET.lock('access_and_login',10)then
WS.send('user',JSON.encode{action=0})
@@ -368,12 +372,12 @@ function NET.signal_die()
WS.send('stream','{"action":4,"data":{"score":0,"survivalTime":0}}')
end
function NET.uploadRecStream(stream)
WS.send('stream','{"action":5,"data":{"stream":"'..data.encode('string','base64',stream)..'"}}')
WS.send('stream','{"action":5,"data":{"stream":"'..loveEncode('string','base64',stream)..'"}}')
end
--Chat
function NET.sendChatMes(mes)
WS.send('chat',"T"..data.encode('string','base64',mes))
WS.send('chat',"T"..loveEncode('string','base64',mes))
end
function NET.quitChat()
WS.send('chat','q')
@@ -392,10 +396,10 @@ function NET.updateWS_app()
local message,op=WS.read('app')
if message then
if op=='ping'then
NET.pong('app',message)
_pong('app',message)
elseif op=='pong'then
elseif op=='close'then
wsCloseMessage(message)
_closeMessage(message)
return
else
local res=_parse(message)
@@ -447,10 +451,10 @@ function NET.updateWS_user()
local message,op=WS.read('user')
if message then
if op=='ping'then
NET.pong('user',message)
_pong('user',message)
elseif op=='pong'then
elseif op=='close'then
wsCloseMessage(message)
_closeMessage(message)
return
else
local res=_parse(message)
@@ -494,10 +498,10 @@ function NET.updateWS_play()
local message,op=WS.read('play')
if message then
if op=='ping'then
NET.pong('play',message)
_pong('play',message)
elseif op=='pong'then
elseif op=='close'then
wsCloseMessage(message)
_closeMessage(message)
return
else
local res=_parse(message)
@@ -566,8 +570,7 @@ function NET.updateWS_play()
if SCN.stack[#SCN.stack-1]=='net_newRoom'then SCN.pop()end
SCN.back()
else
removePlayer(netPLY.list,d.sid)
netPLY.freshPos()
netPLY.remove(d.sid)
removePlayer(PLAYERS,d.sid)
removePlayer(PLY_ALIVE,d.sid)
if SCN.socketRead then SCN.socketRead('leave',d)end
@@ -619,10 +622,10 @@ function NET.updateWS_stream()
local message,op=WS.read('stream')
if message then
if op=='ping'then
NET.pong('stream',message)
_pong('stream',message)
elseif op=='pong'then
elseif op=='close'then
wsCloseMessage(message)
_closeMessage(message)
return
else
local res=_parse(message)
@@ -690,10 +693,10 @@ function NET.updateWS_chat()
local message,op=WS.read('chat')
if message then
if op=='ping'then
NET.pong('chat',message)
_pong('chat',message)
elseif op=='pong'then
elseif op=='close'then
wsCloseMessage(message)
_closeMessage(message)
return
else
local res=_parse(message)
@@ -712,10 +715,10 @@ function NET.updateWS_manage()
local message,op=WS.read('manage')
if message then
if op=='ping'then
NET.pong('manage',message)
_pong('manage',message)
elseif op=='pong'then
elseif op=='close'then
wsCloseMessage(message)
_closeMessage(message)
return
else
local res=_parse(message)

View File

@@ -5,7 +5,7 @@ local gc_stencil,gc_setStencilTest=gc.stencil,gc.setStencilTest
local rnd,min=math.random,math.min
local sin,cos=math.sin,math.cos
local ins=table.insert
local ins,rem=table.insert,table.remove
local setFont=setFont
local posLists={
@@ -66,7 +66,7 @@ local posList
local function _placeSort(a,b)return a.place<b.place end
local PLYlist,PLYmap={},{}
local function freshPosList()
local function freshPos()
table.sort(PLYlist,_placeSort)
if #PLYlist<=5 then
posList=posLists[1]
@@ -83,7 +83,7 @@ end
local netPLY={
list=PLYlist,
map=PLYmap,
freshPos=freshPosList,
freshPos=freshPos,
}
function netPLY.clear()
@@ -99,7 +99,17 @@ function netPLY.add(p)
ins(PLYlist,p)
PLYmap[p.uid]=p
freshPosList()
freshPos()
end
function netPLY.remove(sid)
for i=1,#PLYlist do
if PLYlist[i].sid==sid then
PLYmap[PLYlist[i].sid]=nil
rem(PLYlist,i)
freshPos()
break
end
end
end
function netPLY.getCount()return #PLYlist end