Module:Main Page

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

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("")) p.AddSpinOffFamilyPadding(familyRow, biggestTableLength - #family["games"]) for key2, game in ipairs(family["games"]) do			familyRow:node(mw.html.create("div")				:addClass("game")				:wikitext(" " .. p.FormatReleaseDate(game["mainrelease"]) .. " " .. game["subtitle"] .. "")) 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(" " .. p.FormatReleaseDate(game["mainrelease"]) .. " " .. game["subtitle"] .. "")) 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