Module:Calculator/AgilityObstacle: Difference between revisions

Make costReduction parsing more robust
No edit summary
(Make costReduction parsing more robust)
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


local yesno = require('Module:Shared/Yesno')
local Num = require('Module:Number')
local Constants = require('Module:Constants')
local Constants = require('Module:Constants')
local Agility = require('Module:Skills/Agility')
local Agility = require('Module:Skills/Agility')
Line 20: Line 22:


-- Gets all associated metadata from an obstacle based on its name.
-- Gets all associated metadata from an obstacle based on its name.
local function getObstacle(name)
local function getObstacle(name, costReduction)
name = Shared.specialTitleCase(name)
name = Shared.specialTitleCase(name)
local obstacle = Agility.getObstacle(name) or Agility.getPillar(name)
local obstacle = Agility.getObstacle(name) or Agility.getPillar(name)
Line 37: Line 39:
else
else
slot = obstacle.category + 1
slot = obstacle.category + 1
end
-- Apply cost reduction, if available.
local itemCosts = Agility.getObstacleCosts(obstacle)
if costReduction then
Agility.applyCostReduction(itemCosts, costReduction)
end
end
Line 44: Line 52:
Obstacle = obstacle,
Obstacle = obstacle,
LevelRequirements = Agility.getObstacleRequirements(obstacle),
LevelRequirements = Agility.getObstacleRequirements(obstacle),
ItemCosts = Agility.getObstacleCosts(obstacle),
ItemCosts = itemCosts,
}
}


Line 50: Line 58:
end
end


