增加mesType,新的对战用消息格式(未完成,待测试)

This commit is contained in:
MrZ626
2021-04-03 00:54:29 +08:00
parent 1f8895257d
commit 4170965c64
2 changed files with 76 additions and 41 deletions

View File

@@ -7,10 +7,11 @@ local NET={
} }
local mesType={ local mesType={
OK=true,
Connected=true, Connected=true,
Server=true, Self=true,
Broadcast=true, Broadcast=true,
Private=true,
Server=true,
} }
--Lock & Unlock submodule --Lock & Unlock submodule
@@ -34,15 +35,17 @@ end
local function _parse(res) local function _parse(res)
res=JSON.decode(res) res=JSON.decode(res)
if res then if res then
if mesType[res.message]then if mesType[res.type]then
return res return res
else else
LOG.print( LOG.print(
res.message and( "WS error:"..(
res.reason and res.message..": "..res.reason or res.type and(
res.message res.reason and res.type..": "..res.reason or
)or res.type
"[NO Message]", )or
"[NO Message]"
),
"warning") "warning")
end end
end end
@@ -52,9 +55,9 @@ end
function NET.wsCloseMessage(message) function NET.wsCloseMessage(message)
if message:sub(1,1)=="{"then if message:sub(1,1)=="{"then
local mes=JSON.decode(message) local mes=JSON.decode(message)
LOG.print(text.wsClose..mes.message,"warn") LOG.print(text.wsClose..mes.type,"warn")
else else
LOG.print(text.wsClose..message,"warn") LOG.print(text.wsClose..type,"warn")
end end
end end
@@ -109,16 +112,19 @@ function NET.wsConnectPlay()
end end
end end
function NET.signal_ready() function NET.signal_ready()
WS.send("play","R") if _lock("ready")then
end WS.send("play",'{"action":6,"data":{"ready":true}}')
function NET.uploadRecStream(stream) end
WS.send("stream",data.encode("string","base64",stream))
end
function NET.signal_die()
WS.send("play","D")
end end
function NET.signal_quit() function NET.signal_quit()
WS.send("play","Q") WS.send("play",'{"action":3}')
end
function NET.uploadRecStream(stream)
stream=data.encode("string","base64",stream)
WS.send("stream",'{"action":2,"data":{"stream":"'..stream..'"}}')
end
function NET.signal_die()
WS.send("stream",'{"action":3,"data":{"score":0,"survivalTime":0}}')
end end
--Room --Room
@@ -222,7 +228,7 @@ function NET.TICK_WS_user()
else else
local res=_parse(message) local res=_parse(message)
if res then if res then
if res.message=="Connected"then if res.type=="Connected"then
NET.login=true NET.login=true
if res.id then if res.id then
USER.id=res.id USER.id=res.id
@@ -266,17 +272,31 @@ function NET.TICK_WS_play()
else else
local res=_parse(message) local res=_parse(message)
if res then if res then
if res.message=="Connected"then if res.type=="Connected"then
_unlock("connectPlay")
SCN.go("net_menu") SCN.go("net_menu")
_unlock("connectPlay")
elseif res.action==0 then--Fetch rooms elseif res.action==0 then--Fetch rooms
NET.roomList=res.roomList NET.roomList=res.roomList
_unlock("fetchRoom") _unlock("fetchRoom")
elseif res.action==2 then--Join(create) room -- elseif res.action==1 then
loadGame("netBattle",true,true) elseif res.action==2 then--Player join
_unlock("enterRoom") if res.type=="Self"then
elseif res.action==3 then--Leave room --Create room
SCN.back() loadGame("netBattle",true,true)
_unlock("enterRoom")
else
--Others join room
SCN.socketRead("Join",res.data)
end
elseif res.action==3 then--Player leave
SCN.socketRead("Leave",res.data)
elseif res.action==4 then--Player talk
SCN.socketRead("Talk",res.data)
elseif res.action==5 then--Player change settings
SCN.socketRead("Config",res.data)
elseif res.action==6 then--Player ready
SCN.socketRead("Ready",res.data)
_unlock("ready")
end end
else else
WS.alert("play") WS.alert("play")
@@ -300,7 +320,26 @@ function NET.TICK_WS_stream()
NET.wsCloseMessage(message) NET.wsCloseMessage(message)
return return
else else
--TODO local res=_parse(message)
if res then
if res.type=="Connected"then
--?
elseif res.action==0 then--Game start
SCN.socketRead("Begin",res.data)
elseif res.action==1 then--Game finished
SCN.socketRead("Finish",res.data)
elseif res.action==2 then--Player join
SCN.socketRead("J",res.data)
elseif res.action==3 then--Player leave
SCN.socketRead("L",res.data)
elseif res.action==4 then--Player died
SCN.socketRead("Die",res.data)
elseif res.action==5 then--Receive stream
SCN.socketRead("S",res.data)
end
else
WS.alert("stream")
end
end end
end end
end end

