Module:Media

-- This modules serves as the code for Template:Media. local p = {} local h = {}

local i18n = require("Module:I18n") local image = require("Module:Image") local tab2 = require("Module:Tab2") local utilsGame = require("Module:UtilsGame") local utilsString = require("Module:UtilsString") local utilsTable = require("Module:UtilsTable")

-- In the order in which they are to be presented, from left to right local mediaTypes = {"Artwork", "Render", "Model", "Sprite", "Screenshot", "Map"}

-- Template:InfoField (deprecated - remove once all infoboxes are module-based) function p._InfoboxCall(frame) local args = frame.args local result = args["image"] if string.sub(args["image"], 1, 5) == "File:" then result = image.PrintRescaledDimensions(args["image"], 320, 320) end return result end

-- Template:Media function p.Main(frame) local args = frame:getParent.args return p.main(args) end

function p.main(media) local tabs = h.createTypeTabs(media)

local defaultTypeTab = h.getDefaultTypeTab(media) local result = tab2.Main(tabs, defaultTypeTab, "top", 4, "100%", "", "", "center") return ' ' .. tostring(result) .. ' ' end

function h.createTypeTabs(media) local games = utilsGame.OrderGames(utilsGame.GetGames("all"), "canon") local tabs = {} for mediaKey, mediaValue in pairs(mediaTypes) do		local hasMedia = false for gameKey, gameValue in pairs(games) do if media[mediaValue .. " " .. gameValue] ~= nil then hasMedia = true end end if hasMedia then local tab = { tabName = mediaValue, tabCaption = i18n.getString("captions." .. mediaValue), tabContent = h.createGameTabs(media, mediaValue), }			table.insert(tabs, mediaKey, tab) end end return tabs end

-- Returns the key of the type tab with the most recent game. function h.getDefaultTypeTab(media) local result local games = utilsGame.OrderGames(utilsGame.GetGames("all"), "release") local biggestKeys = {} for mediaKey, mediaValue in pairs(mediaTypes) do		for gameKey, gameValue in pairs(games) do if media[mediaValue .. " " .. gameValue] ~= nil then biggestKeys[mediaKey] = gameKey end end end local biggestValue = 1 local biggestKey = 1 for key, value in pairs(biggestKeys) do		if value > biggestValue then biggestValue = value biggestKey = key end end return biggestKey end

-- Creates game tabs for the media type providen. function h.createGameTabs(media, mediaType) local tabs = {} local outputImage -- Iterates through the games to create a tab local games = utilsGame.OrderGames(utilsGame.GetGames("all"), "canon") local test for key, value in pairs(games) do if not utilsString.isEmpty(media[mediaType .. " " .. value]) then if string.sub(media[mediaType .. " " .. value], 1, 5) == "File:" then outputImage = image.PrintRescaledDimensions(media[mediaType .. " " .. value], 320, 320) -- Adding category for pages with missing images in their infobox if (string.sub(outputImage, 3, 26) == "File:No Image Upload.png") and (mw.title.getCurrentTitle.isContentPage == true) then local mediaType = i18n.plural("files.types." .. string.lower(mediaType)) local game = utilsGame.AbbToGame(value) outputImage = outputImage .. " "				end else outputImage = media[mediaType .. " " .. value] end table.insert(tabs, key, {tabName = value, tabCaption = utilsGame.AbbToGame(value, true), tabContent = outputImage}) end end --Iterates through the games to find the most recent one (default). local mostRecentGame games = utilsGame.OrderGames(utilsGame.GetGames("all"), "release") for key, value in pairs(games) do if not utilsString.isEmpty(media[mediaType .. " " .. value]) then if (string.match(media[mediaType .. " " .. value], "No Image Upload.png") == nil) then mostRecentGame = value end end end -- Getting this game's index in the "all", "canon" list, defaulting to 1 in	-- case it's not found (shouldn't happen except if there's an inconsistency	-- between the utilsGame.GetGames lists) local defaultGameKey = utilsTable.keyOf(utilsGame.OrderGames(utilsGame.GetGames("all"), "canon"), mostRecentGame) if defaultGameKey == nil then defaultGameKey = 1 end local result = tab2.Main(tabs, defaultGameKey, "bottom", 5, "100%", "", "320px", "center") return tostring(result) end

i18n.loadStrings({	en = {		captions = {			Artwork = "Official artwork provided by Nintendo.",			Map = "In-game map of an area.",			Model = "In-game models.",			Render = "Official models provided by Nintendo.",			Screenshot = "In-game screenshots.",			Sprite = "In-game sprites or icons."		}	} })

return p