From f3f63454d3d8c979311a8ffd71498d62df8ce093 Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Thu, 8 Apr 2021 14:00:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=94=E7=BD=91=E6=8E=A8=E8=BF=9B=EF=BC=8C?= =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zframework/init.lua | 1 + parts/net.lua | 102 +++++++++++++++++++++++++++----------- parts/player/player.lua | 4 +- parts/scenes/load.lua | 7 +-- parts/scenes/login.lua | 5 +- parts/scenes/net_game.lua | 34 ++++++++----- parts/scenes/net_menu.lua | 3 ++ 7 files changed, 102 insertions(+), 54 deletions(-) diff --git a/Zframework/init.lua b/Zframework/init.lua index e66c0fd7..359a4857 100644 --- a/Zframework/init.lua +++ b/Zframework/init.lua @@ -444,6 +444,7 @@ function love.errorhandler(msg) end end end +local WS=WS local WSnames={"app","user","play","stream","chat"} local WScolor={ {1,.5,.5,.7}, diff --git a/parts/net.lua b/parts/net.lua index 46b8c23f..ee47c9d5 100644 --- a/parts/net.lua +++ b/parts/net.lua @@ -1,3 +1,4 @@ +local WS=WS local data=love.data local ins,rem=table.insert,table.remove local NET={ @@ -54,8 +55,8 @@ local function _parse(res) end end ---wsEvent -function NET.wsCloseMessage(message) +--WS close message +local function wsCloseMessage(message) local mes=JSON.decode(message) if mes then LOG.print(("%s [%s] %s"):format(text.wsClose,mes.type or"unknown type",mes.reason or""),"warn") @@ -64,6 +65,55 @@ function NET.wsCloseMessage(message) end end +--Connect +function NET.wsconn_app() + WS.connect("app","/app") +end +function NET.wsconn_play() + if _lock("conn_play")then + WS.connect("play","/play",JSON.encode{ + uid=USER.uid, + accessToken=NET.accessToken, + }) + end +end +function NET.wsconn_stream() + if _lock("connectStream")then + WS.connect("stream","/stream",JSON.encode{ + uid=USER.uid, + accessToken=NET.accessToken, + rid=NET.rsid, + }) + end +end +function NET.wsconn_user_pswd(email,password) + if _lock("conn_user")then + WS.connect("user","/user",JSON.encode{ + email=email, + password=password, + }) + end +end +function NET.wsconn_user_token(uid,authToken) + if _lock("conn_user")then + WS.connect("user","/user",JSON.encode{ + uid=uid, + authToken=authToken, + }) + end +end + +--Disconnect +function NET.wsclose_play() + WS.close("play") +end +function NET.wsclose_stream() + WS.close("stream") +end +function NET.wsclose_user() + WS.close("user") +end + --Account function NET.pong(wsName,message) WS.send(wsName,message,"pong") @@ -146,13 +196,8 @@ function NET.enterRoom(roomID,password) end --Play -function NET.wsConnectPlay() - if _lock("connectPlay")then - WS.connect("play","/play",JSON.encode{ - uid=USER.uid, - accessToken=NET.accessToken, - }) - end +function NET.checkPlayDisconn() + return WS.status("play")~="running" end function NET.signal_ready() if _lock("ready")then @@ -162,20 +207,11 @@ end function NET.signal_quit() WS.send("play",'{"action":3}') end -function NET.wsConnectStream() - if _lock("connectStream")then - WS.connect("stream","/stream",JSON.encode{ - uid=USER.uid, - accessToken=NET.accessToken, - rid=NET.rsid, - }) - end +function NET.signal_die() + WS.send("stream",'{"action":4,"data":{"score":0,"survivalTime":0}}') end function NET.uploadRecStream(stream) - WS.send("stream",'{"action":2,"data":{"stream":"'..data.encode("string","base64",stream)..'"}}') -end -function NET.signal_die() - WS.send("stream",'{"action":3,"data":{"score":0,"survivalTime":0}}') + WS.send("stream",'{"action":5,"data":{"stream":"'..data.encode("string","base64",stream)..'"}}') end --Chat @@ -199,7 +235,7 @@ function NET.updateWS_app() NET.pong("app",message) elseif op=="pong"then elseif op=="close"then - NET.wsCloseMessage(message) + wsCloseMessage(message) return else local res=_parse(message) @@ -235,7 +271,7 @@ function NET.updateWS_user() NET.pong("user",message) elseif op=="pong"then elseif op=="close"then - NET.wsCloseMessage(message) + wsCloseMessage(message) return else local res=_parse(message) @@ -252,10 +288,11 @@ function NET.updateWS_user() --Get self infos NET.getUserInfo(USER.uid) + _unlock("conn_user") elseif res.action==0 then--Get accessToken NET.accessToken=res.accessToken LOG.print(text.accessSuccessed) - NET.wsConnectPlay() + NET.wsconn_play() _unlock("accessToken") elseif res.action==1 then--Get userInfo NET.storeUserInfo(res) @@ -279,18 +316,19 @@ function NET.updateWS_play() NET.pong("play",message) elseif op=="pong"then elseif op=="close"then - NET.wsCloseMessage(message) + wsCloseMessage(message) return else local res=_parse(message) if res then if res.type=="Connect"then SCN.go("net_menu") - _unlock("connectPlay") + _unlock("conn_play") elseif res.action==0 then--Fetch rooms NET.roomList=res.roomList _unlock("fetchRoom") 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 @@ -356,8 +394,12 @@ 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) + elseif res.action==9 then--Game finished + SCN.socketRead("Finish",res.data) + NET.wsclose_stream() end else WS.alert("play") @@ -378,7 +420,7 @@ function NET.updateWS_stream() NET.pong("stream",message) elseif op=="pong"then elseif op=="close"then - NET.wsCloseMessage(message) + wsCloseMessage(message) return else local res=_parse(message) @@ -388,13 +430,13 @@ function NET.updateWS_stream() elseif res.action==0 then--Game start SCN.socketRead("Begin",res.data) elseif res.action==1 then--Game finished - SCN.socketRead("Finish",res.data) + --? 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==5 then--Receive stream SCN.socketRead("Stream",res.data) end @@ -417,7 +459,7 @@ function NET.updateWS_chat() NET.pong("chat",message) elseif op=="pong"then elseif op=="close"then - NET.wsCloseMessage(message) + wsCloseMessage(message) return else local res=_parse(message) diff --git a/parts/player/player.lua b/parts/player/player.lua index 7845ca25..1c0da4a5 100644 --- a/parts/player/player.lua +++ b/parts/player/player.lua @@ -1683,9 +1683,9 @@ function Player:die()--Called both when win/lose! self.visTime[i][j]=min(self.visTime[i][j],20) end end - if GAME.NET then + if GAME.net then if self.id==1 then - ins(GAME.rep,GAME.frame+260) + ins(GAME.rep,GAME.frame) ins(GAME.rep,0) else if self.lastRecv and self.lastRecv.id==1 then diff --git a/parts/scenes/load.lua b/parts/scenes/load.lua index 9685dfa1..9063765b 100644 --- a/parts/scenes/load.lua +++ b/parts/scenes/load.lua @@ -180,12 +180,9 @@ local loadingThread=coroutine.wrap(function() TASK.new(NET.updateWS_app) TASK.new(NET.updateWS_user) TASK.new(NET.updateWS_play) - WS.connect("app","/app") + NET.wsconn_app() if USER.authToken then - WS.connect("user","/user",JSON.encode{ - uid=USER.uid, - authToken=USER.authToken, - }) + NET.wsconn_user_token(USER.uid,USER.authToken) end while true do diff --git a/parts/scenes/login.lua b/parts/scenes/login.lua index d6517847..17ac2065 100644 --- a/parts/scenes/login.lua +++ b/parts/scenes/login.lua @@ -6,10 +6,7 @@ local function login() elseif #password==0 then LOG.print(text.noPassword)return end - WS.connect("user","/user",JSON.encode{ - email=email, - password=password, - }) + NET.wsconn_user_pswd(email,password) end local scene={} diff --git a/parts/scenes/net_game.lua b/parts/scenes/net_game.lua index b1e881c1..1300e1ff 100644 --- a/parts/scenes/net_game.lua +++ b/parts/scenes/net_game.lua @@ -23,6 +23,7 @@ local scene={} function scene.sceneBack() NET.signal_quit() + NET.wsclose_stream() love.keyboard.setKeyRepeat(true) end function scene.sceneInit() @@ -187,7 +188,7 @@ function scene.socketRead(cmd,data) end elseif cmd=="Set"then NET.rsid=data.rid - NET.wsConnectStream() + NET.wsconn_stream() TASK.new(NET.updateWS_stream) elseif cmd=="Begin"then if not playing then @@ -196,20 +197,27 @@ function scene.socketRead(cmd,data) upstreamProgress=1 resetGameData("n",data.seed) else - LOG.print("Redundant signal: B(begin)",30,COLOR.green) + LOG.print("Redundant signal: Begin",30,COLOR.green) end elseif cmd=="Finish"then playing=false resetGameData("n") - TEXT.show(text.champion:gsub("$1","SOMEBODY"),640,260,80,"zoomout",.26) - elseif cmd=="Die"then - --? - elseif cmd=="Stream"then - if data.uid==USER.uid then - LOG.print("SELF STREAM") - return + local winnerUID + for _,p in data.result do + if p.place==1 then + winnerUID=p.uid + break + end end - if playing then + 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) + break + end + end + elseif cmd=="Stream"then + if data.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) @@ -225,12 +233,12 @@ function scene.socketRead(cmd,data) end function scene.update(dt) + if NET.checkPlayDisconn()then SCN.back()end + if not playing then return end + local _ local GAME=GAME - if WS.status("play")~="running"then SCN.back()end - if not playing then return end - touchMoveLastFrame=false updateVirtualkey() GAME.frame=GAME.frame+1 diff --git a/parts/scenes/net_menu.lua b/parts/scenes/net_menu.lua index 4246cec5..054d5ebc 100644 --- a/parts/scenes/net_menu.lua +++ b/parts/scenes/net_menu.lua @@ -3,6 +3,9 @@ local scene={} function scene.sceneInit() BG.set("space") end +function scene.sceneBack() + NET.wsclose_play() +end scene.widgetList={ -- WIDGET.newButton{name="ffa", x=640, y=200,w=350,h=120,font=40,code=NULL},