FGO reader JP server
Reading pageFile:S356 Stage1 Dialogue2 Male.ogg

Module:Servant Icon

Cached copy of the original page · revision 1657507 · refresh

Documentation icon Module documentation
Emoji Guako Angry
This Lua module is used primarily on pages in the Main namespace. It can be found on 8,955 pages, or on about 67.31% of those pages.
Edits to this Lua module will be widely noticed. To avoid large-scale disruption and unnecessary server load, any changes to this Lua module should be tested in its sandbox or testcases subpages. The tested changes can then be added to this page in one single edit by an administrator or Content Moderator.
Please consider discussing any changes in the wiki's Discord.

Module for {{Servant Icon}}. Turns a Servant's article name into that Servant's icon.

The file name is built from the Servant's collection number and the requested Ascension stage, with the number padded to three digits: id 472 at stage 1 gives File:S472A1Icon.webp. stage=cos1 yields C1 in place of A1, rawimage=y appends Raw, and an entry carrying iconFile replaces the generated name outright.

Nothing here produces a red link on bad input. A stage the Servant does not have falls back to stage 1, and an article name missing from the data falls back to the Unknown Servant entry. That is what makes these suffixes safe to pass to any Servant:

Uses Module:Arguments, Module:Yesno.

To update:

Parameters are documented at Template:Servant Icon. In articles the module is normally reached through the per-Servant templates, e.g. {{Nero Claudius}}, which call the fromTemplate entry point. It takes the Servant from the template's own name and the size from its first argument, and passes every other argument on.

local p = {}

local DEFAULT_SPOILER_IMAGES = {
	[0] = 'S000BLACK1Icon',
	[1] = 'S000A1Icon',
	[2] = 'S000A1Icon',
	[3] = 'S000A1SIcon',
	[4] = 'S000A1GIcon',
	[5] = 'S000A1GIcon'
}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		wrapper = { 'Template:Servant Icon' }
	})
	return p._main(args)
end

-- For per-Servant templates: {{#invoke:Servant Icon|fromTemplate}}. The template's own name is the Servant, its first argument the size.
function p.fromTemplate(frame)
	local args = require('Module:Arguments').getArgs(frame, { parentFirst = true })
	args.name = frame:getParent():getTitle():gsub('^Template:', '')
	args.size = args.size or args[1]
	return p._main(args)
end

function p._main(args)
	local data = mw.loadData('Module:Servant Icon/data')
	local config = mw.loadData('Module:Servant Icon/config')
	local pagename = args.name or args[1] or mw.title.getCurrentTitle().rootText
	local servantParams = data[pagename] or data['Unknown Servant']
	local id = servantParams['id']
	local size = args.size or args[2] or 75
	local class = servantParams.class or ''
	
	-- Get Ascension Stage Value
	local stage = (args.stage or '1'):lower() .. (servantParams.stageSuffix or '')
	
	local yesno = require('Module:Yesno')
	local isRawText = args.rawtext or false
	local showCode = args.showcode or false
	if yesno(isRawText) then
		if yesno(showCode) then
			local node_result = mw.html.create()
			local outputtext = ''
			if (stage == '1') then
				outputtext = '{{' .. pagename .. '}}'
			else
				outputtext = '{{' .. pagename .. '|stage=' .. stage .. '}}'
			end
			node_result
				:wikitext(mw.getCurrentFrame():expandTemplate{ title = 'Code', args = { outputtext } })
				:done()
			return node_result
		end
		return pagename
	end
	
	-- Create a set from the stages array for faster lookup
	local stagesSet = {}
	for _, s in ipairs(servantParams.stages) do
		stagesSet[s] = true
	end

	local tmpStage = stage
	local rarity = servantParams.rarity or 5
	-- Check and trim suffixes for Grail Ascension on low-rarity Servants
	if rarity > 0 and rarity <= 3 then
		if (rarity < 3 and stage:find("%ds$")) or stage:find("%dg$") then
			tmpStage = stage:sub(1, -2)
		end
	end
	-- Check and trim suffixes for Grand Servants
	if config.grand_classes[class] and stage:find("%dp$") then
		tmpStage = stage:sub(1, -2)
	end

	-- If the processed stage is not in the set, default to '1'
	if not stagesSet[tmpStage] then
		stage = '1'
	end
	stage = stage:upper()
	
	-- Check Ascension Prefix
	if servantParams['iconPrefix'] then
		stage = servantParams['iconPrefix']:upper() .. stage
	else
		if stage:match("^COS%d") then
			stage = stage:gsub("COS", "C")
		-- Check if stage starts with a digit, optionally followed by only letters.
		-- This correctly handles cases like: "1", "4", "1G", "2S"
		-- while ignoring cases like: "C1", "SP1", "ORT1"
		elseif stage:match("^%d[%a]*$") then
			stage = 'A' .. stage
		end
	end
	
	--Check Framless Image
	local isRawImage = args.rawimage or false
	local fileNameProstfix = ''
	if yesno(isRawImage) then
		fileNameProstfix = 'Raw'
	end
	
	local idStr = tostring(id)
	--Padding ID string with zeros to a 3 character length
	idStr = ('0'):rep(3-#idStr) .. idStr

	local node_result = mw.html.create()
	local link = args.link or pagename
	local function fileLink(fileName)
		return '[[File:' .. fileName .. '.webp|' .. size .. 'px|link=' .. link .. ']]'
	end
	local filePath = fileLink((servantParams['iconFile'] or ('S' .. idStr .. stage .. 'Icon')) .. fileNameProstfix)
	
	local inumText = args.inum or nil
	local function withInum(file, numStyle)
		if inumText ~= nil then
			return mw.getCurrentFrame():expandTemplate{ title = 'Inum', args = { file, inumText, numStyle } }
		end
		return file
	end

	local icon = node_result:tag('span'):addClass('servant_icon')
	if yesno(args.isSpoiler) then
		-- Raw icons have no rarity frame, so there is one Raw placeholder
		local spoilerImage = args.spoilercover or (fileNameProstfix ~= '' and 'S000A1IconRaw' or DEFAULT_SPOILER_IMAGES[rarity] or 'S000A1Icon')
		-- Spans with the [[Template:ImageSwitch]] CSS classes: that template's divs would break an inline icon out of its paragraph.
		-- Inum goes inside each image so the whole icon switches; around them its invisible top layer would catch the hover.
		local numStyle = 'line-height:1.75' -- undoes the container's line-height:0, which pushes the number down
		icon:tag('span')
			:addClass('switch-img-container')
			:tag('span'):addClass('switch-img-base'):wikitext(withInum(fileLink(spoilerImage), numStyle)):done()
			:tag('span'):addClass('switch-img-overlay'):wikitext(withInum(filePath, numStyle))
	else
		icon:wikitext(withInum(filePath))
	end

	return node_result
end

return p;