Module:Util

-- 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 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 ():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.query('Skills,Skills__Required', 'Skills.WikiName=NextSkill,Skills__Required._value=Prev', {			join = 'Skills._ID=Skills__Required._rowID',			groupBy = 'NextSkill,Prev',			limit = 5000,		}) skills = List.map(query, function (v) return v.NextSkill end) elseif #skills == 0 then return {}, {} else query = cargo.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), ', ')),			limit = 5000,		}) 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}}, --		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