整理代码
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
local data=love.data
|
local loveCompress=love.data.compress
|
||||||
|
|
||||||
local int=math.floor
|
local int=math.floor
|
||||||
local char,byte=string.char,string.byte
|
local char,byte=string.char,string.byte
|
||||||
@@ -371,7 +371,7 @@ do--function DATA.saveRecording()
|
|||||||
JSON.encode(getModList()).."\n"..
|
JSON.encode(getModList()).."\n"..
|
||||||
DATA.dumpRecording(GAME.rep)
|
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)
|
ins(REPLAY,fileName)
|
||||||
FILE.save(REPLAY,'conf/replay')
|
FILE.save(REPLAY,'conf/replay')
|
||||||
return true
|
return true
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
local data=love.data
|
local loveEncode,loveDecode=love.data.encode,love.data.decode
|
||||||
local rem=table.remove
|
local rem=table.remove
|
||||||
|
|
||||||
local WS,TIME=WS,TIME
|
local WS,TIME=WS,TIME
|
||||||
@@ -66,6 +66,11 @@ function NET.getlock(name)
|
|||||||
return TIME()<locks[name]
|
return TIME()<locks[name]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--Pong back
|
||||||
|
local function _pong(wsName,message)
|
||||||
|
WS.send(wsName,message or"",'pong')
|
||||||
|
end
|
||||||
|
|
||||||
--Parse json message
|
--Parse json message
|
||||||
local function _parse(res)
|
local function _parse(res)
|
||||||
res=JSON.decode(res)
|
res=JSON.decode(res)
|
||||||
@@ -74,10 +79,12 @@ local function _parse(res)
|
|||||||
return res
|
return res
|
||||||
else
|
else
|
||||||
LOG.print(
|
LOG.print(
|
||||||
"WS error:"..(
|
"Error ws-mes type:"..(
|
||||||
res.type and(
|
res.type and(
|
||||||
res.reason and res.type..": "..res.reason or
|
res.reason and
|
||||||
res.type
|
res.type..": "..res.reason
|
||||||
|
or
|
||||||
|
res.type
|
||||||
)or
|
)or
|
||||||
"[NO Message]"
|
"[NO Message]"
|
||||||
),
|
),
|
||||||
@@ -87,7 +94,7 @@ local function _parse(res)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--WS close message
|
--WS close message
|
||||||
local function wsCloseMessage(message)
|
local function _closeMessage(message)
|
||||||
local mes=JSON.decode(message:sub(3))
|
local mes=JSON.decode(message:sub(3))
|
||||||
if mes then
|
if mes then
|
||||||
LOG.print(("%s [%s] %s"):format(text.wsClose,mes.type or"unknown type",mes.reason or""),'error')
|
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
|
if d.uid~=USER.uid then
|
||||||
for _,P in next,PLAYERS do
|
for _,P in next,PLAYERS do
|
||||||
if P.uid==d.uid then
|
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
|
if res then
|
||||||
DATA.pumpRecording(stream,P.stream)
|
DATA.pumpRecording(stream,P.stream)
|
||||||
else
|
else
|
||||||
@@ -215,9 +222,6 @@ function NET.tryLogin(ifAuto)
|
|||||||
SFX.play('finesseError')
|
SFX.play('finesseError')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
function NET.pong(wsName,message)
|
|
||||||
WS.send(wsName,type(message)=='string'and message or"",'pong')
|
|
||||||
end
|
|
||||||
function NET.getAccessToken()
|
function NET.getAccessToken()
|
||||||
if NET.lock('access_and_login',10)then
|
if NET.lock('access_and_login',10)then
|
||||||
WS.send('user',JSON.encode{action=0})
|
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}}')
|
WS.send('stream','{"action":4,"data":{"score":0,"survivalTime":0}}')
|
||||||
end
|
end
|
||||||
function NET.uploadRecStream(stream)
|
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
|
end
|
||||||
|
|
||||||
--Chat
|
--Chat
|
||||||
function NET.sendChatMes(mes)
|
function NET.sendChatMes(mes)
|
||||||
WS.send('chat',"T"..data.encode('string','base64',mes))
|
WS.send('chat',"T"..loveEncode('string','base64',mes))
|
||||||
end
|
end
|
||||||
function NET.quitChat()
|
function NET.quitChat()
|
||||||
WS.send('chat','q')
|
WS.send('chat','q')
|
||||||
@@ -392,10 +396,10 @@ function NET.updateWS_app()
|
|||||||
local message,op=WS.read('app')
|
local message,op=WS.read('app')
|
||||||
if message then
|
if message then
|
||||||
if op=='ping'then
|
if op=='ping'then
|
||||||
NET.pong('app',message)
|
_pong('app',message)
|
||||||
elseif op=='pong'then
|
elseif op=='pong'then
|
||||||
elseif op=='close'then
|
elseif op=='close'then
|
||||||
wsCloseMessage(message)
|
_closeMessage(message)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
local res=_parse(message)
|
local res=_parse(message)
|
||||||
@@ -447,10 +451,10 @@ function NET.updateWS_user()
|
|||||||
local message,op=WS.read('user')
|
local message,op=WS.read('user')
|
||||||
if message then
|
if message then
|
||||||
if op=='ping'then
|
if op=='ping'then
|
||||||
NET.pong('user',message)
|
_pong('user',message)
|
||||||
elseif op=='pong'then
|
elseif op=='pong'then
|
||||||
elseif op=='close'then
|
elseif op=='close'then
|
||||||
wsCloseMessage(message)
|
_closeMessage(message)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
local res=_parse(message)
|
local res=_parse(message)
|
||||||
@@ -494,10 +498,10 @@ function NET.updateWS_play()
|
|||||||
local message,op=WS.read('play')
|
local message,op=WS.read('play')
|
||||||
if message then
|
if message then
|
||||||
if op=='ping'then
|
if op=='ping'then
|
||||||
NET.pong('play',message)
|
_pong('play',message)
|
||||||
elseif op=='pong'then
|
elseif op=='pong'then
|
||||||
elseif op=='close'then
|
elseif op=='close'then
|
||||||
wsCloseMessage(message)
|
_closeMessage(message)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
local res=_parse(message)
|
local res=_parse(message)
|
||||||
@@ -566,8 +570,7 @@ function NET.updateWS_play()
|
|||||||
if SCN.stack[#SCN.stack-1]=='net_newRoom'then SCN.pop()end
|
if SCN.stack[#SCN.stack-1]=='net_newRoom'then SCN.pop()end
|
||||||
SCN.back()
|
SCN.back()
|
||||||
else
|
else
|
||||||
removePlayer(netPLY.list,d.sid)
|
netPLY.remove(d.sid)
|
||||||
netPLY.freshPos()
|
|
||||||
removePlayer(PLAYERS,d.sid)
|
removePlayer(PLAYERS,d.sid)
|
||||||
removePlayer(PLY_ALIVE,d.sid)
|
removePlayer(PLY_ALIVE,d.sid)
|
||||||
if SCN.socketRead then SCN.socketRead('leave',d)end
|
if SCN.socketRead then SCN.socketRead('leave',d)end
|
||||||
@@ -619,10 +622,10 @@ function NET.updateWS_stream()
|
|||||||
local message,op=WS.read('stream')
|
local message,op=WS.read('stream')
|
||||||
if message then
|
if message then
|
||||||
if op=='ping'then
|
if op=='ping'then
|
||||||
NET.pong('stream',message)
|
_pong('stream',message)
|
||||||
elseif op=='pong'then
|
elseif op=='pong'then
|
||||||
elseif op=='close'then
|
elseif op=='close'then
|
||||||
wsCloseMessage(message)
|
_closeMessage(message)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
local res=_parse(message)
|
local res=_parse(message)
|
||||||
@@ -690,10 +693,10 @@ function NET.updateWS_chat()
|
|||||||
local message,op=WS.read('chat')
|
local message,op=WS.read('chat')
|
||||||
if message then
|
if message then
|
||||||
if op=='ping'then
|
if op=='ping'then
|
||||||
NET.pong('chat',message)
|
_pong('chat',message)
|
||||||
elseif op=='pong'then
|
elseif op=='pong'then
|
||||||
elseif op=='close'then
|
elseif op=='close'then
|
||||||
wsCloseMessage(message)
|
_closeMessage(message)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
local res=_parse(message)
|
local res=_parse(message)
|
||||||
@@ -712,10 +715,10 @@ function NET.updateWS_manage()
|
|||||||
local message,op=WS.read('manage')
|
local message,op=WS.read('manage')
|
||||||
if message then
|
if message then
|
||||||
if op=='ping'then
|
if op=='ping'then
|
||||||
NET.pong('manage',message)
|
_pong('manage',message)
|
||||||
elseif op=='pong'then
|
elseif op=='pong'then
|
||||||
elseif op=='close'then
|
elseif op=='close'then
|
||||||
wsCloseMessage(message)
|
_closeMessage(message)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
local res=_parse(message)
|
local res=_parse(message)
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ local gc_stencil,gc_setStencilTest=gc.stencil,gc.setStencilTest
|
|||||||
|
|
||||||
local rnd,min=math.random,math.min
|
local rnd,min=math.random,math.min
|
||||||
local sin,cos=math.sin,math.cos
|
local sin,cos=math.sin,math.cos
|
||||||
local ins=table.insert
|
local ins,rem=table.insert,table.remove
|
||||||
local setFont=setFont
|
local setFont=setFont
|
||||||
|
|
||||||
local posLists={
|
local posLists={
|
||||||
@@ -66,7 +66,7 @@ local posList
|
|||||||
local function _placeSort(a,b)return a.place<b.place end
|
local function _placeSort(a,b)return a.place<b.place end
|
||||||
|
|
||||||
local PLYlist,PLYmap={},{}
|
local PLYlist,PLYmap={},{}
|
||||||
local function freshPosList()
|
local function freshPos()
|
||||||
table.sort(PLYlist,_placeSort)
|
table.sort(PLYlist,_placeSort)
|
||||||
if #PLYlist<=5 then
|
if #PLYlist<=5 then
|
||||||
posList=posLists[1]
|
posList=posLists[1]
|
||||||
@@ -83,7 +83,7 @@ end
|
|||||||
local netPLY={
|
local netPLY={
|
||||||
list=PLYlist,
|
list=PLYlist,
|
||||||
map=PLYmap,
|
map=PLYmap,
|
||||||
freshPos=freshPosList,
|
freshPos=freshPos,
|
||||||
}
|
}
|
||||||
|
|
||||||
function netPLY.clear()
|
function netPLY.clear()
|
||||||
@@ -99,7 +99,17 @@ function netPLY.add(p)
|
|||||||
|
|
||||||
ins(PLYlist,p)
|
ins(PLYlist,p)
|
||||||
PLYmap[p.uid]=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
|
end
|
||||||
|
|
||||||
function netPLY.getCount()return #PLYlist end
|
function netPLY.getCount()return #PLYlist end
|
||||||
|
|||||||
Reference in New Issue
Block a user