function p.calculateCourse(obstacleNames, checkDoubleSlots)
local function getObstacles(obstacleNames, checkDoubleSlots, costReduction)
-- Collect all obstacles and filter out nill values.
-- Collect all obstacles and filter out nill values.
local courseObstacles = {}
local courseObstacles = {}
Line 56: Line 64:
for _, v in pairs(obstacleNames) do
for _, v in pairs(obstacleNames) do
local currObstacle = getObstacle(v)
local currObstacle = getObstacle(v, costReduction)
if currObstacle then
if currObstacle then
if checkDoubleSlots and courseSlots[currObstacle.Slot] == true then
if checkDoubleSlots and courseSlots[currObstacle.Slot] == true then
Line 67: Line 75:
end
end
return courseObstacles
end
function p.calculateCourse(obstacleNames, checkDoubleSlots, costReduction)
local funcPoolCosts = function(tbl, item, amount)
Shared.addOrUpdate(tbl, item, function(x) x = x or 0 return x + amount end)
end
local courseObstacles = getObstacles(obstacleNames, checkDoubleSlots, costReduction)
-- Calculate the highest level requirements and the total amount of items
-- Calculate the highest level requirements and the total amount of items
-- required to build this agility course.
-- required to build this agility course.
local courseLevelRequirements = {}
local courseLevelRequirements = {}
local courseItemCosts = {}
local courseItemCosts = {
['Items'] = {}
}
for _, course in pairs(courseObstacles) do
for _, obstacle in pairs(courseObstacles) do
for skill, level in pairs(course.LevelRequirements) do
-- Pool together highest level requirements for the entire course.
for skill, level in pairs(obstacle.LevelRequirements) do
Shared.addOrUpdate(courseLevelRequirements, skill,  
Shared.addOrUpdate(courseLevelRequirements, skill,  
function(x)
function(x)
Line 80: Line 101:
end
end
for item, amount in pairs(course.ItemCosts) do
-- Pool together total item costs to build the entire course course.
Shared.addOrUpdate(courseItemCosts, item,  
local obstacleCosts = obstacle.ItemCosts
function(x)
if obstacleCosts['GP'] then funcPoolCosts(courseItemCosts, 'GP', obstacleCosts['GP']) end
x = x or 0 return x + amount
if obstacleCosts['SC'] then funcPoolCosts(courseItemCosts, 'SC', obstacleCosts['SC']) end
end)
for item, amount in pairs(obstacleCosts['Items']) do
funcPoolCosts(courseItemCosts['Items'], item, amount)
end
end
end
end
Line 124: Line 147:


function p._getCourseList(args)
function p._getCourseList(args)
-- Parse optional parameters
local costReduction = {
['GP']  = Num.toNumberOrDefault(args['gpCostReduction'], 0),
['SC']  = Num.toNumberOrDefault(args['scCostReduction'], 0),
['Item'] = Num.toNumberOrDefault(args['itemCostReduction'], 0),
}
local obstacleNames = parseObstacleArgs(args)
local obstacleNames = parseObstacleArgs(args)
local courseRequirements = p.calculateCourse(obstacleNames, true)
local courseRequirements = p.calculateCourse(obstacleNames, true, costReduction)
local includeItems = args['includeitems'] or true
local includeSkills = args['includeskills'] or true
local includeObstacles = args['includeObstacles'] or true
local html = mw.html.create()
local html = mw.html.create()
     local div = html:tag('div')
     local div = html:tag('div')
      
      
     if includeObstacles then
     if yesno(args['includeObstacles'], true) == true then
div:tag('b'):wikitext('Obstacles')
div:tag('b'):wikitext('Obstacles')
local tbl = mw.html.create("table")
local tbl = mw.html.create("table")
Line 156: Line 182:
     end
     end


     if includeItems then
     if yesno(args['includeitems'], true) then
     div:tag('b'):wikitext('Items Required')
     div:tag('b'):wikitext('Items Required')
     local ul = div:tag('ul')
     local ul = div:tag('ul')
    
    
     local itemList = Shared.sortDictionary(courseRequirements.CourseItemCosts,  
    local courseItems = courseRequirements.CourseItemCosts
    -- Put GP and SC at the top, and remove them from the list
    -- to avoid sorting and re-adding them below.
    if courseItems['GP'] then ul:tag('li'):wikitext(getItemIcon('GP', courseItems['GP'])) end
    if courseItems['SC'] then ul:tag('li'):wikitext(getItemIcon('SC', courseItems['SC'])) end
 
     local itemList = Shared.sortDictionary(courseItems['Items'],  
     function(a, b) return a.item < b.item end,
     function(a, b) return a.item < b.item end,
     function(a, b) return {item = a, amount = b} end)
     function(a, b) return {item = a, amount = b} end)
Line 170: Line 202:
     end
     end
if includeSkills then
if yesno(args['includeskills'], true) then
div:tag('b'):wikitext('Skills Required')
div:tag('b'):wikitext('Skills Required')
     local ul2 = div:tag('ul')
     local ul2 = div:tag('ul')
Line 218: Line 250:
local function getCosts(costsTable)
local function getCosts(costsTable)
local res = {}
   
-- Order table with GP, SC first, then the other items.
-- Order table with GP, SC first, then the other items.
    local sortedCosts = {}
     if costsTable['GP'] then table.insert(res, getItemIcon('GP', costsTable['GP'])) end
     if costsTable['GP'] then table.insert(sortedCosts, {item = 'GP', amount = costsTable['GP']}) end
     if costsTable['SC'] then table.insert(res, getItemIcon('SC', costsTable['SC'])) end
     if costsTable['SC'] then table.insert(sortedCosts, {item = 'SC', amount = costsTable['SC']}) end
 
   
     local sortedCosts = Shared.sortDictionary(costsTable['Items'],
     for k, v in pairs(costsTable) do
function(a, b) return a.item < b.item end,
    if k ~= 'GP' and k ~= 'SC' then
function(a, b) return {item = a, amount = b} end)
    table.insert(sortedCosts, {item = k, amount = v})
 
    end
    end
   
    local res = {}
     for _, v in pairs(sortedCosts) do
     for _, v in pairs(sortedCosts) do
     table.insert(res, getItemIcon(v.item, v.amount))
     table.insert(res, getItemIcon(v.item, v.amount))
Line 257: Line 287:
end
end


--== Start of table formatting ==--
--== Parse optional parameters==--
local showTotals = yesno(args['showtotals'], true)
local obstacleMastery = yesno(args['obstacleMastery'], false)
local costReduction = {
['GP']  = Num.toNumberOrDefault(args['gpCostReduction'], 0),
['SC']  = Num.toNumberOrDefault(args['scCostReduction'], 0),
['Item'] = Num.toNumberOrDefault(args['itemCostReduction'], 0),
}
local obstacleNames = parseObstacleArgs(args)
local obstacleNames = parseObstacleArgs(args)
local courseRequirements = p.calculateCourse(obstacleNames, true)
local courseRequirements = p.calculateCourse(obstacleNames, true, costReduction)
 
-- Todo: Add cost reduction?
local showTotals = args['showtotals'] or true
local obstacleMastery = args['obstacleMastry'] or false


--== Start of table formatting ==--
local tbl = mw.html.create("table")
local tbl = mw.html.create("table")
       :addClass("wikitable stickyHeader")
       :addClass("wikitable stickyHeader")
Line 304: Line 340:
function p.test()
function p.test()
local obstacles = {"Rope Climb","Monkey Bars"," Balance Seesaw","Elite Pillar of Conflict"}
local obstacles = {"Rope Climb","Monkey Bars"," Balance Seesaw","Elite Pillar of Conflict"}
local obs = p._getCourseTable(obstacles)
local obs = p.calculateCourse(obstacles)




Debug.log(obs)
Debug.log(obs.CourseItemCosts)
end
end
return p
return p
1,309

edits