继续推进联网,实现接入stream和老代码的更新

This commit is contained in:
MrZ626
2021-04-05 20:49:17 +08:00
parent 14302d0106
commit 046a41177f
3 changed files with 56 additions and 50 deletions

View File

@@ -62,8 +62,8 @@ do--Connect
else
res,err=SOCK:receive(ctLen)
if not res then readCHN:push(err)return end
local reason=JSON.decode(res)
readCHN:push((code or"XXX")..":"..(reason and reason.message or"Server Error"))
res=JSON.decode(res)
readCHN:push((code or"XXX")..":"..(res and res.reason or"Server Error"))
end
end
SOCK:settimeout(0)
@@ -175,8 +175,8 @@ while true do--Running
readCHN:push(op)
SOCK:close()
if type(res)=="string"then
local reason=JSON.decode(res)
readCHN:push(reason and reason.message or"WS Error")
res=JSON.decode(res)
readCHN:push(res and res.reason or"WS Error")
else
readCHN:push("WS Error")
end

View File

@@ -5,6 +5,7 @@ local NET={
roomList={},
accessToken=false,
rid=false,
rsid=false,
}
local mesType={
@@ -163,9 +164,9 @@ end
function NET.wsConnectStream()
if _lock("connectStream")then
WS.connect("stream","/stream",JSON.encode{
id=USER.id,
uid=USER.id,
accessToken=NET.accessToken,
rid=NET.rid,
rid=NET.rsid,
})
end
end
@@ -288,7 +289,7 @@ function NET.updateWS_play()
elseif res.action==0 then--Fetch rooms
NET.roomList=res.roomList
_unlock("fetchRoom")
-- elseif res.action==1 then
elseif res.action==1 then--Create room (not used)
elseif res.action==2 then--Player join
if res.type=="Self"then
--Create room
@@ -308,6 +309,7 @@ function NET.updateWS_play()
SCN.socketRead("Ready",res.data)
_unlock("ready")
elseif res.action==7 then--All ready
elseif res.action==8 then--Sure ready
SCN.socketRead("Set",res.data)
end
else
@@ -340,9 +342,13 @@ function NET.updateWS_stream()
SCN.socketRead("Begin",res.data)
elseif res.action==1 then--Game finished
SCN.socketRead("Finish",res.data)
elseif res.action==2 then--Player died
elseif res.action==2 then--Player join
--?
elseif res.action==3 then--Player leave
--?
elseif res.action==4 then--Player died
SCN.socketRead("Die",res.data)
elseif res.action==3 then--Receive stream
elseif res.action==5 then--Receive stream
SCN.socketRead("Stream",res.data)
end
else

View File

@@ -1,4 +1,3 @@
local data=love.data
local gc=love.graphics
local tc=love.touch
@@ -142,19 +141,24 @@ function scene.gamepadUp(key)
end
end
function scene.socketRead(cmd,args)
function scene.socketRead(cmd,data)
if cmd=="Join"then
if playerInitialized then
local L=SPLITSTR(args[1],",")
textBox:push{
COLOR.lR,L[1],
COLOR.dY,"#"..L[2].." ",
COLOR.lR,data.username,
COLOR.dY,"#"..data.uid.." ",
COLOR.Y,text.joinRoom,
}
end
for i=1,#args do
local L=SPLITSTR(args[i],",")
ins(PLY_NET,{name=L[1],id=L[2],sid=L[3],conf=L[4],ready=L[5]=="1"})
local L=data.players
for i=1,#L do
ins(PLY_NET,{
sid=L[i].sid,
uid=L[i].uid,
username=L[i].username,
conf=L[i].config,
ready=L[i].ready,
})
end
playerInitialized=true
SFX.play("click")
@@ -163,88 +167,84 @@ function scene.socketRead(cmd,args)
end
elseif cmd=="Leave"then
textBox:push{
COLOR.lR,args[1],
COLOR.dY,"#"..args[2].." ",
COLOR.lR,data.username,
COLOR.dY,"#"..data.uid.." ",
COLOR.Y,text.leaveRoom,
}
for i=1,#PLY_NET do
if PLY_NET[i].id==args[2]then
if PLY_NET[i].id==data.uid then
rem(PLY_NET,i)
break
end
end
for i=1,#PLAYERS do
if PLAYERS[i].userID==args[2]then
if PLAYERS[i].userID==data.uid then
rem(PLAYERS,i)
break
end
end
for i=1,#PLY_ALIVE do
if PLY_ALIVE[i].userID==args[2]then
if PLY_ALIVE[i].userID==data.uid then
rem(PLY_ALIVE,i)
break
end
end
initPlayerPosition(true)
elseif cmd=="Talk"then
local _,text=pcall(data.decode,"string","base64",args[3])
if not _ then text=args[3]end
textBox:push{
COLOR.W,args[1],
COLOR.dY,"#"..args[2].." ",
COLOR.sky,text
COLOR.W,data.username,
COLOR.dY,"#"..data.uid.." ",
COLOR.sky,data.message or"[_]",
}
elseif cmd=="Config"then
if tostring(USER.id)~=args[2]then
if tostring(USER.id)~=data.uid then
for i=1,#PLY_NET do
if PLY_NET[i].id==args[2]then
PLY_NET[i].conf=args[4]
PLY_NET[i].p:setConf(args[4])
if PLY_NET[i].id==data.uid then
PLY_NET[i].conf=data.config
PLY_NET[i].p:setConf(data.config)
return
end
end
resetGameData("qn")
end
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
local _,stream=pcall(data.decode,"string","base64",args[2])
if _ then
pumpRecording(stream,P.stream)
else
LOG.print("Bad stream from "..P.userName.."#"..P.userID)
end
end
end
end
elseif cmd=="Ready"then
local L=PLY_ALIVE
for i=1,#L do
if L[i].subID==args[1]then
if L[i].subID==data.uid then
L[i].ready=true
SFX.play("reach",.6)
break
end
end
elseif cmd=="Set"then
NET.rsid=data.rid
NET.wsConnectStream()
elseif cmd=="Begin"then
if not playing then
playing=true
lastUpstreamTime=0
upstreamProgress=1
resetGameData("n",tonumber(args[1]))
resetGameData("n",data.seed)
else
LOG.print("Redundant signal: B(begin)",30,COLOR.green)
end
elseif cmd=="Finish"then
playing=false
resetGameData("n")
for i=1,#PLY_NET do
if PLY_NET[i].sid==args[1]then
TEXT.show(text.champion:gsub("$1",PLY_NET[i].name.."#"..PLY_NET[i].id),640,260,80,"zoomout",.26)
break
TEXT.show(text.champion:gsub("$1","SOMEBODY"),640,260,80,"zoomout",.26)
elseif cmd=="Die"then
LOG.print("One player failed",COLOR.sky)
elseif cmd=="Stream"then
if playing and data.uid~=PLAYERS[1].subID then
for _,P in next,PLAYERS do
if P.subID==data.uid then
local res,stream=pcall(love.data.decode,"string","base64",data.stream)
if res then
pumpRecording(stream,P.stream)
else
LOG.print("Bad stream from "..P.userName.."#"..P.userID)
end
end
end
end
end