GameConfigMgrProxy.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. local RemoteProxy = requireProxy("remote/RemoteProxy")
  2. local GameConfigMgrProxy = class("GameConfigMgrProxy", RemoteProxy)
  3. GameConfigMgrProxy.NAME = global.ProxyTable.GameConfigMgrProxy
  4. local cjson = require("cjson")
  5. local function getPath()
  6. local LoginProxy = global.Facade:retrieveProxy(global.ProxyTable.Login)
  7. if not LoginProxy then
  8. return "configs_origin"
  9. end
  10. local selectServer = LoginProxy:GetSelectedServer()
  11. if not selectServer then
  12. return "configs_origin"
  13. end
  14. return string.format("configs_%s", selectServer.stab)
  15. end
  16. function GameConfigMgrProxy:ctor()
  17. GameConfigMgrProxy.super.ctor(self)
  18. self._configs = {}
  19. self._serverConfigs = {}
  20. self._updateHandler = {}
  21. self:readLocalConfig()
  22. end
  23. function GameConfigMgrProxy:getConfigByKey(key)
  24. local config = self._configs[key]
  25. if not config then
  26. return nil
  27. end
  28. return config.Value
  29. end
  30. function GameConfigMgrProxy:readLocalConfig()
  31. local path = getPath()
  32. local userData = UserData:new(path)
  33. local jsonStr = userData:getStringForKey("configs", "")
  34. if not jsonStr or string.len(jsonStr) <= 0 then
  35. return
  36. end
  37. local jsonData = cjson.decode(jsonStr)
  38. for k, v in pairs(jsonData) do
  39. self:decodeConfigs(v)
  40. end
  41. end
  42. function GameConfigMgrProxy:saveLocalConfig()
  43. local path = getPath()
  44. local userData = UserData:new(path)
  45. local jsonStr = self:encodeConfigs(self._configs)
  46. userData:setStringForKey("configs", jsonStr)
  47. end
  48. function GameConfigMgrProxy:encodeConfigs()
  49. local jsonStr = cjson.encode(self._configs)
  50. return jsonStr
  51. end
  52. function GameConfigMgrProxy:decodeConfigs(config)
  53. self._configs[config.LableName] = config
  54. end
  55. function GameConfigMgrProxy:getContentFix()
  56. local fixxed = {}
  57. for k, v in pairs(self._configs) do
  58. fixxed[v.LableName] = v.Ver
  59. end
  60. return fixxed
  61. end
  62. function GameConfigMgrProxy:getServerConfingByKey(key)
  63. local config = self._serverConfigs[key]
  64. if not config then
  65. return nil
  66. end
  67. return config
  68. end
  69. function GameConfigMgrProxy:RespGameConfig(msg)
  70. local header = msg:GetHeader()
  71. local jsonData = ParseRawMsgToJson(msg)
  72. if not jsonData then
  73. return
  74. end
  75. self:decodeConfigs(jsonData)
  76. self:saveLocalConfig()
  77. print("++++++++++++++new config, LableName: " .. jsonData.LableName .. " Ver: " .. jsonData.Ver)
  78. -- 更新
  79. local handler = self._updateHandler[jsonData.LableName]
  80. if header.param1 == 1 and handler then
  81. handler()
  82. end
  83. end
  84. function GameConfigMgrProxy:handle_MSG_SC_GAME_DATA_SET(msg)
  85. local jsonData = ParseRawMsgToJson(msg)
  86. if not jsonData then
  87. return
  88. end
  89. if next(jsonData) then
  90. for key, value in pairs(jsonData) do
  91. if tonumber(value) then
  92. global.ConstantConfig[key] = tonumber(value)
  93. else
  94. global.ConstantConfig[key] = value
  95. end
  96. if key == "warehouse_max_num" then
  97. local NPCStorageProxy = global.Facade:retrieveProxy(global.ProxyTable.NPCStorageProxy)
  98. NPCStorageProxy:LoadConfig()
  99. elseif key == "team_num" then
  100. local TeamProxy = global.Facade:retrieveProxy(global.ProxyTable.TeamProxy)
  101. TeamProxy:UpdateMemberMaxNum()
  102. end
  103. end
  104. end
  105. end
  106. function GameConfigMgrProxy:handle_MSG_MSG_SC_SERVER_CONFIG(msg)
  107. local jsonData = ParseRawMsgToJson(msg)
  108. if not jsonData then
  109. return
  110. end
  111. if jsonData.LableName == "cfg_Buff" then
  112. jsonData.LableName = "cfg_buff"
  113. if next(jsonData.Value) then
  114. local data = {}
  115. for i, v in pairs(jsonData.Value) do
  116. data[v.ID] = v
  117. end
  118. self._serverConfigs[jsonData.LableName] = data
  119. end
  120. end
  121. if jsonData.LableName == "cfg_Magic" then
  122. jsonData.LableName = "cfg_magicinfo"
  123. end
  124. end
  125. function GameConfigMgrProxy:RegisterMsgHandler()
  126. GameConfigMgrProxy.super.RegisterMsgHandler(self)
  127. local configKey = "cfg_stditem"
  128. self:RegisterUpdateConfigHandler(configKey, function()
  129. local ItemConfigProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemConfigProxy)
  130. ItemConfigProxy:LoadConfig()
  131. end)
  132. local configKey = "cfg_suit"
  133. self:RegisterUpdateConfigHandler(configKey, function()
  134. local ItemTipsProxy = global.Facade:retrieveProxy(global.ProxyTable.ItemTipsProxy)
  135. ItemTipsProxy:LoadConfig()
  136. end)
  137. local msgType = global.MsgType
  138. LuaRegisterMsgHandler(msgType.MSG_SC_GAME_CONFIG, handler(self, self.RespGameConfig))
  139. LuaRegisterMsgHandler(msgType.MSG_SC_GAME_DATA_SET, handler(self, self.handle_MSG_SC_GAME_DATA_SET))
  140. LuaRegisterMsgHandler(msgType.MSG_SC_SERVER_CONFIG, handler(self, self.handle_MSG_MSG_SC_SERVER_CONFIG))
  141. end
  142. function GameConfigMgrProxy:RegisterUpdateConfigHandler(configKey, callback)
  143. self._updateHandler[configKey] = callback
  144. end
  145. return GameConfigMgrProxy