FANDOM


--
-- Module to provide access to Abilities data
--
local p = {}
 
local dataModule = "Module:Abilities/data"
 
libTraits = require("Module:LibTraits")
 
-- debug constole:  =p.get({args={"nature's fury-ii", 'name'}})
function p.get(frame)
  local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
  local data = mw.loadData(dataModule)
  local abilityIdArg = string.lower(args[1])
  -- split abilityId and rank (e.g. flying-ii)
  local abilityId, rank = libTraits.splitRank(abilityIdArg)
  if abilityId == nil then
    abilityId = abilityIdArg
    rank = nil
  end
  local attr = string.lower(args[2])
  local ability = data[abilityId]
  if ability == nil then
    return "??" .. abilityId .. "??"
  end
  local value = ability[attr]
 
  if type(value) == "table" then
      utils = require("Module:LibTableUtils")
      if value[1] == nil then 
          return utils.keysToString(value)
      else 
          return utils.listToString(value)
      end
  elseif  ( attr == "name" ) then
     if rank ~= nil and rank:len() > 0 then
       return value .. " " .. string.upper(rank)
     else
       return value         
     end         
  else
     return value
  end
end
 
--
-- like get but returns multi values. Used for table columns to reduce 
-- memory usage of big tables 
-- =p.getx({args={ "armored", "ยง", "name", "type", "energy cost","heroes"}})
--
function p.getx(frame)
  local args = frame.args
  local data = mw.loadData(dataModule)
  local abilityId = string.lower(args[1])
  local sep = string.lower(args[2])      
  local result = {}
  local ability = data[abilityId]
  if ability == nil then
    return ""
  end
  local c = 0;
  for _, a in ipairs(args) do
    c = c + 1
	if c > 2 then
	  local attr = string.lower(a)
	  local value = ability[attr]
      if ( attr == "heroes" ) then
        value = keysToString(value, "/")
      elseif  ( attr == "effects" ) then
        return listToString(value, "/")        
      end 
	  result[#result+1] = value or ""
	end
  end
  --mw.log("result:", table.concat(result, sep))
  return table.concat(result, sep)
end
 
--
-- ids|<filter>
--   filter: "trait", "special attack", "basic attack" for abilities or traits, not filter=all
--   returns string list with ability ids ("a real stunner/abs of steel/aegis bandage/../../...")
--
function p.ids(frame)
  local args = frame.args
  local filterType = nil
  if args[1] ~= nil then
    filterType = string.lower(args[1])
  end
  local abilityIds = {}
  local data = mw.loadData(dataModule)
  for aid, ability in pairs(data) do
    --mw.log("lookup -> ", aid, filterType)
    if filterType == nil or string.lower(ability["type"] or "") == filterType then
      abilityIds[#abilityIds+1] = aid
    end
  end
  table.sort(abilityIds)
  return table.concat(abilityIds, "/")
end
 
return p
Community content is available under CC-BY-SA unless otherwise noted.