Module:CosmeticCrates
From MCC Island Wiki
More actions
Documentation for this module may be created at Module:CosmeticCrates/doc
local getArgs = require('Module:Arguments').getArgs
local errorModule = require('Module:Error')
local expand = require('Module:Utils').expand
local isAnimated = require('Module:Utils').isAnimated
local data = require("Module:CosmeticCrates/Data")
if not data then
return "Error: Unable to load data"
end
local p = {}
local function findCosmetic(name)
for crateName, crateData in pairs(data) do
local tiers = {}
local totalChance = 0
local found = false
for _, cosmeticData in ipairs(crateData) do
local cosmeticName, chance = cosmeticData[1], tonumber(cosmeticData[2])
local tier = cosmeticData["tier"]
if cosmeticName == name then
totalChance = totalChance + chance
if tier ~= nil then
tiers[tier] = chance
end
found = true
end
end
if found then
return {
crate = crateName,
chance = tostring(totalChance) .. '%',
tiers = tiers
}
end
end
return nil
end
local function notFound(name)
return errorModule.error{
message = 'Cosmetic "' .. name .. '" not found in any crate.',
tag = 'span'
}
end
function p.getCrate(frame)
local args = getArgs(frame)
local name = args.name
local result = findCosmetic(name)
return result and result.crate or notFound(name)
end
function p.getChance(frame)
local args = getArgs(frame)
local name = args.name
local result = findCosmetic(name)
return result and result.chance or notFound(name)
end
function p.makeSentence(frame)
local args = getArgs(frame)
local name = args.name
local result = findCosmetic(name)
if not result then return notFound(name) end
local crate = expand(frame, 'Display Link', result.crate, 'size = 20px')
return string.format(
"'''%s''' chance when opening a %s",
result.chance, crate
)
end
function p.makeTable(frame)
local args = getArgs(frame)
local crate = args.crate
local perRow = args.per_row or 5
local crateData = data[crate]
if not crateData then return 'No data found for crate: ' .. crate end
local result = string.format(
'{| class="wikitable"\n|-\n|colspan="5"|[[File:%s.png|20px|link=%s]] <b>%s</b>\n|-\n',
crate, crate, crate
)
local count = 0
for _, entry in ipairs(crateData) do
if count % perRow == 0 and count > 0 then
result = result .. '|-\n'
end
local name, chance = entry[1], entry[2]
local weaponTier = ' ' .. string.rep('[[File:Gargantuan Fish.png|16px|link=]]', tonumber(entry["tier"]) or 0)
local filename = name .. '.png'
local cell
if isAnimated(frame, filename) then
local afix = expand(frame, 'AFix', filename, string.format("link=%s", name))
cell = string.format('|%s [[%s]]%s <br /> %.2f%%\n', afix, name, weaponTier, chance)
else
cell = string.format('|[[File:%s|150px|center|link=%s]] <br /> [[%s]]%s <br /> <b>%.2f%%</b>\n', filename, name, name, weaponTier, chance)
end
result = result .. cell
count = count + 1
end
result = result .. '|}'
return result
end
return p