Module:AssistsTable

From Fire Emblem Heroes Wiki
Jump to: navigation, search
Template-info.svg Documentation

Used on every Hero page, to specify and show which assist skills they learn, and when.

local p = {}
local cargo = mw.ext.cargo
local getArgs = require 'Module:Arguments'.getArgs
local Util = require('Module:Util')
local List = require 'Module:ListUtil'
local escq = require 'Module:EscQ'.main1

local MAX_ASSISTS = 4

local assistsList = function (args)
  local RARITY_TXT = Util.getRarityTexts()

  local assists = List.select(List.generate(MAX_ASSISTS, function (i)
    return Util.isNilOrEmpty(args[i * 3 - 2]) and {} or {
      skill = args[i * 3 - 2],
      default = tonumber(args[i * 3 - 1]),
      unlock = tonumber(args[i * 3]),
    }
  end), function (x) return not Util.isNilOrEmpty(x.skill) end)
  if #assists == 0 then
    return '<div>This unit owns no Assist skills.</div>'
  end
  local assistsQueryResult = List.map(assists, function (w)
    local queryResult = cargo.query('Skills', '_pageName,Name,UseRange,Description,SP', {
      where = ("Scategory='assist' AND Name='%s'"):format(escq(w.skill)),
      groupBy = '_pageName',
      limit = 1,
    })
    return #queryResult > 0 and queryResult[1] or {_pageName = w.skill, AssistName = w.skill}
  end)

  -- Initialize the table
  local tbl = mw.html.create('table')
      :addClass('wikitable default unsortable skills-table')
      :css('text-align','center')

  -- Table Headers
  tbl:tag('th'):wikitext('Name')
  tbl:tag('th'):wikitext('Range')
  tbl:tag('th'):wikitext('Description')
  tbl:tag('th'):wikitext('SP')
  tbl:tag('th'):wikitext(mw.getCurrentFrame():expandTemplate{title = 'Hover', args = {'Default',
    'Skills are listed at the lowest rarity they are automatically learned at, regardless of whether or not the Hero can be obtained at that rarity.'}})
  tbl:tag('th'):wikitext(mw.getCurrentFrame():expandTemplate{title = 'Hover', args = {'Unlock',
    'Skills are listed at the lowest rarity they can be learned at, regardless of whether or not the Hero can be obtained at that rarity.'}})

  -- Go through all the assists in the query result
  for i, q in ipairs(assistsQueryResult) do
    local as = assists[i]
    local tr = tbl:tag('tr')

    tr:tag('td'):wikitext('[[' .. q._pageName .. '|' .. (q.Name ~= '' and q.Name or q._pageName) .. ']]') -- Name
    tr:tag('td'):wikitext(tonumber(q.UseRange) or '—') -- Range
    tr:tag('td'):wikitext(q.Description == '' and '—' or q.Description) -- Effect
    local cost = tonumber(q.SP) or '—'
    tr:tag('td'):wikitext(cost == 0 and '—' or cost) -- SP
    tr:tag('td'):wikitext(RARITY_TXT[as.default] or '—') -- Default
    tr:tag('td'):wikitext(RARITY_TXT[as.unlock] or '—') -- Unlock
  end
  return tostring(tbl)
end

return require 'Module:MakeMWModule'.makeMWModule {assistsList = assistsList}