Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:CosmeticCrates

From MCC Island Wiki

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 getCrateOrChance(frame, field)
	local args = getArgs(frame)
	local name = args.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
				if field == "crate" then
					return crateName
				elseif field == "chance" then
					return chance .. '%'
				end
			end
		end
	end
	
	return errorModule.error{
		message = 'Cosmetic "' .. name .. '" not found in any crate.',
		tag = 'span'
	}
end

p.getCrate = function(frame)
	return getCrateOrChance(frame, "crate")
end
p.getChance = function(frame)
	return getCrateOrChance(frame, "chance")
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