Module:Main Page: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
No edit summary
No edit summary
Line 12: Line 12:
-- Get all stored games
-- Get all stored games
local tables = "Games"
local tables = "Games"
local fields = "name, subtitle, initialism, mainrelease, type, family, canonicity"
local fields = "name, subtitle, initialism, mainrelease, type, family"
local args = {
local args = {
orderBy = "mainrelease DESC"
orderBy = "mainrelease DESC"
Line 20: Line 20:
-- Generate tabs
-- Generate tabs
local maingames = mw.html.create("div")
local maingames = mw.html.create("div")
:addClass("game-container main-games")
:addClass("game-container")
local remakes = mw.html.create("div")
local remakes = mw.html.create("div")
:addClass("game-container remakes")
:addClass("game-container")
local spinoffs = mw.html.create("div")
local spinoffs = mw.html.create("div")
:addClass("game-container spin-offs")
:addClass("game-container")
-- Create a table of game families
-- Create a table of game families
Line 54: Line 54:
-- Categorizes games in the proper tabs
-- Categorizes games in the proper tabs
-- Run first to add all TBA games so that they appear first despite being
-- last in the query since it's sorted and they're empty
for key, game in ipairs(games) do
for key, game in ipairs(games) do
local releaseYear = p.FormatReleaseDateYear(game["mainrelease"])
if p.FormatReleaseDate(game["mainrelease"]) == "TBA" then
if game["type"] == "main" then
if game["type"] == "main" then
maingames:node(mw.html.create("div")
p.AddGameToFlexList(maingames, game)
:addClass("game")
elseif game["type"] == "remake" then
:wikitext("<span class='year'>" .. releaseYear .. "</span>[[" .. game["name"] .. "|" .. game["subtitle"] .. "]][[File:" .. game["initialism"] .. " Tile.png|link=]]"))
p.AddGameToFlexList(remakes, game)
elseif game["type"] == "remake" then
elseif game["type"] == "spin-off" and utilsCode.IsEmpty(game["family"]) then
remakes:node(mw.html.create("div")
p.AddGameToFlexList(spinoffs, game)
:addClass("game")
end
:wikitext("<span class='year'>" .. releaseYear .. "</span>[[" .. game["name"] .. "|" .. game["subtitle"] .. "]][[File:" .. game["initialism"] .. " Tile.png|link=]]"))
end
elseif game["type"] == "spin-off" and utilsCode.IsEmpty(game["family"]) == true then
end
spinoffs:node(mw.html.create("div")
:addClass("game")
-- Run again for other games
:wikitext("<span class='year'>" .. releaseYear .. "</span>[[" .. game["name"] .. "|" .. game["subtitle"] .. "]][[File:" .. game["initialism"] .. " Tile.png|link=]]"))
for key, game in ipairs(games) do
if not (p.FormatReleaseDate(game["mainrelease"]) == "TBA") then
if game["type"] == "main" then
p.AddGameToFlexList(maingames, game)
elseif game["type"] == "remake" then
p.AddGameToFlexList(remakes, game)
elseif game["type"] == "spin-off" and utilsCode.IsEmpty(game["family"]) then
p.AddGameToFlexList(spinoffs, game)
end
end
end
end
end
Line 99: Line 109:
familyRow:node(mw.html.create("div")
familyRow:node(mw.html.create("div")
:addClass("game")
:addClass("game")
:wikitext("<span class='year'>" .. p.FormatReleaseDateYear(game["mainrelease"]) .. "</span>[[" .. game["name"] .. "|" .. game["subtitle"] .. "]][[File:" .. game["initialism"] .. " Tile.png|link=]]"))
:wikitext("<span class='year'>" .. p.FormatReleaseDate(game["mainrelease"]) .. "</span>[[" .. game["name"] .. "|" .. game["subtitle"] .. "]][[File:" .. game["initialism"] .. " Tile.png|link=]]"))
end
end
p.AddSpinOffFamilyPadding(familyRow, biggestTableLength - #family["games"])
p.AddSpinOffFamilyPadding(familyRow, biggestTableLength - #family["games"])
Line 118: Line 128:
-- Only keeps the 4 first digit of a release date, should always return the year
-- Only keeps the 4 first digit of a release date, should always return the year
-- If the year provided is empty, it results TBA
-- If the year provided is empty, it results TBA
function p.FormatReleaseDateYear(release)
function p.FormatReleaseDate(release)
local releaseYear = "TBA"
local releaseYear = "TBA"
if not utilsCode.IsEmpty(release) then
if not utilsCode.IsEmpty(release) then
Line 124: Line 134:
end
end
return releaseYear
return releaseYear
end
function p.AddGameToFlexList(element, game)
element:node(mw.html.create("div")
:addClass("game")
:wikitext("<span class='year'>" .. p.FormatReleaseDate(game["mainrelease"]) .. "</span>[[" .. game["name"] .. "|" .. game["subtitle"] .. "]][[File:" .. game["initialism"] .. " Tile.png|link=]]"))
end
end



Revision as of 07:56, 12 November 2019

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

local p = {}
local cargo = mw.ext.cargo
local tab2 = require("Module:Tab2")
local utilsCode = require("Module:UtilsCode")
local utilsTable = require("Module:UtilsTable")

function p._DisplayGameList(frame)
	return p.DisplayGameList()
end

function p.DisplayGameList()
	-- Get all stored games
	local tables = "Games"
	local fields = "name, subtitle, initialism, mainrelease, type, family"
	local args = {
		orderBy = "mainrelease DESC"
	}
	local games = cargo.query(tables, fields, args)
	
	-- Generate tabs
	local maingames = mw.html.create("div")
		:addClass("game-container")
	local remakes = mw.html.create("div")
		:addClass("game-container")
	local spinoffs = mw.html.create("div")
		:addClass("game-container")
	
	-- Create a table of game families
	local gameFamilies = {}
	for key, game in ipairs(games) do
		-- Check if the game belongs to a family
		if not utilsCode.IsEmpty(game["family"]) then
			local familyExists = false
			--Check if it exists
			for key2, family in ipairs(gameFamilies) do
				if family["name"] == game["family"] then
					familyExists = true
				end
			end
			
			-- If the family doesn't exist, create it first
			if familyExists == false then
				table.insert(gameFamilies, { name = game["family"], games= {} })
			end
			
			-- Add the game to the family
			for key2, family in ipairs(gameFamilies) do
				if family["name"] == game["family"] then
					table.insert(family["games"], game)
				end
			end
		end
	end
	
	-- Categorizes games in the proper tabs
	-- Run first to add all TBA games so that they appear first despite being
	-- last in the query since it's sorted and they're empty 
	for key, game in ipairs(games) do
		if p.FormatReleaseDate(game["mainrelease"]) == "TBA" then
			if game["type"] == "main" then
				p.AddGameToFlexList(maingames, game)
			elseif game["type"] == "remake" then
				p.AddGameToFlexList(remakes, game)
			elseif game["type"] == "spin-off" and utilsCode.IsEmpty(game["family"]) then
				p.AddGameToFlexList(spinoffs, game)
			end
		end
	end
	
	-- Run again for other games
	for key, game in ipairs(games) do
		if not (p.FormatReleaseDate(game["mainrelease"]) == "TBA") then
			if game["type"] == "main" then
				p.AddGameToFlexList(maingames, game)
			elseif game["type"] == "remake" then
				p.AddGameToFlexList(remakes, game)
			elseif game["type"] == "spin-off" and utilsCode.IsEmpty(game["family"]) then
				p.AddGameToFlexList(spinoffs, game)
			end
		end
	end
	
	-- Get biggest family size to add padding to smaller ones
	local firstLoop = true
	local biggestTableLength = 0
	for key, family in ipairs(gameFamilies) do
		if firstLoop then
			biggestTableLength = #family["games"]
			firstLoop = false
		end
		if #family["games"] > biggestTableLength then
			biggestTableLength = #family["games"]
		end
	end
	
	-- Generating spin-off families of games
	local familyContainer = mw.html.create("div")
			:addClass("family-container")
	for key, family in ipairs(gameFamilies) do
		local familyRow = mw.html.create("div")
			:addClass("spin-off-family")
		
		familyRow:node(mw.html.create("div")
			:addClass("spin-off-logo")
			:wikitext("[[File:" .. family["name"] .. " Family Tile.png|link=" .. family["name"] .. "]]"))
		
		p.AddSpinOffFamilyPadding(familyRow, biggestTableLength - #family["games"])
		for key2, game in ipairs(family["games"]) do
			familyRow:node(mw.html.create("div")
				:addClass("game")
				:wikitext("<span class='year'>" .. p.FormatReleaseDate(game["mainrelease"]) .. "</span>[[" .. game["name"] .. "|" .. game["subtitle"] .. "]][[File:" .. game["initialism"] .. " Tile.png|link=]]"))
		end
		p.AddSpinOffFamilyPadding(familyRow, biggestTableLength - #family["games"])
		familyContainer:node(familyRow)
	end
	spinoffs:node(familyContainer)
	
	-- Fill tabs
	local tabs = {}
	table.insert(tabs, 1, {tabName = "Main Series", tabCaption = "", tabContent = tostring(maingames)})
	table.insert(tabs, 2, {tabName = "Remakes", tabCaption = "", tabContent = tostring(remakes)})
	table.insert(tabs, 3, {tabName = "Spin-Offs", tabCaption = "", tabContent = tostring(spinoffs)})
	
	-- Displays the result
	return tab2.Main(tabs, 1, "top", 3, "", "100%", "", "center")
end

-- Only keeps the 4 first digit of a release date, should always return the year
-- If the year provided is empty, it results TBA
function p.FormatReleaseDate(release)
	local releaseYear = "TBA"
	if not utilsCode.IsEmpty(release) then
		releaseYear = string.sub(release, 1, 4)
	end
	return releaseYear
end

function p.AddGameToFlexList(element, game)
	element:node(mw.html.create("div")
		:addClass("game")
		:wikitext("<span class='year'>" .. p.FormatReleaseDate(game["mainrelease"]) .. "</span>[[" .. game["name"] .. "|" .. game["subtitle"] .. "]][[File:" .. game["initialism"] .. " Tile.png|link=]]"))
end

function p.AddSpinOffFamilyPadding(element, nb)
	local i = 0
	while i < nb do
		element:node(mw.html.create("div")
			:addClass("spin-off-padding"))
		i = i + 1
	end
end

return p