ServerCommerceActRank.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. --------------------------------
  2. -- 文件名 : WeekendLoopActRank.lua
  3. -- 文件说明 : 周末冲刺-排行榜
  4. -- 创建时间 : 2024/12/09
  5. -- 创建人 : FC
  6. --------------------------------
  7. local Msg = require("core.Msg")
  8. local Util = require("common.Util")
  9. local Lang = require("common.Lang")
  10. local CommonDB = require("common.CommonDB")
  11. local Broadcast = require("broadcast.Broadcast")
  12. local CommonDefine = require("common.CommonDefine")
  13. local Timer = require("core.Timer")
  14. local Config = require("Config")
  15. local Grid = require("bag.Grid")
  16. local ObjHuman = require("core.ObjHuman")
  17. local MailExcel = require("excel.mail")
  18. local MailManager = require("mail.MailManager")
  19. local RoleDBLogic = require("role.RoleDBLogic")
  20. local ServerCommerceMiddle = require("serverCommerce.ServerCommerceMiddle")
  21. local ServerCommerceDefine = require("serverCommerce.ServerCommerceActDefine")
  22. local ServerCommerceRankConf = require("excel.ServerCommerce").CommerceRank
  23. local ServerCommerceManager = require("serverCommerce.ServerCommerceManager")
  24. -- 缓存的排行榜数据
  25. local COMMERCE_RANK_CACHE = {}
  26. -- 缓存的ServerKey
  27. local COMMERCE_RANK_SERVER_KEY = nil
  28. -- 缓存的服务器排名
  29. local COMMERCE_RANK_SERVER_RANK = nil
  30. -- 待发送全服奖励的玩家
  31. local COMMRCE_RANK_SERVERMAIL_HUMAN = nil
  32. ----------------------------------------- 内部处理开始 -------------------------------------
  33. function CommercerActRank_QueryRankInfo(nRankType, nRankSubType)
  34. ServerCommerceMiddle.CommerveMiddle_QueryRankInfo(nRankType, nRankSubType)
  35. end
  36. -- 创建缓存
  37. local function CommercerActRank_CreateCache(nRankType, nRankSubType, nServerKey)
  38. if not COMMERCE_RANK_CACHE then
  39. COMMERCE_RANK_CACHE = {}
  40. end
  41. if not COMMERCE_RANK_CACHE[nRankType] then
  42. COMMERCE_RANK_CACHE[nRankType] = {}
  43. end
  44. if not COMMERCE_RANK_CACHE[nRankType][nRankSubType] then
  45. COMMERCE_RANK_CACHE[nRankType][nRankSubType] = {}
  46. end
  47. if not COMMERCE_RANK_CACHE[nRankType][nRankSubType][nServerKey] then
  48. COMMERCE_RANK_CACHE[nRankType][nRankSubType][nServerKey] = {}
  49. end
  50. end
  51. -- 获取缓存的排行榜数据
  52. local function CommercerActRank_GetCacheRankData(nRankType, nRankSubType, nServerKey)
  53. return COMMERCE_RANK_CACHE[nRankType][nRankSubType][nServerKey]
  54. end
  55. local function CommercerActRank_GetCacheRankDataByRank(nRankType, nRankSubType, nServerKey, nRank)
  56. return COMMERCE_RANK_CACHE[nRankType][nRankSubType][nServerKey][nRank]
  57. end
  58. -- 清空对应的排行榜
  59. local function CommercerActRank_ClearCache(nRankType, nRankSubType, nServerKey)
  60. COMMERCE_RANK_CACHE[nRankType][nRankSubType][nServerKey] = {}
  61. end
  62. -- 插入排行榜数据
  63. local function CommercerActRank_InsertRank(nRankType, nRankSubType, nServerKey, nRank, tRankInfo)
  64. local tCacheRankData = CommercerActRank_GetCacheRankData(nRankType, nRankSubType, nServerKey)
  65. tCacheRankData[nRank] = tRankInfo
  66. end
  67. local function CommercerActRank_GetRankInfoByUuid(nRankType, nRankSubType, uuid)
  68. local tCacheRankData = CommercerActRank_GetCacheRankData(nRankType, nRankSubType, COMMERCE_RANK_SERVER_KEY)
  69. if not tCacheRankData then
  70. return nil, nil
  71. end
  72. local nGetRank, tGetRankInfo = nil, nil
  73. for nRank, v in ipairs(tCacheRankData) do
  74. if v.uuid == uuid then
  75. nGetRank = nRank
  76. tGetRankInfo = v
  77. break
  78. end
  79. end
  80. return nGetRank, tGetRankInfo
  81. end
  82. -- 包装自己名次数据
  83. local function CommercerActRank_WrapOwnerData(nRankType, nRankSubType, net, uuid)
  84. local nRank, tRankInfo = CommercerActRank_GetRankInfoByUuid(nRankType, nRankSubType, uuid)
  85. net.rank = nRank or -1
  86. net.rankValue = tRankInfo and tRankInfo.nValue or 0
  87. local len = 0
  88. if nRank and tRankInfo then
  89. for _, v in ipairs(ServerCommerceRankConf) do
  90. if v.nRankType == nRankSubType then
  91. if v.nOrder[1] <= nRank and v.nOrder[2] >= nRank then
  92. for index, itemInfo in ipairs(v.Prize) do
  93. len = len + 1
  94. Grid.makeItem(net.items[index], itemInfo[1], itemInfo[2])
  95. end
  96. break
  97. end
  98. end
  99. end
  100. end
  101. net.items[0] = len
  102. end
  103. -- 包装档位名次数据
  104. local function CommercerActRank_WrapRankList(nRankType, nRankSubType, net, nRank)
  105. local tRankData = CommercerActRank_GetCacheRankDataByRank(nRankType, nRankSubType, COMMERCE_RANK_SERVER_KEY)
  106. if tRankData and tRankData ~= -1 then
  107. net.uid = tRankData.uuid
  108. net.name = tRankData.name
  109. net.head = tRankData.head
  110. net.rankValue = tRankData.nValue
  111. net.headFrame = tRankData.headFrame
  112. else
  113. net.uid = "-1"
  114. net.name = ""
  115. net.head = -1
  116. net.rankValue = 0
  117. net.headFrame = -1
  118. end
  119. local len = 0
  120. for _, v in ipairs(ServerCommerceRankConf) do
  121. if v.nRankType == nRankSubType then
  122. if v.nOrder[1] <= nRank and v.nOrder[2] >= nRank then
  123. for index, itemInfo in ipairs(v.Prize) do
  124. len = len + 1
  125. Grid.makeItem(net.items[index], itemInfo[1], itemInfo[2])
  126. end
  127. break
  128. end
  129. end
  130. end
  131. net.items[0] = len
  132. end
  133. ----------------------------------------- 外部调用 -------------------------------------
  134. function CommercerActRank_ResetData(human)
  135. if not human then
  136. return
  137. end
  138. --CommercerActRank_ResetCharge(human)
  139. end
  140. -- 活动结束
  141. function CommercerActRank_End()
  142. end
  143. function ClearCache()
  144. COMMERCE_RANK_CACHE = {}
  145. COMMERCE_RANK_SERVER_KEY = nil
  146. COMMERCE_RANK_SERVER_RANK = nil
  147. COMMRCE_RANK_SERVERMAIL_HUMAN = nil
  148. end
  149. -- 玩家点数改变
  150. function CommercerActRank_HumanPointChange(human, nNowPoint)
  151. ServerCommerceMiddle.CommerveMiddle_HumanPointChange(human, nNowPoint)
  152. end
  153. -- 服务器总点数改变
  154. function onAllPointChange()
  155. local nNowPoint = CommonDB.GetCommerceActInfo_Point()
  156. ServerCommerceMiddle.CommerveMiddle_ServerPointChange(nNowPoint)
  157. end
  158. -- 定时请求数据
  159. function CommercerActRank_CreateTime(nTime, nRankType, nRankSubType)
  160. Timer.addLater(nTime, CommercerActRank_QueryRankInfo, nRankType, nRankSubType)
  161. end
  162. -- 起服活动开启 请求排行榜数据
  163. function CommercerActRank_InitServerQueryRank()
  164. CommercerActRank_QueryRankInfo(CommonDefine.COMMONRANK_TYPE_SERVERCOMMERCE,
  165. CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_SERVER)
  166. -- 延迟20秒请求 个人排行榜
  167. Timer.addLater(20, CommercerActRank_QueryRankInfo,
  168. CommonDefine.COMMONRANK_TYPE_SERVERCOMMERCE, CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_HUMAN)
  169. end
  170. -- 获取到排行榜数据
  171. function CommercerActRank_GetRankInfo(tGetData)
  172. local nRankType = tGetData.nRankType
  173. local nRankSubType = tGetData.nRankSubType
  174. local nServerKey = tGetData.nServerKey
  175. local nBegin = tGetData.nBegin
  176. local nEnd = tGetData.nEnd
  177. local tRankData = tGetData.tRankData
  178. CommercerActRank_CreateCache(nRankType, nRankSubType, nServerKey)
  179. if COMMERCE_RANK_SERVER_KEY == nil then
  180. print("[CommercerActRank_GetRankInfo] 设置 nServerKey = "..nServerKey)
  181. COMMERCE_RANK_SERVER_KEY = nServerKey
  182. else
  183. if COMMERCE_RANK_SERVER_KEY ~= nServerKey then
  184. print("[CommercerActRank_GetRankInfo] 一个战区的 nServerKey 为什么会改变")
  185. COMMERCE_RANK_SERVER_KEY = nServerKey
  186. end
  187. end
  188. if nBegin == 1 then
  189. CommercerActRank_ClearCache(nRankType, nRankSubType, nServerKey)
  190. end
  191. local nAllLen = tRankData[0]
  192. if 0 >= nAllLen then
  193. return
  194. else
  195. for i = 1, nAllLen, 1 do
  196. CommercerActRank_InsertRank(nRankType, nRankSubType, nServerKey, tRankData[i].nRank, tRankData[i].tRankData)
  197. end
  198. end
  199. -- 结束重新创建定时器, 因为用一次就呗删除了
  200. if nEnd == 1 then
  201. local nTime = ServerCommerceDefine.COMMERCEACT_RANKUPDATE
  202. CommercerActRank_CreateTime(nTime, nRankType, nRankSubType)
  203. end
  204. end
  205. -- 获取到排名信息
  206. function CommercerActRank_GetServerRank(tData)
  207. COMMERCE_RANK_SERVER_RANK = tData.nRank
  208. if tData.nOperate == ServerCommerceDefine.COMMERCEACT_SENDSERVERMAIL then
  209. CommercerActRank_SendServerRankPrize(tData)
  210. else
  211. CommercerActRank_SendServerMail2Human()
  212. end
  213. end
  214. -- 发送服务器排行榜奖励
  215. function CommercerActRank_SendServerRankPrize(tData)
  216. local nRank = tData.nRank
  217. local tPrize = nil
  218. for _, v in ipairs(ServerCommerceRankConf) do
  219. if v.nRankType == CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_SERVER then
  220. if v.nOrder[1] <= nRank and v.nOrder[2] >= nRank then
  221. tPrize = v.Prize
  222. break
  223. end
  224. end
  225. end
  226. local szLogText = "[CommercerActRank_SendServerRankPrize] 发送服务器奖励 nRank = "..nRank
  227. if not tPrize then
  228. szLogText = szLogText .. "获取不到对应奖励"
  229. ServerCommerceManager.CommerveManager_WriteLog(szLogText)
  230. return
  231. end
  232. local nSendMail = ServerCommerceManager.CommerveManager_GetCommDBSendMail()
  233. if true == nSendMail then
  234. return
  235. end
  236. -- 设置当前发送邮件奖励
  237. ServerCommerceManager.CommerveManager_SetCommDBSendMail()
  238. -- 遍历在线玩家
  239. local mailConfig = MailExcel.mail[ServerCommerceDefine.COMMERCEACT_SERVERMAILID]
  240. local title = mailConfig.title
  241. local senderName = mailConfig.senderName
  242. local content = mailConfig.content
  243. for uuid, human in pairs(ObjHuman.onlineUuid) do
  244. MailManager.add(MailManager.SYSTEM, uuid,
  245. title, Util.format(content, rank), tPrize, senderName)
  246. -- 设置该玩家已经发送邮件
  247. ServerCommerceManager.CommerveManager_SetHumanSendServerMail(human, true)
  248. -- 记录信息
  249. ServerCommerceManager.CommerveManager_WriteLog(szLogText, human)
  250. end
  251. -- 告诉中心服全服邮件在线的处理完成
  252. ServerCommerceMiddle.CommerceMiddle_TellServerMailOk()
  253. end
  254. -- 发送玩家邮件奖励
  255. function CommercerActRank_SendHumanRankPrize(tData)
  256. local nRank, uuid = tData.nRank, tData.uuid
  257. local tPrize = nil
  258. for _, v in ipairs(ServerCommerceRankConf) do
  259. if v.nRankType == CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_HUMAN then
  260. if v.nOrder[1] <= nRank and v.nOrder[2] >= nRank then
  261. tPrize = v.Prize
  262. break
  263. end
  264. end
  265. end
  266. local szLogText = "[CommercerActRank_SendHumanRankPrize] 发送个人奖励 nRank = "..nRank
  267. if not tPrize then
  268. szLogText = szLogText .. "获取不到对应奖励"
  269. ServerCommerceManager.CommerveManager_WriteLog(szLogText)
  270. return
  271. end
  272. local human = ObjHuman.onlineUuid[uuid]
  273. local bNowSave = false
  274. if not human then
  275. human = RoleDBLogic.getDb(uuid)
  276. bNowSave = true
  277. end
  278. if not human then
  279. szLogText = szLogText.." 获取不到玩家DB数据信息 uuid = "..uuid
  280. ServerCommerceManager.CommerveManager_WriteLog(szLogText)
  281. return
  282. end
  283. local bSendMail = ServerCommerceManager.CommerveManager_GetHumanSendPlayerMail(human)
  284. if true == bSendMail then
  285. szLogText = szLogText .. "已经发送了邮件为什么又一次走到这里 name = "..human.db.name
  286. ServerCommerceManager.CommerveManager_WriteLog(szLogText)
  287. return
  288. end
  289. -- 设置发送了邮件奖励
  290. ServerCommerceManager.CommerveManager_SetHumanSendPlayerMail(human, true)
  291. -- 发奖励
  292. local mailConfig = MailExcel.mail[ServerCommerceDefine.COMMERCEACT_HUMANMAILID]
  293. local title = mailConfig.title
  294. local senderName = mailConfig.senderName
  295. local content = mailConfig.content
  296. MailManager.add(MailManager.SYSTEM, uuid,
  297. title, Util.format(content, rank), tPrize, senderName)
  298. -- 写日志
  299. szLogText = szLogText.."发送奖励成功"
  300. ServerCommerceManager.CommerveManager_WriteLog(szLogText, human)
  301. if true == bNowSave then
  302. RoleDBLogic.saveRole(human.db)
  303. end
  304. end
  305. -- 玩家登录发送玩家邮件奖励(全服邮件奖励)
  306. function CommercerActRank_SendServerMailHumanLogin(human)
  307. if not COMMRCE_RANK_SERVERMAIL_HUMAN then
  308. COMMRCE_RANK_SERVERMAIL_HUMAN = {}
  309. end
  310. table.insert(COMMRCE_RANK_SERVERMAIL_HUMAN, human.db._id)
  311. -- 不存在请求一次排名
  312. if not COMMERCE_RANK_SERVER_RANK then
  313. ServerCommerceMiddle.CommerceMiddle_QueryServerRank(ServerCommerceDefine.COMMERCEACT_GETSERVERRANK)
  314. else
  315. CommercerActRank_SendServerMail2Human()
  316. end
  317. end
  318. -- 发送活动结束后登录的的玩家(全服邮件奖励)
  319. function CommercerActRank_SendServerMail2Human()
  320. if not COMMRCE_RANK_SERVERMAIL_HUMAN or not COMMERCE_RANK_SERVER_RANK then
  321. return
  322. end
  323. local tPrize = nil
  324. for _, v in ipairs(ServerCommerceRankConf) do
  325. if v.nRankType == CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_SERVER then
  326. if v.nOrder[1] <= COMMERCE_RANK_SERVER_RANK and v.nOrder[2] >= COMMERCE_RANK_SERVER_RANK then
  327. tPrize = v.Prize
  328. break
  329. end
  330. end
  331. end
  332. if not tPrize then
  333. return
  334. end
  335. local mailConfig = MailExcel.mail[ServerCommerceDefine.COMMERCEACT_SERVERMAILID]
  336. local title = mailConfig.title
  337. local senderName = mailConfig.senderName
  338. local content = mailConfig.content
  339. for _, uuid in ipairs(COMMRCE_RANK_SERVERMAIL_HUMAN) do
  340. -- 设置该玩家已经发送邮件
  341. local human = ObjHuman.onlineUuid[uuid]
  342. if not human then
  343. human = RoleDBLogic.getDb(uuid)
  344. end
  345. if human then
  346. MailManager.add(MailManager.SYSTEM, uuid,
  347. title, Util.format(content, COMMERCE_RANK_SERVER_RANK), tPrize, senderName)
  348. -- 记录信息
  349. ServerCommerceManager.CommerveManager_WriteLog("[CommercerActRank_SendServerMail2Human] 玩家登录发送全服奖励邮件完成", human)
  350. end
  351. end
  352. COMMRCE_RANK_SERVERMAIL_HUMAN = nil
  353. end
  354. -- 玩家登录活动结束发送个人排行榜邮件
  355. function CommercerActRank_SendHumanMailHumanLogin(human)
  356. local tPrize, nRank = nil, 0
  357. for _, v in ipairs(ServerCommerceRankConf) do
  358. if v.nRankType == CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_HUMAN then
  359. if v.nOrder[1] >= nRank then
  360. tPrize = v.Prize
  361. nRank = v.nOrder[1]
  362. end
  363. end
  364. end
  365. local mailConfig = MailExcel.mail[ServerCommerceDefine.COMMERCEACT_HUMANMAILID]
  366. local title = mailConfig.title
  367. local senderName = mailConfig.senderName
  368. local content = mailConfig.content
  369. local newcontent = string.gsub(content, "{1}", "21+")
  370. MailManager.add(MailManager.SYSTEM, human.db_id,
  371. title, newcontent, tPrize, senderName)
  372. -- 写日志
  373. ServerCommerceManager.CommerveManager_WriteLog("[CommercerActRank_SendHumanMailHumanLogin] 玩家登录发送邮件完成", human)
  374. end
  375. ----------------------------------------- 客户端请求 -------------------------------------
  376. -- 客户端请求
  377. function CommercerActRank_Query(human, nRankSubType)
  378. local tMsgData = Msg.gc.GC_SERVEERCOMMERCE_ACT_RANK_QUERY
  379. local maxSendRank = CommonDefine.COMMONRANK_SENDPRIZE_LEN
  380. local uuid = human.db._id
  381. if CommonDefine.COMMONRANK_SUB_TYPE_SERVERCOMMERCE_SERVER == nRankSubType then
  382. uuid = Config.NEW_SVR_INDEX
  383. end
  384. CommercerActRank_WrapOwnerData(CommonDefine.COMMONRANK_TYPE_SERVERCOMMERCE, nRankSubType, msgRet.ownerData, uuid)
  385. for rank = 1, maxSendRank do
  386. CommercerActRank_WrapRankList(CommonDefine.COMMONRANK_TYPE_SERVERCOMMERCE, nRankSubType, msgRet.list[rank], rank)
  387. end
  388. msgRet.list[0] = maxSendRank
  389. Msg.send(msgRet, human.fd)
  390. end