View File

@@ -102,7 +102,7 @@ function scene.keyDown(key)
VK[k].pressTime=10 VK[k].pressTime=10
end end
elseif key=="space"then elseif key=="space"then
if not PLAYERS[1].ready then if not NET.getLock("ready")then
NET.signal_ready() NET.signal_ready()
end end
end end
@@ -143,10 +143,8 @@ function scene.gamepadUp(key)
end end
end end
function scene.socketRead(mes) function scene.socketRead(cmd,args)
local cmd=mes:sub(1,1) if cmd=="Join"then
local args=SPLITSTR(mes:sub(2),";")
if cmd=="J"then
if playerInitialized then if playerInitialized then
local L=SPLITSTR(args[1],",") local L=SPLITSTR(args[1],",")
textBox:push{ textBox:push{
@@ -164,7 +162,7 @@ function scene.socketRead(mes)
if not playing then if not playing then
resetGameData("qn") resetGameData("qn")
end end
elseif cmd=="L"then elseif cmd=="Leave"then
textBox:push{ textBox:push{
COLOR.lR,args[1], COLOR.lR,args[1],
COLOR.dY,"#"..args[2].." ", COLOR.dY,"#"..args[2].." ",
@@ -189,7 +187,7 @@ function scene.socketRead(mes)
end end
end end
initPlayerPosition(true) initPlayerPosition(true)
elseif cmd=="T"then elseif cmd=="Talk"then
local _,text=pcall(data.decode,"string","base64",args[3]) local _,text=pcall(data.decode,"string","base64",args[3])
if not _ then text=args[3]end if not _ then text=args[3]end
textBox:push{ textBox:push{
@@ -197,7 +195,7 @@ function scene.socketRead(mes)
COLOR.dY,"#"..args[2].." ", COLOR.dY,"#"..args[2].." ",
COLOR.sky,text COLOR.sky,text
} }
elseif cmd=="C"then elseif cmd=="Config"then
if tostring(USER.id)~=args[2]then if tostring(USER.id)~=args[2]then
for i=1,#PLY_NET do for i=1,#PLY_NET do
if PLY_NET[i].id==args[2]then if PLY_NET[i].id==args[2]then
@@ -208,7 +206,7 @@ function scene.socketRead(mes)
end end
resetGameData("qn") resetGameData("qn")
end end
elseif cmd=="S"then elseif cmd=="Stream"then
if playing and args[1]~=PLAYERS[1].subID then if playing and args[1]~=PLAYERS[1].subID then
for _,P in next,PLAYERS do for _,P in next,PLAYERS do
if P.subID==args[1]then if P.subID==args[1]then
@@ -221,7 +219,7 @@ function scene.socketRead(mes)
end end
end end
end end
elseif cmd=="R"then elseif cmd=="Ready"then
local L=PLY_ALIVE local L=PLY_ALIVE
for i=1,#L do for i=1,#L do
if L[i].subID==args[1]then if L[i].subID==args[1]then
@@ -230,7 +228,7 @@ function scene.socketRead(mes)
break break
end end
end end
elseif cmd=="B"then elseif cmd=="Begin"then
if not playing then if not playing then
playing=true playing=true
lastUpstreamTime=0 lastUpstreamTime=0
@@ -239,7 +237,7 @@ function scene.socketRead(mes)
else else
LOG.print("Redundant signal: B(begin)",30,COLOR.green) LOG.print("Redundant signal: B(begin)",30,COLOR.green)
end end
elseif cmd=="F"then elseif cmd=="Finish"then
playing=false playing=false
resetGameData("n") resetGameData("n")
for i=1,#PLY_NET do for i=1,#PLY_NET do
@@ -248,8 +246,6 @@ function scene.socketRead(mes)
break break
end end
end end
else
LOG.print("Illegal message: ["..mes.."]",30,COLOR.green)
end end
end end