Module:Pets

From Melvor Idle

Data for this page is stored in Module:Pets/data


--This module contains all sorts of functions for getting data on pets

local p = {}

local PetData = mw.loadData('Module:Pets/data')
local Constants = mw.loadData('Module:Constants/data')

local Shared = require( "Module:Shared" )
local Icons = require('Module:Icons')
local Skills = require('Module:Skills')
local Zones = require('Module:CombatAreas')


function p.getPetByID(ID)
  local result = Shared.clone(PetData.Pets[ID + 1])
  if result ~= nil then
    result.id = ID
  end
  return result
end

function p.getPet(name)
  local result = nil
  name = string.gsub(name, "%%27", "'")
  name = string.gsub(name, "'", "'")
  name = string.gsub(name, "'", "'")
  for i, pet in pairs(PetData.Pets) do
    local PetName = string.gsub(pet.name, '#', '')
    if(name == PetName) then
      result = Shared.clone(pet)
      --Make sure every pet has an id, and account for Lua being 1-index
      result.id = i - 1
      break
    end
  end
  return result
end

function p.getPetBySkill(skillName)
  local result = nil
  skillName = string.gsub(skillName, "%%27", "'")
  skillName = string.gsub(skillName, "'", "'")
  skillName = string.gsub(skillName, "'", "'")
  for i, pet in pairs(PetData.Pets) do
    if(Skills.getSkillID(skillName) == pet.skill) then
      result = Shared.clone(pet)
      --Make sure every pet has an id, and account for Lua being 1-index
      result.id = i - 1
      break
    end
  end
  return result
end

function p.getPetTable(frame)
  local petName = frame.args ~= nil and frame.args[1] or frame
  local pet = p.getPet(petName)
  local result = '{| class="wikitable"\r\n'
  result = result..'!Pet!!Name!!Effect'
  result = result..'\r\n|-\r\n|'..Icons.Icon({pet.name, type='pet', size='60', notext=true})
  result = result..'||[['..pet.name..']]||'..pet.description
  result = result..'\r\n|}'
  return result
end

function p.getPetTableBySkill(frame)
  local result = nil
  local skillName = frame.args ~= nil and frame.args[1] or frame
  skillName = string.gsub(skillName, "%%27", "'")
  skillName = string.gsub(skillName, "'", "'")
  skillName = string.gsub(skillName, "'", "'")
  local pet = p.getPetBySkill(skillName)
  result = p.getPetTable(pet.name)
  return result
end

function p.getPetSidebar(frame)
  local args = frame.args ~= nil and frame.args or frame
  local result = nil
  local name = (args.name ~= nil and args.name ~= '') and args.name or args[1]
  local pet = p.getPet(name)
  local skill = (args.skill ~= nil and args.skill ~= '') and args.skill or Icons.Icon({Skills.getSkillName(pet.skill), type='skill'})
  local effect = (args.effect ~= nil and args.effect ~= '') and args.effect or pet.description
  
  result = '{| class="wikitable" style="float:right; clear:right;"\r\n|-\r\n'
  result = result..'! '..name..'\r\n|-\r\n| '
  result = result..Icons.Icon({name, type='pet', size='250', notext=true})
  result = result..'\r\n|-\r\n| Pet ID: '..pet.id
  result = result..'\r\n|-\r\n| Skill: '..skill
  result = result..'\r\n|-\r\n| style ="width: 250px;"|Effect: '..effect..'\r\n|}'

  return result
end

function p.getPetPageTable()
  local result = ''

  local petList = {}
  local acquiredOverrides = {
    ['Ripper the Reindeer'] = '[[Events#Christmas_Event_2020|Christmas Event 2020]]',
  }
  local effectOverrides = {
    ['Ripper the Reindeer'] = 'None',
  }
  local acquired = nil
  local temp = nil
  for i, pet in pairs(PetData.Pets) do
    temp = Shared.clone(pet)

    if(effectOverrides[temp.name] ~= nil) then
      temp.description = effectOverrides[temp.name] 
    end
    if(temp.name == 'Asura') then temp.acquiredBy = 'Slayer' end

    acquired = ''
    if(Skills.getSkillID(temp.acquiredBy) ~= nil) then
      acquired = 'skill'
    elseif Zones.getAreaFilterType('slayer', temp.acquiredBy) ~= nil then
      acquired = 'slayer'
    end
    temp.type = acquired

    table.insert(petList, temp)
  end

  result = result..'{|class="wikitable lighttable"'
  result = result..'\r\n|-\r\n! Name !! Image !! Acquired From !! Effect'

  table.sort(petList, function(a, b)
                         return p.getPet(a.name).id < p.getPet(b.name).id
                       end)

  for i, thisPet in pairs(petList) do
    result = result..'\r\n|-\r\n|[['..thisPet.name..']]'
    result = result..'||'..Icons.Icon({thisPet.name, size='60', type='pet', notext=true})

    if acquiredOverrides[thisPet.name] ~= nil then
      result = result..'||'..acquiredOverrides[thisPet.name]
    else
      result = result..'||'..Icons.Icon({thisPet.acquiredBy, type=thisPet.type})
    end
    result = result..'||'..thisPet.description
  end
  result = result..'\r\n|}'

  return result
end

function p.getPetNavbox(frame)
  --•
  local result = '{| class="wikitable" style="margin:auto; text-align:center; clear:both; width: 100%"'
  result = result..'\r\n|-\r\n!colspan="2"|[[Pets]]'

  local skillPetList = {}
  local otherPetList = {}
  for i, petData in Shared.skpairs(PetData.Pets) do
    if petData.skill ~= nil and petData.skill >= 0 and petData.name ~= "Ty" then
      table.insert(skillPetList, Icons.Icon({petData.name, type='pet'}))
    else
      table.insert(otherPetList, Icons.Icon({petData.name, type='pet'}))
    end
  end
  table.sort(skillPetList, function(a, b) return a < b end)
  table.sort(otherPetList, function(a, b) return a < b end)
  result = result..'\r\n|-\r\n!Skill Pets\r\n|'..table.concat(skillPetList, ' • ')
  result = result..'\r\n|-\r\n!Other Pets\r\n|'..table.concat(otherPetList, ' • ')
  result = result..'\r\n|}'
  return result
end

return p