c1_lua/lua/app/common/data_manager.lua
2023-05-25 17:05:55 +08:00

313 lines
8.3 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")
self:initManager("MailData", "app/userdata/mail/mail_data")
self:initManager("ActivityData", "app/userdata/activity/activity_data")
self:initManager("GoldPigData", "app/userdata/activity/gold_pig/gold_pig_data")
self:initManager("BountyData", "app/userdata/bounty/bounty_data")
self:initManager("DailyTaskData", "app/userdata/task/daily_task_data")
self:initManager("IdleData", "app/userdata/idle/idle_data")
self:initManager("FundData", "app/userdata/fund/fund_data")
-- self:initManager("SevenDayData", "app/userdata/activity/seven_day/seven_day_data")
self:initManager("ShopData", "app/userdata/shop/shop_data")
self:initManager("SummonData", "app/userdata/summon/summon_data")
end
function DataManager:initManager(name, path)
if self[name] then
self._cacheManager[name] = self[name]
end
if name == "BattleData" and self._cacheManager[name] then
else
self[name] = require(path):create()
end
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()
self.ActivityData:clear()
self.MailData:clear()
self.GoldPigData:clear()
self.BountyData:clear()
self.DailyTaskData:clear()
self.IdleData:clear()
self.FundData:clear()
-- self.SevenDayData:clear()
self.ShopData:clear()
self.SummonData:clear()
ModuleManager.TaskManager:clear()
end
function DataManager:initWithServerData(data)
self:init()
Time:setServerTimeZone(0)
Time:updateServerTime(data.now_ts)
Time:updateServerTimeToday(data.today_ts)
Logger.logHighlight("时间数据 -- %s %s", data.now_ts, data.today_ts)
self.initWithServer = true
if EDITOR_MODE then
Logger.logHighlight("initWithServerData")
Logger.printTable(data)
end
self.todayFirstLogin = data.today_first_login
self.PlayerData:init(data)
self.ChapterData:init(data.chapter)
self.HeroData:init(data.bag.heroes)
self.BagData:init(data.bag)
self.FormationData:init(data.fight_info)
self.TutorialData:init(data.guide)
self.MailData:init(data.mail_info)
self.ActivityData:init()
self.GoldPigData:init(data.pig, true)
self.BountyData:init(data.bounty)
-- 任务要在BountyData之后初始化依赖BountyData的数据
self.DailyTaskData:init(data.task_daily)
self.IdleData:init(data.idle)
-- self.SevenDayData:init(data.SevenDayData)
self.ShopData:initBase()
self.ShopData:initActGift(data.act) -- 礼包购买信息
self.ShopData:initMallDaily(data.mall_daily) -- 每日特惠
self.ShopData:initCommonDailyGoldGift(data.mall_idle and data.mall_idle.ad_count) -- 常驻金币礼包
self.ShopData:initGrowUpGift(data.act_grow_up_gift) -- 成长礼包
self.ShopData:initLevelUpGift(data.act_level_up_gift) -- 助力礼包
self.SummonData:init(data.summon, true)
-- 成长基金要在ShopData和PlayerDataBagData还有之后初始化依赖这些数据
if data.fund then
self.FundData:init(data.fund.funds)
end
self:scheduleGlobal()
self:checkDataBind()
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: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
end
end, 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