Module:Spawn Locations

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search

local p = {}

local Franchise = require("Module:Franchise")
local Term = require("Module:Term")
local utilsCargo = require("Module:UtilsCargo")
local utilsMarkup = require("Module:UtilsMarkup")
local utilsTable = require("Module:UtilsTable")

function p.Main(frame)
	local pageName = frame:getParent().args[1] or mw.title.getCurrentTitle().subpageText
	local cargoResults = utilsCargo.query("LocationFeatures, Terminologies", "LocationFeatures._pageName=location, LocationFeatures.baseGame=baseGame, LocationFeatures.game=game, Terminologies.redirectTarget", {
		join = "LocationFeatures.subject = Terminologies._pageName",
		where = utilsCargo.allOf(
			utilsCargo.anyOf({ 
				subject = pageName,
				redirectTarget = pageName, -- so that synoyms can be used in Gallery Lists
			}),
			-- Entries referring to sublocations are not listed to avoid redudancy in the infobox list. 
			-- e.g. for Rats in TWW, we wait to display Jail Maze as a habitat but not Jail.
			"sublocations__full IS NULL"
		),
		sortBy = "location"
	})

	local locationsByBaseGame = utilsTable.groupBy(cargoResults, "baseGame")
	local gameLists = {}
	local gameCount = 0
	local lastGame
	for game, results in pairs(locationsByBaseGame) do
		local locations = utilsTable.map(results, "location")
		locations = utilsTable.unique(locations) -- remove duplicates
		local termGame = Franchise.superseder(game) or game
		local locationLinks = utilsTable.map(locations, function(page)
			return Term.link(page, termGame)
		end)
		
		gameLists[game] = locationLinks
		gameCount = gameCount + 1
		lastGame = game
	end

	if gameCount == 0 then
		return ""
	elseif gameCount == 1 then
		return utilsMarkup.list(gameLists[lastGame])
	else
		gameLists = utilsTable.mapValues(gameLists, function(locations)
			return table.concat(locations, ", ")
		end)
		return frame:expandTemplate({
			title = "Infobox Game Blocks",
			args = gameLists,
		})
	end
end

return p