Module:LootTableUtil
Jump to navigation
Jump to search
This module holds the cargo query information for modules that need to query the loot tables table.
Downstream dependencies: Module:Meteorite drops & Module:Drops table display
- Subpages
local cargoUtil = require("Module:CargoUtil")
local argsUtil = require("Module:ArgsUtil")
local lootTableUtil = {}
-- Function to query loot table and adjust values
function lootTableUtil.queryLootTable(tableID, lootTableChance, defaultMinQuantity, defaultMaxQuantity)
-- Define the query options
local queryOptions = {
tables = "lootTable",
fields = "itemName, yieldMin, yieldMax, percentChance, tableID",
where = "tableID='" .. tableID .. "'"
}
-- Execute the query
local results = cargoUtil.queryData(queryOptions)
if not results then
return {}
end
-- Process the results
local processedResults = {}
for _, result in ipairs(results) do
local percentChance = tonumber(result.percentChance) or 0
local adjustedChance = lootTableChance * percentChance * 100
local yieldMin = tonumber(result.yieldMin) or 0
local yieldMax = tonumber(result.yieldMax) or 0
-- Use default min/max quantities if the values from the loot table are 0
if yieldMin == 0 then yieldMin = tonumber(defaultMinQuantity) end
if yieldMax == 0 then yieldMax = tonumber(defaultMaxQuantity) end
local yieldText = (yieldMin == yieldMax) and tostring(yieldMin) or (tostring(yieldMin) .. " - " .. tostring(yieldMax))
table.insert(processedResults, {
itemName = result.itemName,
yieldText = yieldText,
adjustedChance = adjustedChance
})
end
return processedResults
end
-- Function to get all results from multiple loot tables
function lootTableUtil.getAllResults(args)
local allResults = {}
for i = 1, math.huge do
local tableID = args["table" .. i]
local lootTableChance = tonumber(args["table" .. i .. "chance"]) or 0
local defaultMinQuantity = args["table" .. i .. "min"]
local defaultMaxQuantity = args["table" .. i .. "max"]
if not tableID then
break
end
local results = lootTableUtil.queryLootTable(tableID, lootTableChance, defaultMinQuantity, defaultMaxQuantity)
for _, result in ipairs(results) do
table.insert(allResults, result)
end
end
return allResults
end
-- Function to process input arguments and store data in the Cargo table
function lootTableUtil.processAndStoreInput(frame)
local args = argsUtil.merge(frame) -- Merge arguments from the current frame and its parent
local output = mw.html.create('div') -- Create an HTML div element for output
local pagename = mw.title.getCurrentTitle().text -- Get the current page name
-- Iterate over each table provided in the arguments
for i = 1, math.huge do
local tableID = args["table" .. i]
if not tableID then
break
end
-- Get the percent chance, yield min, and yield max from the arguments
local percentChance = (tonumber(args["table" .. i .. "chance"]) or 0) * 100 -- Multiply by 100 to convert to percentage
local yieldMin = tonumber(args["table" .. i .. "min"]) or 0
local yieldMax = tonumber(args["table" .. i .. "max"]) or 0
-- Prepare the data to be stored
local data = {
_table = "itemDrops",
name = pagename,
tableID = tableID,
percentChance = percentChance,
yieldMin = yieldMin,
yieldMax = yieldMax
}
-- Attempt to store the data and capture the result
local status, result = pcall(function()
return cargoUtil.storeData(data)
end)
-- Output the result of the storage operation only if there was an error
if not status then
output:wikitext("Error storing data for table " .. tableID .. ": " .. result .. "<br>")
end
end
return tostring(output)
end
return lootTableUtil