大改联网的lock子模块,大改对战的玩家数据结构,重做联网对战准备界面

This commit is contained in:
MrZ626
2021-04-08 23:43:10 +08:00
parent 2bdbc4d792
commit ca267fd565
8 changed files with 104 additions and 100 deletions

View File

@@ -12,7 +12,7 @@ return{
local N=2
for i=1,#PLY_NET do
if PLY_NET[i].uid==USER.uid then
PLAYERS[1].subID=PLY_NET[1].sid
PLAYERS[1].sid=PLY_NET[1].sid
else
PLY.newRemotePlayer(N,false,PLY_NET[i])
N=N+1

View File

@@ -1,6 +1,6 @@
local WS=WS
local data=love.data
local ins,rem=table.insert,table.remove
local WS,TIME=WS,TIME
local NET={
login=false,
allow_online=false,
@@ -19,20 +19,26 @@ local mesType={
}
--Lock & Unlock submodule
local locks={}
local function _lock(name,T)
if locks[name]and TIME()<locks[name]then
return false
else
local locks do
local rawset=rawset
locks=setmetatable({},{
__index=function(self,k)rawset(self,k,-1e99)return -1e99 end,
__newindex=function(self,k)rawset(self,k,-1e99)end,
})
end
function NET.lock(name,T)
if TIME()>=locks[name]then
locks[name]=TIME()+(T or 1e99)
return true
else
return false
end
end
local function _unlock(name)
locks[name]=false
function NET.unlock(name)
locks[name]=-1e99
end
function NET.getLock(name)
return locks[name]
function NET.getlock(name)
return TIME()<locks[name]
end
--Parse json message
@@ -70,7 +76,7 @@ function NET.wsconn_app()
WS.connect("app","/app")
end
function NET.wsconn_user_pswd(email,password)
if _lock("wsc_user",5)then
if NET.lock("wsc_user",5)then
WS.connect("user","/user",JSON.encode{
email=email,
password=password,
@@ -78,7 +84,7 @@ function NET.wsconn_user_pswd(email,password)
end
end
function NET.wsconn_user_token(uid,authToken)
if _lock("wsc_user",5)then
if NET.lock("wsc_user",5)then
WS.connect("user","/user",JSON.encode{
uid=uid,
authToken=authToken,
@@ -86,7 +92,7 @@ function NET.wsconn_user_token(uid,authToken)
end
end
function NET.wsconn_play()
if _lock("wsc_play",5)then
if NET.lock("wsc_play",5)then
WS.connect("play","/play",JSON.encode{
uid=USER.uid,
accessToken=NET.accessToken,
@@ -94,7 +100,7 @@ function NET.wsconn_play()
end
end
function NET.wsconn_stream()
if _lock("wsc_stream",5)then
if NET.lock("wsc_stream",5)then
WS.connect("stream","/stream",JSON.encode{
uid=USER.uid,
accessToken=NET.accessToken,
@@ -116,7 +122,7 @@ function NET.pong(wsName,message)
WS.send(wsName,message,"pong")
end
function NET.getAccessToken()
if _lock("accessToken",3)then
if NET.lock("accessToken",3)then
WS.send("user",JSON.encode{action=0})
end
end
@@ -154,7 +160,7 @@ end
--Room
function NET.fetchRoom()
if _lock("fetchRoom",3)then
if NET.lock("fetchRoom",3)then
WS.send("play",JSON.encode{
action=0,
data={
@@ -166,7 +172,7 @@ function NET.fetchRoom()
end
end
function NET.createRoom()
if _lock("enterRoom",3)then
if NET.lock("enterRoom",3)then
WS.send("play",JSON.encode{
action=1,
data={
@@ -179,7 +185,7 @@ function NET.createRoom()
end
end
function NET.enterRoom(roomID,password)
if _lock("enterRoom",3)then
if NET.lock("enterRoom",3)then
NET.rid=roomID
WS.send("play",JSON.encode{
action=2,
@@ -197,12 +203,12 @@ function NET.checkPlayDisconn()
return WS.status("play")~="running"
end
function NET.signal_ready(ready)
if _lock("ready",3)then
if NET.lock("ready",3)then
WS.send("play",'{"action":6,"data":{"ready":'..tostring(ready)..'}}')
end
end
function NET.signal_quit()
if _lock("quit",3)then
if NET.lock("quit",3)then
WS.send("play",'{"action":3}')
end
end
@@ -287,12 +293,12 @@ function NET.updateWS_user()
--Get self infos
NET.getUserInfo(USER.uid)
_unlock("wsc_user")
NET.unlock("wsc_user")
elseif res.action==0 then--Get accessToken
NET.accessToken=res.accessToken
LOG.print(text.accessSuccessed)
NET.wsconn_play()
_unlock("accessToken")
NET.unlock("accessToken")
elseif res.action==1 then--Get userInfo
NET.storeUserInfo(res)
end
@@ -323,26 +329,19 @@ function NET.updateWS_play()
local d=res.data
if res.type=="Connect"then
SCN.go("net_menu")
_unlock("wsc_play")
NET.unlock("wsc_play")
elseif res.action==0 then--Fetch rooms
NET.roomList=res.roomList
_unlock("fetchRoom")
NET.unlock("fetchRoom")
elseif res.action==1 then--Create room (not used)
--?
elseif res.action==2 then--Player join
if res.type=="Self"then
--Create room
TABLE.clear(PLY_NET)
ins(PLY_NET,{
uid=USER.uid,
username=USER.username,
sid=d.sid,
ready=d.ready,
conf=dumpBasicConfig(),
})
if d.players then
for _,p in next,d.players do
ins(PLY_NET,{
ins(PLY_NET,p.uid==USER.uid and 1 or #PLY_NET+1,{
uid=p.uid,
username=p.username,
sid=p.sid,
@@ -352,7 +351,7 @@ function NET.updateWS_play()
end
end
loadGame("netBattle",true,true)
_unlock("enterRoom")
NET.unlock("enterRoom")
else
--Load other players
ins(PLY_NET,{
@@ -368,22 +367,22 @@ function NET.updateWS_play()
if not d.uid then
NET.wsclose_stream()
SCN.back()
_unlock("quit")
NET.unlock("quit")
else
for i=1,#PLY_NET do
if PLY_NET[i].uid==d.uid then
if PLY_NET[i].sid==d.sid then
rem(PLY_NET,i)
break
end
end
for i=1,#PLAYERS do
if PLAYERS[i].userID==d.uid then
if PLAYERS[i].sid==d.sid then
rem(PLAYERS,i)
break
end
end
for i=1,#PLY_ALIVE do
if PLY_ALIVE[i].userID==d.uid then
if PLY_ALIVE[i].sid==d.sid then
rem(PLY_ALIVE,i)
break
end
@@ -404,23 +403,16 @@ function NET.updateWS_play()
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
for i=1,#PLY_NET do
if PLY_NET[i].uid==d.uid then
if PLY_NET[i].ready~=d.ready then
PLY_NET[i].ready=d.ready
SFX.play("reach",.6)
end
break
end
end
NET.unlock("ready")
elseif res.action==7 then--Ready
--?
elseif res.action==8 then--Set
@@ -456,7 +448,7 @@ function NET.updateWS_stream()
local res=_parse(message)
if res then
if res.type=="Connect"then
_unlock("wsc_stream")
NET.unlock("wsc_stream")
elseif res.action==0 then--Game start
SCN.socketRead("Go",res.data)
elseif res.action==1 then--Game finished

View File

@@ -707,7 +707,7 @@ function draw.norm_remote(P)
--Draw username
setFont(30)
gc_setColor(1,1,1)
mStr(P.userName,150,-60)
mStr(P.username,150,-60)
--Fill field
gc_setColor(0,0,0,.6)

View File

@@ -134,10 +134,9 @@ local function newEmptyPlayer(id,mini)
P.atker,P.atking,P.lastRecv={}
--Network-related
P.userName="_"
P.userID=-1
P.subID=-1
P.ready=false
P.username="_"
P.uid=-1
P.sid=-1
P.dropDelay,P.lockDelay=0,0
P.showTime=false
@@ -240,7 +239,7 @@ local function loadRemoteEnv(P,confStr)--Load gameEnv
conf=JSON.decode(conf)
else
conf={}
LOG.print("Bad conf from "..P.userName.."#"..P.userID)
LOG.print("Bad conf from "..P.username.."#"..P.uid)
end
P.gameEnv={}--Current game setting environment
@@ -383,10 +382,9 @@ function PLY.newRemotePlayer(id,mini,data)
P.streamProgress=1
data.p=P
P.userID=data.uid
P.userName=data.username
P.subID=data.sid
P.ready=data.ready
P.uid=data.uid
P.username=data.username
P.sid=data.sid
loadRemoteEnv(P,data.conf)
applyGameEnv(P)
@@ -408,8 +406,8 @@ function PLY.newPlayer(id,mini)
P.type="human"
P.sound=true
P.userID=USER.uid
P.subID=-1
P.uid=USER.uid
P.sid=-1
loadGameEnv(P)
applyGameEnv(P)

View File

@@ -222,7 +222,7 @@ function Player:setConf(confStr)
end
end
else
LOG.print("Bad conf from "..self.userName.."#"..self.userID)
LOG.print("Bad conf from "..self.username.."#"..self.uid)
end
end
@@ -340,7 +340,7 @@ function Player:attack(R,send,time,line,fromStream)
if self.type=="human"then--Local player attack others
ins(GAME.rep,GAME.frame)
ins(GAME.rep,
R.subID+
R.sid+
send*0x100+
time*0x10000+
line*0x100000000+
@@ -350,7 +350,7 @@ function Player:attack(R,send,time,line,fromStream)
if fromStream and R.type=="human"then--Local player receiving lines
ins(GAME.rep,GAME.frame)
ins(GAME.rep,
self.subID+
self.sid+
send*0x100+
time*0x10000+
line*0x100000000+

View File

@@ -396,7 +396,7 @@ function update.remote_alive(P,dt)
local line=int(event/0x100000000)%0x10000
local L=PLY_ALIVE
for i=1,#L do
if L[i].subID==sid then
if L[i].sid==sid then
P:attack(L[i],amount,time,line,true)
if SETTING.atkFX>0 then
P:createBeam(L[i],amount,P.cur.color)
@@ -408,7 +408,7 @@ function update.remote_alive(P,dt)
local L=PLY_ALIVE
local sid=event%0x100
for i=1,#L do
if L[i].subID==sid then
if L[i].sid==sid then
P:receive(
L[i],
int(event/0x100)%0x100,--amount

View File

@@ -28,7 +28,6 @@ function scene.sceneInit()
textBox.hide=true
textBox:clear()
resetGameData("n")
noTouch=not SETTING.VKSwitch
playing=false
lastUpstreamTime=0
@@ -36,7 +35,7 @@ function scene.sceneInit()
end
function scene.touchDown(x,y)
if noTouch then return end
if noTouch or not playing then return end
local t=onVirtualkey(x,y)
if t then
@@ -45,7 +44,7 @@ function scene.touchDown(x,y)
end
end
function scene.touchUp(x,y)
if noTouch then return end
if noTouch or not playing then return end
local t=onVirtualkey(x,y)
if t then
@@ -53,7 +52,7 @@ function scene.touchUp(x,y)
end
end
function scene.touchMove()
if noTouch or touchMoveLastFrame then return end
if noTouch or touchMoveLastFrame or not playing then return end
touchMoveLastFrame=true
local L=tc.getTouches()
@@ -93,9 +92,7 @@ function scene.keyDown(key)
VK[k].pressTime=10
end
elseif key=="space"then
if not NET.getLock("ready")then
NET.signal_ready(not PLAYERS[1].ready)
end
NET.signal_ready(not PLY_NET[1].ready)
end
end
function scene.keyUp(key)
@@ -142,9 +139,6 @@ function scene.socketRead(cmd,d)
COLOR.Y,text.joinRoom,
}
SFX.play("click")
if not playing then
resetGameData("qn")
end
elseif cmd=="Leave"then
textBox:push{
COLOR.lR,d.username,
@@ -163,6 +157,9 @@ function scene.socketRead(cmd,d)
elseif cmd=="Go"then
if not playing then
playing=true
for i=1,#PLY_NET do
PLY_NET[i].ready=false
end
lastUpstreamTime=0
upstreamProgress=1
resetGameData("n",d.seed)
@@ -171,7 +168,6 @@ function scene.socketRead(cmd,d)
end
elseif cmd=="Finish"then
playing=false
resetGameData("n")
local winnerUID
for _,p in next,d.result do
if p.place==1 then
@@ -189,12 +185,12 @@ function scene.socketRead(cmd,d)
elseif cmd=="Stream"then
if d.uid~=USER.uid and playing then
for _,P in next,PLAYERS do
if P.userID==d.uid then
if P.uid==d.uid then
local res,stream=pcall(love.data.decode,"string","base64",d.stream)
if res then
pumpRecording(stream,P.stream)
else
LOG.print("Bad stream from "..P.userName.."#"..P.userID)
LOG.print("Bad stream from "..P.username.."#"..P.uid)
end
end
end
@@ -237,19 +233,37 @@ function scene.update(dt)
end
function scene.draw()
drawFWM()
if playing then
drawFWM()
--Players
for p=textBox.hide and 1 or 2,#PLAYERS do
PLAYERS[p]:draw()
--Players
for p=textBox.hide and 1 or 2,#PLAYERS do
PLAYERS[p]:draw()
end
--Virtual keys
drawVirtualkeys()
--Warning
drawWarning()
else
setFont(40)
for i=1,#PLY_NET do
local p=PLY_NET[i]
if p.ready then
gc.setColor(.4,1,.4)
else
gc.setColor(1,1,1)
end
gc.rectangle("fill",50,60+50*i+14,30,30)
gc.setColor(.5,.5,.5)
gc.print("#"..p.uid,90,60+50*i)
gc.setColor(1,1,1)
gc.print(p.username,230,60+50*i)
end
end
--Virtual keys
drawVirtualkeys()
--Warning
drawWarning()
--New message
if textBox.new and textBox.hide then
setFont(30)
@@ -259,11 +273,11 @@ function scene.draw()
end
scene.widgetList={
textBox,
WIDGET.newKey{name="ready",x=640,y=440,w=200,h=80,color="yellow",font=40,code=pressKey"space",hide=function()
WIDGET.newKey{name="ready",x=900,y=560,w=400,h=100,color="yellow",font=40,code=pressKey"space",hide=function()
return
playing or
not textBox.hide or
NET.getLock("ready")
NET.getlock("ready")
end},
WIDGET.newKey{name="hideChat",fText="...",x=380,y=35,w=60,font=35,code=pressKey"\\"},
WIDGET.newKey{name="quit",fText="X",x=900,y=35,w=60,font=40,code=pressKey"escape"},

View File

@@ -52,7 +52,7 @@ function scene.keyDown(k)
end
end
elseif k=="return"then
if NET.getLock("fetchRoom")then return end
if NET.getlock("fetchRoom")then return end
if NET.roomList[selected].private then
LOG.print("Can't enter private room now")
return
@@ -63,7 +63,7 @@ function scene.keyDown(k)
end
function scene.update(dt)
if not NET.getLock("fetchRoom")then
if not NET.getlock("fetchRoom")then
fetchTimer=fetchTimer-dt
if fetchTimer<=0 then
fetchRoom()
@@ -103,8 +103,8 @@ end
local function hide_noRoom()return #NET.roomList==0 end
scene.widgetList={
WIDGET.newText{name="refreshing",x=640,y=255,font=45,hide=function()return not NET.getLock("fetchRoom")end},
WIDGET.newText{name="noRoom", x=640,y=260,font=40,hide=function()return #NET.roomList>0 or NET.getLock("fetchRoom")end},
WIDGET.newText{name="refreshing",x=640,y=255,font=45,hide=function()return not NET.getlock("fetchRoom")end},
WIDGET.newText{name="noRoom", x=640,y=260,font=40,hide=function()return #NET.roomList>0 or NET.getlock("fetchRoom")end},
WIDGET.newKey{name="refresh", x=240,y=620,w=140,h=140,font=35,code=fetchRoom, hide=function()return fetchTimer>3.26 end},
WIDGET.newKey{name="new", x=440,y=620,w=140,h=140,font=25,code=pressKey"n"},
WIDGET.newKey{name="join", x=640,y=620,w=140,h=140,font=40,code=pressKey"return", hide=hide_noRoom},