c1_lua/lua/app/common/data_manager.lua
2023-04-22 00:46:52 +08:00

281 lines
6.7 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local DataManager = {
initByServer = false
}
function DataManager:init()
self.cdCallBack = {}
self._cacheManager = {}
self:initManager("GameSettingData", "app/userdata/game_setting/game_setting_data")
self:initManager("PlayerData", "app/userdata/player/player_data")
self:initManager("ChapterData", "app/userdata/chapter/chapter_data")
self:initManager("HeroData", "app/userdata/hero/hero_data")
self:initManager("BagData", "app/userdata/bag/bag_data")
self:initManager("BattleData", "app/userdata/battle/battle_data")
self:initManager("FormationData", "app/userdata/formation/formation_data")
self:initManager("TutorialData", "app/userdata/tutorial/tutorial_data")
end
function DataManager:initManager(name, path)
if self[name] then
self._cacheManager[name] = self[name]
end
self[name] = require(path):create()
end
function DataManager:checkDataBind()
local changeBindFunc = function(baseData, curBaseData)
local data = baseData.data
if data then
local bindList = baseData.bindList
if bindList then
for fieldName, list in pairs(bindList) do
for _, v in ipairs(list) do
if v.binder.unBind then
v.binder:unBind(baseData, fieldName)
end
if v.binder.bind then
if baseData.data[fieldName] ~= curBaseData.data[fieldName] then
v.binder:bind(curBaseData, fieldName, v.bindFunc, true)
else
v.binder:bind(curBaseData, fieldName, v.bindFunc)
end
end
end
end
end
baseData:clearBindAll()
end
end
if self._cacheManager then -- 如果已经存在就检查一下绑定
for name, baseData in pairs(self._cacheManager) do
if name == "BagData" then
changeBindFunc(baseData.ItemData, self[name].ItemData)
else
changeBindFunc(baseData, self[name])
end
end
end
end
function DataManager:clear()
self.initWithServer = false
if self.cacheTimer then
SchedulerManager:unscheduleGlobal(self.cacheTimer)
self.cacheTimer = nil
end
self.cdCallBack = {}
self.PlayerData:clear()
self.ChapterData:clear()
self.HeroData:clear()
self.BagData:clear()
self.FormationData:clear()
ModuleManager.TaskManager:clear()
end
function DataManager:initWithServerData(data)
self:init()
Time:setServerTimeZone(0)
self.initWithServer = true
if EDITOR_MODE then
Logger.logHighlight("initWithServerData")
Logger.printTable(data)
end
self.PlayerData:init(data.PlayerData)
self.ChapterData:init(data.ChapterData)
self.HeroData:init(data.HeroData)
self.BagData:init(data.BagData)
self.FormationData:init(data.FormationData)
self.TutorialData:init(data.TutorialData)
self:scheduleGlobal()
self:checkDataBind()
end
function DataManager:onServerTimeBack(serverTime, loginCount, loginTime)
self.loginCount = loginCount or 1
self.loginTime = loginTime or Time:getServerTime()
self:scheduleGlobal()
end
-- 是否首次登录
function DataManager:getIsFirstLogin()
local nowTime = Time:getServerTime()
if self.registerTs%86400 == nowTime %8640 and self:getIsTodayFirstLogin() then
return true
end
return false
end
function DataManager:getIsTodayFirstLogin()
return self.todayFirstLogin or false
end
function DataManager:getIsInitWithServer()
return self.initWithServer
end
function DataManager:getLoginTime()
return self.loginTime or 0
end
function DataManager:registerDataCd(dataName)
if not dataName then
return
end
for k, v in ipairs(self.cdCallBack) do
if v == dataName then
return
end
end
table.insert(self.cdCallBack, dataName)
end
function DataManager:unregisterDataCd(dataName)
if not dataName then
return
end
for k, v in ipairs(self.cdCallBack) do
if v == dataName then
table.remove(self.cdCallBack, k)
break
end
end
end
function DataManager:registerCrossDayFunc(bindId, func)
if not bindId or not func then
return
end
if not self.crossDayCallbacks then
self.crossDayCallbacks = {}
end
for i, info in ipairs(self.crossDayCallbacks) do
if info.bindId == bindId then
self.crossDayCallbacks[i].func = func
self.crossDayCallbacks[i].open = true
return
end
end
table.insert(self.crossDayCallbacks,{
bindId = bindId,
func = func,
open = true
})
end
function DataManager:unregisterCrossDayFunc(bindId)
if not bindId then
return
end
if not self.crossDayCallbacks then
return
end
for i, info in ipairs(self.crossDayCallbacks) do
if info.bindId == bindId then
self.crossDayCallbacks[i].open = false
return
end
end
end
function DataManager:scheduleGlobal()
if self.cacheTimer then
return
end
self.crossDayTS = Time:getOverOfServerToday()
self.cacheTimer = SchedulerManager:scheduleGlobal(function (inter)
for k, v in ipairs(self.cdCallBack) do
if self[v] and self[v].updateCd then
self[v]:updateCd()
end
end
if Time:getServerTime() > self.crossDayTS then
self.crossDayTS = Time:getOverOfServerToday()
if self.crossDayCallbacks then
for i, info in ipairs(self.crossDayCallbacks) do
if info.func and info.open then
info.func()
end
end
end
ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.X_LOGIN_DAY)
end
end, 1)
end
-- 获取登录天数
function DataManager:getLoginCount()
return self.loginCount or 1
end
function DataManager:getSignInfo()
local nowTime = Time:getServerTime()
local lastSignTime = self.signInfo.latest_at // 1000
local todayBeginTime = nowTime - nowTime % 86400
local canSign = lastSignTime < todayBeginTime
if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.SIGNIN) then
canSign = false
end
return self.signInfo.count or 0, canSign, self.hasSigned
end
function DataManager:setSignCount(count)
self.hasSigned = true
self.signInfo.count = count
self.signInfo.latest_at = Time:getServerTime() * 1000
--Logger.logHighlight("签到成功次数:"..count)
end
function DataManager:resetSignInInfo()
self.hasSigned = false
end
function DataManager:setLoginSuccess(success)
self.loginSuccess = success
end
function DataManager:getLoginSuccess()
return self.loginSuccess
end
-- 获取建号时间
function DataManager:getCreatePlayerTime()
return self.createPlayerTime or Time:getServerTime()
end
-- 记录sync了多少次数据如果以后游戏中要回到登录界面则此值应当被清除
function DataManager:markSyncDataCount()
if not self.syncDataCount then
self.syncDataCount = 1
else
self.syncDataCount = self.syncDataCount + 1
end
end
function DataManager:getSyncDataCount()
return self.syncDataCount or 0
end
function DataManager:needDealGm()
return self:getSyncDataCount() >= 2
end
function DataManager:getManager(name, path)
if self[name] then
return self[name]
end
self[name] = require(path):create()
return self[name]
end
return DataManager