云存档功能完成,暂时只能通过控制台使用

This commit is contained in:
MrZ626
2021-06-06 22:32:13 +08:00
parent 2e5f943b79
commit d3170ab219
4 changed files with 128 additions and 16 deletions

View File

@@ -84,9 +84,6 @@ end
function STRING.packBin(s)
return data.encode('string','base64',data.compress('string','zlib',s))
end
function STRING.packText(s)
return data.encode('string','base64',data.compress('string','gzip',s))
end
function STRING.unpackBin(str)
local res
res,str=pcall(data.decode,'string','base64',str)
@@ -94,6 +91,9 @@ function STRING.unpackBin(str)
res,str=pcall(data.decompress,'string','zlib',str)
if res then return str end
end
function STRING.packText(s)
return data.encode('string','base64',data.compress('string','gzip',s))
end
function STRING.unpackText(str)
local res
res,str=pcall(data.decode,'string','base64',str)
@@ -101,5 +101,11 @@ function STRING.unpackText(str)
res,str=pcall(data.decompress,'string','gzip',str)
if res then return str end
end
function STRING.packTable(t)
return STRING.packText(JSON.encode(t))
end
function STRING.unpackTable(t)
return JSON.decode(STRING.unpackText(t))
end
return STRING

View File

@@ -1,4 +1,3 @@
local data=love.data
local next,type=next,type
local TABLE={}
@@ -188,16 +187,4 @@ do--function TABLE.dumpDeflate(L,t)
TABLE.dumpDeflate=dump
end
function TABLE.pack(t)
return STRING.packText(TABLE.dumpDeflate(t))
end
function TABLE.unpack(s)
s=loadstring(STRING.unpackText(s))
if s then
setfenv(s,{})
return s()
end
end
return TABLE

View File

@@ -7,6 +7,8 @@ local yield=YIELD
local NET={
allow_online=false,
accessToken=false,
cloudData={},
roomList={},--Local roomlist, updated frequently
roomState={--A copy of room structure on server
roomInfo={
@@ -163,6 +165,16 @@ function NET.wsconn_stream(srid)
TASK.new(NET.updateWS_stream)
end
end
function NET.wsconn_manage()
if WS.status('wsc_manage')=='dead'then NET.unlock('wsc_manage')end
if NET.lock('wsc_manage',5)then
WS.connect('manage','/manage',JSON.encode{
uid=USER.uid,
authToken=USER.authToken,
},10)
TASK.new(NET.updateWS_manage)
end
end
--Disconnect
function NET.wsclose_app()WS.close('app')end
@@ -225,6 +237,68 @@ function NET.freshPlayerCount()
end
end
--Save
function NET.uploadSave()
if NET.lock('uploadSave',10)then
WS.send('user','{"action":2,"data":{"sections":'..JSON.encode{
{section=1,data=STRING.packTable(STAT)},
{section=2,data=STRING.packTable(RANKS)},
{section=3,data=STRING.packTable(SETTING)},
{section=4,data=STRING.packTable(keyMap)},
{section=5,data=STRING.packTable(VK_org)},
{section=6,data=STRING.packTable(FILE.load('conf/vkSave1'))},
{section=7,data=STRING.packTable(FILE.load('conf/vkSave2'))},
}..'}}')
LOG.print("Uploading")
end
end
function NET.downloadSave()
if NET.lock('downloadSave',10)then
WS.send('user','{"action":3,"data":{"sections":[1,2,3,4,5,6,7]}}')
LOG.print("Downloading")
end
end
function NET.loadSavedData(sections)
for _,sec in next,sections do
if sec.section==1 then
NET.cloudData.STAT=STRING.unpackTable(sec.data)
elseif sec.section==2 then
NET.cloudData.RANKS=STRING.unpackTable(sec.data)
elseif sec.section==3 then
NET.cloudData.SETTING=STRING.unpackTable(sec.data)
elseif sec.section==4 then
NET.cloudData.keyMap=STRING.unpackTable(sec.data)
elseif sec.section==5 then
NET.cloudData.VK_org=STRING.unpackTable(sec.data)
elseif sec.section==6 then
NET.cloudData.vkSave1=STRING.unpackTable(sec.data)
elseif sec.section==7 then
NET.cloudData.vkSave2=STRING.unpackTable(sec.data)
end
end
if STAT.version==NET.cloudData.STAT.version then
TABLE.update(NET.cloudData.STAT,STAT)
FILE.save(STAT,'conf/data')
TABLE.update(NET.cloudData.RANKS,RANKS)
FILE.save(RANKS,'conf/unlock')
TABLE.update(NET.cloudData.SETTING,SETTING)
FILE.save(SETTING,'conf/settings')
TABLE.update(NET.cloudData.keyMap,keyMap)
FILE.save(keyMap,'conf/key')
TABLE.update(NET.cloudData.VK_org,VK_org)
FILE.save(VK_org,'conf/virtualkey')
FILE.save(NET.cloudData.vkSave1,'conf/vkSave1','q')
FILE.save(NET.cloudData.vkSave2,'conf/vkSave2','q')
else
LOG.print(text.versionNotMatch,60)
end
end
--Room
function NET.fetchRoom()
if NET.lock('fetchRoom',3)then
@@ -399,6 +473,13 @@ function NET.updateWS_user()
NET.wsconn_play()
elseif res.action==1 then--Get userInfo
USERS.updateUserData(res.data)
elseif res.action==2 then--Upload successed
NET.unlock('uploadSave')
LOG.print(text.exportSuccess)
elseif res.action==3 then--Download successed
NET.unlock('downloadSave')
NET.loadSavedData(res.data.sections)
LOG.print(text.importSuccess)
end
else
WS.alert('user')
@@ -631,5 +712,41 @@ function NET.updateWS_chat()
end
end
end
function NET.updateWS_manage()
while true do
yield()
if WS.status('manage')=='running'then
local message,op=WS.read('manage')
if message then
if op=='ping'then
NET.pong('manage',message)
elseif op=='pong'then
elseif op=='close'then
wsCloseMessage(message)
return
else
local res=_parse(message)
if res then
if res.type=='Connect'then
LOG.print("Manage connected",'warn')
elseif res.action==0 then
LOG.print("success",'message')
elseif res.action==9 then
LOG.print("success",'message')
elseif res.action==10 then
LOG.print(TABLE.dump(res.data))
elseif res.action==11 then
LOG.print(TABLE.dump(res.data))
elseif res.action==12 then
LOG.print(TABLE.dump(res.data))
end
else
WS.alert('manage')
end
end
end
end
end
end
return NET

View File

@@ -685,6 +685,8 @@ function commands.mng_connInfo()WS.send('manage','{"action":10}')end
function commands.mng_playMgrInfo()WS.send('manage','{"action":11}')end
function commands.mng_streamMgrInfo()WS.send('manage','{"action":12}')end
function commands.upload()NET.uploadSave()end
function commands.download()NET.downloadSave()end
local combKey={}
function combKey.x()