Module:HeroList

From Fire Emblem Heroes Wiki
Jump to: navigation, search

Collects all heroes and builds the table shown at Hero List.


local Util = require('Module:Util')
local p = {}
local cargo = mw.ext.cargo

local getRarityWikitext = function (summonRarities, rewardRarities, properties)
  local rarities = {}
  if summonRarities ~= '' then
    rarities = Util.strsplit(',', summonRarities)
  else
  	rarities = Util.strsplit(',', rewardRarities)
  end

  -- Find min and max rarity
  local minRarity = rarities[1]
  local maxRarity = rarities[1]
  for _, rarity in ipairs(rarities) do
    if rarity < minRarity then
      minRarity = rarity
    end
    if rarity > maxRarity then
      maxRarity = rarity
    end
  end

  -- Get suffix
  local suffix = ''
  if properties:match('story') then
  	suffix = '<br>[[Story Maps|Story]]'
  elseif properties:match('demoted_240') then
  	suffix = '<br><span title="This Hero\'s rarity changed from 4★-5★, on April 10, 2018." style="border-bottom:0;text-decoration:underline dotted;cursor:help;">*</span>'
  elseif properties:match('ghb') then
  	suffix = '<br>[[Grand Hero Battle maps|Grand Hero Battle]]'
  elseif properties:match('tempest') then
  	suffix = '<br>[[Tempest Trials]]'
  elseif properties:match('special') then
  	suffix = '<br>[[:Category:Special Heroes|Special]]'
  elseif properties:match('legendary') then
  	suffix = '<br>[[:Category:Legendary Heroes|Legendary]]'
  end

  -- Get wikitext
  local wikitext = ''
  if minRarity == maxRarity then
  	wikitext = mw.ustring.format('%s[[File:Icon Rarity %s.png|x20px|link=|alt=★]]%s', minRarity, minRarity, suffix)
  else
  	wikitext = mw.ustring.format('%s[[File:Icon Rarity %s.png|x20px|link=|alt=★]]–%s[[File:Icon Rarity %s.png|x20px|link=|alt=★]]%s', minRarity, minRarity, maxRarity, maxRarity, suffix)
  end
  return wikitext
end

function p._heroList(args)
  -- Cargo Query
  local WEAPON_SORT = Util.getWeaponSortOrder()
  local MOVE_SORT = Util.getMoveSortOrder()
  local ORIGIN_SORT = Util.getOriginSortOrder()
  
  local tables = 'Heroes'
  local queryFields = "_pageName,Name,Title,Origin,WeaponType,MoveType,SummonRarities,RewardRarities,ReleaseDate,Properties"
  local queryArgs = {
  groupBy = '_pageName',
  limit = '500'
  }
  
  local heroQueryResult = cargo.query(tables, queryFields, queryArgs)
  if heroQueryResult == nil then
      return "(no values)"
  end

  -- Initialize the table
  local tbl = mw.html.create('table')
      :addClass('wikitable default sortable')
      :css('text-align','center')
      :css('width','100%')

  -- Table headers
  tbl:tag('th')
     :wikitext('Icon')       
     :addClass('unsortable')
     :css('width', '75px')
  tbl:tag('th')
     :wikitext('Hero')
     :css('width', '30%')
  tbl:tag('th')
     :wikitext('Appears in')
     :css('width', '30%')
  tbl:tag('th')
     :wikitext('Weapon type')
     :css('width', '9%')
  tbl:tag('th')
     :wikitext('Movement type')
     :css('width', '9%')
  tbl:tag('th')
     :wikitext('Rarities')
     :css('width', '10%')
  tbl:tag('th')
     :wikitext('Release date')
     :css('width', '12%')

  if type( heroQueryResult ) == "table" then
      -- Go through all the heroes in the query result
      for _, hero in ipairs(heroQueryResult) do
          local tr = tbl:tag('tr')
          tr:addClass('hero-filter-element'):done()

          -- Icon
          tr:tag('td')
            :wikitext(Util.getHeroIcon(hero._pageName, '75px'))
            
          tr:tag('td')
            :wikitext( '[[' .. hero._pageName .. '|' .. hero.Name .. ': ' .. hero.Title .. ']]' )
          
          -- Origin
          tr:tag('td')
            :wikitext( '[[' .. hero.Origin .. ']]' )
            :attr( 'data-sort-value', ORIGIN_SORT[hero.Origin] )
          
          -- Weapon Type
          tr:attr( 'data-weapon-type', hero.WeaponType )
            :tag('td')
            :wikitext( '[[File:Icon Class ' .. hero.WeaponType .. '.png|35px]]' )
            :attr( 'data-sort-value', WEAPON_SORT[hero.WeaponType] )
          
          -- Movement Type
          tr:attr( 'data-move-type', hero.MoveType )
            :tag('td')
            :wikitext( '[[File:Icon Move ' .. hero.MoveType .. '.png|25px]]' )
            :attr( 'data-sort-value', MOVE_SORT[hero.MoveType] )
          
          -- Rarity
          local rarity = getRarityWikitext(hero.SummonRarities, hero.RewardRarities, hero.Properties)

          tr:tag('td')
            :wikitext( rarity )

          local heroReleaseDate = hero.ReleaseDate
          if heroReleaseDate == nil or heroReleaseDate == '' then
            tr:tag('td')
             :wikitext( 'N/A' )
             :attr( 'data-sort-value', '-' )
          else
            tr:tag('td')
             :wikitext( hero.ReleaseDate )
          end
          
      end
      return tostring(tbl)
    end
end
    
function p.heroList(frame)
    return p._heroList()
end

return p