Zelda Wiki

Want to contribute to this wiki?
Sign up for an account, and get started!

Come join the Zelda Wiki community Discord server!

READ MORE

Zelda Wiki
Advertisement

Documentation for this module may be created at Module:Main Page/Documentation

local p = {}

local File = require("Module:File")
local Franchise = require("Module:Franchise")
local utilsLayout = require("Module:UtilsLayout")
local utilsMarkup = require("Module:UtilsMarkup")
local utilsString = require("Module:UtilsString")
local utilsTable = require("Module:UtilsTable")

local SPINOFF_FAMILIES = {
	{
		name = "Hyrule Warriors",
		page = "Hyrule Warriors",
	},
	{
		name = "Tingle",
		page = "Tingle (Series)",
	},
	{
		name = "BS-X",
		page = "BS-X",
	},
	{
		name = "CD-i",
		page = "Philips CD-i"
	},
}

function p.Games(frame)
	return p.printGames()
end

function p.printGames()
	local gamesByType = utilsTable.groupBy(Franchise.enumGames(), Franchise.type)
	
	local main = gamesByType["main"]
	local remakes = gamesByType["remake"]
	local spinoffs = gamesByType["spin-off"]
	
	-- By default remakes are placed after their originals, so we have to re-sort by release date
	table.sort(remakes, function(a, b)
		local aRelease = Franchise.releaseDate(a)
		local bRelease = Franchise.releaseDate(b)
		return aRelease < bRelease and aRelease ~= bRelease and aRelease ~= ""
	end)
	
	main = utilsTable.reverse(main)
	remakes = utilsTable.reverse(remakes)
	spinoffs = utilsTable.reverse(spinoffs)
	
	local mainTiles = p.printGameContainer(main)
	local remakeTiles = p.printGameContainer(remakes)
	local spinoffTiles = p.printSpinoffContainer(spinoffs)
	
	local tabData = {
		{
			label = "Main Series",
			content = mainTiles,
		},
		{
			label = "Remakes",
			content = remakeTiles,
		},
		{
			label = "Spin-Offs",
			content = spinoffTiles
		}
	}
	return utilsLayout.tabs(tabData, {
		align = "center",
		tabOptions = {
			stretch = true,
		},
	})
end

function p.printGameContainer(games)
	local html = mw.html.create("div"):addClass("game-container")
	for _, game in ipairs(games) do
		p.addGame(html, game)
	end
	return tostring(html)
end

function p.printSpinoffContainer(games)
	local html = mw.html.create("div"):addClass("game-container")
	local gamesByFamily = utilsTable.groupBy(games, Franchise.family)
	
	for _, game in ipairs(gamesByFamily[""]) do
		p.addGame(html, game)
	end
	gamesByFamily[""] = nil
	
	local biggestFamilyLength = 0
	for k, v in pairs(gamesByFamily) do
		biggestFamilyLength = math.max(biggestFamilyLength, #v)
	end
	
	html = html:tag("div"):addClass("family-container")
	for _, family in ipairs(SPINOFF_FAMILIES) do
		html:node(p.printFamily(family, gamesByFamily, biggestFamilyLength))
	end
	html = html:done()
	
	
	return tostring(html)
end

function p.printFamily(family, gamesByFamily, biggestFamilyLength)
	local games = gamesByFamily[family.name] or {}
	local paddingCount = biggestFamilyLength - #games
	local logo = utilsMarkup.file(family.name .. " Family Tile.png", {
		link = family.page
	})
	
	local html = mw.html.create("div"):addClass("spin-off-family")
	html:tag("div")
		:addClass("spin-off-logo")
		:wikitext(logo)
		:done()
	for i = 1, paddingCount do
		html:tag("div"):addClass("spin-off-padding"):done()
	end
	for _, game in ipairs(games) do
		p.addGame(html, game)
	end
	for i = 1, paddingCount do
		html:tag("div"):addClass("spin-off-padding"):done()
	end
	
	return tostring(html)
end

function p.addGame(html, game)
	local release = p.printRelease(game)
	local link = utilsMarkup.link(Franchise.article(game), Franchise.shortName(game))
	local tile, tileExists = File.image(game .. " Tile.png", {link = ""})
	
	if tileExists then
		html:tag("div")
			:addClass("game")
			:wikitext(release..link..tile)
			:done()
	end
end

function p.printRelease(game)
	local release = Franchise.releaseDate(game)
	if utilsString.isEmpty(release) then
		release = "TBA"
	else
		release = release:sub(1, 4)
	end
	return utilsMarkup.class("year", release)
end

return p
Advertisement