local config = {
actionId = 45001, -- put this on your chest
baseChance = 100, -- all percents are based on 1000 -- 100/1000 = 10% || 1/1000 = 0.1%
cooldown = {
active = true, -- false will disable cooldown
key = 11111,
value = 64800 -- 18 hours. or alternatively use 60*60*18
},
chanceIncreasers = {
{itemId = 1111, chanceIncrease = 10}, -- put less valuable / lower tier increase first
{itemId = 2222, chanceIncrease = 25}, -- [itemId] = {chanceIncrease = ##}
{itemId = 3333, chanceIncrease = 50} -- put most valuable last
},
rewards = {
{itemId = 1111, minCount = 1, maxCount = 1, chance = 500}, -- 50%
{itemId = 2222, minCount = 1, maxCount = 10, chance = 50}, -- 5%
{itemId = 3333, minCount = 10, maxCount = 50, chance = 5} -- 0.5%
}
}
local rewardChest = Action()
function rewardChest.onUse(player, item, fromPosition, target, toPosition, isHotkey)
if config.cooldown.active then
local currentTime = os.time()
local currentStorage = player:getStorageValue(config.cooldown.key)
if currentStorage > currentTime then
player:sendCancelMessage("Currently on cooldown. Can use again in ".. os.date("!%Hh %Mm %Ss", currentStorage - currentTime) .."")
return true
end
player:setStorageValue(config.cooldown.key, (currentTime + config.cooldown.value))
end
local chance = config.baseChance
for i = 1, # config.chanceIncreasers do -- loop through chanceIncreasers
local increaserCount = player:getItemCount(config.chanceIncreasers[i].itemId)
local increasersUsed = 0
while chance < 1000 and increaserCount > 0 do -- uses as many increasers as possible to get to 100% chance, then stops using them.
chance = chance + config.chanceIncreasers[i].chanceIncrease
increaserCount = increaserCount - 1
increasersUsed = increasersUsed + 1
end
player:removeItem(config.chanceIncreasers[i].itemId, increasersUsed)
end
local rand = math.random(1000)
if rand > chance then
player:say("No reward.", TALKTYPE_MONSTER_SAY)
return true
end
local rewardList = {} -- this section is forcing a single item to be chosen from the rewards section
while #rewardList < 1 do
for i = 1, #config.rewards do
rand = math.random(1000)
if rand <= config.rewards[i].chance then
rewardList[#rewardList + 1] = i
end
end
end
rand = math.random(#rewardList)
player:addItem(config.rewards[rand].itemId, math.random(config.rewards[rand].minCount, config.rewards[rand].maxCount), true)
player:say("Received " .. ItemType(config.rewards[rand].itemId):getName():lower() .. " as reward.", TALKTYPE_MONSTER_SAY)
return true
end
rewardChest:aid(config.actionId)
rewardChest:register()