Module:Calculator/AgilityObstacle: Difference between revisions

From Melvor Idle
No edit summary
No edit summary
Line 8: Line 8:
local Items = require('Module:Items')
local Items = require('Module:Items')
local Icons = require('Module:Icons')
local Icons = require('Module:Icons')
local Debug = require('Module:Debug') -- Comment out when Module is finalised.


-- Adds the the highest level to the dictionary, or the level, if the key isn't present.
-- Adds the the highest level to the dictionary, or the level, if the key isn't present.
Line 54: Line 55:
local function getItemRequirements(obstacle)
local function getItemRequirements(obstacle)
local itemRequirements = {}
local itemRequirements = {}
if obstacle.gpCost > 0 then
concatItemRequirements(itemRequirements, 'GP', obstacle.gpCost)
end
if obstacle.scCost > 0 then
concatItemRequirements(itemRequirements, 'GP', obstacle.scCost)
end
for j, itemCost in ipairs(obstacle.itemCosts) do
local item = Items.getItemByID(itemCost.id)
concatItemRequirements(itemRequirements, item.name, itemCost.quantity)
end
return itemRequirements
end
end


local function getObstacle(name)
local function getObstacle(name)
name = Shared.titleCase(name)
name = Shared.specialTitleCase(name)
local obstacle = Agility.getObstacle(name)
local obstacle = Agility.getObstacle(name)
if obstacle == nil then
if obstacle == nil then
return nil
return nil
end
end
 
-- TODO: Handle pillars and return the same format/table.
-- if obstacle == pillar.....
local obstacleInfo = {
local obstacleInfo = {
Name,
Name = obstacle.name,
LevelRequirements = {},
Slot = obstacle.category + 1,
ItemCosts = {},
LevelRequirements = getLevelRequirements(obstacle),
ItemCosts = getItemRequirements(obstacle),
}
}


local levelRequirements = getLevelRequirements(obstacle)
return obstacleInfo
-- Build and return obstacle information
mw.log(levelRequirements)
for k, v in pairs(levelRequirements) do
mw.log(k)
mw.log(v)
end
end
end


Line 85: Line 98:


function p._main(args)
function p._main(args)
 
return Debug.toString(args)
end
end


function p.test()
function p.test()
local obst = 'pipe climb'
Debug.log(getObstacle("a lovely jog"))
getObstacle(obst)
end
end


return p
return p

Revision as of 23:51, 21 April 2024

Documentation for this module may be created at Module:Calculator/AgilityObstacle/doc

local p = {}

local Num = require('Module:Number')
local Constants = require('Module:Constants')
local Agility = require('Module:Skills/Agility')
local Shared = require('Module:Shared')
local Skills = require('Module:Skills')
local Items = require('Module:Items')
local Icons = require('Module:Icons')
local Debug = require('Module:Debug') -- Comment out when Module is finalised.

-- Adds the the highest level to the dictionary, or the level, if the key isn't present.
local function addHighestLevelRequirement(tbl, skill, level)
	Shared.addOrUpdate(tbl, skill, 
		function(x)
			if x then
				return math.max(x, level)
			else
				return level
			end
		end
	)
end

-- Adds the item amount to the dictionary, or just the amount if the key isn't present.
local function concatItemRequirements(tbl, item, amount)
	Shared.addOrUpdate(tbl, item, 
		function(x)
			x = x or 0
			return x + amount
		end
	)
end

--- Gets all required levels to build the given obstacle.
local function getLevelRequirements(obstacle)
	local levelRequirements = {}
	
	-- Add agility level requirement.
	addHighestLevelRequirement(levelRequirements, 'Agility', Skills.getRecipeLevel('Agility', obstacle))

	-- Add other level requirements.
	if type(obstacle.skillRequirements) == 'table' then
		for i, skillReq in ipairs(obstacle.skillRequirements) do
			local skillName = Constants.getSkillName(skillReq.skillID)
			if skillName ~= nil then
				addHighestLevelRequirement(levelRequirements, skillName, skillReq.level)
			end
		end
	end
	
	return levelRequirements
end

local function getItemRequirements(obstacle)
	local itemRequirements = {}

	if obstacle.gpCost > 0 then
		concatItemRequirements(itemRequirements, 'GP', obstacle.gpCost)
	end
	if obstacle.scCost > 0 then
		concatItemRequirements(itemRequirements, 'GP', obstacle.scCost)
	end
	
	for j, itemCost in ipairs(obstacle.itemCosts) do
		local item = Items.getItemByID(itemCost.id)
		concatItemRequirements(itemRequirements, item.name, itemCost.quantity)
	end
	
	return itemRequirements
end

local function getObstacle(name)
	name = Shared.specialTitleCase(name)
	local obstacle = Agility.getObstacle(name)
	if obstacle == nil then
		return nil
	end
	
	-- TODO: Handle pillars and return the same format/table.
	-- if obstacle == pillar.....
	
	local obstacleInfo = {
		Name = obstacle.name,
		Slot = obstacle.category + 1,
		LevelRequirements = getLevelRequirements(obstacle),
		ItemCosts = getItemRequirements(obstacle),
	}

	return obstacleInfo
end

function p.main(frame)
	local args = frame:getParent().args
	return p._main(args)

end

function p._main(args)
	return Debug.toString(args)
end

function p.test()
	Debug.log(getObstacle("a lovely jog"))
end

return p