box.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 resAdapt = require("adapt.resAdapt")
  15. local bagData = require("data.bag")
  16. local root = {}
  17. ----------------------------------------
  18. -- 盲盒
  19. ----------------------------------------
  20. -- 模块信息
  21. function root:box_blind_get_info(role, msg)
  22. local id = msg.id
  23. local ret = {
  24. boxInfoList = util_box:pack_box_info_list(id)
  25. }
  26. return code.OK, ret
  27. end
  28. -- 开盲盒
  29. function root:box_blind_open(role, msg)
  30. local id = msg.id
  31. local count = msg.count or 1
  32. if is_empty(id) then
  33. return code.PARAMTER_ERROR
  34. end
  35. local uid = role.uid
  36. local conf = boxAdapt:blind_get_box_key_info(id)
  37. local items = nil
  38. for i = 1, count do
  39. -- 掉落物品
  40. local itemId, itemCount = util_box:get_box_drop_item_and_count(id)
  41. if not is_empty(itemId) and not is_empty(itemCount) then
  42. local ok, costItems = bagData:is_enough(uid, {{id = gameConst.ITEM_ID.DIAMOND, count = conf.price}})
  43. if not ok then
  44. break
  45. end
  46. -- 消耗
  47. local keyEvent = string.format("box-blind-%s", tostring(id))
  48. bagData:consume_items(uid, costItems, keyEvent)
  49. -- 获取物品
  50. local _items = {{id = itemId, count = itemCount}}
  51. bagData:add_items(uid, _items, keyEvent)
  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. ----------------------------------------
  65. -- 追梦
  66. ----------------------------------------
  67. -- 开奖
  68. function root:box_dream_open(role, msg)
  69. local itemId = msg.itemId
  70. local odds = msg.odds
  71. if is_empty(itemId) or is_empty(odds) or odds < 5 or odds > 85 then
  72. return code.PARAMTER_ERROR
  73. end
  74. local confItem = resAdapt:get_item_conf(itemId)
  75. if is_empty(confItem) then
  76. return code.BAG.ITEM_NOT_EXIST
  77. end
  78. local uid = role.uid
  79. -- 消耗
  80. local costItems = {
  81. {id = gameConst.ITEM_ID.DIAMOND, count = math.floor(confItem.price / confItem.priceConst * odds)}
  82. }
  83. local ok, _costItems = bagData:is_enough(uid, costItems)
  84. if not ok then
  85. return code.BAG.ITEM_NOT_ENOUGH
  86. end
  87. -- 消耗
  88. local keyEvent = string.format("box-dream-%s", tostring(itemId))
  89. bagData:consume_items(uid, _costItems, keyEvent)
  90. -- 随机道具
  91. local awardList = {
  92. {id = itemId, count = 1, weight = odds},
  93. {id = gameConst.ITEM_ID.N1, count = 1, weight = 100 - odds}
  94. }
  95. local index = random_list_by_weight(awardList)
  96. local dropItem = {id = awardList[index].id, count = awardList[index].count}
  97. bagData:add_items(uid, {dropItem}, keyEvent)
  98. -- 记录/统计
  99. local isWin = dropItem.id == itemId
  100. util_box:dream_add_record(uid, itemId, odds, dropItem, isWin)
  101. util_box:dream_add_odds_times(odds, isWin)
  102. return code.OK, {dropItem = dropItem}
  103. end
  104. -- 精彩瞬间
  105. function root:box_dream_brilliant(role, msg)
  106. local list = util_box:dream_get_brilliants()
  107. return code.OK, {list = list}
  108. end
  109. -- 掉落记录
  110. function root:box_dream_records(role, msg)
  111. local list = util_box:dream_get_records()
  112. return code.OK, {list = list}
  113. end
  114. -- 全局统计
  115. function root:box_dream_statement(role, msg)
  116. local oddsTimesList = util_box:dream_get_statement()
  117. return code.OK, {oddsTimesList = oddsTimesList}
  118. end
  119. return root