CloudStorageProxy.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local CloudStorageProxy = class("CloudStorageProxy", RemoteProxy)
  3. CloudStorageProxy.NAME = global.ProxyTable.CloudStorageProxy
  4. local cjson = require("cjson")
  5. --------------------------------------------------------------
  6. --- 数据云存储
  7. --- !!!注意json格式
  8. --- !!!无上传成功返回
  9. --- 将数据存储到服务器 上线时在某个消息发下来
  10. --- 本地也会缓存一份数据,可在不经过服务器做逻辑
  11. --- 请求存储云端时不等服务器返回,默认成功
  12. --------------------------------------------------------------
  13. function CloudStorageProxy:ctor()
  14. CloudStorageProxy.super.ctor(self)
  15. self._data = {}
  16. self:Init()
  17. self._storageTimerID = nil
  18. self._serverTimerID = nil
  19. end
  20. function CloudStorageProxy:Init()
  21. self:loadLocalCache()
  22. end
  23. function CloudStorageProxy:loadLocalCache()
  24. local fileUtil = cc.FileUtils:getInstance()
  25. local writablePath = fileUtil:getWritablePath()
  26. local LoginProxy = global.Facade:retrieveProxy( global.ProxyTable.Login )
  27. local roleID = LoginProxy:GetSelectedRoleID()
  28. local module = global.L_ModuleManager:GetCurrentModule()
  29. local modulePath = module:GetSubModPath()
  30. local path = writablePath .. modulePath ..global.MMO.LOCAL_USERDATA.."cloud_storage_cache_" .. roleID
  31. local jsonStr = global.FileUtilCtl:getStringFromFile(path)
  32. if not jsonStr or string.len(jsonStr) == 0 then
  33. return false
  34. end
  35. local jsonData = cjson.decode(jsonStr)
  36. if not jsonData then
  37. return false
  38. end
  39. --
  40. self._data = jsonData
  41. end
  42. function CloudStorageProxy:storageLocalCache()
  43. if not self._data then
  44. return false
  45. end
  46. if self._storageTimerID then
  47. UnSchedule(self._storageTimerID)
  48. self._storageTimerID = nil
  49. end
  50. self._storageTimerID = PerformWithDelayGlobal(function()
  51. self._storageTimerID = nil
  52. local fileUtil = cc.FileUtils:getInstance()
  53. local writablePath = fileUtil:getWritablePath()
  54. local LoginProxy = global.Facade:retrieveProxy( global.ProxyTable.Login )
  55. local roleID = LoginProxy:GetSelectedRoleID()
  56. local module = global.L_ModuleManager:GetCurrentModule()
  57. local modulePath = module:GetSubModPath()
  58. local path = writablePath .. modulePath .. global.MMO.LOCAL_USERDATA.."cloud_storage_cache_" .. roleID
  59. local jsonStr = cjson.encode(self._data)
  60. global.FileUtilCtl:writeStringToFile(jsonStr, path)
  61. end, 1)
  62. return true
  63. end
  64. function CloudStorageProxy:getStorage(key)
  65. return self._data[key]
  66. end
  67. function CloudStorageProxy:setStorage(key, value)
  68. if type(key) ~= "string" then
  69. print("ERRORRRRRRRRRRRRRRRRRRRR setStorage key is string, have to")
  70. return false
  71. end
  72. self._data[key] = value
  73. self:storageLocalCache()
  74. self:RequestCloudStorage()
  75. end
  76. function CloudStorageProxy:RespCloudStorage(jsonStr)
  77. print(jsonStr)
  78. -- 本地有数据时,不使用服务器数据
  79. if next(self._data) then
  80. return
  81. end
  82. if nil == jsonStr or "" == jsonStr then
  83. return nil
  84. end
  85. local jsonData = cjson.decode(jsonStr)
  86. if nil == jsonData then
  87. return nil
  88. end
  89. self._data = jsonData
  90. -- 同步本地缓存
  91. self:storageLocalCache()
  92. -- 广播
  93. global.Facade:sendNotification(global.NoticeTable.CloudStorageInit)
  94. end
  95. function CloudStorageProxy:RequestCloudStorage()
  96. if self._serverTimerID then
  97. UnSchedule(self._serverTimerID)
  98. self._serverTimerID = nil
  99. end
  100. self._serverTimerID = PerformWithDelayGlobal(function()
  101. self._serverTimerID = nil
  102. local jsonStr = cjson.encode(self._data)
  103. if not jsonStr then
  104. return nil
  105. end
  106. print("jsonStr", jsonStr)
  107. -- out of buffer size limit
  108. if string.len(jsonStr) > 4096 then
  109. return nil
  110. end
  111. LuaSendMsg(global.MsgType.MSG_CS_CLOUD_STORAGE_CHANGE, 0, 0, 0, 0, jsonStr, string.len(jsonStr))
  112. end, 1)
  113. end
  114. function CloudStorageProxy:RegisterMsgHandler()
  115. CloudStorageProxy.super.RegisterMsgHandler(self)
  116. local msgType = global.MsgType
  117. end
  118. ---------------------------------------------------------------
  119. function GET_CLOUD_DATA(key)
  120. local CloudStorageProxy = global.Facade:retrieveProxy(global.ProxyTable.CloudStorageProxy)
  121. return CloudStorageProxy:getStorage(key)
  122. end
  123. function SET_CLOUD_DATA(key, value)
  124. local CloudStorageProxy = global.Facade:retrieveProxy(global.ProxyTable.CloudStorageProxy)
  125. CloudStorageProxy:setStorage(key, value)
  126. end
  127. return CloudStorageProxy