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

View File

@@ -5,28 +5,48 @@ local NET={
roomList=false, 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 --Account
function NET.pong(wsName,message) function NET.pong(wsName,message)
WS.send(wsName,message,"pong") WS.send(wsName,message,"pong")
end end
function NET.getAccessToken() function NET.getAccessToken()
if NET.lock("accessToken")then
WS.send("user",JSON.encode{action=0}) WS.send("user",JSON.encode{action=0})
end
end end
function NET.getSelfInfo() function NET.getSelfInfo()
if NET.lock("getSelfInfo")then
WS.send("user",JSON.encode{ WS.send("user",JSON.encode{
action=1, action=1,
data={ data={
id=USER.id, id=USER.id,
}, },
}) })
end
end end
--Play --Play
function NET.wsConnectPlay() function NET.wsConnectPlay()
if NET.lock("connectPlay")then
WS.connect("play","/play",JSON.encode{ WS.connect("play","/play",JSON.encode{
id=USER.id, id=USER.id,
accessToken=USER.accessToken, accessToken=USER.accessToken,
}) })
end
end end
function NET.signal_ready() function NET.signal_ready()
WS.send("play","R") WS.send("play","R")
@@ -56,7 +76,7 @@ function NET.createRoom()
WS.send("play",JSON.encode{ WS.send("play",JSON.encode{
action=1, action=1,
data={ data={
type=nil, type="classic",
name=(USER.name or"???").."'s room", name=(USER.name or"???").."'s room",
password=nil, password=nil,
conf=dumpBasicConfig(), conf=dumpBasicConfig(),
@@ -64,6 +84,7 @@ function NET.createRoom()
}) })
end end
function NET.enterRoom(roomID,password) function NET.enterRoom(roomID,password)
if NET.lock("enterRoom")then
WS.send("play","/play",JSON.encode{ WS.send("play","/play",JSON.encode{
action=2, action=2,
data={ data={
@@ -72,6 +93,7 @@ function NET.enterRoom(roomID,password)
password=password, password=password,
} }
}) })
end
end end
--Chat --Chat