123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- --[[
- Descripttion:roll房
- version:
- Author: Neo,Huang
- Date: 2023-11-16 21:45:09
- LastEditors: Neo,Huang
- LastEditTime: 2023-11-16 23:20:12
- --]]
- local timer = require("timer")
- local baseService = require("baseService")
- local lib_game_redis = require("lib_game_redis")
- local redisUtil = require("utils.redisUtil")
- local util_mail = require("utils.util_mail")
- local timeUtil = require("utils.timeUtil")
- local nodeMgr = require("nodeMgr")
- local util_roll = require("utils.util_roll")
- local resAdapt = require("adapt.resAdapt")
- local bagData = require("data.bag")
- local timerAward = nil
- local root = {rollList = {}}
- -- 载入活跃roll房
- local MAIN_KEY = "roll:room"
- local function _get_room_key(rid)
- return string.format("%s:%s", MAIN_KEY, tostring(rid))
- end
- local function _get_room_award_key()
- return string.format("%s:award", MAIN_KEY)
- end
- local function _load_active_roll_info()
- local ridList = lib_game_redis:smembers(MAIN_KEY)
- log.info("_load_active_roll_info ridList[%s]", tostring(ridList))
- if is_empty(ridList) then
- return
- end
- for _, v in ipairs(ridList) do
- local rid = tonumber(v)
- local key = _get_room_key(rid)
- local awardTime = redisUtil.hget_int(key, "awardTime")
- if not is_empty(awardTime) then
- table.insert(root.rollList, {id = rid, awardTime = awardTime})
- end
- end
- log.info("_load_active_roll_info rollList[%s]", tostring(root.rollList))
- end
- -- 发奖
- local function _roll_award(rid)
- log.info("_roll_award 房间ID[%s]", tostring(rid))
- if is_empty(rid) then
- return
- end
- local key = _get_room_key(rid)
- -- 删除roll信息
- lib_game_redis:del(key)
- lib_game_redis:srem(MAIN_KEY, rid)
- lib_game_redis:sismember(_get_room_award_key(), rid)
- local signupUidList = redisUtil.hget_json(key, "signupUidList")
- if is_empty(signupUidList) then
- -- 没有玩家参与
- return
- end
- local itemIdList = redisUtil.hget_json(key, "itemIdList")
- if is_empty(itemIdList) then
- -- 无奖励物品
- return
- end
- local mapUidItems = {}
- for _, id in ipairs(itemIdList) do
- -- 随机中奖玩家
- local index = math.random(1, #signupUidList)
- local uid = signupUidList[index]
- if mapUidItems[uid] == nil then
- mapUidItems[uid] = {}
- end
- table.insert(mapUidItems[uid], {id = id, count = 1})
- end
- log.info("_roll_award mapUidItems[%s]", tostring(mapUidItems))
- local name = redisUtil.hget(key, "name")
- local title = "roll房结算"
- -- 中奖玩家发奖
- for uid, items in pairs(mapUidItems) do
- local keyEvent = string.format("roll-%s", tostring(rid))
- bagData:add_items(uid, items, keyEvent)
- -- 邮件
- local cnt = string.format("恭喜您,参与%sroll房活动中获得以下奖励,奖励已发送至背包,请查收。", tostring(name))
- for _, v in ipairs(items) do
- cnt = cnt + string.format("\n%s", resAdapt:get_item_name(v.id))
- end
- util_mail:add_mail(uid, 0, title, cnt, {}, 0, nil, nil, "system")
- end
- -- 未中奖玩家邮件
- for _, uid in ipairs(signupUidList) do
- if mapUidItems[uid] == nil then
- local cnt = string.format("很遗憾,您参与的%sroll房活动中未获得奖励。", tostring(name))
- util_mail:add_mail(uid, 0, title, cnt, {}, 0, nil, nil, "system")
- end
- end
- end
- -- 定时器结束
- local function _timeout_award()
- log.info("_timeout_award rollList[%s]", tostring(root.rollList))
- if is_empty(root.rollList) then
- return
- end
- local rollList = {}
- local currTime = skynet_time()
- for _, v in ipairs(root.rollList) do
- log.info("_timeout_award 房间ID[%s] 发奖时间[%s]", tostring(v.id), timeUtil.toString(v.awardTime))
- if currTime >= v.awardTime then
- -- 发奖
- _roll_award(v.id)
- else
- table.insert(rollList, table.copy(v))
- end
- end
- root.rollList = rollList
- end
- function root.onStart()
- math.randomseed(os.time())
- _load_active_roll_info()
- timerAward = timer.timeOut(60, _timeout_award)
- end
- function root.onStop()
- -- 取消定时器
- timerAward.func = nil
- end
- -- 新增roll房
- function root.add_roll(rid)
- local key = _get_room_key(rid)
- local awardTime = redisUtil.hget_int(key, "awardTime")
- local isAwarded = lib_game_redis:sismember(_get_room_award_key(), rid)
- if not is_empty(awardTime) and not isAwarded then
- local isMatch = false
- for _, v in ipairs(root.rollList) do
- if v.id == rid then
- isMatch = true
- v.awardTime = awardTime
- break
- end
- end
- if not isMatch then
- table.insert(root.rollList, {id = rid, awardTime = awardTime})
- -- 广播 - 新房
- local roomInfo = util_roll:pack_roll_room_info(rid)
- if not is_empty(roomInfo) then
- nodeMgr.broadcast_proto_notify("on_roll_new", {room = roomInfo})
- end
- end
- end
- end
- -- 更新roll房
- function root.update_roll(rid)
- local key = _get_room_key(rid)
- local awardTime = redisUtil.hget_int(key, "awardTime")
- local isAwarded = lib_game_redis:sismember(_get_room_award_key(), rid)
- if not is_empty(awardTime) and not isAwarded then
- local isMatch = false
- for _, v in ipairs(root.rollList) do
- if v.id == rid then
- isMatch = true
- v.awardTime = awardTime
- break
- end
- end
- if not isMatch then
- table.insert(root.rollList, {id = rid, awardTime = awardTime})
- end
- end
- end
- -- 删除roll房
- function root.del_roll(rid)
- local key = _get_room_key(rid)
- -- 删除roll信息
- lib_game_redis:del(key)
- lib_game_redis:srem(MAIN_KEY, rid)
- for k, v in ipairs(root.rollList) do
- if v.id == rid then
- table.remove(root.rollList, k)
- break
- end
- end
- end
- baseService.start(root, ".rollSrv", true)
|