联网推进,整理代码

This commit is contained in:
MrZ626
2021-04-08 14:00:15 +08:00
parent 194bd8085f
commit f3f63454d3
7 changed files with 102 additions and 54 deletions

View File

@@ -444,6 +444,7 @@ function love.errorhandler(msg)
end end
end end
end end
local WS=WS
local WSnames={"app","user","play","stream","chat"} local WSnames={"app","user","play","stream","chat"}
local WScolor={ local WScolor={
{1,.5,.5,.7}, {1,.5,.5,.7},

View File

@@ -1,3 +1,4 @@
local WS=WS
local data=love.data local data=love.data
local ins,rem=table.insert,table.remove local ins,rem=table.insert,table.remove
local NET={ local NET={
@@ -54,8 +55,8 @@ local function _parse(res)
end end
end end
--wsEvent --WS close message
function NET.wsCloseMessage(message) local function wsCloseMessage(message)
local mes=JSON.decode(message) local mes=JSON.decode(message)
if mes then if mes then
LOG.print(("%s [%s] %s"):format(text.wsClose,mes.type or"unknown type",mes.reason or""),"warn") 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
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 --Account
function NET.pong(wsName,message) function NET.pong(wsName,message)
WS.send(wsName,message,"pong") WS.send(wsName,message,"pong")
@@ -146,13 +196,8 @@ function NET.enterRoom(roomID,password)
end end
--Play --Play
function NET.wsConnectPlay() function NET.checkPlayDisconn()
if _lock("connectPlay")then return WS.status("play")~="running"
WS.connect("play","/play",JSON.encode{
uid=USER.uid,
accessToken=NET.accessToken,
})
end
end end
function NET.signal_ready() function NET.signal_ready()
if _lock("ready")then if _lock("ready")then
@@ -162,20 +207,11 @@ end
function NET.signal_quit() function NET.signal_quit()
WS.send("play",'{"action":3}') WS.send("play",'{"action":3}')
end end
function NET.wsConnectStream() function NET.signal_die()
if _lock("connectStream")then WS.send("stream",'{"action":4,"data":{"score":0,"survivalTime":0}}')
WS.connect("stream","/stream",JSON.encode{
uid=USER.uid,
accessToken=NET.accessToken,
rid=NET.rsid,
})
end
end end
function NET.uploadRecStream(stream) function NET.uploadRecStream(stream)
WS.send("stream",'{"action":2,"data":{"stream":"'..data.encode("string","base64",stream)..'"}}') WS.send("stream",'{"action":5,"data":{"stream":"'..data.encode("string","base64",stream)..'"}}')
end
function NET.signal_die()
WS.send("stream",'{"action":3,"data":{"score":0,"survivalTime":0}}')
end end
--Chat --Chat
@@ -199,7 +235,7 @@ function NET.updateWS_app()
NET.pong("app",message) NET.pong("app",message)
elseif op=="pong"then elseif op=="pong"then
elseif op=="close"then elseif op=="close"then
NET.wsCloseMessage(message) wsCloseMessage(message)
return return
else else
local res=_parse(message) local res=_parse(message)
@@ -235,7 +271,7 @@ function NET.updateWS_user()
NET.pong("user",message) NET.pong("user",message)
elseif op=="pong"then elseif op=="pong"then
elseif op=="close"then elseif op=="close"then
NET.wsCloseMessage(message) wsCloseMessage(message)
return return
else else
local res=_parse(message) local res=_parse(message)
@@ -252,10 +288,11 @@ function NET.updateWS_user()
--Get self infos --Get self infos
NET.getUserInfo(USER.uid) NET.getUserInfo(USER.uid)
_unlock("conn_user")
elseif res.action==0 then--Get accessToken elseif res.action==0 then--Get accessToken
NET.accessToken=res.accessToken NET.accessToken=res.accessToken
LOG.print(text.accessSuccessed) LOG.print(text.accessSuccessed)
NET.wsConnectPlay() NET.wsconn_play()
_unlock("accessToken") _unlock("accessToken")
elseif res.action==1 then--Get userInfo elseif res.action==1 then--Get userInfo
NET.storeUserInfo(res) NET.storeUserInfo(res)
@@ -279,18 +316,19 @@ function NET.updateWS_play()
NET.pong("play",message) NET.pong("play",message)
elseif op=="pong"then elseif op=="pong"then
elseif op=="close"then elseif op=="close"then
NET.wsCloseMessage(message) wsCloseMessage(message)
return return
else else
local res=_parse(message) local res=_parse(message)
if res then if res then
if res.type=="Connect"then if res.type=="Connect"then
SCN.go("net_menu") SCN.go("net_menu")
_unlock("connectPlay") _unlock("conn_play")
elseif res.action==0 then--Fetch rooms elseif res.action==0 then--Fetch rooms
NET.roomList=res.roomList NET.roomList=res.roomList
_unlock("fetchRoom") _unlock("fetchRoom")
elseif res.action==1 then--Create room (not used) elseif res.action==1 then--Create room (not used)
--?
elseif res.action==2 then--Player join elseif res.action==2 then--Player join
local d=res.data local d=res.data
if res.type=="Self"then if res.type=="Self"then
@@ -356,8 +394,12 @@ function NET.updateWS_play()
SCN.socketRead("Ready",res.data) SCN.socketRead("Ready",res.data)
_unlock("ready") _unlock("ready")
elseif res.action==7 then--All ready elseif res.action==7 then--All ready
--?
elseif res.action==8 then--Sure ready elseif res.action==8 then--Sure ready
SCN.socketRead("Set",res.data) SCN.socketRead("Set",res.data)
elseif res.action==9 then--Game finished
SCN.socketRead("Finish",res.data)
NET.wsclose_stream()
end end
else else
WS.alert("play") WS.alert("play")
@@ -378,7 +420,7 @@ function NET.updateWS_stream()
NET.pong("stream",message) NET.pong("stream",message)
elseif op=="pong"then elseif op=="pong"then
elseif op=="close"then elseif op=="close"then
NET.wsCloseMessage(message) wsCloseMessage(message)
return return
else else
local res=_parse(message) local res=_parse(message)
@@ -388,13 +430,13 @@ function NET.updateWS_stream()
elseif res.action==0 then--Game start elseif res.action==0 then--Game start
SCN.socketRead("Begin",res.data) SCN.socketRead("Begin",res.data)
elseif res.action==1 then--Game finished elseif res.action==1 then--Game finished
SCN.socketRead("Finish",res.data) --?
elseif res.action==2 then--Player join elseif res.action==2 then--Player join
--? --?
elseif res.action==3 then--Player leave elseif res.action==3 then--Player leave
--? --?
elseif res.action==4 then--Player died elseif res.action==4 then--Player died
SCN.socketRead("Die",res.data) --?
elseif res.action==5 then--Receive stream elseif res.action==5 then--Receive stream
SCN.socketRead("Stream",res.data) SCN.socketRead("Stream",res.data)
end end
@@ -417,7 +459,7 @@ function NET.updateWS_chat()
NET.pong("chat",message) NET.pong("chat",message)
elseif op=="pong"then elseif op=="pong"then
elseif op=="close"then elseif op=="close"then
NET.wsCloseMessage(message) wsCloseMessage(message)
return return
else else
local res=_parse(message) local res=_parse(message)

View File

@@ -1683,9 +1683,9 @@ function Player:die()--Called both when win/lose!
self.visTime[i][j]=min(self.visTime[i][j],20) self.visTime[i][j]=min(self.visTime[i][j],20)
end end
end end
if GAME.NET then if GAME.net then
if self.id==1 then if self.id==1 then
ins(GAME.rep,GAME.frame+260) ins(GAME.rep,GAME.frame)
ins(GAME.rep,0) ins(GAME.rep,0)
else else
if self.lastRecv and self.lastRecv.id==1 then if self.lastRecv and self.lastRecv.id==1 then

View File

@@ -180,12 +180,9 @@ local loadingThread=coroutine.wrap(function()
TASK.new(NET.updateWS_app) TASK.new(NET.updateWS_app)
TASK.new(NET.updateWS_user) TASK.new(NET.updateWS_user)
TASK.new(NET.updateWS_play) TASK.new(NET.updateWS_play)
WS.connect("app","/app") NET.wsconn_app()
if USER.authToken then if USER.authToken then
WS.connect("user","/user",JSON.encode{ NET.wsconn_user_token(USER.uid,USER.authToken)
uid=USER.uid,
authToken=USER.authToken,
})
end end
while true do while true do

View File

@@ -6,10 +6,7 @@ local function login()
elseif #password==0 then elseif #password==0 then
LOG.print(text.noPassword)return LOG.print(text.noPassword)return
end end
WS.connect("user","/user",JSON.encode{ NET.wsconn_user_pswd(email,password)
email=email,
password=password,
})
end end
local scene={} local scene={}

View File

@@ -23,6 +23,7 @@ local scene={}
function scene.sceneBack() function scene.sceneBack()
NET.signal_quit() NET.signal_quit()
NET.wsclose_stream()
love.keyboard.setKeyRepeat(true) love.keyboard.setKeyRepeat(true)
end end
function scene.sceneInit() function scene.sceneInit()
@@ -187,7 +188,7 @@ function scene.socketRead(cmd,data)
end end
elseif cmd=="Set"then elseif cmd=="Set"then
NET.rsid=data.rid NET.rsid=data.rid
NET.wsConnectStream() NET.wsconn_stream()
TASK.new(NET.updateWS_stream) TASK.new(NET.updateWS_stream)
elseif cmd=="Begin"then elseif cmd=="Begin"then
if not playing then if not playing then
@@ -196,20 +197,27 @@ function scene.socketRead(cmd,data)
upstreamProgress=1 upstreamProgress=1
resetGameData("n",data.seed) resetGameData("n",data.seed)
else else
LOG.print("Redundant signal: B(begin)",30,COLOR.green) LOG.print("Redundant signal: Begin",30,COLOR.green)
end end
elseif cmd=="Finish"then elseif cmd=="Finish"then
playing=false playing=false
resetGameData("n") resetGameData("n")
TEXT.show(text.champion:gsub("$1","SOMEBODY"),640,260,80,"zoomout",.26) local winnerUID
elseif cmd=="Die"then for _,p in data.result do
--? if p.place==1 then
elseif cmd=="Stream"then winnerUID=p.uid
if data.uid==USER.uid then break
LOG.print("SELF STREAM") end
return
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 for _,P in next,PLAYERS do
if P.userID==data.uid then if P.userID==data.uid then
local res,stream=pcall(love.data.decode,"string","base64",data.stream) local res,stream=pcall(love.data.decode,"string","base64",data.stream)
@@ -225,12 +233,12 @@ function scene.socketRead(cmd,data)
end end
function scene.update(dt) function scene.update(dt)
if NET.checkPlayDisconn()then SCN.back()end
if not playing then return end
local _ local _
local GAME=GAME local GAME=GAME
if WS.status("play")~="running"then SCN.back()end
if not playing then return end
touchMoveLastFrame=false touchMoveLastFrame=false
updateVirtualkey() updateVirtualkey()
GAME.frame=GAME.frame+1 GAME.frame=GAME.frame+1

View File

@@ -3,6 +3,9 @@ local scene={}
function scene.sceneInit() function scene.sceneInit()
BG.set("space") BG.set("space")
end end
function scene.sceneBack()
NET.wsclose_play()
end
scene.widgetList={ scene.widgetList={
-- WIDGET.newButton{name="ffa", x=640, y=200,w=350,h=120,font=40,code=NULL}, -- WIDGET.newButton{name="ffa", x=640, y=200,w=350,h=120,font=40,code=NULL},