Module:OCG-TCG card

From Yugipedia
Revision as of 02:37, 9 September 2023 by Deltaneos (talk | contribs) (group localities by language)
Jump to: navigation, search
local Card = require('Module:Card')

-- ------------------------------------
-- Overwrite `Module:Card` data
-- ------------------------------------
Card.config.defaultImage    = 'Back-EN.png'
Card.config.enableMainLinks = false
Card.config.langs           = { 'en', 'fr', 'de', 'it', 'pt', 'es', 'ja', 'tc', 'sc', 'ko' }
Card.config.rows            = {
	'cardType',
	'property',
	'attribute',
	'type',
	'level',
	'rank',
	'pendulumScale',
	'linkArrows',
	'atkDef',
	'atkLink',
	'effectType',
	'lore',
	'limitationText',
	'password'
}
Card.config.types = {
	type     = {
		'Dragon', 'Spellcaster', 'Zombie', 'Warrior', 'Beast-Warrior', 'Beast',
		'Winged Beast', 'Fiend', 'Fairy', 'Insect', 'Dinosaur', 'Reptile',
		'Fish', 'Sea Serpent', 'Machine', 'Thunder', 'Aqua', 'Pyro', 'Rock', 
		'Plant', 'Divine-Beast', 'Psychic',	'Creator God', 'Wyrm', 'Cyberse',
		'Illusion'
	},
	summon   = { 'Fusion', 'Ritual', 'Synchro', 'Xyz', 'Link' },
	ability  = { 'Flip', 'Toon', 'Spirit', 'Union', 'Gemini' },
	tuner    = { 'Tuner' },
	pendulum = { 'Pendulum' },
	normal   = { 'Normal' },
	effect   = { 'Effect' },
	unknown  = { '?', '???' },
}

table.insert(Card.styles, 'Module:OCG-TCG card/styles.css')

-- Set defaults for some extra params, not present in the base module.
Card.sets = {}
Card.isOcg = false
Card.isTcg = false
Card.tcgSetLangs = {
	{ name = 'English',     localities = { 'Worldwide English', 'North American English', 'European English', 'Australian English' } },
	{ name = 'French',      localities = { 'French', 'French-Canadian' } },
	{ name = 'German',      localities = { 'German'     } },
	{ name = 'Italian',     localities = { 'Italian'    } },
	{ name = 'Portuguese',  localities = { 'Portuguese' } },
	{ name = 'Spanish',     localities = { 'Spanish'    } },
}
Card.ocgSetLangs = {
	{ name = 'Japanese',            localities = { 'Japanese', 'Japanese-Asian' } },
	{ name = 'English',             localities = { 'Asian-English'       } },
	{ name = 'Simplified Chinese',  localities = { 'Simplified Chinese'  } },
	{ name = 'Traditional Chinese', localities = { 'Traditional Chinese' } },
	{ name = 'Korean',              localities = { 'Korean'              } }
}

local TCG = 'Yu-Gi-Oh! Trading Card Game'
local OCG = 'Yu-Gi-Oh! Official Card Game'



-- ------------------------------------
-- Override `Module:Card` methods
--     These functions exist in `Module:Card`, but are overridden here to
--     make them more specific to OCG/TCG cards
-- ------------------------------------

-- Set data on initialization, not covered by 
function Card:setCustomArgs(args)
	-- Set properties related to the sets the card has been released in.
	self:setReleaseData()
end

-- Render the section displaying what sets the card has been released in.
function Card:renderSetsSection()
	local output = ''

	if (self.isTcg) then
		output = output .. "\n==''TCG'' sets==\n"

		local tcgSetsSwitcher = mw.html.create('div')
		tcgSetsSwitcher:addClass('switcher-container switcher-container-sets')
		
		for _, language in pairs(self.tcgSetLangs) do
			local sets = self.sets[TCG][language.name]

			if sets then
				tcgSetsSwitcher:wikitext(self.renderSetTab(language, sets))
			end
		end

		output = output .. tostring(tcgSetsSwitcher)
	end

	if (self.isOcg) then
		output = output .. "\n==''OCG'' sets==\n"

		local ocgSetsSwitcher = mw.html.create('div')
		ocgSetsSwitcher:addClass('switcher-container switcher-container-sets')

		for _, language in pairs(self.ocgSetLangs) do
			local sets = self.sets[OCG][language.name]

			if sets then
				ocgSetsSwitcher:wikitext(self.renderSetTab(language, sets))
			end
		end

		output = output .. tostring(ocgSetsSwitcher)
	end

	return output
