RandomChest_1.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. --- 宝箱读表随机生成道具
  2. RandomChest = {}
  3. local this = {}
  4. chestReward = "T$chestReward"
  5. --- 使用宝箱生成道具
  6. ---@param actor any 玩家对象
  7. ---@param itemConfigId number 道具配置ID
  8. ---@param count number 数量
  9. function RandomChest.generateChestItem(actor, itemConfigId, count)
  10. local itemRow = ConfigDataManager.getTable("cfg_item", "id", itemConfigId)
  11. if table.isNullOrEmpty(itemRow) then
  12. gameDebug.print("RandomChest.generateChestItem itemConfigId error")
  13. return
  14. end
  15. if itemRow[1].fashionbox == "1" then
  16. return
  17. end
  18. local allItem = {}
  19. local tipItem = {}
  20. if tonumber(itemRow[1].type) == ItemType.BOX and tonumber(itemRow[1].subtype) == ItemSubType.EQUIP_BOX then
  21. local rewardRecord = getplaydef(actor, chestReward)
  22. for i = 1, count do
  23. local res = this.getItemAndCount(actor, itemRow[1].useparam)
  24. if table.isNullOrEmpty(res) then
  25. gameDebug.print("RandomChest.generateChestItem getItemAndCount res is nil")
  26. else
  27. for itemId, itemCount in pairs(res) do
  28. if table.isNullOrEmpty(rewardRecord) then
  29. local temp = {}
  30. temp[itemId] = 1
  31. setplaydef(actor, chestReward, temp)
  32. else
  33. if rewardRecord[itemId] then
  34. rewardRecord[itemId] = rewardRecord[itemId] + 1
  35. else
  36. rewardRecord[itemId] = 1
  37. end
  38. setplaydef(actor, chestReward, rewardRecord)
  39. end
  40. table.mergeAdd(allItem, { [itemId] = itemCount })
  41. end
  42. end
  43. end
  44. end
  45. if table.count(allItem) > 0 then
  46. Bag.sendRewards(actor, allItem, "使用宝箱")
  47. this.excHorseLamp(actor, allItem)
  48. end
  49. end
  50. function this.excHorseLamp(actor, itemMap)
  51. if table.isNullOrEmpty(itemMap) then
  52. return
  53. end
  54. local name = getbaseinfo(actor, "rolename")
  55. for cfgId, count in pairs(itemMap) do
  56. local item = ConfigDataManager.getById("cfg_item", cfgId)
  57. if not table.isNullOrEmpty(item) then
  58. local itemName = item.name
  59. local noticeId = item.runninghorselamp
  60. if #noticeId > 0 then
  61. noticeTip.noticeinfo(actor, noticeId, name, itemName)
  62. end
  63. end
  64. end
  65. end
  66. --- 获取随机生成的道具数量
  67. ---@param actor any 玩家对象
  68. ---@param tableName string 配置表名
  69. function this.getItemAndCount(actor, tableName)
  70. local res = {}
  71. local boxConfig = ConfigDataManager.getList(tableName)
  72. if table.isNullOrEmpty(boxConfig) then
  73. gameDebug.print("RandomChest.generateChestItem useParam error")
  74. return res
  75. end
  76. local idMap = {}
  77. -- 职业限制判断
  78. local playerCareer = tonumber(getbaseinfo(actor, "getbasecareer"))
  79. -- 等级限制判断
  80. local playerLevel = getbaseinfo(actor, "level")
  81. local rewardRecord = getplaydef(actor, chestReward)
  82. for _, config in pairs(boxConfig) do
  83. if config.career ~= "" and tonumber(playerCareer) ~= tonumber(string.split(config.career, "#")[1]) then
  84. goto tag
  85. end
  86. local lv_split = string.split(config.level, "#")
  87. if config.level ~= "" and
  88. (tonumber(playerLevel) < tonumber(lv_split[1])
  89. or tonumber(playerLevel) > tonumber(lv_split[2])) then
  90. goto tag
  91. end
  92. local id = config.id
  93. local item = config.item
  94. local probability = string.split(config.probability, "|")
  95. local pro1 = string.split(probability[1], "#")
  96. if tonumber(config.type) == 1 then
  97. if not rewardRecord or not rewardRecord[item] then
  98. idMap[id] = pro1[2]
  99. else
  100. for _, v in pairs(probability) do
  101. local strs = string.split(v, "#")
  102. idMap[id] = tonumber(strs[1]) <= tonumber(rewardRecord[item]) and strs[2] or pro1[2]
  103. end
  104. end
  105. else
  106. local weight = pro1[2]
  107. if rewardRecord and rewardRecord[item] then
  108. for _, v in pairs(probability) do
  109. local v_split = string.split(v, "#")
  110. if tonumber(v_split[1]) ~= 0 then
  111. weight = tonumber(v_split[1]) >= rewardRecord[item] and tonumber(v_split[2]) or tonumber(pro1[2])
  112. end
  113. end
  114. end
  115. if tonumber(randomex(actor, weight, 10000)) == 1 then
  116. local num_split = string.split(config.num, "#")
  117. res[item] = math.random(num_split[1], num_split[2])
  118. end
  119. end
  120. :: tag ::
  121. end
  122. if table.isNullOrEmpty(idMap) then
  123. gameDebug.print("RandomChest.generateChestItem itemMap is nil")
  124. return res
  125. end
  126. if tonumber(boxConfig[1].type) == 1 then
  127. local id = randombyweight(actor, idMap, 1)[1]
  128. local num = ConfigDataManager.getTableValue(tableName, "num", "id", id)
  129. local itemId = ConfigDataManager.getTableValue(tableName, "item", "id", id)
  130. local num_split = string.split(num, "#")
  131. res[itemId] = math.random(num_split[1], num_split[2])
  132. return res
  133. else
  134. return res
  135. end
  136. end