Module:Pets

From Melvor Idle
Revision as of 01:40, 14 January 2021 by ByteFoolish (talk | contribs) (Skill link try 2)

Data for this page is stored in Module:GameData/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')


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 or args[1]
  local pet = p.getPet(name)
  local skillLink = '{{Skill|'
  local skill = args.skill ~= nil and args.skill or skillLink..Skills.getSkillName(pet.skill)..'}}'
  local effect = args.effect ~= nil 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

return p