--[[ Descripttion:开箱子 version: Author: Neo,Huang Date: 2023-11-15 22:34:35 LastEditors: Neo,Huang LastEditTime: 2023-11-15 22:41:18 --]] local timeUtil = require("utils.timeUtil") local redisUtil = require("utils.redisUtil") local util_player = require("utils.util_player") local mysqlUtil = require("utils.mysqlUtil") local lib_game_redis = require("lib_game_redis") local baseAdapt = require("base.baseAdapt") local boxAdapt = require("adapt.boxAdapt") local resAdapt = require("adapt.resAdapt") local MODULE_NAME = "box" local root = {} ---------------------------------------- -- 盲盒 ---------------------------------------- local function _get_box_key(boxId) return string.format("%s:%s", MODULE_NAME, tostring(boxId)) end -- 获取开箱物品 function root:get_box_drop_item_and_count(boxId) local confList = boxAdapt:blind_get_box_item_list(boxId) if is_empty(confList) then return end -- 更新权重 for _, v in ipairs(confList) do v.weight = 0 local price = resAdapt:get_item_price(v.itemId) if not is_empty(price) then v.weight = math.floor(v.weightByPrice / price) end end local index = random_list_by_weight(confList) return confList[index].itemId, confList[index].count end -- 新增开箱记录 function root:add_record(uid, boxId, itemId, count) if is_empty(uid) or is_empty(boxId) or is_empty(itemId) then return false end local currTime = timeUtil.now(uid) local key = _get_box_key(boxId) local dropList = redisUtil.hget_json(key, "dropList") table.insert(dropList, {uid = uid, time = currTime, itemId = itemId, count = count}) -- 限制数量 if #dropList > 20 then table.remove(dropList, 1) end redisUtil.hset(key, "dropList", dropList) return true end -- 打包箱子信息 function root:pack_box_info(boxId) if is_empty(boxId) then return end local key = _get_box_key(boxId) local dropList = redisUtil.hget_json(key, "dropList") for _, v in ipairs(dropList) do v.playerInfo = util_player:get_base_info(v.uid) end local info = { id = boxId, dropList = dropList } return info end -- 打包所有箱子信息 function root:pack_box_info_list(boxId) local boxInfoList = {} if is_empty(boxId) then local conf = baseAdapt:getConfig("BlindBoxConfig") for _, v in ipairs(conf) do local info = self:pack_box_info(v.boxId) if not is_empty(info) then table.insert(boxInfoList, info) end end else local info = self:pack_box_info(boxId) if not is_empty(info) then table.insert(boxInfoList, info) end end return boxInfoList end ---------------------------------------- -- 追梦 ---------------------------------------- local function _get_dream_key() return "box:dream" end local function _get_dream_odds_tims_key(ti) return string.format("box:dream:odds-times:%s", timeUtil.toDate(ti)) end -- 新增记录 function root:dream_add_record(uid, itemId, odds, dropItem, isWin) if is_empty(uid) or is_empty(itemId) or is_empty(odds) or is_empty(dropItem) then return false end local price = resAdapt:get_item_price(itemId) if is_empty(price) then return false end local dropTime = timeUtil.now() local expireTime = dropItem + timeUtil.SEC_PER_DAY * 365 local sql = string.format( "INSERT INTO `mdl_dream` (`uid`,`itemId`,`dropItem`,`price`,`odds`,`isWin`,`dropTime`,`expireTime`)" .. " VALUES (%s,%s,'%s',%s,%s,%s,%s,%s); ", tostring(uid), tostring(itemId), cjson_encode(dropItem), tostring(price), tostring(odds), tostring(isWin and 1 or 0), tostring(dropTime), tostring(expireTime) ) return mysqlUtil:insert(sql) end -- 新增概率次数 function root:dream_add_odds_times(odds, isWin) if is_empty(odds) then return false end local key = _get_dream_odds_tims_key() local subKey = string.format("odds:%s:%s", tostring(odds), tostring(isWin and 1 or 0)) local times = redisUtil.hget_int(key, subKey) times = times + 1 redisUtil.hset(key, subKey, times) end -- 获取精彩瞬间记录 function root:dream_get_brilliants() local minPrice = 100000 local count = 20 local sql = string.format( "SELECT * FROM `mdl_dream` WHERE `price`>=%s AND `isWin`=1 ORDER BY dropTime DESC limit %s", tostring(minPrice), tostring(count) ) local ok, ret = mysqlUtil:select(sql) if not ok or is_empty(ret) then return end local list = {} for _, v in ipairs(ret) do local uid = tonumber(v.uid) local info = { playerInfo = util_player:get_base_info(uid), itemId = tonumber(v.itemId), dropItem = cjson_decode(v.dropItem), price = tonumber(v.price), odds = tonumber(v.odds), dropTime = tonumber(v.dropTime) } table.insert(list, info) end return list end -- 掉落记录 function root:dream_get_records() local count = 20 local sql = string.format("SELECT * FROM `mdl_dream` ORDER BY dropTime DESC limit %s", tostring(count)) local ok, ret = mysqlUtil:select(sql) if not ok or is_empty(ret) then return end local list = {} for _, v in ipairs(ret) do local uid = tonumber(v.uid) local info = { playerInfo = util_player:get_base_info(uid), itemId = tonumber(v.itemId), dropItem = cjson_decode(v.dropItem), price = tonumber(v.price), odds = tonumber(v.odds), dropTime = tonumber(v.dropTime) } table.insert(list, info) end return list end -- 次数统计 function root:dream_get_statement() local days = 3 local mapOddsWinTims = {} local mapOddsLoseTims = {} local currTime = timeUtil.now() for i = 0, days - 2 do local ti = currTime - timeUtil.SEC_PER_DAY * i local key = _get_dream_odds_tims_key(ti) for odds = 5, 85 do -- 中奖 local subKey = string.format("odds:%s:1", tostring(odds)) local times = redisUtil.hget_int(key, subKey) mapOddsWinTims[odds] = (mapOddsWinTims[odds] or 0) + times -- 不中奖 subKey = string.format("odds:%s:0", tostring(odds)) times = redisUtil.hget_int(key, subKey) mapOddsLoseTims[odds] = (mapOddsLoseTims[odds] or 0) + times end end local oddsTimesList = {} for odds = 5, 85 do if not is_empty(mapOddsWinTims[odds]) or not is_empty(mapOddsLoseTims[odds]) then local info = { odds = odds, winTimes = mapOddsWinTims[odds] or 0, loseTimes = mapOddsLoseTims[odds] or 0 } table.insert(oddsTimesList, info) end end return oddsTimesList end -- 删除天统计 function root:dream_del_day_statement(ti) if is_empty(ti) then return end local key = _get_dream_odds_tims_key(ti) lib_game_redis:del(key) end return root