-------------------------------- -- 文件名 : WeekendLoopActRank.lua -- 文件说明 : 周末冲刺-排行榜 -- 创建时间 : 2024/12/09 -- 创建人 : FC -------------------------------- local Msg = require("core.Msg") local Util = require("common.Util") local Lang = require("common.Lang") local CommonDB = require("common.CommonDB") local Broadcast = require("broadcast.Broadcast") local CommonDefine = require("common.CommonDefine") local Timer = require("core.Timer") local Config = require("Config") local Grid = require("bag.Grid") local ObjHuman = require("core.ObjHuman") local MailExcel = require("excel.mail") local MailManager = require("mail.MailManager") local RoleDBLogic = require("role.RoleDBLogic") local ServerCommerceMiddle = require("serverCommerce.ServerCommerceMiddle") local ServerCommerceDefine = require("serverCommerce.ServerCommerceActDefine") local ServerCommerceRankConf = require("excel.ServerCommerce").CommerceRank local ServerCommerceManager = require("serverCommerce.ServerCommerceManager") -- 缓存的排行榜数据 local COMMERCE_RANK_CACHE = {} -- 缓存的ServerKey local COMMERCE_RANK_SERVER_KEY = nil -- 缓存的服务器排名 local COMMERCE_RANK_SERVER_RANK = nil -- 待发送全服奖励的玩家 local COMMRCE_RANK_SERVERMAIL_HUMAN = nil ----------------------------------------- 内部处理开始 ------------------------------------- function CommercerActRank_QueryRankInfo(nRankType, nRankSubType) ServerCommerceMiddle.CommerveMiddle_QueryRankInfo(nRankType, nRankSubType) end -- 创建缓存 local function CommercerActRank_CreateCache(nRankType, nRankSubType, nServerKey) if not COMMERCE_RANK_CACHE then COMMERCE_RANK_CACHE = {} end if not COMMERCE_RANK_CACHE[nRankType] then COMMERCE_RANK_CACHE[nRankType] = {} end if not COMMERCE_RANK_CACHE[nRankType][nRankSubType] then COMMERCE_RANK_CACHE[nRankType][nRankSubType] = {} end if not COMMERCE_RANK_CACHE[nRankType][nRankSubType][nServerKey] then COMMERCE_RANK_CACHE[nRankType][nRankSubType][nServerKey] = {} end end -- 获取缓存的排行榜数据 local function CommercerActRank_GetCacheRankData(nRankType, nRankSubType, nServerKey) return COMMERCE_RANK_CACHE[nRankType][nRankSubType][nServerKey] end local function CommercerActRank_GetCacheRankDataByRank(nRankType, nRankSubType, nServerKey, nRank) return COMMERCE_RANK_CACHE[nRankType][nRankSubType][nServerKey][nRank] end -- 清空对应的排行榜 local function CommercerActRank_ClearCache(nRankType, nRankSubType, nServerKey) COMMERCE_RANK_CACHE[nRankType][nRankSubType][nServerKey] = {} end -- 插入排行榜数据 local function CommercerActRank_InsertRank(nRankType, nRankSubType, nServerKey, nRank, tRankInfo) local tCacheRankData = CommercerActRank_GetCacheRankData(nRankType, nRankSubType, nServerKey) tCacheRankData[nRank] = tRankInfo end local function CommercerActRank_GetRankInfoByUuid(nRankType, nRankSubType, uuid) local tCacheRankData = CommercerActRank_GetCacheRankData(nRankType, nRankSubType, COMMERCE_RANK_SERVER_KEY) if not tCacheRankData then return nil, nil end local nGetRank, tGetRankInfo = nil, nil for nRank, v in ipairs(tCacheRankData) do if v.uuid == uuid then nGetRank = nRank tGetRankInfo = v break end end return nGetRank, tGetRankInfo end -- 包装自己名次数据 local function CommercerActRank_WrapOwnerData(nRankType, nRankSubType, net, uuid) local nRank, tRankInfo = CommercerActRank_GetRankInfoByUuid(nRankType, nRankSubType, uuid) net.rank = nRank or -1 net.rankValue = tRankInfo and tRankInfo.nValue or 0 local len = 0 if nRank and tRankInfo then for _, v in ipairs(ServerCommerceRankConf) do if v.nRankType == nRankSubType then if v.nOrder[1] <= nRank and v.nOrder[2] >= nRank then for index, itemInfo in ipairs(v.Prize) do len = len + 1 Grid.makeItem(net.items[index], itemInfo[1], itemInfo[2]) end break end end end end net.items[0] = len end -- 包装档位名次数据 local function CommercerActRank_WrapRankList(nRankType, nRankSubType, net, nRank) local tRankData = CommercerActRank_GetCacheRankDataByRank(nRankType, nRankSubType, COMMERCE_RANK_SERVER_KEY) if tRankData and tRankData ~= -1 then net.uid = tRankData.uuid net.name = tRankData.name net.head = tRankData.head net.rankValue = tRankData.nValue net.headFrame = tRankData.headFrame else net.uid = "-1" net.name = "" net.head = -1 net.rankValue = 0 net.headFrame = -1 end local len = 0 for _, v in ipairs(ServerCommerceRankConf) do if v.nRankType == nRankSubType then if v.nOrder[1] <= nRank and v.nOrder[2] >= nRank then for index, itemInfo in ipairs(v.Prize) do len = len + 1 Grid.makeItem(net.items[index], itemInfo[1], itemInfo[2]) end break end end end net.items[0] = len end ----------------------------------------- 外部调用 ------------------------------------- function CommercerActRank_ResetData(human) if not human then return end --CommercerActRank_ResetCharge(human) end -- 活动结束 function CommercerActRank_End() end function ClearCache() COMMERCE_RANK_CACHE = {} COMMERCE_RANK_SERVER_KEY = nil COMMERCE_RANK_SERVER_RANK = nil COMMRCE_RANK_SERVERMAIL_HUMAN = nil end -- 玩家点数改变 function CommercerActRank_HumanPointChange(human, nNowPoint) ServerCommerceMiddle.CommerveMiddle_HumanPointChange(human, nNowPoint) end -- 服务器总点数改变 function onAllPointChange() local nNowPoint = CommonDB.GetCommerceActInfo_Point() ServerCommerceMiddle.CommerveMiddle_ServerPointChange(nNowPoint) end -- 定时请求数据 function CommercerActRank_CreateTime(nTime, nRankType, nRankSubType) Timer.addLater(nTime, CommercerActRank_QueryRankInfo, nRankType, nRankSubType) end -- 起服活动开启 请求排行榜数据 function CommercerActRank_InitServerQueryRank() CommercerActRank_QueryRankInfo(CommonDefine.COMMONRANK_TYPE_SERVERCOMMERCE, CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_SERVER) -- 延迟20秒请求 个人排行榜 Timer.addLater(20, CommercerActRank_QueryRankInfo, CommonDefine.COMMONRANK_TYPE_SERVERCOMMERCE, CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_HUMAN) end -- 获取到排行榜数据 function CommercerActRank_GetRankInfo(tGetData) local nRankType = tGetData.nRankType local nRankSubType = tGetData.nRankSubType local nServerKey = tGetData.nServerKey local nBegin = tGetData.nBegin local nEnd = tGetData.nEnd local tRankData = tGetData.tRankData CommercerActRank_CreateCache(nRankType, nRankSubType, nServerKey) if COMMERCE_RANK_SERVER_KEY == nil then print("[CommercerActRank_GetRankInfo] 设置 nServerKey = "..nServerKey) COMMERCE_RANK_SERVER_KEY = nServerKey else if COMMERCE_RANK_SERVER_KEY ~= nServerKey then print("[CommercerActRank_GetRankInfo] 一个战区的 nServerKey 为什么会改变") COMMERCE_RANK_SERVER_KEY = nServerKey end end if nBegin == 1 then CommercerActRank_ClearCache(nRankType, nRankSubType, nServerKey) end local nAllLen = tRankData[0] if 0 >= nAllLen then return else for i = 1, nAllLen, 1 do CommercerActRank_InsertRank(nRankType, nRankSubType, nServerKey, tRankData[i].nRank, tRankData[i].tRankData) end end -- 结束重新创建定时器, 因为用一次就呗删除了 if nEnd == 1 then local nTime = ServerCommerceDefine.COMMERCEACT_RANKUPDATE CommercerActRank_CreateTime(nTime, nRankType, nRankSubType) end end -- 获取到排名信息 function CommercerActRank_GetServerRank(tData) COMMERCE_RANK_SERVER_RANK = tData.nRank if tData.nOperate == ServerCommerceDefine.COMMERCEACT_SENDSERVERMAIL then CommercerActRank_SendServerRankPrize(tData) else CommercerActRank_SendServerMail2Human() end end -- 发送服务器排行榜奖励 function CommercerActRank_SendServerRankPrize(tData) local nRank = tData.nRank local tPrize = nil for _, v in ipairs(ServerCommerceRankConf) do if v.nRankType == CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_SERVER then if v.nOrder[1] <= nRank and v.nOrder[2] >= nRank then tPrize = v.Prize break end end end local szLogText = "[CommercerActRank_SendServerRankPrize] 发送服务器奖励 nRank = "..nRank if not tPrize then szLogText = szLogText .. "获取不到对应奖励" ServerCommerceManager.CommerveManager_WriteLog(szLogText) return end local nSendMail = ServerCommerceManager.CommerveManager_GetCommDBSendMail() if true == nSendMail then return end -- 设置当前发送邮件奖励 ServerCommerceManager.CommerveManager_SetCommDBSendMail() -- 遍历在线玩家 local mailConfig = MailExcel.mail[ServerCommerceDefine.COMMERCEACT_SERVERMAILID] local title = mailConfig.title local senderName = mailConfig.senderName local content = mailConfig.content for uuid, human in pairs(ObjHuman.onlineUuid) do MailManager.add(MailManager.SYSTEM, uuid, title, Util.format(content, rank), tPrize, senderName) -- 设置该玩家已经发送邮件 ServerCommerceManager.CommerveManager_SetHumanSendServerMail(human, true) -- 记录信息 ServerCommerceManager.CommerveManager_WriteLog(szLogText, human) end -- 告诉中心服全服邮件在线的处理完成 ServerCommerceMiddle.CommerceMiddle_TellServerMailOk() end -- 发送玩家邮件奖励 function CommercerActRank_SendHumanRankPrize(tData) local nRank, uuid = tData.nRank, tData.uuid local tPrize = nil for _, v in ipairs(ServerCommerceRankConf) do if v.nRankType == CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_HUMAN then if v.nOrder[1] <= nRank and v.nOrder[2] >= nRank then tPrize = v.Prize break end end end local szLogText = "[CommercerActRank_SendHumanRankPrize] 发送个人奖励 nRank = "..nRank if not tPrize then szLogText = szLogText .. "获取不到对应奖励" ServerCommerceManager.CommerveManager_WriteLog(szLogText) return end local human = ObjHuman.onlineUuid[uuid] local bNowSave = false if not human then human = RoleDBLogic.getDb(uuid) bNowSave = true end if not human then szLogText = szLogText.." 获取不到玩家DB数据信息 uuid = "..uuid ServerCommerceManager.CommerveManager_WriteLog(szLogText) return end local bSendMail = ServerCommerceManager.CommerveManager_GetHumanSendPlayerMail(human) if true == bSendMail then szLogText = szLogText .. "已经发送了邮件为什么又一次走到这里 name = "..human.db.name ServerCommerceManager.CommerveManager_WriteLog(szLogText) return end -- 设置发送了邮件奖励 ServerCommerceManager.CommerveManager_SetHumanSendPlayerMail(human, true) -- 发奖励 local mailConfig = MailExcel.mail[ServerCommerceDefine.COMMERCEACT_HUMANMAILID] local title = mailConfig.title local senderName = mailConfig.senderName local content = mailConfig.content MailManager.add(MailManager.SYSTEM, uuid, title, Util.format(content, rank), tPrize, senderName) -- 写日志 szLogText = szLogText.."发送奖励成功" ServerCommerceManager.CommerveManager_WriteLog(szLogText, human) if true == bNowSave then RoleDBLogic.saveRole(human.db) end end -- 玩家登录发送玩家邮件奖励(全服邮件奖励) function CommercerActRank_SendServerMailHumanLogin(human) if not COMMRCE_RANK_SERVERMAIL_HUMAN then COMMRCE_RANK_SERVERMAIL_HUMAN = {} end table.insert(COMMRCE_RANK_SERVERMAIL_HUMAN, human.db._id) -- 不存在请求一次排名 if not COMMERCE_RANK_SERVER_RANK then ServerCommerceMiddle.CommerceMiddle_QueryServerRank(ServerCommerceDefine.COMMERCEACT_GETSERVERRANK) else CommercerActRank_SendServerMail2Human() end end -- 发送活动结束后登录的的玩家(全服邮件奖励) function CommercerActRank_SendServerMail2Human() if not COMMRCE_RANK_SERVERMAIL_HUMAN or not COMMERCE_RANK_SERVER_RANK then return end local tPrize = nil for _, v in ipairs(ServerCommerceRankConf) do if v.nRankType == CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_SERVER then if v.nOrder[1] <= COMMERCE_RANK_SERVER_RANK and v.nOrder[2] >= COMMERCE_RANK_SERVER_RANK then tPrize = v.Prize break end end end if not tPrize then return end local mailConfig = MailExcel.mail[ServerCommerceDefine.COMMERCEACT_SERVERMAILID] local title = mailConfig.title local senderName = mailConfig.senderName local content = mailConfig.content for _, uuid in ipairs(COMMRCE_RANK_SERVERMAIL_HUMAN) do -- 设置该玩家已经发送邮件 local human = ObjHuman.onlineUuid[uuid] if not human then human = RoleDBLogic.getDb(uuid) end if human then MailManager.add(MailManager.SYSTEM, uuid, title, Util.format(content, COMMERCE_RANK_SERVER_RANK), tPrize, senderName) -- 记录信息 ServerCommerceManager.CommerveManager_WriteLog("[CommercerActRank_SendServerMail2Human] 玩家登录发送全服奖励邮件完成", human) end end COMMRCE_RANK_SERVERMAIL_HUMAN = nil end -- 玩家登录活动结束发送个人排行榜邮件 function CommercerActRank_SendHumanMailHumanLogin(human) local tPrize, nRank = nil, 0 for _, v in ipairs(ServerCommerceRankConf) do if v.nRankType == CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_HUMAN then if v.nOrder[1] >= nRank then tPrize = v.Prize nRank = v.nOrder[1] end end end local mailConfig = MailExcel.mail[ServerCommerceDefine.COMMERCEACT_HUMANMAILID] local title = mailConfig.title local senderName = mailConfig.senderName local content = mailConfig.content local newcontent = string.gsub(content, "{1}", "21+") MailManager.add(MailManager.SYSTEM, human.db_id, title, newcontent, tPrize, senderName) -- 写日志 ServerCommerceManager.CommerveManager_WriteLog("[CommercerActRank_SendHumanMailHumanLogin] 玩家登录发送邮件完成", human) end ----------------------------------------- 客户端请求 ------------------------------------- -- 客户端请求 function CommercerActRank_Query(human, nRankSubType) local tMsgData = Msg.gc.GC_SERVEERCOMMERCE_ACT_RANK_QUERY local maxSendRank = CommonDefine.COMMONRANK_SENDPRIZE_LEN local uuid = human.db._id if CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_SERVER == nRankSubType then uuid = Config.NEW_SVR_INDEX end CommercerActRank_WrapOwnerData(CommonDefine.COMMONRANK_TYPE_SERVERCOMMERCE, nRankSubType, msgRet.ownerData, uuid) for rank = 1, maxSendRank do CommercerActRank_WrapRankList(CommonDefine.COMMONRANK_TYPE_SERVERCOMMERCE, nRankSubType, msgRet.list[rank], rank) end msgRet.list[0] = maxSendRank Msg.send(msgRet, human.fd) end