Module:Calculator/AgilityObstacle: Difference between revisions

From Melvor Idle
No edit summary
No edit summary
Line 136: Line 136:


function p._main(args)
function p._main(args)
return table.concat(args, ',')
end
function p.a()
local obstacleNames = {}
local obstacleNames = {}
for i = 1, #args do
for i = 1, #args do

Revision as of 00:30, 22 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, 'SC', 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.calculateCourse(obstacles, checkDoubleSlots)
	-- Collect all obstacles and filter out nill values.
	local courseObstacles = {}
	local courseSlots = {}
	
	for _, v in pairs(obstacles) do
		local currObstacle = getObstacle(v)
		if currObstacle then
			if checkDoubleSlots and courseSlots[currObstacle.Slot] == true then
				error('There are two or more obstacles in the same slot. Obstacle: ' .. currObstacle.Name .. ' Slot: ' .. currObstacle.Slot)
			end
			
			courseSlots[currObstacle.Slot] = true
			table.insert(courseObstacles, currObstacle) 
		end
	end
	
	-- Calculate the highest level requirements and the total amount of items
	-- required to build this agility course.
	local courseLevelRequirements = {}
	local courseItemCosts = {}
	
	for _, course in pairs(courseObstacles) do
		for skill, level in pairs(course.LevelRequirements) do
			addHighestLevelRequirement(courseLevelRequirements, skill, level)
		end
		
		for item, amount in pairs(course.ItemCosts) do
			concatItemRequirements(courseItemCosts, item, amount)
		end
	end
	
	return {
		CourseLevelRequirements = courseLevelRequirements,
		CourseItemCosts = courseItemCosts
	}
end

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

end

function p._main(args)
	return table.concat(args, ',')
end

function p.a()
	local obstacleNames = {}
	for i = 1, #args do
    	if args[i] then
    		table.insert(obstacleNames, args[i]:match("^%s*(.-)%s*$"))
    	end
	end
	
	local courseRequirements = p.calculateCourse(obstacleNames, true)

	-------- TEMP --------
	local html = mw.html.create()
    local div = html:tag('div')
    div:tag('h2'):wikitext('Items Required')
    
    local ul = div:tag('ul')
    for k, v in pairs(courseRequirements.CourseItemCosts) do
    	ul:tag('li'):wikitext(k .. ': ' .. v)
    end
	 
	div:tag('h2'):wikitext('Skills Required')
	
    local ul2 = div:tag('ul')
    for k, v in pairs(courseRequirements.CourseLevelRequirements) do
    	ul:tag('li'):wikitext(k .. ': ' .. v)
    end
    
    return tostring(html)
end

function p.test()
	mw.log( p._main({
		' Cargo Net ',' Balance Beam','Pipe Climb'
	}))
end

return p