RandomChest.lua 5.5 KB

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