From d3170ab2193a1d9c99df9452e86f2f547f53e4ac Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Sun, 6 Jun 2021 22:32:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=91=E5=AD=98=E6=A1=A3=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=AE=8C=E6=88=90=EF=BC=8C=E6=9A=82=E6=97=B6=E5=8F=AA=E8=83=BD?= =?UTF-8?q?=E9=80=9A=E8=BF=87=E6=8E=A7=E5=88=B6=E5=8F=B0=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zframework/stringExtend.lua | 12 +++- Zframework/tableExtend.lua | 13 ---- parts/net.lua | 117 +++++++++++++++++++++++++++++++++++ parts/scenes/app_console.lua | 2 + 4 files changed, 128 insertions(+), 16 deletions(-) diff --git a/Zframework/stringExtend.lua b/Zframework/stringExtend.lua index 96d4192b..920b899b 100644 --- a/Zframework/stringExtend.lua +++ b/Zframework/stringExtend.lua @@ -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 \ No newline at end of file diff --git a/Zframework/tableExtend.lua b/Zframework/tableExtend.lua index 18d3b359..d3724753 100644 --- a/Zframework/tableExtend.lua +++ b/Zframework/tableExtend.lua @@ -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 \ No newline at end of file diff --git a/parts/net.lua b/parts/net.lua index dc06f611..394e3d02 100644 --- a/parts/net.lua +++ b/parts/net.lua @@ -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 \ No newline at end of file diff --git a/parts/scenes/app_console.lua b/parts/scenes/app_console.lua index b1930bf4..e22d6cc7 100644 --- a/parts/scenes/app_console.lua +++ b/parts/scenes/app_console.lua @@ -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()