Module:Calculator/AgilityObstacle: Difference between revisions

From Melvor Idle
No edit summary
No edit summary
Line 37: Line 37:
local obstacle = Agility.getObstacle(name) or Agility.getPillar(name)
local obstacle = Agility.getObstacle(name) or Agility.getPillar(name)
if obstacle == nil then
if obstacle == nil then
return nil
error("Unknown Agility obstacle or pillar name: " .. name)
end
end


Line 55: Line 55:
Name = obstacle.name,
Name = obstacle.name,
Slot = slot,
Slot = slot,
Obstacle = obstacle,
LevelRequirements = Agility.getObstacleRequirements(obstacle),
LevelRequirements = Agility.getObstacleRequirements(obstacle),
ItemCosts = Agility.getObstacleCosts(obstacle),
ItemCosts = Agility.getObstacleCosts(obstacle),
Line 62: Line 63:
end
end


function p.calculateCourse(obstacles, checkDoubleSlots)
function p.calculateCourse(obstacleNames, checkDoubleSlots)
-- Collect all obstacles and filter out nill values.
-- Collect all obstacles and filter out nill values.
local courseObstacles = {}
local courseObstacles = {}
local courseSlots = {}
local courseSlots = {}
for _, v in pairs(obstacles) do
for _, v in pairs(obstacleNames) do
local currObstacle = getObstacle(v)
local currObstacle = getObstacle(v)
if currObstacle then
if currObstacle then
Line 86: Line 87:
for _, course in pairs(courseObstacles) do
for _, course in pairs(courseObstacles) do
for skill, level in pairs(course.LevelRequirements) do
for skill, level in pairs(course.LevelRequirements) do
addHighestLevelRequirement(courseLevelRequirements, skill, level)
Shared.addOrUpdate(courseLevelRequirements, skill,  
function(x)
if x then return math.max(x, level) else return level end
end)
end
end
for item, amount in pairs(course.ItemCosts) do
for item, amount in pairs(course.ItemCosts) do
concatItemRequirements(courseItemCosts, item, amount)
Shared.addOrUpdate(courseItemCosts, item,  
function(x)
x = x or 0 return x + amount
end)
end
end
end
end
 
return {
return {
Obstacles = courseObstacles,
CourseLevelRequirements = courseLevelRequirements,
CourseLevelRequirements = courseLevelRequirements,
CourseItemCosts = courseItemCosts
CourseItemCosts = courseItemCosts
Line 102: Line 110:
-- Stupid shenanigans to filter out numeric parameters.
-- Stupid shenanigans to filter out numeric parameters.
-- Because just iterating over #args doesn't work for some reason...
-- Because just iterating over #args doesn't work for some reason...
function getNumericArgs(args)
function parseObstacleArgs(args)
local ret = {}
local obstacleNames = {}
for k, v in pairs(args) do
for k, v in pairs(args) do
k = tonumber(k)
k = tonumber(k)
if k then table.insert(ret, v) end
if k then  
table.insert(obstacleNames, v:match("^%s*(.-)%s*$"))  
end
end
end
return ret
return obstacleNames
end
end


Line 117: Line 128:


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



Revision as of 15:07, 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

local function getObstacle(name)
	name = Shared.specialTitleCase(name)
	local obstacle = Agility.getObstacle(name) or Agility.getPillar(name)
	if obstacle == nil then
		error("Unknown Agility obstacle or pillar name: " .. name)
	end

	-- Resolve obstacle slot.
	local slot = 0
	if string.find(name, 'Pillar') then
		if string.find(name, 'Elite') then
			slot = 'Elite Pillar'
		else
			slot = 'Pillar'
		end
	else
		slot = obstacle.category + 1
	end
	
	local obstacleInfo = {
		Name = obstacle.name,
		Slot = slot,
		Obstacle = obstacle,
		LevelRequirements = Agility.getObstacleRequirements(obstacle),
		ItemCosts = Agility.getObstacleCosts(obstacle),
	}

	return obstacleInfo
end

function p.calculateCourse(obstacleNames, checkDoubleSlots)
	-- Collect all obstacles and filter out nill values.
	local courseObstacles = {}
	local courseSlots = {}
	
	for _, v in pairs(obstacleNames) 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
				Shared.addOrUpdate(courseLevelRequirements, skill, 
					function(x)	
						if x then return math.max(x, level)	else return level end 
					end)
		end
		
		for item, amount in pairs(course.ItemCosts) do
				Shared.addOrUpdate(courseItemCosts, item, 
					function(x)
						x = x or 0 return x + amount
					end)
		end
	end

	return {
		Obstacles = courseObstacles,
		CourseLevelRequirements = courseLevelRequirements,
		CourseItemCosts = courseItemCosts
	}
end

-- Stupid shenanigans to filter out numeric parameters.
-- Because just iterating over #args doesn't work for some reason...
function parseObstacleArgs(args)
	local obstacleNames = {}
	for k, v in pairs(args) do
		k = tonumber(k)
		if k then 
			table.insert(obstacleNames, v:match("^%s*(.-)%s*$")) 
		end
	end
	
	return obstacleNames
end

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

function p._main(args)
	local obstacleNames = parseObstacleArgs(args)
	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
    	ul2:tag('li'):wikitext(k .. ': ' .. v)
    end
    
    return tostring(html)
end

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

function p._getCourseTable(obstacleNames)
	local result = ''
	local obstacles = {}
	for i, name in ipairs(obstacleNames) do
		local obst = Agility.getObstacle(Shared.trim(name))
		if obst == nil then
			result = result .. Shared.printError('Invalid Obstacle Name "' .. name .. '"') .. '<br/>'
		else
			table.insert(obstacles, obst)
		end
	end

	result = result..'{| class="wikitable sortable stickyHeader"'
	result = result..'\r\n|- class="headerRow-0"'
	result = result..'\r\n!Slot!!Name!!Bonuses!!Requirements!!Cost'

	local catLog = {}
	
	table.sort(obstacles, function(a, b) return a.category < b.category end)

	local catCounts = {}
	for i, obst in ipairs(obstacles) do
		if catCounts[obst.category] == nil then
			catCounts[obst.category] = 1
		else
			catCounts[obst.category] = catCounts[obst.category] + 1
		end
	end

	for i, obst in ipairs(obstacles) do
		result = result..'\r\n|-'
		result = result..'\r\n|'
		if catLog[obst.category] == nil then
			local rowspan = catCounts[obst.category]
			result = result..'rowspan="'..rowspan..'" style="border:1px solid black"|'..(obst.category + 1)..'||'
			catLog[obst.category] = true
		end
		result = result..obst.name

		local bonuses = {}
		--After that, adding the bonuses
		for bonusName, bonusValue in pairs(obst.modifiers) do
			table.insert(bonuses, Constants._getModifierText(bonusName, bonusValue))
		end
		if Shared.tableIsEmpty(bonuses) then
			table.insert(bonuses, '<span style="color:red">None :(</span>')
		end
		result = result..'||'..table.concat(bonuses, '<br/>')

		--Grabbing requirements to create
		result = result..'|| ' .. formatObstacleRequirements(obst)

		--Finally, the cost
		result = result..'|| data-sort-value="'..obst.gpCost..'"|'.. formatObstacleCosts(obst)
	end

	result = result..'\r\n|}'

	return result
end

function p.getCourseTable(frame)
	local obstNameStr = frame.args ~= nil and frame.args[1] or frame
	local obstacleNames = Shared.splitString(obstNameStr, ',')
	
	return p._getCourseTable(obstacleNames)
end

return p