Module:CardList

From Infinity Wars Wiki
Jump to navigation Jump to search

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

-- This is Lua

local p = {}
local cargo = mw.ext.cargo

local rarities = {"Common","Uncommon","Rare","Epic","Legendary"}

local types = {
	{"Character", "Characters", Cards = {Common={},Uncommon={},Rare={},Epic={},Legendary={}}},
	{"Ability", "Abilities", Cards = {Common={},Uncommon={},Rare={},Epic={},Legendary={}}},
	{"Hero", "Heroes", Cards = {Common={},Uncommon={},Rare={},Epic={},Legendary={}}},
	{"Relic", "Relics", Cards = {Common={},Uncommon={},Rare={},Epic={},Legendary={}}}
}

function typeHasAnyCards(t) 
	if #t["Common"] + #t["Uncommon"] + #t["Rare"] + #t["Epic"] + #t["Legendary"] > 0 then return true end
	return false
end

function p.Faction(frame)
	return p.Main('Faction')	
end

function p.Set(frame)
	return p.Main('CardSet')
end

function p.CardType(frame)
	return p.Main('CardType')
end

function p.Rarity(frame)
	return p.Main('Rarity')
end

function p.Main( field )
	local output = ""
    local tables = 'Cards'
    local fields = '_pageName=Page,Cost,Rarity,CardType=Type'
    local args = {
        where = 'NOT(Tags HOLDS "Token") AND '..field..'="'..mw.title.getCurrentTitle().text..'"',
        groupBy = '_pageName',
        orderBy = 'Cost'
    }
    local cards = cargo.query( tables, fields, args )
    for i,card in ipairs(cards) do
    	for i2,type in ipairs(types) do
    		if card.Type == type[1] then
    			table.insert(type.Cards[card.Rarity], card)
			end
    	end
    end
    
    for i,type in ipairs(types) do
    	if typeHasAnyCards(type.Cards) then
    		output = output .. '\n==' .. type[2] .. '==\n\n'
    		for i2, rarity in ipairs(rarities) do
    			if #type.Cards[rarity] > 0 then
    				output = output .. '\n===' .. rarity .. '===\n<div class="factiongallery">'
    				for i3, card in ipairs(type.Cards[rarity]) do
    					output = output .. '<div class="cardimage" style="order:'..(card.Cost or 0)..';">[[File:'..card.Page..'.png|x376px|link='..card.Page..']]<div>[['..card.Page..']]</div></div>\n'
    				end
    				output = output .. "</div>"
    			end
			end
    	end
    end
    
    return output
end

return p