util_box.lua 2.6 KB

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