end

function Card.renderSetTab(language, sets)
	local output = '<div>'
	output = output .. '<div class="switcher-label>' .. language.name .. '</div>'

	for _, locality in pairs(language.localities) do
		if (sets[locality]) then
			output = output .. '<p><b>' .. locality .. '</b></p>'
			output = output .. '<p>Sets found: ' .. (#sets[locality]) .. '</p>'
		end
	end

	output = output .. '</div>'

	return output
end



-- ------------------------------------
-- Custom functions
--    Functions specific to this module
-- ------------------------------------

-- Find all sets that the card appears in.
-- @return table Array of elements containing information on each print
-- Example of an array element
--[[
	["cardNumber"]   = "PP02-FR007",
	["rarities"]     = { "Super Rare", "Secret Rare" },
	["printedName"]  = "Chaleur, Héros Élémentaire",
	["release"]      = "Yu-Gi-Oh! Trading Card Game",
	["language"]     = "French",
	["locality"]     = "French",
	["setPage"]      = "Premium Pack 2 (TCG)",
	["setName"]      = "Premium Pack 2",
	["localSetName"] = "Paquet Premium 2",
	["releaseDate"]  = "2008-07-29",
]]--
function Card:findSets()
	return mw.smw.ask{
		'[[Set contains::' .. self.pageName .. ']][[-Has subobject.Release::' .. OCG .. '||' .. TCG .. ']]',
		'?Card number                          = cardNumber',
		'?Rarity#                              = rarities',
		'?Printed name                         = printedName',
		'?-Has subobject.Release#              = release',
		'?-Has subobject.Language#             = language',
		'?-Has subobject.Locality#             = locality',
		'?-Has subobject.Set page#             = setPage',
		'?-Has subobject.Set page.English name = setName',
		'?-Has subobject.Local name            = localSetName',
		'?-Has subobject.Release date#         = releaseDate',
		mainlabel = '-',
		orderBy = 'Release',
		limit = 500
	} or {}
end

-- Reformat the result of `Card:findSets`
-- Group by release (OCG, TCG), then by locality (Japanese, French, French-Canadian, etc.)
-- @return table e.g.
--[[
	['Yu-Gi-Oh! Official Card Game'] = {
		['Japanese'] = {
			['Japanese']       = { ... },
			['Japanese-Asian'] = { ... },
		},
		['Korean']  = {
			['Korean'] = { ... }
		},
	}
	['Yu-Gi-Oh! Trading Card Game'] = {
		['English'] = {
			['Worldwide English']      = { ... },
			['North American English'] = { ... },
			['European English']       = { ... }
		},
		['German'] = {
			['German'] = { ... }
		}
	}
]]--
function Card.groupSetsByLocality(sets)
	local groupedSets = {}
	local release
	local language
	local locality

	for i, set in pairs(sets) do
		release = set.release or ''
		language = set.language or ''
		locality = set.locality or ''

		groupedSets[release] = groupedSets[release] or {}
		groupedSets[release][language] = groupedSets[release][language] or {}
		groupedSets[release][language][locality] = groupedSets[release][language][locality] or {}

		table.insert(groupedSets[release][language][locality], set)
	end

	return groupedSets
end

-- Set properties related to the sets the card has been relased in
function Card:setReleaseData()
	-- Populate the set data
	self.sets = self.groupSetsByLocality(self:findSets())

	-- Set OCG/TCG booleans based on what sets the card appears in
	self.isOcg = self.sets[OCG] and true or false
	self.isTcg = self.sets[TCG] and true or false
	self.isOcgOnly = self.isOcg and not self.isTcg
	self.isTcgOnly = self.isTcg and not self.isOcg

	if self.isOcg     then table.insert(self.categories, 'OCG cards') end
	if self.isTcg     then table.insert(self.categories, 'TCG cards') end
	if self.isOcgOnly then table.insert(self.categories, 'OCG-only cards') end
	if self.isTcgOnly then table.insert(self.categories, 'TCG-only cards') end

	-- Use the Japanese backing as the default image if the card is OCG-only.
	if (self.isOcgOnly) then
		Card.config.defaultImage = 'Back-JP.png'
	end
end

return Card