From 046a41177f787f52526dc0ddaa1036999fa439a0 Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Mon, 5 Apr 2021 20:49:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E6=8E=A8=E8=BF=9B=E8=81=94?= =?UTF-8?q?=E7=BD=91=EF=BC=8C=E5=AE=9E=E7=8E=B0=E6=8E=A5=E5=85=A5stream?= =?UTF-8?q?=E5=92=8C=E8=80=81=E4=BB=A3=E7=A0=81=E7=9A=84=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zframework/websocket.lua | 8 ++-- parts/net.lua | 16 +++++--- parts/scenes/net_game.lua | 82 +++++++++++++++++++-------------------- 3 files changed, 56 insertions(+), 50 deletions(-) diff --git a/Zframework/websocket.lua b/Zframework/websocket.lua index 114c92ba..a0f30002 100644 --- a/Zframework/websocket.lua +++ b/Zframework/websocket.lua @@ -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 diff --git a/parts/net.lua b/parts/net.lua index 904dc6b1..c4086d41 100644 --- a/parts/net.lua +++ b/parts/net.lua @@ -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 diff --git a/parts/scenes/net_game.lua b/parts/scenes/net_game.lua index eee3a317..4a0690d1 100644 --- a/parts/scenes/net_game.lua +++ b/parts/scenes/net_game.lua @@ -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