c1_lua/lua/app/userdata/activity/activity_data.lua
2025-09-26 09:44:16 +08:00

644 lines
16 KiB
Lua

local ActivityData = class("ActivityData", BaseData)
function ActivityData:ctor()
end
function ActivityData:clear()
self.checkedUIMap = nil
self.taskData = nil
self.taskIds = nil
end
function ActivityData:setDirty()
self.data.isDirty = not self.data.isDirty
end
function ActivityData:getActOpenKey()
return ModuleManager.MODULE_KEY.ACT_ALL_OPEN
end
function ActivityData:init(data)
data = data or {}
end
--region 兑换相关
function ActivityData:initExchangeData(data)
if EDITOR_MODE then
Logger.logHighlight("活动的兑换数据")
Logger.printTable(data)
end
self.exchangeData = data and data.exchange_count or {}
self.exchangeTime = data and data.exchange_at or {}
end
-- 获取已兑换次数
function ActivityData:getExchangeCount(id)
if self.exchangeData == nil then
return 0
end
return self.exchangeData[id] or 0
end
function ActivityData:onActivityExchange(id, count)
if self.exchangeData == nil then
return
end
if not self.exchangeData[id] then
self.exchangeData[id] = 0
end
self.exchangeData[id] = self.exchangeData[id] + (count or 0)
self.exchangeTime[id] = Time:getServerTime()
self:setDirty()
end
function ActivityData:getExchangeTime(id)
if self.exchangeTime == nil then
return 1
end
return self.exchangeTime[id] or 1
end
--endregion
--region
function ActivityData:initTaskData(data)
data = data or {}
data.task_data = data.task_data or {}
if EDITOR_MODE then
Logger.logHighlight("活动任务数据")
Logger.printTable(data)
end
self.taskData = self.taskData or {}
for i, info in ipairs(data.task_data) do
self.taskData[info.activity_id] = self.taskData[info.activity_id] or {}
self.taskData[info.activity_id][info.data.id] = info.data
end
if not self.isInit then
self.isInit = true
-- 监听任务
for id, info in pairs(self:getTaskConfig()) do
ModuleManager.TaskManager:registerTask("ActivityData", info.type, function(count)
self:addTaskProgressCallback(info.type, count)
end)
end
-- 跨天
DataManager:registerCrossDayFunc("ActivityData", function()
self:setDirty()
end)
end
end
function ActivityData:addTaskProgressCallback(taskType, count)
for actId, data in pairs(self.taskData) do
for i, taskId in ipairs(self:getAllTaskIdsByActId(actId)) do
if self:getTaskType(taskId) == taskType then
self:addTaskProgress(actId, taskId, count)
end
end
end
end
function ActivityData:initActivityData(actId, data)
if EDITOR_MODE then
Logger.logHighlight("初始化活动任务数据:" .. actId)
Logger.printTable(data)
end
if self.taskData == nil then
self.taskData = {}
end
for i, info in pairs(data) do
if self.taskData[actId] == nil then
self.taskData[actId] = {}
end
self.taskData[actId][info.id] = info
end
end
function ActivityData:getTaskData(actId, taskId)
if taskId then
if self.taskData[actId] == nil then
self.taskData[actId] = {}
end
if self.taskData[actId][taskId] == nil then
self.taskData[actId][taskId] = {id = taskId, progress = 0, stage_claimed = 0}
end
return self.taskData[actId][taskId]
else
return self.taskData[actId]
end
end
-- 获取活动全部任务id
function ActivityData:getAllTaskIdsByActId(actId)
self.taskIds = self.taskIds or {}
if self.taskIds[actId] == nil then
self.taskIds[actId] = {}
for id, info in pairs(self:getTaskConfig()) do
if self:getActId(id) == actId then
table.insert(self.taskIds[actId], id)
end
end
table.sort(self.taskIds, function(a, b) return a.id < b.id end)
end
return self.taskIds[actId]
end
-- 获取任务当前进度
function ActivityData:getTaskProg(actId, taskId)
taskId = tonumber(taskId)
local data = self:getTaskData(actId, taskId)
return data and data.progress or 0
end
-- 任务是否已领取
function ActivityData:isTaskReceived(actId, taskId)
taskId = tonumber(taskId)
local data = self:getTaskData(actId, taskId)
return data and data.stage_claimed and data.stage_claimed > 0
end
-- 任务是否已完成
function ActivityData:isTaskFinished(actId, taskId)
return self:getTaskProg(actId, taskId) >= self:getTaskTarget(taskId)
end
-- 任务是否可领取
function ActivityData:canClaimTask(actId, taskId)
return self:isTaskFinished(actId, taskId) and not self:isTaskReceived(actId, taskId)
end
-- 重置任务数据
function ActivityData:resetTaskData(actId)
for taskId, data in pairs(self:getTaskData(actId)) do
data.stage_claimed = 0
data.progress = 0
end
self:setDirty()
end
-- 添加任务进度
function ActivityData:addTaskProgress(actId, taskId, count)
if not self:isTaskFinished(actId, taskId) then
local taskType = self:getTaskType(taskId)
local data = self:getTaskData(actId, taskId)
if table.containValue(GConst.TaskConst.TASK_TYPE, taskType) then
data.progress = math.max(data.progress or 0, count)
else
data.progress = (data.progress or 0) + count
end
end
self:setDirty()
end
-- 领奖成功
function ActivityData:onTaskClaimed(actId, taskId)
local data = self:getTaskData(actId, taskId)
data.stage_claimed = 1
self:setDirty()
end
--region 任务配置
function ActivityData:getTaskConfig(taskId)
if taskId then
return ConfigManager:getConfig("act_task")[taskId]
else
return ConfigManager:getConfig("act_task")
end
end
-- 获取任务活动id
function ActivityData:getActId(taskId)
return self:getTaskConfig(taskId).activity
end
-- 获取任务类型
function ActivityData:getTaskType(taskId)
return self:getTaskConfig(taskId).type
end
-- 获取任务目标
function ActivityData:getTaskTarget(taskId)
return self:getTaskConfig(taskId).number_1
end
-- 获取任务奖励
function ActivityData:getTaskReward(taskId)
return self:getTaskConfig(taskId).reward
end
-- 获取任务展示要求
function ActivityData:getTaskDisplay(taskId)
return self:getTaskConfig(taskId).display
end
--endregion
--endregion
function ActivityData:initSkipPopInfo()
self.loginPopSkipFlag = false
local flag = LocalData:getSkipPopFlag()
self.loginPopSkipFlag = flag == 1
local lastTime = LocalData:getSkipPopUITime()
if lastTime ~= Time:getBeginningOfServerToday() then
self.loginPopSkipFlag = false
self.theDayFirstLogin = true
flag = 0
else
self.theDayFirstLogin = false
end
LocalData:setSkipPopFlag(flag)
LocalData:setSkipPopUITime()
end
function ActivityData:getLoginPopSkipFlag()
if not GFunc.IsGotServerTime() then
return false
end
if self.loginPopSkipFlag == nil then
self:initSkipPopInfo()
end
return self.loginPopSkipFlag
end
function ActivityData:setLoginPopSkipFlag(value)
if self.loginPopSkipFlag == value then
return
end
self.loginPopSkipFlag = value
local flag = 0
if self.loginPopSkipFlag then
flag = 1
end
LocalData:setSkipPopFlag(flag)
LocalData:setSkipPopUITime()
end
function ActivityData:getTheDayFirstLogin()
if not GFunc.IsGotServerTime() then
return false
end
if self.theDayFirstLogin == nil then
self:initSkipPopInfo()
end
return self.theDayFirstLogin
end
---- 进入过某个界面
function ActivityData:getCheckUI(moduleName)
if not self.checkedUIMap then
return false
end
return self.checkedUIMap[moduleName]
end
function ActivityData:setCheckUI(moduleName)
if not moduleName then
return
end
if not self.checkedUIMap then
self.checkedUIMap = {}
end
self.checkedUIMap[moduleName] = true
end
---- 活动通用接口
function ActivityData:getActConfig(actId)
if actId then
return ConfigManager:getConfig("activity")[actId]
else
return ConfigManager:getConfig("activity")
end
end
-- 判定活动是否存在
function ActivityData:getActExist(actId)
return self:getActConfig(actId) ~= nil
end
-- 判定活动类型
function ActivityData:getActType(actId)
if not self:getActExist(actId) then
return
end
return self:getActConfig(actId).act_type
end
-- 获取活动ui类型
function ActivityData:getActUIType(actId)
if not self:getActExist(actId) then
return
end
return self:getActConfig(actId).ui_type
end
-- 获得活动时间类型
function ActivityData:getActTimeType(actId)
if not self:getActExist(actId) then
return
end
return self:getActConfig(actId).time_type
end
function ActivityData:getActCfgStartTime(actId)
if not self:getActExist(actId) then
return 0
end
return self:getActConfig(actId).start_time_1 or 0
end
function ActivityData:getActCfgEndTime(actId)
if not self:getActExist(actId) then
return 0
end
return self:getActConfig(actId).end_time_1 or 0
end
function ActivityData:getActCfgExtraTime(actId)
if not self:getActExist(actId) then
return 0
end
return self:getActConfig(actId).extra_time_1 or 0
end
function ActivityData:getActCfgStartTime2(actId)
if not self:getActExist(actId) then
return 0
end
return self:getActConfig(actId).start_time_2
end
function ActivityData:getActCfgEndTime2(actId)
if not self:getActExist(actId) then
return 0
end
return self:getActConfig(actId).end_time_2
end
function ActivityData:getActCfgExtraTime2(actId)
if not self:getActExist(actId) then
return 0
end
return self:getActConfig(actId).extra_time_2
end
function ActivityData:getTurntableAdCount(actId)
if not self:getActExist(actId) then
return 0
end
return self:getActConfig(actId).ad_times or 0
end
function ActivityData:getActFuncOpen(actId)
if not self:getActExist(actId) then
return
end
return self:getActConfig(actId).func_open
end
-- 获取活动开始是否忽略创号天数
function ActivityData:getActIgnoreCreateDay(actId)
if not self:getActExist(actId) then
return
end
return self:getActConfig(actId).circle_limit
end
-- 获取活动相关内容是否可展示
function ActivityData:canShowActContent(actId)
if actId == nil then
return true
end
if not self:getActExist(actId) then
return true
end
local startTime, endTime, extraTime = self:getActTimeInfo(actId)
local hour = GFunc.getConstIntValue("act_show_cultivation_time")
return Time:getServerTime() >= (startTime - hour * 3600)
end
-- 获取活动时间配置信息
function ActivityData:getActTimeInfo(actId)
if actId == nil then
return nil, nil, nil
end
if not self:getActExist(actId) then
return nil, nil, nil
end
local startDay = self:getActCfgStartTime(actId)
local endDay = self:getActCfgEndTime(actId)
local extraDay = self:getActCfgExtraTime(actId)
local startDay2 = self:getActCfgStartTime2(actId)
local endDay2 = self:getActCfgEndTime2(actId)
local extraDay2 = self:getActCfgExtraTime2(actId)
local timeType = self:getActTimeType(actId)
local startTime = 0
local endTime = 0
local extraTime = 0
if timeType == 1 then -- 开服时间
startTime = DataManager.PlayerData:getServerOpenBeginTime()
startTime = startTime + (startDay - 1) * 86400
endTime = startTime + endDay * 86400
extraTime = endTime + extraDay * 86400
elseif timeType == 2 then -- 建号时间
startTime = DataManager.PlayerData:getCreatePlayerBeginTime()
startTime = startTime + (startDay - 1) * 86400
endTime = startTime + endDay * 86400
extraTime = endTime + extraDay * 86400
elseif timeType == 3 then -- 固定时间
startTime = Time:getCertainTimeByStr(startDay2)
endTime = Time:getCertainTimeByStr(endDay2)
extraTime = Time:getCertainTimeByStr(extraDay2)
end
-- Logger.logHighlight(actId)
-- Logger.logHighlight(startTime)
-- Logger.logHighlight(endTime)
return startTime, endTime, extraTime
end
-- 活动是否在开启时间
function ActivityData:getActIsOpen(actId)
local startTime, endTime = self:getActTimeInfo(actId)
if startTime == nil or endTime == nil then
return false
end
return Time:getServerTime() >= startTime and Time:getServerTime() <= endTime
end
-- 活动是否已经结束
function ActivityData:getActIsOver(actId)
local startTime, endTime = self:getActTimeInfo(actId)
if startTime == nil or endTime == nil then
return false
end
return Time:getServerTime() >= endTime
end
-- 获得活动时间戳, 返回即将开的下一次的时间戳
function ActivityData:getActStampTime(actId, customStartTime)
if not self:getActExist(actId) then
return
end
local config = self:getActConfig(actId)
local duration
local pause
if config.circle then
duration = config.circle[1]
pause = config.circle[2]
end
local startDay = self:getActCfgStartTime(actId)
local endDay = self:getActCfgEndTime(actId)
local extraDay = self:getActCfgExtraTime(actId)
local timeType = self:getActTimeType(actId)
local openDay = 1
if timeType == 1 then -- 开服时间
openDay = DataManager.PlayerData:getServerOpenDay()
elseif timeType == 2 then -- 建号时间
openDay = DataManager.PlayerData:getCreateDay()
else
pause = nil
end
if pause then
if customStartTime then
local startTime = customStartTime + duration * 86400 + pause * 86400
local endTime = startTime + duration * 86400
local extraTime = endTime + extraDay * 86400
return startTime, endTime, extraTime
end
while (startDay <= endDay) do
if startDay >= openDay then -- 即将开
break
-- elseif startDay <= openDay and (endDay + extraDay) > openDay then -- 正在开
-- break
else
startDay = startDay + duration + pause
end
end
end
local startTime = 0
local endTime = 0
local extraTime = 0
if timeType == 1 then -- 开服时间
startTime = DataManager.PlayerData:getServerOpenBeginTime()
startTime = startTime + (startDay - 1) * 86400
endTime = startTime + endDay * 86400
extraTime = endTime + extraDay * 86400
elseif timeType == 2 then -- 建号时间
startTime = DataManager.PlayerData:getCreatePlayerBeginTime()
startTime = startTime + (startDay - 1) * 86400
endTime = startTime + endDay * 86400
extraTime = endTime + extraDay * 86400
elseif timeType == 3 then -- 固定时间
startTime = DataManager.ActTimeData:getActStartTime(actId) or 0
endTime = DataManager.ActTimeData:getActEndTime(actId) or 0
extraTime = DataManager.ActTimeData:getActExtraTime(actId) or 0
if startTime < Time:getServerTime() then -- 只需要即将开的
startTime = 0
endTime = 0
extraTime = 0
end
elseif timeType == 4 then -- 基于固定时间顺延(服务器已处理,同3一样即可)
startTime = DataManager.ActTimeData:getActStartTime(actId) or 0
endTime = DataManager.ActTimeData:getActEndTime(actId) or 0
extraTime = DataManager.ActTimeData:getActExtraTime(actId) or 0
if startTime < Time:getServerTime() then -- 只需要即将开的
startTime = 0
endTime = 0
extraTime = 0
end
end
return startTime, endTime, extraTime
end
function ActivityData:getActPushInfo()
if not ModuleManager:getIsOpen(DataManager.ActivityData:getActOpenKey(), true) then
return
end
local nextOpenInfo = {}
local nextExtraInfo = {} -- 只会在当前开着的活动中查找
local ids = self:getCurVersionIDs()
local curTime = Time:getServerTime()
for actType, info in pairs(DataManager.ActTimeData:getActTypeTimeList()) do
local config = self:getActConfig(info.actId)
if config.begin_mail and info.startTime > curTime then
table.insert(nextOpenInfo, {actId = info.actId, actType = config.act_type, startTime = info.startTime, endTime = info.endTime, extraTime = info.extraTime})
elseif config.end_mail and info.startTime <= curTime and info.endTime > curTime then
table.insert(nextExtraInfo, {actId = info.actId, actType = config.act_type, startTime = info.startTime, endTime = info.endTime, extraTime = info.extraTime})
else
for _, id in ipairs(ids) do
local newConfig = self:getActConfig(id)
if newConfig.begin_mail and id >= info.actId and newConfig.type == actType then
local startTime, endTime, extraTime = self:getActStampTime(id, info.startTime)
if startTime and startTime > curTime then
table.insert(nextOpenInfo, {actId = id, actType = config.act_type, startTime = startTime, endTime = endTime, extraTime = extraTime})
end
end
end
end
end
return nextOpenInfo, nextExtraInfo
end
function ActivityData:getCurVersionIDs()
if not self.curVersionIds then
self.curVersionIds = {}
local versionIds = {}
for id, info in pairs(self:getActConfig()) do
if info.version then
if not versionIds[info.version] then
versionIds[info.version] = {}
end
table.insert(versionIds[info.version], id)
end
end
local version = 0
for v, list in pairs(versionIds) do
if v > version then
self.curVersionIds = list
end
end
end
return self.curVersionIds
end
function ActivityData:tryResetActItem(actId)
local cfg = self:getActConfig(actId)
if cfg and cfg.parameter then
local clearItemIds = cfg.parameter
for i, id in ipairs(clearItemIds) do
DataManager.BagData.ItemData:clearItemById(id)
end
end
end
return ActivityData