Module:Calculator/AgilityObstacle: Difference between revisions

Add support for more currencies
(Potential fix for AgilityCourseTable template)
(Add support for more currencies)
 
Line 14: Line 14:
     ['AP']  = function(amount) return Icons._Currency('AP', amount) end,
     ['AP']  = function(amount) return Icons._Currency('AP', amount) end,
     ['ASC'] = function(amount) return Icons._Currency('ASC', amount) end,
     ['ASC'] = function(amount) return Icons._Currency('ASC', amount) end,
}
local currencies = {
'GP', 'SC', 'AP', 'ASC'
}
}


Line 90: Line 94:


function p.calculateCourse(obstacleNames, checkDoubleSlots, costReduction)
function p.calculateCourse(obstacleNames, checkDoubleSlots, costReduction)
local funcPoolCosts = function(tbl, item, amount)
function poolItems(tbl, item, amount)
Shared.addOrUpdate(tbl, item, function(x) x = x or 0 return x + amount end)
Shared.addOrUpdate(tbl, item, function(x) x = x or 0 return x + amount end)
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
Line 102: Line 104:
['Items'] = {}
['Items'] = {}
}
}
local courseObstacles = getObstacles(obstacleNames, checkDoubleSlots, costReduction)
 
for _, obstacle in pairs(courseObstacles) do
for _, obstacle in pairs(courseObstacles) do
-- Pool together highest level requirements for the entire course.
-- Pool together highest level requirements for the entire course.
Line 114: Line 117:
-- Pool together total item costs to build the entire course course.
-- Pool together total item costs to build the entire course course.
local obstacleCosts = obstacle.ItemCosts
local obstacleCosts = obstacle.ItemCosts
if obstacleCosts['GP'] then funcPoolCosts(courseItemCosts, 'GP', obstacleCosts['GP']) end
if obstacleCosts['SC'] then funcPoolCosts(courseItemCosts, 'SC', obstacleCosts['SC']) end
-- Iterate over currencies and pool these costs
for _, curr in ipairs(currencies) do
if obstacleCosts[curr] then
poolItems(courseItemCosts, curr, obstacleCosts[curr])
end
end
for item, amount in pairs(obstacleCosts['Items']) do
for item, amount in pairs(obstacleCosts['Items']) do
funcPoolCosts(courseItemCosts['Items'], item, amount)
poolItems(courseItemCosts['Items'], item, amount)
end
end
end
end
Line 198: Line 206:
    
    
     local courseItems = courseRequirements.CourseItemCosts
     local courseItems = courseRequirements.CourseItemCosts
     -- Put GP and SC at the top, and remove them from the list
     -- Put currencies at the top.
     -- to avoid sorting and re-adding them below.
     for _, curr in ipairs(currencies) do
    if courseItems['GP'] then ul:tag('li'):wikitext(getItemIcon('GP', courseItems['GP'])) end
    if courseItems[curr] then
     if courseItems['SC'] then ul:tag('li'):wikitext(getItemIcon('SC', courseItems['SC'])) end
    ul:tag('li'):wikitext(getItemIcon(curr, courseItems[curr]))
    end
     end


     local itemList = Shared.sortDictionary(courseItems['Items'],  
     local itemList = Shared.sortDictionary(courseItems['Items'],  
Line 259: Line 269:
local function getCosts(costsTable)
local function getCosts(costsTable)
local res = {}
local res = {}
   
-- Order table with GP, SC first, then the other items.
-- Order table with GP, SC first, then the other items.
     if costsTable['GP'] then table.insert(res, getItemIcon('GP', costsTable['GP'])) end
     for _, curr in ipairs(currencies) do
    if costsTable['SC'] then table.insert(res, getItemIcon('SC', costsTable['SC'])) end
    if costsTable[curr] then  
    table.insert(res, getItemIcon(curr, costsTable[curr]))
    end
    end    


     local sortedCosts = Shared.sortDictionary(costsTable['Items'],  
     local sortedCosts = Shared.sortDictionary(costsTable['Items'],  
Line 372: Line 385:
end
end


function p.test()
--local obstacles = {"Rope Climb","Monkey Bars"," Balance Seesaw","Elite Pillar of Conflict"}
--local obs = p.calculateCourse(obstacles)
mw.logObject(getObstacle('rope trap'))
end
return p
return p
2,875

edits