From ae1dd6dfaa80fa6410e97d6aea5232713e0bc7d4 Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Mon, 19 Apr 2021 21:39:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7users=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E5=B9=B6=E4=BF=AE=E5=A4=8D=E9=97=AE=E9=A2=98=EF=BC=8C=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E5=8A=A0=E8=BD=BD=E7=94=A8=E6=88=B7=E5=A4=B4=E5=83=8F?= =?UTF-8?q?=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- parts/users.lua | 61 ++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/parts/users.lua b/parts/users.lua index c0af85d0..b6d14155 100644 --- a/parts/users.lua +++ b/parts/users.lua @@ -1,31 +1,40 @@ -local loadImage=love.graphics.newImage -local fs=love.filesystem - -local texture_noImage=DOGC{32,32, - {"setCL",0,0,0}, - {"fRect",0,0,32,32}, - {"setCL",1,1,1}, - {"setLW",3}, - {"dLine",0,0,31,31}, - {"dLine",0,31,31,0}, -} - -local function _getEmptyUser() - return{ - username="[X]", - motto="Techmino haowan", - hash="", - new=false, - } +local gc,fs=love.graphics,love.filesystem +local function loadAvatar(path) + local img=gc.newImage(path) + local canvas=gc.newCanvas(128,128) + gc.push() + gc.origin() + gc.setColor(1,1,1) + gc.setCanvas(canvas) + mDraw(img,64,64,nil,128/math.max(img:getWidth(),img:getHeight())) + gc.setCanvas() + gc.pop() + return canvas end +local texture_noImage=DOGC{120,120, + {"setCL",0,0,0}, + {"fRect",0,0,120,120}, + {"setCL",1,1,1}, + {"setLW",6}, + {"dLine",9,9,110,110}, + {"dLine",9,110,110,9}, +} + local db_img={} local db=setmetatable({},{__index=function(self,k) local file="cache/user"..k..".dat" - local d=fs.getInfo(file)and JSON.decode(fs.read(file))or _getEmptyUser() + local d= + fs.getInfo(file)and JSON.decode(fs.read(file))or + { + username="[X]", + motto="Techmino haowan", + hash="", + new=false, + } 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) + if type(d.hash)=="string"and #d.hash>0 and fs.getInfo("cache/"..d.hash)then + db_img[k]=loadAvatar("cache/"..d.hash) end return d end}) @@ -39,12 +48,12 @@ function USERS.updateUserData(data) fs.write("cache/user"..uid..".dat",JSON.encode{ username=data.username, motto=data.motto, - hash=data.hash, + hash=data.hash or db[uid].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=type(data.hash)=="string"and data.hash>0 and data.hash + fs.write("cache/"..data.hash,love.data.decode("string","base64",data.avatar:sub(data.avatar:find","+1))) + db_img[uid]=loadAvatar("cache/"..data.hash) + db[uid].hash=type(data.hash)=="string"and #data.hash>0 and data.hash db[uid].new=true end end