box.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. --[[
  2. Descripttion:接口 - 开箱子
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-07-05 17:32:01
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-07-05 20:29:29
  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, count = util_box:get_box_drop_item_and_count(id)
  37. if not is_empty(itemId) and not is_empty(count) then
  38. local costItems = {{id = gameConst.ITEM_ID.DIAMOND, count = conf.diamondPrice}}
  39. if not bagData:is_enough(uid, costItems) then
  40. -- 绑金不足,使用金币
  41. costItems = {{id = gameConst.ITEM_ID.GOLD, count = conf.goldPrice}}
  42. if not bagData:is_enough(uid, costItems) then
  43. break
  44. end
  45. end
  46. -- 消耗
  47. local eventId = string.format("box-blind-%s", tostring(id))
  48. bagData:consume_items(uid, costItems, eventId)
  49. -- 获取物品
  50. local _items = {{id = itemId, count = count}}
  51. bagData:add_items(uid, _items, eventId)
  52. items = itemUtil.items_merge(items, _items)
  53. end
  54. end
  55. if is_empty(items) then
  56. return code.BAG.ITEM_NOT_ENOUGH
  57. end
  58. local ret = {
  59. items = items,
  60. boxInfo = util_box:pack_box_info(id)
  61. }
  62. return code.OK, ret
  63. end
  64. return root