Module:Magic

Revision as of 18:54, 6 October 2020 by Falterfire (talk | contribs) (Added ability to get all spells for a given rune.)

Data pulled from Module:GameData/data


local p = {}

local MagicData = mw.loadData('Module:Magic/data')

local Shared = require('Module:Shared')
local Icons = require('Module:Icons')

function processSpell(section, index)
  local result = Shared.clone(MagicData[section][index])
  result.id = index - 1
  result.type = section
  return result
end

function p.getSpell(name, type)
  local section = type
  if type == nil or type == 'Spell' or type == 'Standard' then
    section = 'Spells'
  elseif type == 'Curse' then
    section = 'Curses'
  elseif type == 'Aurora' then
    section = 'Auroras'
  elseif type == 'Alt Magic' or type == 'Alternative Magic' then
    section='AltMagic'
  end
  
  if MagicData[section] ~= nil then
    local result = nil
    for i, spell in pairs(MagicData[section]) do
      if spell.name == name then
        result = processSpell(section, i)
      end
    end
    return result
  else
    return nil
  end
end

function p.getSpellByID(type, id)
  local section = type
  if type == nil or type == 'Spell' or type == 'Standard' then
    section = 'Spells'
  elseif type == 'Curse' then
    section = 'Curses'
  elseif type == 'Aurora' then
    section = 'Auroras'
  elseif type == 'Alt Magic' or type == 'Alternative Magic' then
    section='AltMagic'
  end
  
  if MagicData[section] ~= nil then
    return processSpell(section, id + 1)
  else
    return nil
  end
end

function p.getSpellsForRune(runeID)
  local spellList = {}
  for secName, secArray in Shared.skpairs(MagicData) do
    for i, spell in pairs(secArray) do
      local foundSpell = false
      for j, req in pairs(spell.runesRequired) do
        if req.id == runeID then
          table.insert(spellList, processSpell(secName, i))
          foundSpell = true
          break
        end
      end
      if spell.runesRequiredAlt ~= nil and not foundSpell then
        for j, req in pairs(spell.runesRequiredAlt) do
          if req.id == runeID then
            table.insert(spellList, processSpell(secName, i))
            break
          end
        end
      end
    end
  end
  table.sort(spellList, function(a, b) 
               if a.type ~= b.type then
                 return p.getSpellTypeIndex(a.type) < p.getSpellTypeIndex(b.type)
               else
                 return a.magicLevelRequired < b.magicLevelRequired
               end
             end)
  return spellList
end

function p.getSpellTypeIndex(type)
  if type == 'Spells' then
    return 0
  elseif type == 'Curses' then
    return 1
  elseif type == 'Auroras' then
    return 2
  elseif type == 'Ancient' then
    return 3
  elseif type == 'AltMagic' then
    return 4
  end
  return -1
end

function p.getSpellTypeLink(type)
  if type == 'Spells' then
    return Icons.Icon({'Magic#Standard Magic', 'Standard', img='Standard', type='spellType'})
  elseif type == 'Curses' then
    return Icons.Icon({'Magic#Curses', 'Curse', img='Curse', type='spellType'})
  elseif type == 'Auroras' then
    return Icons.Icon({'Magic#Auroras', 'Aurora', img='Aurora', type='spellType'})
  elseif type == 'Ancient' then
    return Icons.Icon({'Magic#Ancient Magicks', 'Ancient', img='Ancient', type='spellType'})
  elseif type == 'AltMagic' then
    return Icons.Icon({'Alt. Magic', type='skill'})
  end
  return ''
end

return p