Difference between revisions of "Module:CombatAreas"

From Melvor Idle
(Added p.getDungeonRequirements)
(Formatting changes)
Line 81: Line 81:
 
     if area.requiresCompletion ~= nil and area.requiresCompletion >= 0 then
 
     if area.requiresCompletion ~= nil and area.requiresCompletion >= 0 then
 
       local dung = p.getAreaByID('dungeon', area.requiresCompletion)
 
       local dung = p.getAreaByID('dungeon', area.requiresCompletion)
       return 'Completing '..Icons.Icon({dung.name, type='dungeon'})
+
       return Icons.Icon({dung.name, type='dungeon'})..' Completed'
 
     else
 
     else
 
       return ''
 
       return ''
Line 133: Line 133:
 
   local result = p._getAreaStat(area, 'dungeonReq')
 
   local result = p._getAreaStat(area, 'dungeonReq')
 
   if result ~= '' then
 
   if result ~= '' then
     result = '\r\n|-\r\n|Requirements:<br/>'..result
+
     result = "\r\n|-\r\n|'''Requirements:'''<br/>"..result
 
   end
 
   end
 
   return result
 
   return result

Revision as of 16:40, 18 February 2021

Data is pulled from Module:CombatAreas/data


local p = {}

local Constants = mw.loadData('Module:Constants/data')
local AreaData = mw.loadData('Module:CombatAreas/data')

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

function processArea(area, index, type)
  local result = Shared.clone(area)
  result.id = index - 1
  if result.name == nil then
    result.name = result.areaName
  end
  result.type = type
  return result
end

function p.getArea(name)
  local result = nil
  --There are three types of areas but the lists are pretty short so looping all of them isn't a real issue
  for i, area in pairs(AreaData.combatAreas) do
    if area.areaName == name then
      return processArea(area, i, 'combat')
    end
  end

  for i, area in pairs(AreaData.slayerAreas) do
    if area.areaName == name then
      return processArea(area, i, 'slayer')
    end
  end

  for i, area in pairs(AreaData.dungeons) do
    if area.name == name then
      return processArea(area, i, 'dungeon')
    end
  end

  return nil
end

function p.getAreaByID(type, id)
  if type == 'dungeon' then type = 'dungeons'
  elseif type == 'combat' then type = 'combatAreas'
  elseif type == 'slayer' then type = 'slayerAreas' end
  return processArea(AreaData[type][id + 1], id + 1)
end

function p.getAreaFilterType(type, name)
  local areaName = nil
  if type == 'dungeon' then areas = AreaData.dungeons
  elseif type == 'combat' then areas = AreaData.combatAreas
  elseif type == 'slayer' then areas = AreaData.slayerAreas 
  else return nil end

  for i, area in pairs(areas) do
    if type == 'dungeon' then areaName = area.name
    else areaName = area.areaName end

    if areaName == name then
      return processArea(area, i, type)
    end
  end

  return nil
end

function p._getAreaStat(area, statName)
  if statName == 'slayerLevel' then
    return Icons._SkillReq('Slayer', area.slayerLevel)
  elseif statName == 'slayerItem' then
    if area.slayerItem ~= nil and area.slayerItem > 0 then
      local slayItem = Items.getItemByID(area.slayerItem)
      return Icons.Icon({slayItem.name, type='item'})
    else
      return 'None'
    end
  elseif statName == 'dungeonReq' then
    if area.requiresCompletion ~= nil and area.requiresCompletion >= 0 then
      local dung = p.getAreaByID('dungeon', area.requiresCompletion)
      return Icons.Icon({dung.name, type='dungeon'})..' Completed'
    else
      return ''
    end
  end

  return area[statName]
end

function p.getAreaStat(frame)
  local areaName = frame.args ~= nil and frame.args[1] or frame[1]
  local statName = frame.args ~= nil and frame.args[2] or frame[2]
  local area = p.getArea(areaName)
  if area == nil then
    return "ERROR: Could not find an area named "..areaName
  end

  return p._getAreaStat(area, statName)
end

function p.getMonsterAreas(monsterID)
  local areaArray = {}
  --There are three types of areas but the lists are pretty short so looping all of them isn't a real issue
  for i, area in pairs(AreaData.combatAreas) do
    if Shared.contains(area.monsters, monsterID) then
      table.insert(areaArray, processArea(area, i, 'combat'))
    end
  end

  for i, area in pairs(AreaData.slayerAreas) do
    if Shared.contains(area.monsters, monsterID) then
      table.insert(areaArray, processArea(area, i, 'slayer'))
    end
  end

  for i, area in pairs(AreaData.dungeons) do
    if Shared.contains(area.monsters, monsterID) then
      table.insert(areaArray, processArea(area, i, 'dungeon'))
    end
  end
  return areaArray
end

function p.getDungeonRequirements(frame)
  local areaName = frame.args ~= nil and frame.args[1] or frame
  local area = p.getArea(areaName)
  if area == nil then
    return "ERROR: Could not find an area named "..areaName
  end

  local result = p._getAreaStat(area, 'dungeonReq')
  if result ~= '' then
    result = "\r\n|-\r\n|'''Requirements:'''<br/>"..result
  end
  return result
end

return p