Fire Emblem Heroes Wiki
Advertisement
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Template-info Documentation

This module blackboxes several FEH-related operations (eg. Fetching heroes, skills, skill info) and is intended for use by other Lua modules. This module should not be called from #invoke directly.

mf(str)

Removes special characters from str so that it is usable as a file name; equivalent to {{MF}}.

isNilOrEmpty(val)

Checks if the specified value is nil or empty

returnDefaultIfEmpty(val)

Returns '-' if the specified value is nil or empty

getHeroIcon(hero,size,resplendent)

  • Input: Hero name, image size, whether to obtain the Resplendent Attire (default false)
  • Output: Icon of the Hero that links to the Hero page

getWeaponSortOrder()

Obtains the sort order of weapon types, by color then by weapon. Uses WeaponTypes.

Return values

A table that maps full weapon types to increasing values, e.g. 'Red Sword'1, 'Red Bow'2.

getMoveSortOrder()

Obtains the sort order of move types. Uses MoveTypes.

Return values

A table that maps move types to increasing values, e.g. 'Infantry'1, 'Armored'2.

getSkillChains(skills)

Returns two tables representing dependencies between the given skills. The first table contains WikiName of skills as keys and lists of the skill's prerequisites as values; the second table is for descendants of skills.

If a table is passed as an argument, it must be a list that contains only the WikiNames of skills to be queried. Otherwise all existing skills are used.

getRarityText()

Returns an array containing rarity texts from 1 ★ to 5 ★.

getDifficulties()

Returns a sorted array of difficulties.

difficultySort(x, y)

Returns the relative order between two unit tab names, based on their difficulties. Can be used as the comparison function to table.sort.

-- This module blackboxes several FEH-related operations (eg. Fetching heroes, skills, skill info)
-- and is intended for use by other Lua modules. 
-- This module should not be called from #invoke directly.

local p = {}
local mw = mw
local cargo = mw.ext.cargo
local Cargo = require 'Module:CargoUtil'
local List = require 'Module:ListUtil'
local Hash = require 'Module:HashUtil'
local escq = require 'Module:EscQ'.main1
local mf = require('Module:MF').main1

p.mf = mf
p.mf2 = require 'Module:MF2'.main1

----------------------------------
----------------------------------
-- Hero-related functions
----------------------------------
----------------------------------

p.getHeroIcon = function (hero, size, resplendent)
	return ('[[File:%s%s_Face_FC.webp|%s|link=%s]]'):format(
		mf(hero), resplendent and '_Resplendent' or '', size, hero)
end

----------------------------------
----------------------------------
-- Sort-related functions
----------------------------------
----------------------------------

p.getWeaponSortOrder = function ()
	return Hash.invert(List.map_self(cargo.query('WeaponTypes', 'WikiName', {
		groupBy = '_pageName',
		orderBy = 'ColorSort,Sort',
		limit = 50,
	}), function (x) return x.WikiName end))
end

p.getMoveSortOrder = function ()
	return Hash.invert(List.map_self(cargo.query('MoveTypes', 'WikiName', {
		groupBy = '_pageName',
		orderBy = 'Sort',
		limit = 50,
	}), function (x) return x.WikiName end))
end

p.getSkillChains = function (skills)
	local query = nil
	if skills == nil then
		query = Cargo.full_query('Skills,Skills__Required', 'Skills.WikiName=NextSkill,Skills__Required._value=Prev', {
			join = 'Skills._ID=Skills__Required._rowID',
			groupBy = 'NextSkill,Prev',
		})
		skills = List.map(query, function (v) return v.NextSkill end)
	elseif #skills == 0 then
		return {}, {}
	else
		query = Cargo.full_query('Skills,Skills__Required', 'Skills.WikiName=NextSkill,Skills__Required._value=Prev', {
			join = 'Skills__Required._rowID=Skills._ID',
			groupBy = 'NextSkill,Prev',
			where = ("WikiName IN (%s)"):format(table.concat(List.map(skills, function (v) return ("'%s'"):format(escq(v)) end), ', ')),
		})
	end
	local prereq = Hash.generate(skills, function () return {} end)
	local after = Hash.generate(skills, function () return {} end)
	for _, v in ipairs(query) do
		if v.Prev ~= '' then
			local p = prereq[v.NextSkill]
			if p then
				p[#p + 1] = v.Prev
			end
			local n = after[v.Prev]
			if n then
				n[#n + 1] = v.NextSkill
			end
		end
	end
	return prereq, after
end

----------------------------------
----------------------------------
-- Misc. General functions
----------------------------------
----------------------------------

-- Checks if the specified value is nil or empty
p.isNilOrEmpty = function (val)
	if val == nil then
		return true
	end
	-- avoid creating a substring here
	local _, _, b, e = mw.ustring.find(val, '^[\t\r\n\f ]*().-()[\t\r\n\f ]*$')
	return b == e
end

-- Returns '-' if the specified value is nil or empty
p.returnDefaultIfEmpty = function (val)
	return p.isNilOrEmpty(val) and '-' or val
end

p.getRarityTexts = function ()
	local frame = mw.getCurrentFrame()
	local z = {
		frame:expandTemplate{title = 'Rarity', args = {1}},
		frame:expandTemplate{title = 'Rarity', args = {2}},
		frame:expandTemplate{title = 'Rarity', args = {3}},
		frame:expandTemplate{title = 'Rarity', args = {4}},
		frame:expandTemplate{title = 'Rarity', args = {5}},
		[4.5] = frame:expandTemplate{title = 'Rarity', args = {5}},
		['4 SR'] = frame:expandTemplate{title = 'Rarity', args = {'4 SR'}},
		['4 SHSR'] = frame:expandTemplate{title = 'Rarity', args = {'4 SHSR'}},
--		S = frame:expandTemplate{title = 'Rarity', args = {'S'}},
	}
	for i, v in ipairs(z) do
		z[tostring(i)] = v
	end
	return z
end

local DIFFICULTIES = {'Normal', 'Hard', 'Lunatic', 'Infernal', 'Abyssal'}

p.getDifficulties = function ()
	return Hash.clone(DIFFICULTIES)
end

p.difficultySort = function (x, y)
	local i1 = List.find_if(DIFFICULTIES, function (dif) return mw.ustring.find(x, dif, 1, true) end) or #DIFFICULTIES + 1
	local i2 = List.find_if(DIFFICULTIES, function (dif) return mw.ustring.find(y, dif, 1, true) end) or #DIFFICULTIES + 1
	if i1 < i2 then
		return true
	elseif i2 < i1 then
		return false
	end

	local lv1 = tonumber(mw.ustring.match(x, '^[^/]+/LV.(%d+)/%d+ battles$')) or math.huge
	local lv2 = tonumber(mw.ustring.match(y, '^[^/]+/LV.(%d+)/%d+ battles$')) or math.huge
	return lv1 < lv2 or (lv1 == lv2 and x < y)
end

return p
Advertisement