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'

p.escq = function (str) return mw.ustring.gsub(mw.getCurrentFrame:callParserFunction('#titleparts', str == nil and '' or tostring(str)), "'", "\\'") end

p.mf = function (str) return mw.ustring.gsub(mw.getCurrentFrame:callParserFunction('#titleparts', str == nil and  or tostring(str)), '[^%a%d._-%s]', ) end

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

p.getHeroIcon = function (hero, size) return (''):format(p.mf(hero), size, hero) end

p.getHeroLowestRarity = function (hero, key) local rarities = cargo.query('Heroes', 'SummonRarities__full=s,RewardRarities__full=r', {   where = ("_pageName='%s'"):format(p.escq(hero)),    groupBy = '_pageName',    limit = 10,  }) if #rarities > 0 then local s = List.concat_self(key == 'reward' and {} or p.strsplit(',', rarities[1].s),     key == 'summon' and {} or p.strsplit(',', rarities[1].r)) for rarity = 1, 5 do     if List.find(s, tostring(rarity)) then return rarity end end end return 6 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.getOriginSortOrder = function local results = cargo.query('GameSortValue', '_pageName,value1', {   groupBy = '_pageName',    limit = 50,  }) return Hash.from_pairs(results, function (x)   return x._pageName, tonumber(x.value1)  end) end

p.getSkillChains = function (skills) local query = nil if skills == nil then query = cargo.query('Skills,Skills__Required', 'Skills.WikiName=Next,Skills__Required._value=Prev', {     join = 'Skills._ID=Skills__Required._rowID',      groupBy = 'Next,Prev',      limit = 5000,    }) skills = List.map(query, function (v) return v.Next end) elseif #skills == 0 then return {}, {} else query = cargo.query('Skills,Skills__Required', 'Skills.WikiName=Next,Skills__Required._value=Prev', {     join = 'Skills__Required._rowID=Skills._ID',      groupBy = 'Next,Prev',      where = ("WikiName IN (%s)"):format(table.concat(List.map(skills, function (v) return ("'%s'"):format(p.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.Next] if p then p[#p + 1] = v.Prev end local n = after[v.Prev] if n then n[#n + 1] = v.Next end end end return prereq, after end

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

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

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

-- Copied from http://lua-users.org/wiki/StringTrim p.trim = function (s) return mw.ustring.gsub(s, '^%s*(.-)%s*$', '%1') end

-- Copied from http://lua-users.org/wiki/SplitJoin -- strsplit(seperator, string) p.strsplit = function (d, p) if p == nil then return '' end local t = {} local ll = 0 if #p == 1 then return {p} end while true do   l = mw.ustring.find(p, d, ll, true) -- find the next d in the string if l ~= nil then -- if "not not" found then.. table.insert(t, mw.ustring.sub(p, ll, l - 1)) -- Save it in our array. ll = l + 1 -- save just after where we found it for searching next time. else table.insert(t, mw.ustring.sub(p, ll)) -- Save what's left in our array. break -- Break at end, as it should be, according to the lua manual. end end return t 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

return p