rollSrv.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. --[[
  2. Descripttion:roll房
  3. version:
  4. Author: Neo,Huang
  5. Date: 2023-11-16 21:45:09
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2023-11-16 23:20:12
  8. --]]
  9. local timer = require("timer")
  10. local baseService = require("baseService")
  11. local lib_game_redis = require("lib_game_redis")
  12. local redisUtil = require("utils.redisUtil")
  13. local util_mail = require("utils.util_mail")
  14. local timeUtil = require("utils.timeUtil")
  15. local nodeMgr = require("nodeMgr")
  16. local util_roll = require("utils.util_roll")
  17. local resAdapt = require("adapt.resAdapt")
  18. local bagData = require("data.bag")
  19. local timerAward = nil
  20. local root = {rollList = {}}
  21. -- 载入活跃roll房
  22. local MAIN_KEY = "roll:room"
  23. local function _get_room_key(rid)
  24. return string.format("%s:%s", MAIN_KEY, tostring(rid))
  25. end
  26. local function _get_room_award_key()
  27. return string.format("%s:award", MAIN_KEY)
  28. end
  29. local function _load_active_roll_info()
  30. local ridList = lib_game_redis:smembers(MAIN_KEY)
  31. log.info("_load_active_roll_info ridList[%s]", tostring(ridList))
  32. if is_empty(ridList) then
  33. return
  34. end
  35. for _, v in ipairs(ridList) do
  36. local rid = tonumber(v)
  37. local key = _get_room_key(rid)
  38. local awardTime = redisUtil.hget_int(key, "awardTime")
  39. if not is_empty(awardTime) then
  40. table.insert(root.rollList, {id = rid, awardTime = awardTime})
  41. end
  42. end
  43. log.info("_load_active_roll_info rollList[%s]", tostring(root.rollList))
  44. end
  45. -- 发奖
  46. local function _roll_award(rid)
  47. log.info("_roll_award 房间ID[%s]", tostring(rid))
  48. if is_empty(rid) then
  49. return
  50. end
  51. local key = _get_room_key(rid)
  52. -- 删除roll信息
  53. lib_game_redis:del(key)
  54. lib_game_redis:srem(MAIN_KEY, rid)
  55. lib_game_redis:sismember(_get_room_award_key(), rid)
  56. local signupUidList = redisUtil.hget_json(key, "signupUidList")
  57. if is_empty(signupUidList) then
  58. -- 没有玩家参与
  59. return
  60. end
  61. local itemIdList = redisUtil.hget_json(key, "itemIdList")
  62. if is_empty(itemIdList) then
  63. -- 无奖励物品
  64. return
  65. end
  66. local mapUidItems = {}
  67. for _, id in ipairs(itemIdList) do
  68. -- 随机中奖玩家
  69. local index = math.random(1, #signupUidList)
  70. local uid = signupUidList[index]
  71. if mapUidItems[uid] == nil then
  72. mapUidItems[uid] = {}
  73. end
  74. table.insert(mapUidItems[uid], {id = id, count = 1})
  75. end
  76. log.info("_roll_award mapUidItems[%s]", tostring(mapUidItems))
  77. local name = redisUtil.hget(key, "name")
  78. local title = "roll房结算"
  79. -- 中奖玩家发奖
  80. for uid, items in pairs(mapUidItems) do
  81. local keyEvent = string.format("roll-%s", tostring(rid))
  82. bagData:add_items(uid, items, keyEvent)
  83. -- 邮件
  84. local cnt = string.format("恭喜您,参与%sroll房活动中获得以下奖励,奖励已发送至背包,请查收。", tostring(name))
  85. for _, v in ipairs(items) do
  86. cnt = cnt + string.format("\n%s", resAdapt:get_item_name(v.id))
  87. end
  88. util_mail:add_mail(uid, 0, title, cnt, {}, 0, nil, nil, "system")
  89. end
  90. -- 未中奖玩家邮件
  91. for _, uid in ipairs(signupUidList) do
  92. if mapUidItems[uid] == nil then
  93. local cnt = string.format("很遗憾,您参与的%sroll房活动中未获得奖励。", tostring(name))
  94. util_mail:add_mail(uid, 0, title, cnt, {}, 0, nil, nil, "system")
  95. end
  96. end
  97. end
  98. -- 定时器结束
  99. local function _timeout_award()
  100. log.info("_timeout_award rollList[%s]", tostring(root.rollList))
  101. if is_empty(root.rollList) then
  102. return
  103. end
  104. local rollList = {}
  105. local currTime = skynet_time()
  106. for _, v in ipairs(root.rollList) do
  107. log.info("_timeout_award 房间ID[%s] 发奖时间[%s]", tostring(v.id), timeUtil.toString(v.awardTime))
  108. if currTime >= v.awardTime then
  109. -- 发奖
  110. _roll_award(v.id)
  111. else
  112. table.insert(rollList, table.copy(v))
  113. end
  114. end
  115. root.rollList = rollList
  116. end
  117. function root.onStart()
  118. math.randomseed(os.time())
  119. _load_active_roll_info()
  120. timerAward = timer.timeOut(60, _timeout_award)
  121. end
  122. function root.onStop()
  123. -- 取消定时器
  124. timerAward.func = nil
  125. end
  126. -- 新增roll房
  127. function root.add_roll(rid)
  128. local key = _get_room_key(rid)
  129. local awardTime = redisUtil.hget_int(key, "awardTime")
  130. local isAwarded = lib_game_redis:sismember(_get_room_award_key(), rid)
  131. if not is_empty(awardTime) and not isAwarded then
  132. local isMatch = false
  133. for _, v in ipairs(root.rollList) do
  134. if v.id == rid then
  135. isMatch = true
  136. v.awardTime = awardTime
  137. break
  138. end
  139. end
  140. if not isMatch then
  141. table.insert(root.rollList, {id = rid, awardTime = awardTime})
  142. -- 广播 - 新房
  143. local roomInfo = util_roll:pack_roll_room_info(rid)
  144. if not is_empty(roomInfo) then
  145. nodeMgr.broadcast_proto_notify("on_roll_new", {room = roomInfo})
  146. end
  147. end
  148. end
  149. end
  150. -- 更新roll房
  151. function root.update_roll(rid)
  152. local key = _get_room_key(rid)
  153. local awardTime = redisUtil.hget_int(key, "awardTime")
  154. local isAwarded = lib_game_redis:sismember(_get_room_award_key(), rid)
  155. if not is_empty(awardTime) and not isAwarded then
  156. local isMatch = false
  157. for _, v in ipairs(root.rollList) do
  158. if v.id == rid then
  159. isMatch = true
  160. v.awardTime = awardTime
  161. break
  162. end
  163. end
  164. if not isMatch then
  165. table.insert(root.rollList, {id = rid, awardTime = awardTime})
  166. end
  167. end
  168. end
  169. -- 删除roll房
  170. function root.del_roll(rid)
  171. local key = _get_room_key(rid)
  172. -- 删除roll信息
  173. lib_game_redis:del(key)
  174. lib_game_redis:srem(MAIN_KEY, rid)
  175. for k, v in ipairs(root.rollList) do
  176. if v.id == rid then
  177. table.remove(root.rollList, k)
  178. break
  179. end
  180. end
  181. end
  182. baseService.start(root, ".rollSrv", true)