ServerCommerceActRank.lua 19 KB

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