-------------------------------- -- 文件名 : ServerCommerceActManger.lua -- 文件说明 : 跨服商业-活动模板管理 -- 创建时间 : 2025/03/26 -- 创建人 : FC -------------------------------- local Util = require("common.Util") local Lang = require("common.Lang") local Broadcast = require("broadcast.Broadcast") local MailExcel = require("excel.mail") local Msg = require("core.Msg") local ObjHuman = require("core.ObjHuman") local MailManager = require("mail.MailManager") local BagLogic = require("bag.BagLogic") local Grid = require("bag.Grid") local PanelDefine = require("broadcast.PanelDefine") local CommonDB = require("common.CommonDB") local BuyLogic = require("topup.BuyLogic") local GuideLogic = require("guide.GuideLogic") local Log = require("common.Log") local YunYingLogic = require("yunying.YunYingLogic") local ServerCommerceCof = require("excel.ServerCommerce") local ServerCommerceActDefine = require("serverCommerce.ServerCommerceActDefine") local Timer = require("core.Timer") local ServerCommerceMiddle = require("serverCommerce.ServerCommerceMiddle") local ServerCommerceActCharge = require("serverCommerce.ServerCommerceActCharge") local ServerCommerceActRank = require("serverCommerce.ServerCommerceActRank") -- 活动信息 tCommerceActInfo = nil -- { -- nStartTime = nil, -- 开始时间 -- nEendTime = nil, -- 结束时间 -- isRun = nil, -- 是否在活动中 -- nOpen = nil, -- 是否打开 -- nBatchID = nil, -- 批次 -- } -- 加载的模块 tCommerceActModuel = {} ----------------------------------------- 内部处理开始 ------------------------------------- -- 下发数据 local function CommerceAct_SendData(tMsgData, fd) Msg.send(tMsgData, fd) end -- 创建玩家DB数据 function CommerceAct_CreateHumanDB(human) local nBatchID = 1 local tCommonDBData = CommonDB.GetCommerceActInfo() if tCommonDBData and nil ~= _G.next(tCommonDBData) then nBatchID = tCommonDBData.nBatchID end human.db.ServerCommerce = { nPoint = 0, -- 当前玩家个人积分 Task = {}, -- 任务信息 Charge = {}, -- 连充豪礼 Shop = {}, -- 战区钜惠 nBatchID = nBatchID, -- 批次 bSendPlayerMail = false, -- 是否发送个人邮件 bSendServerMail = false, -- 是否发送全服邮件 } for nID, module in pairs(tCommerceActModuel) do if module and module.CreatDB then local bRet = module.CreatDB(human) if false == bRet then print("[CommerceAct_CreateHumanDB] nID 初始化DB 数据失败 nID = "..nID) end end end end -- 下发活动数据 function CommerceAct_SendActInfo(human) if not human or not tCommerceActInfo then return end print("[CommerceAct_SendActInfo] 下发活动数据 开始 ") local tMsgData = Msg.gc.GC_SERVEERCOMMERCE_ACT_ALLINFO tMsgData.nStartTime = tCommerceActInfo.nStartTime tMsgData.nEendTime = tCommerceActInfo.nEendTime tMsgData.tActID[0] = #ServerCommerceCof.CommerceAct local nIndex = 1 for id, v in pairs(ServerCommerceCof.CommerceAct) do local tActData = tMsgData.tActID[nIndex] nIndex = nIndex + 1 tActData.ID = id tActData.name = v.name tActData.nSortID = v.sortID tActData.nIcon = v.icon tActData.nPanelID = v.panelID local bRed = false if tCommerceActModuel[id] and tCommerceActModuel[id].isRed then bRed = tCommerceActModuel[id].isRed(human) end print("[CommerceAct_SendActInfo] name = "..tActData.name) tActData.nRed = (bRed == true) and 1 or 0 end YunYingLogic.sendBanner(human) CommerceAct_SendData(tMsgData, human.fd) print("[CommerceAct_SendActInfo] 下发活动数据 结束 ") end -- 各个子活动重置活动数据 local function CommerceAct_ResetData(human) if not human then return end end -- 各个子活动处理结束数据 local function CommerceAct_HandleEndData(human) if not human then return end end -- 所有活动初始化数据开始 local function CommerceAct_BeginAllAct(human) if not human or not tCommerceActInfo then return end CommerceAct_CreateHumanDB(human) print("[CommerceAct_BeginAllAct] 所有活动初始化数据开始 ") end -- 结束所有活动 local function CommerceAct_EndAllAct(human) -- 各个子活动处理数据 CommerceAct_HandleEndData(human) end -- 活动开始 local function CommerceAct_Begin() if not tCommerceActInfo then return end for nID, module in pairs(tCommerceActModuel) do if module and module.ClearCache then module.ClearCache() end end -- 遍历在线玩家 for uuid, human in pairs(ObjHuman.onlineUuid) do CommerceAct_BeginAllAct(human) end end -- 活动结束 local function CommerceAct_End() -- 排行榜结束 ServerCommerceMiddle.CommerceMiddle_QueryServerRank(ServerCommerceActDefine.COMMERCEACT_SENDSERVERMAIL) end -- 延迟向中心服请求数据 local function CommerceAct_LaterTimeQuery(nOperate) print("[CommerceAct_LaterTimeQuery] 延迟向中心服请求数据开始") Timer.addLater(ServerCommerceActDefine.COMMERCEACT_INITSERVERTIME, CommerceAct_GetActOpen, nOperate) end -- 延迟10分钟开始 local function CommerceAct_LaterBeginAct() if not tCommerceActInfo then return end tCommerceActInfo.isRun = true CommerceAct_Begin() end -- 检查活动是否过期 local function CommerceAct_CheckActIsOverTime() local tCommerceDBInfo = CommonDB.GetCommerceActInfo() if not tCommerceDBInfo or nil == _G.next(tCommerceDBInfo) then return true end local nEndTime = tCommerceDBInfo.nEndTime local nNowTime = os.time() return nNowTime >= nEndTime end -- 检查是否开启下一轮活动 local function CommerceAct_CanOpenNext() local tCommerceDBInfo = CommonDB.GetCommerceActInfo() if not tCommerceDBInfo or nil == _G.next(tCommerceDBInfo) then return true end local nDiffDay = Util.diffDay(tCommerceDBInfo.nEndTime) if nDiffDay >= ServerCommerceActDefine.COMMERCEACT_NEXTDAY then return true end return false end -- 创建通用DB数据 local function CommerceAct_CreateCommonDB() local nNowTime = os.time() local tCommerceInfo = CommonDB.GetCommerceActInfo() local nEndTime = nNowTime + ServerCommerceActDefine.COMMERCEACT_LASTDAY * 86400 local tEndDate = os.date("*t",nEndTime) tEndDate.hour = ServerCommerceActDefine.COMMERCEACT_ENDTIME tEndDate.min = 0 tEndDate.sec = 0 nEndTime = os.time(tEndDate) local tDBData = { nBeginTime = nNowTime, nEndTime = nEndTime, nPoint = 0, nSendRankMail = false, } if not tCommerceInfo or nil == _G.next(tCommerceInfo) then tDBData.nBatchID = 1 else tDBData.nBatchID = tDBData.nBatchID + 1 end CommonDB.SetCommerceActInfo(tDBData) end -- 创建缓存数据 local function CommerceAct_CreateCacheInfo(nOpen, bLater) local tCommerceInfo = CommonDB.GetCommerceActInfo() if not tCommerceInfo or nil == _G.next(tCommerceInfo) then print("[CommerceAct_CreateCacheInfo] 为什么会不存在数据\n") return end tCommerceActInfo = {} tCommerceActInfo.nStartTime = tCommerceInfo.nBeginTime tCommerceActInfo.nEendTime = tCommerceInfo.nEndTime tCommerceActInfo.isRun = bLater == true and false or true tCommerceActInfo.nOpen = nOpen tCommerceActInfo.nBatchID = tCommerceInfo.nBatchID print("[CommerceAct_CreateCacheInfo] 创建缓存数据 nStartTime = "..tCommerceActInfo.nStartTime.." nEendTime = "..tCommerceActInfo.nEendTime .." nOpen = "..tCommerceActInfo.nOpen.." nBatchID = " ..tCommerceActInfo.nBatchID.." isRun = "..(tCommerceActInfo.isRun == true and 1 or 0)) if bLater == true then Timer.addLater(ServerCommerceActDefine.COMMERCEACT_BEGINDELATTIME, CommerceAct_LaterBeginAct) end end -- 清理排行榜数据 local function CommerceAct_ClearRankInfo() ServerCommerceMiddle.CommerceMiddle_ClearRank() end -- 起服初始化数据 local function CommerceAct_ActCheckOpen_InitServer(nOpen, nOperate) if ServerCommerceActDefine.COMMERCEACT_NOOPEN ~= nOpen then local tCommerceInfo = CommonDB.GetCommerceActInfo() -- 不存在数据 if not tCommerceInfo or nil == _G.next(tCommerceInfo) then print("[CommerceAct_ActCheckOpen_InitServer] 不存在commonDB数据开始创建") CommerceAct_CreateCommonDB() else -- 存在数据检查是否结束 if true == CommerceAct_CheckActIsOverTime() then local bOpenNext = CommerceAct_CanOpenNext() if true == bOpenNext then -- 开启下一轮 if tCommerceInfo.nBatch < ServerCommerceActDefine.COMMERCEACT_ENDBATCH then CommerveManager_WriteLog("[CommerveManager-onZeroAll] 当前条件满足开启下一轮活动") CommerceAct_CreateCommonDB() -- 清理排行榜数据 CommerceAct_ClearRankInfo() end end end end local bLater = nOperate == ServerCommerceActDefine.COMMERCEACT_ZERO and true or false CommerceAct_CreateCacheInfo(nOpen, bLater) ServerCommerceActRank.CommercerActRank_InitServerQueryRank() end end -- 获取玩家当前点数 local function CommerceAct_GetHumanPoint(human) return human.db.ServerCommerce.nPoint end -- 设置玩家当前点数 local function CommerceAct_SetHumanPoint(human, nValue) human.db.ServerCommerce.nPoint = nValue end ----------------------------------------- 外部调用开始 ------------------------------------- function onZeroAll(funcID) local nNowTime = os.time() local tDate = os.date("*t",nNowTime) -- 活动未开启 if tDate.hour == ServerCommerceActDefine.COMMERCEACT_BEGINTIME then -- 获取DB数据 local tCommerceDBInfo = CommonDB.GetCommerceActInfo() if not tCommerceDBInfo or nil == _G.next(tCommerceDBInfo) then CommerceAct_LaterTimeQuery(ServerCommerceActDefine.COMMERCEACT_ZERO) return end -- 存在DB数据说明已经满足条件了 if true == CommerceAct_CheckActIsOverTime() then local bOpenNext = CommerceAct_CanOpenNext() if false == bOpenNext then return end -- 开启下一轮 if tCommerceDBInfo.nBatch < ServerCommerceActDefine.COMMERCEACT_ENDBATCH then CommerveManager_WriteLog("[CommerveManager-onZeroAll] 当前条件满足开启下一轮活动") CommerceAct_CreateCommonDB() -- 存在数据说明时间是满足的 CommerceAct_CreateCacheInfo(ServerCommerceActDefine.COMMERCEACT_OPEN, true) -- 清理排行榜数据 CommerceAct_ClearRankInfo() end end elseif tDate.hour == ServerCommerceActDefine.COMMERCEACT_ENDTIME then if not tCommerceActInfo or tCommerceActInfo.nOpen == ServerCommerceActDefine.COMMERCEACT_NOOPEN then return end local tCommerceDBInfo = CommonDB.GetCommerceActInfo() if not tCommerceDBInfo or nil == _G.next(tCommerceDBInfo) then print("[CommerceAct_onZeroAll] 存在缓存数据, 但是获取不到DB数据") return end local nEndTime = tCommerceDBInfo.nEndTime if nEndTime <= nNowTime then -- 活动结束处理 CommerceAct_End() tCommerceActInfo.nSendRankMail = true end end end -- 请求活动打开信息 function CommerceAct_GetActOpen(nOperate) print("[CommerceAct_GetActOpen] 开始向中心服请求活动数据") ServerCommerceMiddle.CommerceMiddle_IsActOpen(nOperate) end -- 中心服回复活动信息 function CommerceAct_ActCheckOpen(nOpen, nOperate) print("[CommerceAct_ActCheckOpen] 收到活动信息开始处理 nOpen = "..nOpen.." nOperate ="..nOperate) CommerceAct_ActCheckOpen_InitServer(nOpen, nOperate) end -- 起服初始化 function CommerceAct_Init() if _G.is_middle == true then -- 中心服起服获取信息 ServerCommerceMiddle.CommerceMiddle_InitServer() return end local nNowTime = os.time() for nID, v in pairs(ServerCommerceCof.CommerceAct) do if v.moduleFn then local moduleFn = load("return require(\"" .. v.moduleFn .. "\")")() if moduleFn then tCommerceActModuel[nID] = moduleFn if moduleFn.Init then local bRet = moduleFn.Init() if false == bRet then print("[CommerceAct_Init] 初始化模块数据失败 nID ".. nID) end end print("[CommerceAct_Init] 加载模块成功 name = "..v.name) else print("[CommerceAct_Init] 加载模块失败 nID ".. nID) end else print("[CommerceAct_Init] 未配置 name = "..v.name) end end print("[CommerceAct_Init] 延迟进行请求数据操作") -- 延迟向中心服请求数据 CommerceAct_LaterTimeQuery(ServerCommerceActDefine.COMMERCEACT_SERVEROPEN) end -- 是否还在活动期间 function CommerceAct_IsRun() if not tCommerceActInfo then return false end return tCommerceActInfo.isRun end -- 玩家登录 function onLogin(human, funcID) if not human then return end -- 下发活动基础数据 if true == CommerceAct_IsRun() then local tCommonDBData = CommonDB.GetCommerceActInfo() if not tCommonDBData or nil == _G.next(tCommonDBData) then print("[onLogin] 玩家登录不存在对应的数据通用DB数据!!!!") return end -- 不存在数据直接创建 if not human.db.ServerCommerce then CommerceAct_CreateHumanDB(human) else -- 用记在玩家身上的批次 和 CommonDB 中的数据比较 local nOldBatchID = human.db.ServerCommerce.nBatchID local nNowBatchID = tCommonDBData.nBatchID if nNowBatchID ~= nOldBatchID then CommerceAct_CreateHumanDB(human) end end CommerceAct_SendActInfo(human) for _, module in pairs(tCommerceActModuel) do if module and module.onLogin then module.onLogin(human) end end else -- 不在活动中 local tCommonDBData = CommonDB.GetCommerceActInfo() if not tCommonDBData or nil == _G.next(tCommonDBData) then print("[onLogin] 玩家登录不存在对应的数据通用DB数据!!!!") return end -- 不存在对应DB数据 if not human.db.ServerCommerce then return end -- 未发送个人邮件都是未上榜的 local bSendPlayerMail = CommerveManager_GetHumanSendPlayerMail(human) if false == bSendPlayerMail then CommerveManager_SetHumanSendPlayerMail(human, true) ServerCommerceActRank.CommercerActRank_SendHumanMailHumanLogin(human) end -- 全服邮件 local bSendServerMail = CommerveManager_GetHumanSendServerMail(human) if false == bSendServerMail and true == tCommonDBData.bSendServerMail then CommerveManager_SetHumanSendServerMail(human, true) ServerCommerceActRank.CommercerActRank_SendServerMailHumanLogin(human) end end end -- 是否开启 function isOpen(human, YYInfo, funcConfig) local bRet = CommerceAct_IsRun() local nRet = bRet and 1 or 0 print("CommerceAct_isOpen 结束 nRet = "..nRet) return bRet end -- function isActive(human, YYInfo, funcConfig) return not isOpen(human, YYInfo, funcConfig) end function isRed(human, YYInfo, funcConfig) local bRet = false for _, module in pairs(tCommerceActModuel) do if module and module.isRed then bRet = module.isRed(human) if true == bRet then break end end end return bRet end function onCharge(human, price, funcID, buyID) if not human then return end if false == CommerceAct_IsRun() then return end print("[onCharge] 跨服商业活动 玩家充值回调开始 name = "..human.db.name.." price = "..price) for _, module in pairs(tCommerceActModuel) do if module and module.onCharge then module.onCharge(human, price, funcID, buyID) end end end -- 增加整个服务器的点数 function CommerveManager_AddServerPoint(nAddPoint) local tCommonDBData = CommonDB.GetCommerceActInfo() local nNewPoint = tCommonDBData.nPoint + nAddPoint CommonDB.SetCommerceActInfo_Point(nNewPoint) for _, module in pairs(tCommerceActModuel) do if module and module.onAllPointChange then module.onAllPointChange() end end end -- 增加个人积分 function CommerveManager_AddHumanPint(human, nPoint) local nNowPoint = CommerceAct_GetHumanPoint(human) local nNewPoint = nNowPoint + nPoint CommerceAct_SetHumanPoint(human ,nNewPoint) ServerCommerceActRank.CommercerActRank_HumanPointChange(human, nNewPoint) end -- 写日志 function CommerveManager_WriteLog(szLogText, human) if human then szLogText = szLogText.." name = "..human.db.name.." id = "..human.db._id end Log.write(Log.LOGID_OSS_COMMON_ACT, szLogText) end -- 设置全服发送邮件奖励 function CommerveManager_SetCommDBSendMail() local bSendMail = CommonDB.GetCommerceActInfo_SendServerMail() if nil == bSendMail then return end CommonDB.SetCommerceActInfo_SendServerMail(true) end -- 获取全服发送邮件标识 function CommerveManager_GetCommDBSendMail() return CommonDB.GetCommerceActInfo_SendServerMail() end -- 设置玩家已经获取了全服奖励邮件 function CommerveManager_SetHumanSendServerMail(human, nValue) human.db.ServerCommerce.bSendServerMail = nValue end -- 获取玩家全服奖励邮件状态 function CommerveManager_GetHumanSendServerMail(human) return human.db.ServerCommerce.bSendServerMail end -- 设置玩家已经获取了个人奖励邮件 function CommerveManager_SetHumanSendPlayerMail(human, nValue) human.db.ServerCommerce.bSendPlayerMail = nValue end -- 获取玩家全服奖励邮件状态 function CommerveManager_GetHumanSendPlayerMail(human) return human.db.ServerCommerce.bSendPlayerMail end