RandomChest.lua 5.2 KB

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