前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >简单的每周限时活动管理

简单的每周限时活动管理

原创
作者头像
深雾
修改2020-07-21 10:58:36
3.9K0
修改2020-07-21 10:58:36
举报
文章被收录于专栏:工具类工具类

前言

接到一个需求的时候,首先会想有没有类似功能,或者以后再碰到这类功能如何处理,这次接到一个每周限时活动的任务,把具有共通点的活动时间统一管理,以后写活动只需专注业务功能即可

每周活动限时管理

需求:活动日程为每周x日x点到y点

时间数据

时间放在全局配置表,对应活动id

代码语言:lua
复制
--全局变量
SettingConfig.SettingType = {
    LuckyShop              = 1, --幸运商店
    TokenTimeMgr          = 2, --战令活动时间管理
    WeeklyActivityMgr     = 3, --限时活动管理
}

function WeeklyActivityMgr:Init()
    --获取库数据
    self.cache = server.settingCenter:GetData(SettingConfig.SettingType.WeeklyActivityMgr)
    --初始化活动数据
    self.cache = self.cache or {}
    self.cache.data = self.cache.data or {}
    for _, id in pairs(WeeklyActivityConfig.ActiveType) do
        self.cache.data[id] = self.cache.data[id] or {id = id,state = 0,startTime = 0,endTime = 0}
    end
    server.settingCenter:SetData(SettingConfig.SettingType.WeeklyActivityMgr,self.cache.data)
end

设置活动开始和结束时间

方式:每天判定当日有活动,则设置当日活动时间

代码语言:lua
复制
--每天检测活动
function WeeklyActivityMgr:onDayTimer()
    self:RefreshAcitvityTime()
    self:SendClientMsg()
end

--设置当天活动时间
function WeeklyActivityMgr:RefreshAcitvityTime()
    if not self.cache or not self.cache.data then
        return
    end
    for id, data in pairs(self.cache.data) do
        if self:isTodayOpen(id) then
            --刷新今天活动开启时间
            self:SetActivityTime(data)
        end
    end
end

--今天活动是否开启
function WeeklyActivityMgr:isTodayOpen(activityid)
    local acitvitycfg = self:GetAcitvitiTimeConfig(activityid)
    local isweekday = false
    --判断周几
    local nowTime = lua_app.now()
    local today = tonumber(os.date("%d",nowTime))
    local month = tonumber(os.date("%m",nowTime))
    local year = tonumber(os.date("%Y",nowTime))
    if acitvitycfg.week then
        local weekday = lua_util.getWeekComm(year,month,today)
        --本日是否开启
        for _, day in pairs(acitvitycfg.week) do
            if weekday == day then
                isweekday = true
                break
            end
        end
    else
        isweekday = true
    end
    return isweekday
end

活动时间设置

代码语言:lua
复制
--设置活动时间
function WeeklyActivityMgr:SetActivityTime(data)
    local acitvitycfg = self:GetAcitvitiTimeConfig(data.id)
    local nowTime = lua_app.now()
    local today = tonumber(os.date("%d",nowTime))
    local month = tonumber(os.date("%m",nowTime))
    local year = tonumber(os.date("%Y",nowTime))
    if acitvitycfg.time2 then
        --此时是否为开启时间段
        -- 指定时间的时间戳
        local startmin = acitvitycfg.time2[1]
        local endmin = acitvitycfg.time2[2]
        local startTime = os.time({day=today,month=month,year=year,hour=startmin[1],min=startmin[2],sec=0})
        local endTime = os.time({day=today,month=month,year=year,hour=endmin[1],min=endmin[2],sec=0})
        if data.startTime == startTime then
            -- 已经设置好了
            return
        end
        data.startTime = startTime
        data.endTime = endTime - 1
    end
    return true
end

定时器管理活动状态

这里可以活动触发事件,传activityid触发活动开始或结束事件

代码语言:lua
复制
function WeeklyActivityMgr:SecondTimer()
    if self.sectimer then
        lua_app.del_timer(self.sectimer)
        self.sectimer = nil
    end
    local function _DoSecond()
        self.sectimer = lua_app.add_timer(5 * 1000, _DoSecond)
        self:CheckTerm()
    end
    self.sectimer = lua_app.add_timer(5 * 1000, _DoSecond)
end

-- 检测活动时间
function WeeklyActivityMgr:CheckTerm()
    for _, data in pairs(self.cache.data) do
        if data.state == 0 then
            --当达到开启时间刷新
            if lua_app.now() >= data.startTime and lua_app.now() < data.endTime then
                --活动开启初始化数据 最好是事件
                data.state = 1
            end
        elseif data.state == 1 then
            --超过结束时间刷新
            if lua_app.now() > data.endTime then
                --活动关闭处理
                data.state = 0
            end
        end
    end
end

羹火活动示例

当活动开始,updateTime没更新时活动重置,活动结束时出啊一次AcitvityEnd,这里需要每分钟场景发放一次奖励,就一起放定时器了

代码语言:lua
复制
-- 检测活动时间
function GuildBonfireCtrl:CheckTerm()
    local data = self:GetActivityTime()
    if not data then
        return
    end
    if data.state == 1 then
        --是否需要重置 下次活动开始时间
        if self.cache.updateTime < data.startTime then
            self:AcitvityResert()
        end
        self:SendReward()
        self:SendClientMsg()
    elseif data.state == 0 then
    --活动关闭,是否触发活动结束
    if self.cache.updateTime >= data.startTime and  self.cache.updateTime <= data.endTime and lua_app.now() >= data.endTime then
        self:AcitvityEnd()
        end
    end
end

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 每周活动限时管理
    • 时间数据
      • 设置活动开始和结束时间
        • 定时器管理活动状态
        • 羹火活动示例
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档