Module:Calculator/AgilityObstacle: Difference between revisions

no edit summary
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)


2,875

edits