NET添加操作锁保证任务唯一,应用给部分操作

This commit is contained in:
MrZ626
2021-03-29 15:16:23 +08:00
parent d1e074c1ca
commit d37802185b
2 changed files with 48 additions and 20 deletions

View File

@@ -1154,6 +1154,8 @@ do
USER.id=res.id
USER.authToken=res.authToken
NET.getAccessToken()
else
SCN.back()
end
FILE.save(USER,"conf/user","q")
LOG.print(text.loginSuccessed)
@@ -1164,12 +1166,14 @@ do
USER.accessToken=res.accessToken
LOG.print(text.accessSuccessed)
NET.wsConnectPlay()
NET.unlock("accessToken")
elseif res.action==1 then--Get userInfo
if res.id==USER.id then--Own
USER.name=res.username
USER.motto=res.motto
USER.avatar=res.avatar
FILE.save(USER,"conf/user")
NET.unlock("getSelfInfo")
else--Others
LOG.print("Get user info: "..USER.id)
end
@@ -1202,11 +1206,13 @@ do
local res=JSON.decode(message)
if res then
if res.message=="Connected"then
NET.unlock("connectPlay")
SCN.go("net_menu")
elseif res.action==0 then--Fetch rooms
NET.roomList=res.roomList
elseif res.action==2 then--Join room
loadGame("netBattle",true,true)
NET.unlock("enterRoom")
elseif res.action==3 then--Leave room
SCN.back()
end

View File

@@ -5,28 +5,48 @@ local NET={
roomList=false,
}
--Lock & Unlock submodule
local locks={}
function NET.lock(name,T)
if locks[name]and TIME()<locks[name]then
return false
else
locks[name]=TIME()+(T or 1e99)
return true
end
end
function NET.unlock(name)
locks[name]=false
end
--Account
function NET.pong(wsName,message)
WS.send(wsName,message,"pong")
end
function NET.getAccessToken()
WS.send("user",JSON.encode{action=0})
if NET.lock("accessToken")then
WS.send("user",JSON.encode{action=0})
end
end
function NET.getSelfInfo()
WS.send("user",JSON.encode{
action=1,
data={
id=USER.id,
},
})
if NET.lock("getSelfInfo")then
WS.send("user",JSON.encode{
action=1,
data={
id=USER.id,
},
})
end
end
--Play
function NET.wsConnectPlay()
WS.connect("play","/play",JSON.encode{
id=USER.id,
accessToken=USER.accessToken,
})
if NET.lock("connectPlay")then
WS.connect("play","/play",JSON.encode{
id=USER.id,
accessToken=USER.accessToken,
})
end
end
function NET.signal_ready()
WS.send("play","R")
@@ -56,7 +76,7 @@ function NET.createRoom()
WS.send("play",JSON.encode{
action=1,
data={
type=nil,
type="classic",
name=(USER.name or"???").."'s room",
password=nil,
conf=dumpBasicConfig(),
@@ -64,14 +84,16 @@ function NET.createRoom()
})
end
function NET.enterRoom(roomID,password)
WS.send("play","/play",JSON.encode{
action=2,
data={
rid=roomID,
conf=dumpBasicConfig(),
password=password,
}
})
if NET.lock("enterRoom")then
WS.send("play","/play",JSON.encode{
action=2,
data={
rid=roomID,
conf=dumpBasicConfig(),
password=password,
}
})
end
end
--Chat