12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- --[[
- 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 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
- return root
|