Module:Calculator/AgilityObstacle: Difference between revisions

Make costReduction parsing more robust
No edit summary
(Make costReduction parsing more robust)
(5 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 Num = require('Module:Number')
local Constants = require('Module:Constants')
local Constants = require('Module:Constants')
Line 147: Line 148:
function p._getCourseList(args)
function p._getCourseList(args)
-- Parse optional parameters
-- Parse optional parameters
local includeItems = args['includeitems'] or true
local includeSkills = args['includeskills'] or true
local includeObstacles = args['includeObstacles'] or true
local costReduction = {
local costReduction = {
['GP']  = args['gpCostReduction'] or 0,
['GP']  = Num.toNumberOrDefault(args['gpCostReduction'], 0),
['SC']  = args['scCostReduction'] or 0,
['SC']  = Num.toNumberOrDefault(args['scCostReduction'], 0),
['Item'] = args['itemCostReduction'] or 0,
['Item'] = Num.toNumberOrDefault(args['itemCostReduction'], 0),
}
}
Line 163: Line 160:
     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 185: 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')
Line 195: Line 192:
     if courseItems['SC'] then ul:tag('li'):wikitext(getItemIcon('SC', courseItems['SC'])) end
     if courseItems['SC'] then ul:tag('li'):wikitext(getItemIcon('SC', courseItems['SC'])) end


courseItems['GP'] = nil
     local itemList = Shared.sortDictionary(courseItems['Items'],  
courseItems['SC'] = nil
     local itemList = Shared.sortDictionary(courseItems,  
     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 208: 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 263: Line 257:


     local sortedCosts = Shared.sortDictionary(costsTable['Items'],  
     local sortedCosts = Shared.sortDictionary(costsTable['Items'],  
function(a, b) return a.item < b.amount 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 294: Line 288:


--== Parse optional parameters==--
--== Parse optional parameters==--
local showTotals = args['showtotals'] or true
local showTotals = yesno(args['showtotals'], true)
local obstacleMastery = args['obstacleMastery'] or false
local obstacleMastery = yesno(args['obstacleMastery'], false)
local costReduction = {
local costReduction = {
['GP']  = args['gpCostReduction'] or 0,
['GP']  = Num.toNumberOrDefault(args['gpCostReduction'], 0),
['SC']  = args['scCostReduction'] or 0,
['SC']  = Num.toNumberOrDefault(args['scCostReduction'], 0),
['Item'] = args['itemCostReduction'] or 0,
['Item'] = Num.toNumberOrDefault(args['itemCostReduction'], 0),
}
}
964

edits