Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 08:48, 27 August 2025 by MTOnline (talk | contribs) (added makeSentence function)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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