FANDOM


--
-- Module to provide access to Hero (Hv) data
--
local p = {}
 
local dataModule = "Module:Hv/data"
 
-- helper to convert map keys to string list
local function keysToString(map, sep)
  if map == nil then
    return ""
  end
 
  local result = {}
  for key, _ in pairs(map) do
    result[#result+1] = key
  end
  table.sort(result)
  return table.concat(result, sep)
end
 
local function listToString(list)
  if list == nil then
    return ""
  end
 
  local result = {}
  for _, e in ipairs(list) do
    result[#result+1] = e
  end
  return table.concat(result, "/")
end
 
-- helper: 
-- ascensionNo: 0, 1 or 2 or nil = all
-- dataObj: the traits or abilities map
function abilitiesOrTraitsToString(dataObj, ascensionNo)
   local result = {}
   for oid, oascNo in pairs(dataObj) do
       if ascensionNo == nil or ascensionNo == oascNo then
           result[#result+1] = oid
       end
    end
    if ascensionNo ~= nil then
        table.sort(result) 
    else    
        -- sort by ascensions
        table.sort(result, 
        function(a,b)
            ascA = dataObj[a]
            ascB = dataObj[b]
            if ascA == ascB then 
                return a < b
            else
                return ascA < ascB
            end
        end)
    end
    return table.concat(result, "/")
end
 
function evosToString(evosData, ascensionNo)
  local result = {}
  for ascNo, evos in ipairs(evosData) do
    if ascensionNo == nil or ascNo == ascensionNo then
      for evoId, quantity in pairs(evos) do
        result[#result+1] = evoId .. '-' .. quantity
      end
    end
  end
  table.sort(result) 
  return table.concat(result, "/")
end
 
-- console:              =p.get( {args={ 'Tsume', 'abilities' }})
--  Ascension 1 traits   =p.get( {args={ 'Tsume', 'traits', 1 }})   
function p.get(frame)
  local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
  local data = mw.loadData(dataModule)
  local heroId = string.lower(args[1])
  local attr = string.lower(args[2])
  local hero = data[heroId]
  if hero == nil then
    return "??" .. heroId .. "??"
  end
  local value = hero[attr]
 
  if ( attr == "traits" ) then
     return abilitiesOrTraitsToString(value, tonumber(args[3]))
  elseif ( attr == "abilities" ) then
     return abilitiesOrTraitsToString(value, tonumber(args[3]))
  elseif ( attr == "evos" ) then
     return evosToString(value, tonumber(args[3]))
  elseif type(value) == "table" then
      if value[1] == nil then 
          return keysToString(value)
      else 
          return listToString(value)
      end
  else
     return value
  end
end
 
--- helper to match filters on maps and arrays
-- attrValue - string or table. no nested tables!
-- filterValue - string 
local function filterMatches(attrValue, filterValue)
    if type(attrValue) == "string" then
        return attrValue == filterValue
    elseif type(attrValue) == "table" then
        if attrValue[1] == nil then
            return attrValue[filterValue]  -- map
        else
            for _, v in ipairs(attrValue) do -- array
                if v == filterValue then
                    return true
                end
            end
            return false
        end
    else
        return false, "ERROR: arg1 not supported"
    end 
end
 
--
-- ids|<filter attr>|<filter value>
--   filter: e.g: element|Water  or  families|Beast
--   returns string list with hero ids ("abigail the brutal/agnon/../../zurk")
-- debug console: =p.ids({ args={"element", "Water"}} ) 
--     no filter: =p.ids({})
function p.ids(frame)
  local args = frame.args
  local filterType = nil
  local filterValue = nil
  if args[1] ~= nil then
    filterType = string.lower(args[1])
    filterValue = args[2]
    if filterValue == nil then return "ERROR: arg2: missing filter value!" end
  end
  local heroIds = {}
  local data = mw.loadData(dataModule)
  for hid, hero in pairs(data) do
    --mw.log("lookup -> ", hid, filterType, filterValue)
    if filterType == nil or filterMatches(hero[filterType], filterValue) then
      heroIds[#heroIds+1] = hid
    end
  end
  table.sort(heroIds)
  return table.concat(heroIds, "/")
end
 
return p
Community content is available under CC-BY-SA unless otherwise noted.