util_box.lua 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. --[[
  2. Descripttion:开箱子
  3. version:
  4. Author: Neo,Huang
  5. Date: 2023-11-15 22:34:35
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2023-11-15 22:41:18
  8. --]]
  9. local timeUtil = require("utils.timeUtil")
  10. local redisUtil = require("utils.redisUtil")
  11. local util_player = require("utils.util_player")
  12. local baseAdapt = require("base.baseAdapt")
  13. local boxAdapt = require("adapt.boxAdapt")
  14. local resAdapt = require("adapt.resAdapt")
  15. local MODULE_NAME = "box"
  16. local root = {}
  17. local function _get_box_key(boxId)
  18. return string.format("%s:%s", MODULE_NAME, tostring(boxId))
  19. end
  20. -- 获取开箱物品
  21. function root:get_box_drop_item_and_count(boxId)
  22. local confList = boxAdapt:blind_get_box_item_list(boxId)
  23. if is_empty(confList) then
  24. return
  25. end
  26. -- 更新权重
  27. for _, v in ipairs(confList) do
  28. v.weight = 0
  29. local price = resAdapt:get_item_price(v.itemId)
  30. if not is_empty(price) then
  31. v.weight = math.floor(v.weightByPrice / price)
  32. end
  33. end
  34. local index = random_list_by_weight(confList)
  35. return confList[index].itemId, confList[index].count
  36. end
  37. -- 新增开箱记录
  38. function root:add_record(uid, boxId, itemId, count)
  39. if is_empty(uid) or is_empty(boxId) or is_empty(itemId) then
  40. return false
  41. end
  42. local currTime = timeUtil.now(uid)
  43. local key = _get_box_key(boxId)
  44. local dropList = redisUtil.hget_json(key, "dropList")
  45. table.insert(dropList, {uid = uid, time = currTime, itemId = itemId, count = count})
  46. -- 限制数量
  47. if #dropList > 20 then
  48. table.remove(dropList, 1)
  49. end
  50. redisUtil.hset(key, "dropList", dropList)
  51. return true
  52. end
  53. -- 打包箱子信息
  54. function root:pack_box_info(boxId)
  55. if is_empty(boxId) then
  56. return
  57. end
  58. local key = _get_box_key(boxId)
  59. local dropList = redisUtil.hget_json(key, "dropList")
  60. for _, v in ipairs(dropList) do
  61. v.playerInfo = util_player:get_base_info(v.uid)
  62. end
  63. local info = {
  64. id = boxId,
  65. dropList = dropList
  66. }
  67. return info
  68. end
  69. -- 打包所有箱子信息
  70. function root:pack_box_info_list(boxId)
  71. local boxInfoList = {}
  72. if is_empty(boxId) then
  73. local conf = baseAdapt:getConfig("BlindBoxConfig")
  74. for _, v in ipairs(conf) do
  75. local info = self:pack_box_info(v.boxId)
  76. if not is_empty(info) then
  77. table.insert(boxInfoList, info)
  78. end
  79. end
  80. else
  81. local info = self:pack_box_info(boxId)
  82. if not is_empty(info) then
  83. table.insert(boxInfoList, info)
  84. end
  85. end
  86. return boxInfoList
  87. end
  88. return root