Module:Sandbox/PhantomCaleb

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__NEXT", "_pageName, baseGame, game", {		where = utilsCargo.allOf({ subject = pageName }),		sortBy = "_pageName"	})

local locationsByBaseGame = utilsTable.groupBy(cargoResults, "baseGame") local gamesByLocation = utilsTable.groupBy(cargoResults, "_pageName") gamesByLocation = utilsTable.mapValues(gamesByLocation, utilsTable._map("game")) local locationsByGame = {} local gameCount = 0 local lastGame for game, results in pairs(locationsByBaseGame) do		local locations = utilsTable.uniqueBy(results, "_pageName") -- remove duplicates local remakes = Franchise.remakes(game) local gameVersions = utilsTable.concat({game}, remakes) local locationLinks = {} for i, location in ipairs(locations) do			local locationPage = location._pageName local locationLink = Term.link(locationPage, game) local applicableGameVersions = utilsTable.intersection(gamesByLocation[locationPage], gameVersions) local applicableRemakes = utilsTable.intersection(gamesByLocation[locationPage], remakes) if #applicableGameVersions ~= #gameVersions and #applicableRemakes > 0 then local expGame = mw.getCurrentFrame:expandTemplate({					title = "Exp Game",					args = {table.concat(applicableRemakes, ", ")}				}) locationLink = locationLink.." "..expGame end table.insert(locationLinks, locationLink) end locationsByGame[game] = locationLinks gameCount = gameCount + 1 lastGame = game end

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

p.Templates = { ["Subject Locations"] = { description = "Lists the locations in which a given character, creature, enemy, item, or material appears.", purpose = " Lists the locations in which a given character, creature, enemy, item, or material appears. It works by listing all pages which have the given subject as an entry in .", usage = "This template is automatically added by Template:Infobox Enemy.", params = { [1] = {				name = "subject", type = "wiki-page-name", default = " ", desc = "A wiki page name. For testing only.", }		},		examples = { {"Green Bokoblin (Archer)"}, {"Skulltula"}, {"Non-Existent Enemy"}, }	} }

return p