Module:SkillChainChart

From Fire Emblem Heroes Wiki
Jump to: navigation, search

Builds a table for a certain skill type, showing which heroes can learn it by at most 4★. Used to automate Skill Chains 4 Stars List and Skill Chains 5 Stars List.


local p = {}
local cargo = mw.ext.cargo
local Util = require 'Module:Util'
local List = require 'Module:ListUtil'
local escq = Util.escq

function p._SkillChains(kind, stars, ptype)
  -- Initialize the table
  local tbl = mw.html.create('table')
      :addClass('wikitable default sortable')
      :css('text-align','center')

  -- Add table headers
  tbl:tag('th')
     :wikitext('Skill')
     :css('width', '150px')
  
  tbl:tag('th')
     :wikitext('Description')

  tbl:tag('th')
     :wikitext('SP')

  tbl:tag('th')
     :wikitext('Heroes')

  local CAT_COND = {
    Weapons = "s.Scategory='weapon'",
    Assists = "s.Scategory='assist'",
    Specials = "s.Scategory='special'",
    Passives = ("s.Scategory='passive%s'"):format(ptype or ''),
  }
  local skillQuery = cargo.query('Skills=s,Skills__Required=r,Skills=after',
    's._pageName=page,s.Name=Name,s.WikiName=wikiname,s.Description=Effect,s.SP=SP', {
      where = ("%s AND s.Exclusive='No' AND after._pageName IS NULL"):format(CAT_COND[kind]),
      join = 'r._rowID=after._ID,s.WikiName=r._value',
      groupBy = 's._pageName',
      limit = 500,
    })

  for _, skill in ipairs(skillQuery) do

    local heroQuery = cargo.query('Heroes=h,HeroSkills=a', 'h._pageName=hero', {
      join = 'h._pageName=a._pageName',
      where = stars == 4 and
        ([[NOT ((h.SummonRarities__full IS NULL OR h.SummonRarities__full="5") AND (h.RewardRarities__full IS NULL OR h.RewardRarities__full="5")) AND
           (h.Properties__full IS NULL OR h.Properties__full NOT LIKE '%%story%%') AND
           a.skill='%s' AND a.unlockRarity<=4
         ]]):format(skill.wikiname) or
        ([[NOT (h.SummonRarities__full IS NULL AND h.RewardRarities__full IS NULL) AND
           (h.Properties__full IS NULL OR h.Properties__full NOT LIKE '%%story%%') AND
           a.skill='%s' AND (a.unlockRarity=5 OR (
             (h.SummonRarities__full IS NULL OR h.SummonRarities__full NOT LIKE '%%4%%') AND
             (h.RewardRarities__full IS NULL OR h.RewardRarities__full NOT LIKE '%%4%%')))
         ]]):format(skill.wikiname),
      groupBy = 'h._pageName',
      limit = 300,
    })

    -- Only display row if heroes exist that can learn the skill
	if #heroQuery > 0 then
	  local tr = tbl:tag('tr')
	  tr:tag('td')
	    :wikitext('[[' .. skill.page .. '|' .. skill.Name .. ']]')
	  tr:tag('td')
	    :wikitext(skill.Effect)
	  tr:tag('td')
	    :wikitext(skill.SP)
	  tr:tag('td')
        :wikitext(table.concat(List.map_self(heroQuery, function (x) return '[[' .. x.hero .. ']]' end), ', '))
    end
    
  end

  return tostring(tbl)
end

function p.SkillChains(frame)
  local args = frame.args
  if args[1] == 'weapon' then
    return p._SkillChains('Weapons', tonumber(args[2]) or 5)
  elseif args[1] == 'assist' then
    return p._SkillChains('Assists', tonumber(args[2]) or 5)
  elseif args[1] == 'special' then
    return p._SkillChains('Specials', tonumber(args[2]) or 5)
  elseif args[1] == 'passive' then
    return p._SkillChains('Passives', tonumber(args[3]) or 5, args[2])
  end
end

return p