From 30e276b132d7c149e870b025ca8de11ecc990924 Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Tue, 18 May 2021 02:18:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E7=9A=84=E5=AF=B9=E6=88=98=E6=88=BF?= =?UTF-8?q?=E9=97=B4=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=EF=BC=8C=E6=95=B4?= =?UTF-8?q?=E7=90=86=E7=9B=B8=E5=85=B3=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- parts/net.lua | 78 ++++++++++++++++++++------------------ parts/netPlayer.lua | 2 +- parts/scenes/net_game.lua | 22 +++++------ parts/scenes/net_rooms.lua | 6 +-- 4 files changed, 57 insertions(+), 51 deletions(-) diff --git a/parts/net.lua b/parts/net.lua index 07c95e96..3a74ad39 100644 --- a/parts/net.lua +++ b/parts/net.lua @@ -9,19 +9,20 @@ local NET={ allow_online=false, accessToken=false, roomList={}, - roomInfo={ - -- rid=false, - name=false, - -- type=false, - private=false, - -- count=false, + roomState={ + roomInfo={ + name=false, + type=false, + version=false, + }, + roomData={}, + count=false, capacity=false, - allReady=false, - connectingStream=false, - waitingStream=false, + private=false, start=false, }, - connectingStream=false, + allReady=false, + waitingStream=false, streamRoomID=false, UserCount="_", @@ -130,7 +131,7 @@ function NET.wsconn_play() end function NET.wsconn_stream() if NET.lock('wsc_stream',5)then - NET.roomInfo.start=true + NET.roomState.start=true WS.connect('stream','/stream',JSON.encode{ uid=USER.uid, accessToken=NET.accessToken, @@ -145,7 +146,7 @@ function NET.wsclose_app()WS.close('app')end function NET.wsclose_user()WS.close('user')end function NET.wsclose_play()WS.close('play')end function NET.wsclose_stream() - NET.roomInfo.start=false + NET.roomState.start=false WS.close('stream') end @@ -216,10 +217,8 @@ function NET.fetchRoom() end function NET.createRoom(roomName,capacity,roomType,password) if NET.lock('enterRoom',1.26)then - NET.roomInfo.name=roomName - NET.roomInfo.type=roomType - NET.roomInfo.private=not not password - NET.roomInfo.capacity=capacity + NET.roomState.private=not not password + NET.roomState.capacity=capacity WS.send('play',JSON.encode{ action=1, data={ @@ -240,11 +239,6 @@ end function NET.enterRoom(room,password) if NET.lock('enterRoom',1.26)then SFX.play('reach',.6) - NET.roomInfo.name=room.roomInfo.name - NET.roomInfo.type=room.roomInfo.type - NET.roomInfo.private=not not password - NET.roomInfo.capacity=room.capacity - NET.roomInfo.start=room.start WS.send('play',JSON.encode{ action=2, data={ @@ -272,7 +266,7 @@ function NET.changeConfig() WS.send('play','{"action":5,"data":'..JSON.encode({config=dumpBasicConfig()})..'}') end function NET.signal_ready(ready) - if NET.lock('ready',3)and not NET.roomInfo.start then + if NET.lock('ready',3)and not NET.roomState.start then WS.send('play','{"action":6,"data":{"ready":'..tostring(ready)..'}}') end end @@ -433,7 +427,13 @@ function NET.updateWS_play() } end end - --TODO: d.roomInfo,d.roomData (json) + NET.roomState.roomInfo=d.roomInfo + NET.roomState.roomData=d.roomData + NET.roomState.count=d.count + NET.roomState.capacity=d.capacity + NET.roomState.private=d.private + NET.roomState.start=d.start + NET.srid=d.srid loadGame('netBattle',true,true) else --Load other players @@ -445,7 +445,7 @@ function NET.updateWS_play() config=d.config, } if SCN.socketRead then SCN.socketRead('join',d)end - NET.roomInfo.allReady=false + NET.allReady=false end elseif res.action==3 then--Player leave if not d.uid then @@ -467,16 +467,16 @@ function NET.updateWS_play() netPLY.setReady(d.uid,d.ready) elseif res.action==7 then--All Ready SFX.play('reach',.6) - NET.roomInfo.allReady=true + NET.allReady=true elseif res.action==8 then--Set NET.streamRoomID=d.rid - NET.roomInfo.allReady=false - NET.roomInfo.connectingStream=true + NET.allReady=false + NET.connectingStream=true NET.wsconn_stream() elseif res.action==9 then--Game finished NET.wsclose_stream() if SCN.socketRead then SCN.socketRead('finish',d)end - NET.roomInfo.start=false + NET.roomState.start=false end else WS.alert('play') @@ -504,19 +504,25 @@ function NET.updateWS_stream() local d=res.data if res.type=='Connect'then NET.unlock('wsc_stream') - NET.roomInfo.connectingStream=false - NET.roomInfo.waitingStream=true - for _,uid in next,d.connected do - netPLY.setConnect(uid) - end + NET.connectingStream=false + NET.waitingStream=true elseif res.action==0 then--Game start - NET.roomInfo.waitingStream=false - NET.roomInfo.start=true + NET.waitingStream=false + NET.roomState.start=true if SCN.socketRead then SCN.socketRead('go',d)end elseif res.action==1 then--Game finished --? elseif res.action==2 then--Player join - if not d.watch then + if res.type=='Self'then + for _,p in next,d.connected do + if not p.watch then + netPLY.setConnect(p.uid) + end + end + end + if d.watch then + --TODO: Join in-game + else netPLY.setConnect(d.uid) end elseif res.action==3 then--Player leave diff --git a/parts/netPlayer.lua b/parts/netPlayer.lua index f077bbf8..4a55a447 100644 --- a/parts/netPlayer.lua +++ b/parts/netPlayer.lua @@ -108,7 +108,7 @@ function netPLY.setReady(uid,ready) if p.uid==uid then if p.ready~=ready then p.ready=ready - if not ready then NET.roomInfo.allReady=false end + if not ready then NET.allReady=false end SFX.play('spin_0',.6) if i==1 then NET.unlock('ready') diff --git a/parts/scenes/net_game.lua b/parts/scenes/net_game.lua index 842576d5..a81092a3 100644 --- a/parts/scenes/net_game.lua +++ b/parts/scenes/net_game.lua @@ -182,11 +182,11 @@ function scene.socketRead(cmd,d) if not playing then playing=true love.keyboard.setKeyRepeat(false) - netPLY.resetState() - netPLY.mouseMove(0,0) lastUpstreamTime=0 upstreamProgress=1 resetGameData('n',d.seed) + netPLY.mouseMove(0,0) + netPLY.resetState() else LOG.print("Redundant [Go]",'warn') end @@ -277,13 +277,13 @@ function scene.draw() --Ready & Set mark setFont(50) - if NET.roomInfo.allReady then + if NET.allReady then gc.setColor(0,1,.5,.9) mStr(text.ready,640,15) - elseif NET.roomInfo.connectingStream then + elseif NET.connectingStream then gc.setColor(.1,1,.8,.9) mStr(text.connStream,640,15) - elseif NET.roomInfo.waitingStream then + elseif NET.waitingStream then gc.setColor(0,.8,1,.9) mStr(text.waitStream,640,15) end @@ -291,11 +291,11 @@ function scene.draw() --Room info. gc.setColor(1,1,1) setFont(25) - gc.printf(NET.roomInfo.name,0,685,1270,'right') + gc.printf(NET.roomState.roomInfo.name,0,685,1270,'right') setFont(40) - gc.print(netPLY.getCount().."/"..NET.roomInfo.capacity,70,655) - if NET.roomInfo.private then gc.draw(IMG.lock,30,668)end - if NET.roomInfo.start then gc.setColor(0,1,0)gc.print(text.started,230,655)end + gc.print(netPLY.getCount().."/"..NET.roomState.capacity,70,655) + if NET.roomState.private then gc.draw(IMG.lock,30,668)end + if NET.roomState.start then gc.setColor(0,1,0)gc.print(text.started,230,655)end --Profile drawSelfProfile() @@ -319,7 +319,7 @@ scene.widgetList={ hideF=function() return playing or - NET.roomInfo.start or + NET.roomState.start or netPLY.getSelfReady()or NET.getlock('ready') end}, @@ -327,7 +327,7 @@ scene.widgetList={ hideF=function() return playing or - NET.roomInfo.start or + NET.roomState.start or not netPLY.getSelfReady()or NET.getlock('ready') end}, diff --git a/parts/scenes/net_rooms.lua b/parts/scenes/net_rooms.lua index 7eca36a9..0ac98416 100644 --- a/parts/scenes/net_rooms.lua +++ b/parts/scenes/net_rooms.lua @@ -29,9 +29,9 @@ local scene={} function scene.sceneInit() BG.set() - NET.roomInfo.allReady=false - NET.roomInfo.connectingStream=false - NET.roomInfo.waitingStream=false + NET.allReady=false + NET.connectingStream=false + NET.waitingStream=false scrollPos=0 selected=1 fetchRoom()