box.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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:42:18
  8. --]]
  9. local code = require("code")
  10. local util_box = require("utils.util_box")
  11. local gameConst = require("const.gameConst")
  12. local itemUtil = require("utils.itemUtil")
  13. local boxAdapt = require("adapt.boxAdapt")
  14. local bagData = require("data.bag")
  15. local root = {}
  16. -- 盲盒 - 模块信息
  17. function root:box_blind_get_info(role, msg)
  18. local id = msg.id
  19. local ret = {
  20. boxInfoList = util_box:pack_box_info_list(id)
  21. }
  22. return code.OK, ret
  23. end
  24. -- 盲盒 -开盲盒
  25. function root:box_blind_open(role, msg)
  26. local id = msg.id
  27. local count = msg.count or 1
  28. if is_empty(id) then
  29. return code.PARAMTER_ERROR
  30. end
  31. local uid = role.uid
  32. local conf = boxAdapt:blind_get_box_key_info(id)
  33. local items = nil
  34. for i = 1, count do
  35. -- 掉落物品
  36. local itemId, itemCount = util_box:get_box_drop_item_and_count(id)
  37. if not is_empty(itemId) and not is_empty(itemCount) then
  38. local ok, costItems = bagData:is_enough(uid, {{id = gameConst.ITEM_ID.DIAMOND, count = conf.price}})
  39. if not ok then
  40. break
  41. end
  42. -- 消耗
  43. local eventId = string.format("box-blind-%s", tostring(id))
  44. bagData:consume_items(uid, costItems, eventId)
  45. -- 获取物品
  46. local _items = {{id = itemId, count = itemCount}}
  47. bagData:add_items(uid, _items, eventId)
  48. items = itemUtil.items_merge(items, _items)
  49. end
  50. end
  51. if is_empty(items) then
  52. return code.BAG.ITEM_NOT_ENOUGH
  53. end
  54. local ret = {
  55. items = items,
  56. boxInfo = util_box:pack_box_info(id)
  57. }
  58. return code.OK, ret
  59. end
  60. return root