rollSrv.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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:srem(MAIN_KEY, rid)
  54. local signupUidList = redisUtil.hget_json(key, "signupUidList")
  55. if is_empty(signupUidList) then
  56. -- 没有玩家参与
  57. return
  58. end
  59. local itemIdList = redisUtil.hget_json(key, "itemIdList")
  60. if is_empty(itemIdList) then
  61. -- 无奖励物品
  62. return
  63. end
  64. local mapUidItems = {}
  65. for _, id in ipairs(itemIdList) do
  66. -- 随机中奖玩家
  67. local index = math.random(1, #signupUidList)
  68. local uid = signupUidList[index]
  69. if mapUidItems[uid] == nil then
  70. mapUidItems[uid] = {}
  71. end
  72. table.insert(mapUidItems[uid], {id = id, count = 1})
  73. end
  74. log.info("_roll_award mapUidItems[%s]", tostring(mapUidItems))
  75. local settle = {}
  76. local name = redisUtil.hget(key, "name")
  77. local title = "roll房结算"
  78. -- 中奖玩家发奖
  79. for uid, items in pairs(mapUidItems) do
  80. local keyEvent = string.format("roll-%s", tostring(rid))
  81. bagData:add_items(uid, items, keyEvent)
  82. -- 邮件
  83. local cnt = string.format("恭喜您,参与%sroll房活动中获得以下奖励,奖励已发送至背包,请查收。", tostring(name))
  84. for _, v in ipairs(items) do
  85. cnt = cnt + string.format("\n%s", resAdapt:get_item_name(v.id))
  86. end
  87. util_mail:add_mail(uid, 0, title, cnt, {}, 0, nil, nil, "system")
  88. table.insert(settle, {uid = uid, items = items})
  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. -- 保存结算
  98. redisUtil.hset(key, "settle", settle)
  99. -- 保存记录
  100. util_roll:backup_roll_room_info(rid)
  101. end
  102. -- 定时器结束
  103. local function _timeout_award()
  104. log.info("_timeout_award rollList[%s]", tostring(root.rollList))
  105. if is_empty(root.rollList) then
  106. return
  107. end
  108. local rollList = {}
  109. local currTime = skynet_time()
  110. for _, v in ipairs(root.rollList) do
  111. log.info("_timeout_award 房间ID[%s] 发奖时间[%s]", tostring(v.id), timeUtil.toString(v.awardTime))
  112. if currTime >= v.awardTime then
  113. -- 发奖
  114. _roll_award(v.id)
  115. else
  116. table.insert(rollList, table.copy(v))
  117. end
  118. end
  119. root.rollList = rollList
  120. end
  121. function root.onStart()
  122. math.randomseed(os.time())
  123. _load_active_roll_info()
  124. timerAward = timer.timeOut(60, _timeout_award)
  125. end
  126. function root.onStop()
  127. -- 取消定时器
  128. timerAward.func = nil
  129. end
  130. -- 新增roll房
  131. function root.add_roll(rid)
  132. local key = _get_room_key(rid)
  133. local awardTime = redisUtil.hget_int(key, "awardTime")
  134. local isAwarded = lib_game_redis:sismember(_get_room_award_key(), rid)
  135. if not is_empty(awardTime) and not isAwarded then
  136. local isMatch = false
  137. for _, v in ipairs(root.rollList) do
  138. if v.id == rid then
  139. isMatch = true
  140. v.awardTime = awardTime
  141. break
  142. end
  143. end
  144. if not isMatch then
  145. table.insert(root.rollList, {id = rid, awardTime = awardTime})
  146. -- 广播 - 新房
  147. local roomInfo = util_roll:pack_roll_room_info(rid)
  148. if not is_empty(roomInfo) then
  149. nodeMgr.broadcast_proto_notify("on_roll_new", {room = roomInfo})
  150. end
  151. end
  152. end
  153. end
  154. -- 更新roll房
  155. function root.update_roll(rid)
  156. local key = _get_room_key(rid)
  157. local awardTime = redisUtil.hget_int(key, "awardTime")
  158. local isAwarded = lib_game_redis:sismember(_get_room_award_key(), rid)
  159. if not is_empty(awardTime) and not isAwarded then
  160. local isMatch = false
  161. for _, v in ipairs(root.rollList) do
  162. if v.id == rid then
  163. isMatch = true
  164. v.awardTime = awardTime
  165. break
  166. end
  167. end
  168. if not isMatch then
  169. table.insert(root.rollList, {id = rid, awardTime = awardTime})
  170. end
  171. end
  172. end
  173. -- 删除roll房
  174. function root.del_roll(rid)
  175. local key = _get_room_key(rid)
  176. -- 删除roll信息
  177. lib_game_redis:del(key)
  178. lib_game_redis:srem(MAIN_KEY, rid)
  179. for k, v in ipairs(root.rollList) do
  180. if v.id == rid then
  181. table.remove(root.rollList, k)
  182. break
  183. end
  184. end
  185. end
  186. baseService.start(root, ".rollSrv", true)