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

Documentation for this module may be created at Module:CollectionTable/doc

local getArgs = require('Module:Arguments').getArgs
local expand = require('Module:Utils').expand
local isAnimated = require('Module:Utils').isAnimated
local info = require('Module:CosmeticInfo')
local cosmeticData = mw.loadData('Module:CosmeticInfo/Data')

local p = {}

local rarityOrder = {
	Common = 1, Uncommon = 2, Rare = 3,
	Epic = 4, Legendary = 5, Mythic = 6
}

local typeOrder = {
	Hat = 1, Accessory = 2, Cloak = 3, Aura = 4, Trail = 5, Rod = 6
}

local function sortForTable(a, b)
	local ra, rb = rarityOrder[a.rarity], rarityOrder[b.rarity]
	if ra ~= rb then return ra < rb end
	local ta, tb = typeOrder[a.type], typeOrder[b.type]
	if ta ~= tb then return ta < tb end
	return a.name < b.name
end

local function formatImage(frame, cosmetic)
	local filename = cosmetic.name .. ".png"
	if isAnimated(frame, filename) then
		return string.format(
			"%s <br /> [[%s]]",
			expand(frame, "AFix", filename, string.format("link=%s", cosmetic.name)),
			cosmetic.name
		)
	else
		return string.format("[[File:%s|150px|link=%s]] <br /> <br /> [[%s]]",
			filename, cosmetic.name, cosmetic.name)
	end
end

local function formatInfo(frame, cosmetic)
	local rarity = expand(frame, "Rarity", cosmetic.rarity, "Text")
	local category = expand(frame, "CosmeticCategory", cosmetic.category)
	local extra = {}
	
	table.insert(extra, cosmetic.colorable and expand(frame, "Colorable") or "")
	
	if cosmetic.trophies then
		local totalFlag = cosmetic.colorable and "Total: " or ""
		local trophyCount = cosmetic.trophies + (cosmetic.colorable and 10 or 0)
		table.insert(extra, string.format("%s%s", totalFlag, expand(frame, cosmetic.trophyType .. " Trophy", "<b>" .. trophyCount .. "</b>")))
	else
		table.insert(extra, "")
	end
	
	return table.concat({ string.format('%s %s', rarity, category), table.concat(extra, " <br /> ") }, " <br /> ")
end

function p.make(frame)
	local args = getArgs(frame)
	local collection = args[1] or args.collection
	if not collection then return "Error: no collection" end
	
	local cosmetics = {}
	for name, _ in pairs(cosmeticData.cosmetics) do
		if info.getCollection({name = name}) == collection then
			table.insert(cosmetics, {
				name = name,
				rarity = info.getRarity({name = name}),
				category = info.getCategory({name = name}),
				colorable = info.isColorable({name = name}),
				trophyType = info.isBonusTrophies{ name = name } and "Bonus" or "Style",
				trophies = info.getTrophiesAwarded({name = name})
			})
		end
	end
	
	table.sort(cosmetics, sortForTable)
	
	local out = { "<center>", "{| class=\"wikitable\"" }

	local i = 1
	while i <= #cosmetics do
		-- Images row
		for j = 0, 4 do
			local idx = i + j
			if idx > #cosmetics then break end
			table.insert(out, "| " .. formatImage(frame, cosmetics[idx]))
		end

		-- Info row
		table.insert(out, "|-")
		for j = 0, 4 do
			local idx = i + j
			if idx > #cosmetics then break end
			table.insert(out, "| " .. formatInfo(frame, cosmetics[idx]))
		end

		-- Next batch
		i = i + 5
		table.insert(out, "|-")
	end

	table.insert(out, "|}</center>")
	return table.concat(out, "\n")
end

return p