取消USER的username域,USERS独立为模块用于管理所有用户信息缓存(还未测试)

This commit is contained in:
MrZ626
2021-04-16 01:28:30 +08:00
parent d041b0e993
commit aacb11a22b
7 changed files with 89 additions and 30 deletions

View File

@@ -63,6 +63,7 @@ FIELD[1]=newBoard()--Initialize field[1]
NET= require"parts.net"
AIBUILDER= require"parts.AITemplate"
FREEROW= require"parts.freeRow"
USERS= require"parts.users"
TEXTURE= require"parts.texture"
SKIN= require"parts.skin"

View File

@@ -973,7 +973,7 @@ do--function saveRecording()
os.date("%Y/%m/%d %A %H:%M:%S\n")..
GAME.curModeName.."\n"..
VERSION.string.."\n"..
(USER.username or"Player")
"Local Player"
local fileBody=
GAME.seed.."\n"..
JSON.encode(GAME.setting).."\n"..
@@ -1021,7 +1021,7 @@ do--function drawFWM()
local t=TIME()
setFont(25)
gc_setColor(1,1,1,.2+.1*(sin(3*t)+sin(2.6*t)))
mStr(m[SETTING.lang]or m[1],240,60+26*sin(t))
mStr(m[_G["\83\69\84\84\73\78\71"]["\108\97\110\103"]or m[1]],240,60+26*sin(t))
end
end
function drawWarning()

View File

@@ -222,14 +222,12 @@ GAME={--Global game data
RANKS=FILE.load("conf/unlock")or{sprint_10l=0}--Ranks of modes
USER=FILE.load("conf/user")or{--User infomation
--Network infos
username=false,
uid=false,
authToken=false,
--Local data
xp=0,lv=1,
}
USERS=FILE.load("conf/users")or{}
SETTING={--Settings
--Tuning
das=10,arr=2,dascut=0,

View File

@@ -145,30 +145,11 @@ function NET.getUserInfo(id,ifDetail)
WS.send("user",JSON.encode{
action=1,
data={
id=id or USER.uid,
id=id,
detailed=ifDetail or false,
},
})
end
function NET.storeUserInfo(d)
local user=USERS[d.uid]
if not user then
user={}
USERS[d.uid]=user
end
user.uid=d.uid
user.username=d.username
user.motto=d.motto
user.avatar=d.avatar
--Get own name
if d.uid==USER.uid then
USER.username=d.username
FILE.save(USER,"conf/user","q")
end
-- FILE.save(USERS,"conf/users")
end
--Room
function NET.fetchRoom()
@@ -316,14 +297,14 @@ function NET.updateWS_user()
LOG.print(text.loginSuccessed)
--Get self infos
NET.getUserInfo(USER.uid)
NET.getUserInfo()
NET.unlock("wsc_user")
elseif res.action==0 then--Get accessToken
NET.accessToken=res.accessToken
LOG.print(text.accessSuccessed)
NET.wsconn_play()
elseif res.action==1 then--Get userInfo
NET.storeUserInfo(res.data)
USERS.updateUserData(res.data)
end
else
WS.alert("user")
@@ -490,9 +471,8 @@ function NET.updateWS_stream()
elseif res.action==3 then--Player leave
--?
elseif res.action==4 then--Player died
local uid=res.data.uid
for _,P in next,PLY_ALIVE do
if P.uid==uid then
if P.uid==d.uid then
P:lose(true)
break
end

View File

@@ -20,7 +20,6 @@ scene.widgetList={
if USER.uid then
NET.wsclose_play()
NET.wsclose_user()
USER.username=false
USER.uid=false
USER.authToken=false
FILE.save(USER,"conf/user","q")

View File

@@ -38,7 +38,7 @@ function scene.keyDown(k)
kb.isDown("4")and"r99"or
kb.isDown("5")and"unlimited"
)or"solo",
(USER.username or"???").."'s room"
(USERS.getName(USER.uid)or"???").."'s room"
)
lastCreateRoomTime=TIME()
else

81
parts/users.lua Normal file
View File

@@ -0,0 +1,81 @@
local loadImage=love.graphics.newImage
local fs=love.filesystem
local ins=table.insert
local texture_noImage=DOGC{32,32,
{"rgb",0,0,0},
{"rect","fill",0,0,32,32},
{"rgb",1,1,1},
{"wid",3},
{"line",0,0,31,31},
{"line",0,31,31,0},
}
local function _getEmptyUser()
return{
uid=-1,
username="[X]",
motto="Techmino haowan",
hash=false,
new=false,
}
end
local imgReqSeq={}
local db_img={}
local db=setmetatable({},{__index=function(self,k)
local file="cache/user"..k..".dat"
if fs.getInfo(file)then
rawset(self,k,JSON.decode(fs.read(file)))
if fs.getInfo(self[k].hash)then
db_img[k].avatar=loadImage(self[k].hash)
end
else
rawset(self,k,_getEmptyUser())
end
return self[k]
end})
local USERS={}
function USERS.updateUserData(data)
local uid=data.uid
db[uid].username=data.username
db[uid].motto=data.motto
fs.write("cache/user"..uid..".dat",JSON.encode{
username=data.username,
motto=data.motto,
hash=data.hash,
})
if data.avatar then
fs.write("cache/"..data.hash,data.avatar:sub(data.avatar:find","+1))
db_img[uid].avatar=loadImage("cache/"..data.hash)
db[uid].hash=data.hash
db[uid].new=true
end
needSave=true
end
function USERS.getUsername(uid)return db[uid].username end
function USERS.getMotto(uid)return db[uid].motto end
function USERS.getAvatar(uid)
if db_img[uid]then
return db_img[uid]
else
if not db[uid].new then
ins(imgReqSeq,uid)
db[uid].new=true
end
return texture_noImage
end
end
function USERS.update()
if #imgReqSeq>0 and WS.status("user")=="running"then
NET.getUserInfo(imgReqSeq[#imgReqSeq],true)
imgReqSeq[#imgReqSeq]=nil
end
end
return USERS