From bdf1ce640cf4504fc934a5c0c6e7054b7addbf90 Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Thu, 8 Apr 2021 22:23:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=94=E7=BD=91=E6=8E=A8=E8=BF=9B=EF=BC=88?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=8E=A9=E5=AE=B6=E9=80=80=E5=87=BA=E6=9C=BA?= =?UTF-8?q?=E5=88=B6=EF=BC=8C=E9=83=A8=E5=88=86=E8=81=94=E7=BD=91=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E5=A2=9E=E5=8A=A0=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B4?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- parts/net.lua | 54 ++++++++++++++++++++++----------------- parts/scenes/net_game.lua | 3 +-- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/parts/net.lua b/parts/net.lua index 4f970a50..d1903b7d 100644 --- a/parts/net.lua +++ b/parts/net.lua @@ -86,7 +86,7 @@ function NET.wsconn_user_token(uid,authToken) end end function NET.wsconn_play() - if _lock("wsc_play")then + if _lock("wsc_play",5)then WS.connect("play","/play",JSON.encode{ uid=USER.uid, accessToken=NET.accessToken, @@ -94,7 +94,7 @@ function NET.wsconn_play() end end function NET.wsconn_stream() - if _lock("wsc_stream")then + if _lock("wsc_stream",5)then WS.connect("stream","/stream",JSON.encode{ uid=USER.uid, accessToken=NET.accessToken, @@ -116,7 +116,7 @@ function NET.pong(wsName,message) WS.send(wsName,message,"pong") end function NET.getAccessToken() - if _lock("accessToken")then + if _lock("accessToken",3)then WS.send("user",JSON.encode{action=0}) end end @@ -154,7 +154,7 @@ end --Room function NET.fetchRoom() - if _lock("fetchRoom")then + if _lock("fetchRoom",3)then WS.send("play",JSON.encode{ action=0, data={ @@ -166,7 +166,7 @@ function NET.fetchRoom() end end function NET.createRoom() - if _lock("enterRoom",5)then + if _lock("enterRoom",3)then WS.send("play",JSON.encode{ action=1, data={ @@ -179,7 +179,7 @@ function NET.createRoom() end end function NET.enterRoom(roomID,password) - if _lock("enterRoom",5)then + if _lock("enterRoom",3)then NET.rid=roomID WS.send("play",JSON.encode{ action=2, @@ -197,12 +197,14 @@ function NET.checkPlayDisconn() return WS.status("play")~="running" end function NET.signal_ready(ready) - if _lock("ready")then + if _lock("ready",3)then WS.send("play",'{"action":6,"data":{"ready":'..tostring(ready)..'}}') end end function NET.signal_quit() - WS.send("play",'{"action":3}') + if _lock("quit",3)then + WS.send("play",'{"action":3}') + end end function NET.signal_die() WS.send("stream",'{"action":4,"data":{"score":0,"survivalTime":0}}') @@ -363,25 +365,31 @@ function NET.updateWS_play() SCN.socketRead("Join",res.data) end elseif res.action==3 then--Player leave - for i=1,#PLY_NET do - if PLY_NET[i].uid==d.uid then - rem(PLY_NET,i) - break + if not d.uid then + NET.wsclose_stream() + SCN.back() + _unlock("quit") + else + for i=1,#PLY_NET do + if PLY_NET[i].uid==d.uid then + rem(PLY_NET,i) + break + end end - end - for i=1,#PLAYERS do - if PLAYERS[i].userID==d.uid then - rem(PLAYERS,i) - break + for i=1,#PLAYERS do + if PLAYERS[i].userID==d.uid then + rem(PLAYERS,i) + break + end end - end - for i=1,#PLY_ALIVE do - if PLY_ALIVE[i].userID==d.uid then - rem(PLY_ALIVE,i) - break + for i=1,#PLY_ALIVE do + if PLY_ALIVE[i].userID==d.uid then + rem(PLY_ALIVE,i) + break + end end + SCN.socketRead("Leave",res.data) end - 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 diff --git a/parts/scenes/net_game.lua b/parts/scenes/net_game.lua index d17fecc1..973a5f47 100644 --- a/parts/scenes/net_game.lua +++ b/parts/scenes/net_game.lua @@ -21,8 +21,6 @@ local touchMoveLastFrame=false local scene={} function scene.sceneBack() - NET.signal_quit() - NET.wsclose_stream() love.keyboard.setKeyRepeat(true) end function scene.sceneInit() @@ -78,6 +76,7 @@ end function scene.keyDown(key) if key=="escape"then if TIME()-lastBackTime<1 then + NET.signal_quit() SCN.back() else lastBackTime=TIME()