Module:CosmeticCrates: Difference between revisions
From MCC Island Wiki
More actions
m also that |
added makeSentence function |
||
(One intermediate revision by the same user not shown) | |||
Line 17: | Line 17: | ||
end | end | ||
local function | local function findCosmetic(name) | ||
for crateName, crateData in pairs(data) do | for crateName, crateData in pairs(data) do | ||
for _, pair in ipairs(crateData) do | for _, pair in ipairs(crateData) do | ||
local cosmeticName, chance = pair[1], pair[2] | local cosmeticName, chance = pair[1], pair[2] | ||
if cosmeticName == name then | if cosmeticName == name then | ||
return { | |||
crate = crateName, | |||
chance = chance .. '%' | |||
} | |||
end | end | ||
end | end | ||
end | end | ||
return nil | |||
end | |||
local function notFound(name) | |||
return errorModule.error{ | return errorModule.error{ | ||
message = 'Cosmetic "' .. name .. '" not found in any crate.', | message = 'Cosmetic "' .. name .. '" not found in any crate.', | ||
tag = 'span' | tag = 'span' | ||
} | } | ||
end | end | ||
p.getCrate = | function p.getCrate(frame) | ||
return | local args = getArgs(frame) | ||
local name = args.name | |||
local result = findCosmetic(name) | |||
return result and result.crate or notFound(name) | |||
end | end | ||
p.getChance = function(frame) | function p.getChance(frame) | ||
return | 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 | |||
if frame.expandTemplate then | |||
crate = frame:expandTemplate{ title = 'Display Link', args = { result.crate, size = '20px' } } | |||
else | |||
crate = result.crate | |||
end | |||
return string.format( | |||
"'''%s''' chance when opening a %s", | |||
result.chance, crate | |||
) | |||
end | end | ||
Line 54: | Line 78: | ||
local result = string.format( | local result = string.format( | ||
'{| class="wikitable"\n|-\n|colspan="5"|[[File:%s.png|20px|link=%s]] %s\n|-', | '{| class="wikitable"\n|-\n|colspan="5"|[[File:%s.png|20px|link=%s]] %s\n|-\n', | ||
crate, crate, crate | crate, crate, crate | ||
) | ) |
Latest revision as of 08:48, 27 August 2025
Documentation for this module may be created at Module:CosmeticCrates/doc
local getArgs = require('Module:Arguments').getArgs
local errorModule = require('Module:Error')
local data = require("Module:CosmeticCrates/Data")
if not data then
return "Error: Unable to load data"
end
local p = {}
local function isAnimated(frame, filename)
if not frame.preprocess then
return true
end
local content = frame:preprocess('{{:File:' .. filename .. '}}')
return content:find('Category:Animated images') ~= nil
end
local function findCosmetic(name)
for crateName, crateData in pairs(data) do
for _, pair in ipairs(crateData) do
local cosmeticName, chance = pair[1], pair[2]
if cosmeticName == name then
return {
crate = crateName,
chance = chance .. '%'
}
end
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
if frame.expandTemplate then
crate = frame:expandTemplate{ title = 'Display Link', args = { result.crate, size = '20px' } }
else
crate = result.crate
end
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 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]] %s\n|-\n',
crate, crate, crate
)
local count = 0
for _, entry in ipairs(crateData) do
if count % 5 == 0 and count > 0 then
result = result .. '|-\n'
end
local name, chance = entry[1], entry[2]
local filename = name .. '.png'
local cell
if isAnimated(frame, filename) then
local afix = frame:expandTemplate{ title = 'AFix', args = { filename, link = name } }
cell = string.format('|%s [[%s]]<br /> %.2f%%\n', afix, name, chance)
else
cell = string.format('|[[File:%s|150px|center|link=%s]] <br /> [[%s]] <br /> <b>%.2f%%</b>\n', filename, name, name, chance)
end
result = result .. cell
count = count + 1
end
result = result .. '|}'
return result
end
return p