增加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={
OK=true,
Connected=true,
Server=true,
Self=true,
Broadcast=true,
Private=true,
Server=true,
}
--Lock & Unlock submodule
@@ -34,15 +35,17 @@ end
local function _parse(res)
res=JSON.decode(res)
if res then
if mesType[res.message]then
if mesType[res.type]then
return res
else
LOG.print(
res.message and(
res.reason and res.message..": "..res.reason or
res.message
)or
"[NO Message]",
"WS error:"..(
res.type and(
res.reason and res.type..": "..res.reason or
res.type
)or
"[NO Message]"
),
"warning")
end
end
@@ -52,9 +55,9 @@ end
function NET.wsCloseMessage(message)
if message:sub(1,1)=="{"then
local mes=JSON.decode(message)
LOG.print(text.wsClose..mes.message,"warn")
LOG.print(text.wsClose..mes.type,"warn")
else
LOG.print(text.wsClose..message,"warn")
LOG.print(text.wsClose..type,"warn")
end
end
@@ -109,16 +112,19 @@ function NET.wsConnectPlay()
end
end
function NET.signal_ready()
WS.send("play","R")
end
function NET.uploadRecStream(stream)
WS.send("stream",data.encode("string","base64",stream))
end
function NET.signal_die()
WS.send("play","D")
if _lock("ready")then
WS.send("play",'{"action":6,"data":{"ready":true}}')
end
end
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
--Room
@@ -222,7 +228,7 @@ function NET.TICK_WS_user()
else
local res=_parse(message)
if res then
if res.message=="Connected"then
if res.type=="Connected"then
NET.login=true
if res.id then
USER.id=res.id
@@ -266,17 +272,31 @@ function NET.TICK_WS_play()
else
local res=_parse(message)
if res then
if res.message=="Connected"then
_unlock("connectPlay")
if res.type=="Connected"then
SCN.go("net_menu")
_unlock("connectPlay")
elseif res.action==0 then--Fetch rooms
NET.roomList=res.roomList
_unlock("fetchRoom")
elseif res.action==2 then--Join(create) room
loadGame("netBattle",true,true)
_unlock("enterRoom")
elseif res.action==3 then--Leave room
SCN.back()
-- elseif res.action==1 then
elseif res.action==2 then--Player join
if res.type=="Self"then
--Create room
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
else
WS.alert("play")
@@ -300,7 +320,26 @@ function NET.TICK_WS_stream()
NET.wsCloseMessage(message)
return
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

View File

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