继续完善USERS模块,升级头像相关功能

This commit is contained in:
MrZ626
2021-04-16 10:13:54 +08:00
parent ae41e5f2e6
commit 8d0e89faf9
4 changed files with 17 additions and 29 deletions

View File

@@ -250,6 +250,7 @@ SETTING={--Settings
10,13,2,8
},
face={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
dataSaving=false,
--Graphic
block=true,ghost=.3,center=1,

View File

@@ -120,7 +120,7 @@ function NET.wsclose_stream()
WS.close("stream")
end
--Account
--Account & User
function NET.register(username,email,password)
if NET.lock("register")then
WS.send("app",JSON.encode{
@@ -141,12 +141,13 @@ function NET.getAccessToken()
WS.send("user",JSON.encode{action=0})
end
end
function NET.getUserInfo(id,ifDetail)
function NET.getUserInfo(uid)
local hash=not SETTING.dataSaving and USERS.getHash(uid)
WS.send("user",JSON.encode{
action=1,
data={
id=id,
detailed=ifDetail or false,
id=uid,
hash=hash,
},
})
end
@@ -297,7 +298,7 @@ function NET.updateWS_user()
LOG.print(text.loginSuccessed)
--Get self infos
NET.getUserInfo()
NET.getUserInfo(USER.uid)
NET.unlock("wsc_user")
elseif res.action==0 then--Get accessToken
NET.accessToken=res.accessToken

View File

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

View File

@@ -1,8 +1,6 @@
local loadImage=love.graphics.newImage
local fs=love.filesystem
local ins=table.insert
local texture_noImage=DOGC{32,32,
{"setc",0,0,0},
{"rect","fill",0,0,32,32},
@@ -14,27 +12,22 @@ local texture_noImage=DOGC{32,32,
local function _getEmptyUser()
return{
uid=-1,
username="[X]",
motto="Techmino haowan",
hash=false,
hash="",
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())
local d=fs.getInfo(file)and JSON.decode(fs.read(file))or _getEmptyUser()
rawset(self,k,d)
if type(d.hash)=="string"and #d.hash>0 and fs.getInfo(d.hash)then
db_img[k].avatar=loadImage(d.hash)
end
return self[k]
return d
end})
local USERS={}
@@ -51,31 +44,24 @@ function USERS.updateUserData(data)
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].hash=type(data.hash)=="string"and data.hash>0 and 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.getHash(uid)return db[uid].hash end
function USERS.getAvatar(uid)
if db_img[uid]then
return db_img[uid]
else
if not db[uid].new then
ins(imgReqSeq,uid)
NET.getUserInfo(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