You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.9 KiB

  1. local SubGameConfigManager = class("SubGameConfigManager")
  2. function SubGameConfigManager:ctor()
  3. self.subGameConfigs = {}
  4. self.subGameConfigPath = "dataconfig"
  5. self.subGameConfigFileName = "SubGameConfig_%03d.json"
  6. self.isInited = false
  7. end
  8. -- 初始化所有子游戏的配置信息
  9. function SubGameConfigManager:init()
  10. logD("SubGameConfigManager:init()")
  11. self.subGameConfigs = {}
  12. -- 先使用的本地的数据初始化
  13. local subGameIds = app.serverConfigs:getSubGameIds()
  14. for _, gameId in pairs(subGameIds) do
  15. self:_initSubGameConfig(gameId)
  16. end
  17. -- 然后获取服务器最新的数据
  18. self:_requestLastestConfigs()
  19. end
  20. -- 获取单个子游戏的配置信息
  21. -- 如果配置未找到,则返回nil
  22. function SubGameConfigManager:getSubGameConfig(gameId)
  23. logD("SubGameConfigManager:getSubGameConfig(), gameId = " .. tostring(gameId))
  24. gameId = tonumber(gameId)
  25. if not gameId or gameId <= 0 or gameId >= 1000 then
  26. return nil
  27. end
  28. return self.subGameConfigs[gameId]
  29. end
  30. -- 初始化单个子游戏的配置信息
  31. function SubGameConfigManager:_initSubGameConfig(gameId)
  32. logD("SubGameConfigManager:_initSubGameConfig(), gameId = " .. tostring(gameId))
  33. gameId = tonumber(gameId)
  34. if not gameId or gameId <= 0 or gameId >= 1000 then
  35. return
  36. end
  37. local writablePath = cc.FileUtils:getInstance():getWritablePath();
  38. local jsonFileName = string.format(self.subGameConfigFileName, gameId)
  39. local jsonFilePath = self.subGameConfigPath .. "/" .. jsonFileName
  40. if isFileExist(jsonFilePath) then
  41. -- 读取文本数据
  42. local fileString = getFileData(jsonFilePath)
  43. if fileString == nil or fileString == "" then
  44. logD("SubGameConfigManager:_initSubGameConfig(), fileString = " .. tostring(fileString))
  45. return
  46. end
  47. -- 非WIN32要解密
  48. if not isWin32Platform() then
  49. fileString = cc.FilePackage:decrypt(fileString)
  50. end
  51. -- 转json对象
  52. local jsonData = json.decode(fileString)
  53. if not jsonData or type(jsonData) ~= "table" then
  54. logD("SubGameConfigManager:_initSubGameConfig(), jsonData = " .. table.tostring(jsonData))
  55. return
  56. end
  57. self.subGameConfigs[gameId] = jsonData
  58. else
  59. logD("SubGameConfigManager:_initSubGameConfig(), file not exist, gameId = " .. gameId)
  60. end
  61. end
  62. -- 获取所有子游戏的最新配置信息
  63. function SubGameConfigManager:_requestLastestConfigs()
  64. if self.isInited then
  65. return
  66. end
  67. logD("SubGameConfigManager:_requestLastestConfigs()")
  68. local subGameIds = app.serverConfigs:getSubGameIds()
  69. local versionString = ""
  70. for _, gameId in pairs(subGameIds) do
  71. local subGameVersion = app.subGameManager:getSubGameVersionNum(gameId)
  72. if versionString == "" then
  73. versionString = tostring(gameId) .. "," .. tostring(subGameVersion)
  74. else
  75. versionString = versionString .. "|" .. tostring(gameId) .. "," .. tostring(subGameVersion)
  76. end
  77. end
  78. local tt =
  79. {
  80. action = "system.gamestruct",
  81. apkver = getAppVersionNum(),
  82. app = getAppId(),
  83. uid = loadUserId(),
  84. gameid = versionString,
  85. }
  86. logD("SubGameConfigManager:_requestLastestConfigs() tt = ", table.tostring(tt))
  87. httpPost(getGlobalPhpUrl(), tt, handler(self, self.onGetLastestConfigsResponse))
  88. end
  89. function SubGameConfigManager:onGetLastestConfigsResponse(status, response)
  90. logD(string.format("SubGameConfigManager:onGetLastestConfigsResponse(), status = %s response = %s", tostring(status), tostring(response)))
  91. if status ~= "successed" or not response then
  92. return
  93. end
  94. local stResponse = json.decode(response)
  95. if not stResponse or type(stResponse) ~= "table" or not stResponse.result or type(stResponse.result) ~= "table" then
  96. return
  97. end
  98. -- 标记为初始化完成
  99. self.isInited = true
  100. for gameId,gameConfig in pairs(stResponse.result) do
  101. if gameConfig and type(gameConfig) == "table" and table.nums(gameConfig) > 0 then
  102. self:_saveSubGameConfig(gameId, gameConfig)
  103. end
  104. end
  105. -- 重新初始化,将最新的数据读入内存
  106. self:init()
  107. end
  108. -- 保存单个子游戏的配置信息到缓存文件
  109. function SubGameConfigManager:_saveSubGameConfig(gameId, jsonData)
  110. logD("SubGameConfigManager:_saveSubGameConfig(), gameId = " .. tostring(gameId))
  111. logD("SubGameConfigManager:_saveSubGameConfig(), jsonData = " .. tostring(jsonData))
  112. gameId = tonumber(gameId)
  113. if not gameId or gameId <= 0 or gameId >= 1000 then
  114. return
  115. end
  116. if jsonData == nil or jsonData == "" then
  117. return
  118. end
  119. local fileString = json.encode(jsonData)
  120. -- 非WIN32要加密保存
  121. if not isWin32Platform() then
  122. fileString = cc.FilePackage:encrypt(fileString)
  123. end
  124. local writablePath = cc.FileUtils:getInstance():getWritablePath();
  125. local subGameConfigPath = writablePath .. self.subGameConfigPath
  126. if not cc.FileUtils:getInstance():isDirectoryExist(subGameConfigPath) then
  127. cc.FileUtils:getInstance():createDirectory(subGameConfigPath)
  128. end
  129. local jsonFileName = string.format(self.subGameConfigFileName, gameId)
  130. local jsonFilePath = self.subGameConfigPath .. "/" .. jsonFileName
  131. saveStringToFile(fileString, jsonFilePath)
  132. end
  133. return SubGameConfigManager