box.lua 3.7 KB

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