From 8a9adb5c989133c2c1dfa4d85206bc4f7842f788 Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Thu, 8 Apr 2021 18:18:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E4=B8=80=E4=BA=9B=E6=B8=B8=E6=88=8F?= =?UTF-8?q?=E7=9B=B8=E5=85=B3socket=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86?= =?UTF-8?q?=E7=A7=BB=E8=87=B3=E5=9C=BA=E6=99=AF=E5=A4=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- parts/net.lua | 45 ++++++++++++++++++++------ parts/scenes/net_game.lua | 66 ++++++++++----------------------------- 2 files changed, 52 insertions(+), 59 deletions(-) diff --git a/parts/net.lua b/parts/net.lua index b16fe645..05d574fa 100644 --- a/parts/net.lua +++ b/parts/net.lua @@ -318,6 +318,7 @@ function NET.updateWS_play() else local res=_parse(message) if res then + local d=res.data if res.type=="Connect"then SCN.go("net_menu") _unlock("wsc_play") @@ -327,7 +328,6 @@ function NET.updateWS_play() elseif res.action==1 then--Create room (not used) --? elseif res.action==2 then--Player join - local d=res.data if res.type=="Self"then --Create room TABLE.clear(PLY_NET) @@ -363,7 +363,6 @@ function NET.updateWS_play() SCN.socketRead("Join",res.data) end elseif res.action==3 then--Player leave - local d=res.data for i=1,#PLY_NET do if PLY_NET[i].uid==d.uid then rem(PLY_NET,i) @@ -386,14 +385,40 @@ function NET.updateWS_play() 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") - elseif res.action==7 then--All ready + if tostring(USER.uid)~=d.uid then + for i=1,#PLY_NET do + if PLY_NET[i].uid==d.uid then + PLY_NET[i].conf=d.config + PLY_NET[i].p:setConf(d.config) + return + end + end + resetGameData("qn") + end + elseif res.action==6 then--One ready + if d.uid==USER.uid then + if PLAYERS[1].ready~=d.ready then + PLAYERS[1].ready=d.ready + SFX.play("reach",.6) + end + _unlock("ready") + else + for i=1,#PLAYERS do + if PLAYERS[i].userID==d.uid then + if PLAYERS[i].ready~=d.ready then + PLAYERS[i].ready=d.ready + SFX.play("reach",.6) + end + break + end + end + end + elseif res.action==7 then--Ready --? - elseif res.action==8 then--Sure ready - SCN.socketRead("Set",res.data) + elseif res.action==8 then--Set + NET.rsid=d.rid + NET.wsconn_stream() + TASK.new(NET.updateWS_stream) elseif res.action==9 then--Game finished SCN.socketRead("Finish",res.data) NET.wsclose_stream() @@ -425,7 +450,7 @@ function NET.updateWS_stream() if res.type=="Connect"then _unlock("wsc_stream") elseif res.action==0 then--Game start - SCN.socketRead("Begin",res.data) + SCN.socketRead("Go",res.data) elseif res.action==1 then--Game finished --? elseif res.action==2 then--Player join diff --git a/parts/scenes/net_game.lua b/parts/scenes/net_game.lua index ba1a459b..d17fecc1 100644 --- a/parts/scenes/net_game.lua +++ b/parts/scenes/net_game.lua @@ -135,11 +135,11 @@ function scene.gamepadUp(key) end end -function scene.socketRead(cmd,data) +function scene.socketRead(cmd,d) if cmd=="Join"then textBox:push{ - COLOR.lR,data.username, - COLOR.dY,"#"..data.uid.." ", + COLOR.lR,d.username, + COLOR.dY,"#"..d.uid.." ", COLOR.Y,text.joinRoom, } SFX.play("click") @@ -148,8 +148,8 @@ function scene.socketRead(cmd,data) end elseif cmd=="Leave"then textBox:push{ - COLOR.lR,data.username, - COLOR.dY,"#"..data.uid.." ", + COLOR.lR,d.username, + COLOR.dY,"#"..d.uid.." ", COLOR.Y,text.leaveRoom, } if not playing then @@ -157,48 +157,16 @@ function scene.socketRead(cmd,data) end elseif cmd=="Talk"then textBox:push{ - COLOR.W,data.username, - COLOR.dY,"#"..data.uid.." ", - COLOR.sky,data.message or"[_]", + COLOR.W,d.username, + COLOR.dY,"#"..d.uid.." ", + COLOR.sky,d.message or"[_]", } - elseif cmd=="Config"then - if tostring(USER.uid)~=data.uid then - for i=1,#PLY_NET do - if PLY_NET[i].uid==data.uid then - PLY_NET[i].conf=data.config - PLY_NET[i].p:setConf(data.config) - return - end - end - resetGameData("qn") - end - elseif cmd=="Ready"then - if data.uid==USER.uid then - if PLAYERS[1].ready~=data.ready then - PLAYERS[1].ready=data.ready - SFX.play("reach",.6) - end - else - for i=1,#PLAYERS do - if PLAYERS[i].userID==data.uid then - if PLAYERS[i].ready~=data.ready then - PLAYERS[i].ready=data.ready - SFX.play("reach",.6) - end - break - end - end - end - elseif cmd=="Set"then - NET.rsid=data.rid - NET.wsconn_stream() - TASK.new(NET.updateWS_stream) - elseif cmd=="Begin"then + elseif cmd=="Go"then if not playing then playing=true lastUpstreamTime=0 upstreamProgress=1 - resetGameData("n",data.seed) + resetGameData("n",d.seed) else LOG.print("Redundant signal: Begin",30,COLOR.green) end @@ -206,24 +174,24 @@ function scene.socketRead(cmd,data) playing=false resetGameData("n") local winnerUID - for _,p in next,data.result do + for _,p in next,d.result do if p.place==1 then winnerUID=p.uid break end end if not winnerUID then return end - for _,d in next,PLY_NET do - if d.uid==winnerUID then - TEXT.show(text.champion:gsub("$1",d.username),640,260,80,"zoomout",.26) + for _,p in next,PLY_NET do + if p.uid==winnerUID then + TEXT.show(text.champion:gsub("$1",p.username),640,260,80,"zoomout",.26) break end end elseif cmd=="Stream"then - if data.uid~=USER.uid and playing then + if d.uid~=USER.uid and playing then for _,P in next,PLAYERS do - if P.userID==data.uid then - local res,stream=pcall(love.data.decode,"string","base64",data.stream) + if P.userID==d.uid then + local res,stream=pcall(love.data.decode,"string","base64",d.stream) if res then pumpRecording(stream,P.stream) else