Module:Related NPCs

From Final Fantasy XIV Online Wiki
Jump to navigation Jump to search
Documentation for Module:Related NPCs [view] [edit] [history] [purge] (How does this work?)

This module implements {{Related NPCs}}.

-- A module for rendering a gallery of NPCs that appear in a quest, including cutscenes.
-- Invoke: {{#invoke:Related NPCs|main|<quest name>}}

local p = {}

local function fetchNPCs(query)
	local npcs = mw.smw.ask({
		query,
		"?Has canonical name=name",
		"?Has appearance#=image",
		"mainlabel=pagename",
		"limit=25"
	})
	if type(npcs) ~= "table" then
		return {}
	end
	return npcs
end

local function renderGallery(frame, npcs, questid)
	local galleryEntries = {}
	for _, row in ipairs(npcs) do
		galleryEntries[#galleryEntries + 1] = frame:expandTemplate{
			title = "Image gallery line/Quest",
			args = {
				pagename = row.pagename,
				name = row.name,
				image = row.image,
				questid = questid,
			}
		}
	end
	return frame:extensionTag("gallery", table.concat(galleryEntries, "\n"), {
		mode = "packed",
		heights = "200px"
	})
end

function p.main(frame)
	local args = frame.args
	local quest = args[1]
	if not quest or quest == "" then
		quest = mw.title.getCurrentTitle().rootText
	end

	local questid = ""
	local questData = mw.smw.ask({
		"[[" .. quest .. "]]",
		"?Has internal ID=questid",
		"limit=1",
	})
	if type(questData) == "table" and questData[1] then
		questid = questData[1].questid or ""
	end

	local relatedNPCs = fetchNPCs(table.concat({
		"[[Category:NPCs]][[-Has related NPC::" .. quest .. "]]",
		"OR [[Category:NPCs]][[-Has quest giver::" .. quest .. "]]",
	}, " "))

	local relatedPages = {}
	for _, row in ipairs(relatedNPCs) do
		relatedPages[row.pagename] = true
	end

	local cutsceneNPCs = {}
	for _, row in ipairs(fetchNPCs(
		"[[Category:NPCs]][[-Has cutscene NPC::" .. quest .. "]]"
	)) do
		if not relatedPages[row.pagename] then
			cutsceneNPCs[#cutsceneNPCs + 1] = row
		end
	end

	local output = { renderGallery(frame, relatedNPCs, questid) }
	if #cutsceneNPCs > 0 then
		output[#output + 1] = frame:extensionTag("h3", "Cutscene NPCs")
		output[#output + 1] = renderGallery(frame, cutsceneNPCs, questid)
	end
	return table.concat(output, "\n")
end

return p