Module:Ability icon

From Dota 2 Wiki
Jump to: navigation, search

Documentation for Module:Ability icon Jump to code ↴ [ edit | purge ]

This module provides a simple way of generating an ability icon.

Usage[edit]

First, load the module.

local aicon = require('Module:Ability icon')

The function itself has two required and one optional argument:

  • source The abilities source.
  • name The abilities name.
  • data (Optional) Already retrieved Cargo data, to prevent unnecessary queries.

Tests[edit]

NoN 1 test failed.

Name Expected Actual
YesY testDefault
YesY testGivenData
NoN testItem Lua error -- Module:Ability_icon:14: bad argument #3 to 'format' (string expected, got nil)
YesY testItemAbility
YesY testNormalAbility
YesY testRuneAbility


Dependencies

local p = {}

local cargoQuery = mw.ext.cargo.query
local getArgs = require( 'Module:Arguments' ).getArgs

local i18n = {
  error = {
    noAbilityData = 'No data available for this ability'
  }
}

local function getAbilityData( source, name )
  local cargoData = cargoQuery( 'abilities', 'image, type', {
    where = string.format( 'source="%s" AND title="%s"', source, name ),
    groupBy = 'source, title'
  } )[1]
  return cargoData
end

local function getItemIcon( name )
  local cargoData = cargoQuery( 'items', 'image', {
    where = string.format( '_pageName="%s"', name ),
    groupBy = '_pageID'
  } )[1]
  return cargoData.image
end

local function getRuneIcon( name )
  return string.format( 'File:Bottle (%s) icon.png', name )
end

function p.main( frame )
  local args = getArgs( frame )
  return p._main( args )
end

function p._main( args, abilityData )
  abilityData = abilityData or getAbilityData( args.source, args.name )
  assert( abilityData, i18n.error.noAbilityData )

  local abilityType = abilityData.type
  if abilityType == 'item' then
    abilityData.image = getItemIcon( args.source )
  elseif abilityType == 'rune' then
    abilityData.image = getRuneIcon( args.name )
  end

  if abilityData.image ~= '' then
    return abilityData.image
  else
    return 'File:Unknown icon.png'
  end
end

return p