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 23:02, 12 July 2025 by MTOnline (talk | contribs) (oh yeah, i need to use expandTemplate)

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

function p.getChance(frame)
	local args = getArgs(frame)
	local name = args.name
	
	for _, crate in pairs(data) do
		local chance = crate[name]
		if chance then
			return chance .. '%'
		end
	end
	
	return errorModule.error{
		message = 'Cosmetic "' .. name .. '" not found in any crate.',
		tag = 'span'
	}
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 = '{| class="wikitable"\n|-\n'
	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 /> %.2f%%\n', filename, name, name, chance)
		end
		
		result = result .. cell
		count = count + 1
	end
	
	result = result .. '|}'
	return result
end

return p