2,875
edits
No edit summary |
m (Fix derp again) |
||
(46 intermediate revisions by 2 users 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') | ||
local Modifiers = require('Module:Modifiers') | |||
local Agility = require('Module:Skills/Agility') | local Agility = require('Module:Skills/Agility') | ||
local Shared = require('Module:Shared') | local Shared = require('Module:Shared') | ||
local Icons = require('Module:Icons') | local Icons = require('Module:Icons') | ||
local | |||
local iconTable = { | |||
['GP'] = function(amount) return Icons._Currency('GP', amount) end, | |||
['SC'] = function(amount) return Icons._Currency('SC', amount) end, | |||
['AP'] = function(amount) return Icons._Currency('AP', amount) end, | |||
['ASC'] = function(amount) return Icons._Currency('ASC', amount) end, | |||
} | |||
local function getItemIcon(itemName, amount) | local function getItemIcon(itemName, amount) | ||
if | local iconFunc = iconTable[itemName] | ||
if iconFunc then | |||
return iconFunc(amount) | |||
else | |||
return Icons.Icon({itemName, type='item', qty = amount, notext=true}) | |||
end | |||
end | |||
return Icons.Icon({ | local function getObstacleIcon(obstacle) | ||
local obs = obstacle.Obstacle | |||
return Icons.getExpansionIcon(obs.id) .. Icons.Icon({obs.name, type='agility'}) | |||
end | end | ||
-- 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) | ||
if obstacle == nil then | if obstacle == nil then | ||
error("Unknown Agility obstacle or pillar name: " .. name) | error("Unknown Agility obstacle or pillar name: " .. (name or '<name is nil>')) | ||
end | end | ||
Line 40: | Line 48: | ||
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 47: | Line 61: | ||
Obstacle = obstacle, | Obstacle = obstacle, | ||
LevelRequirements = Agility.getObstacleRequirements(obstacle), | LevelRequirements = Agility.getObstacleRequirements(obstacle), | ||
ItemCosts = | ItemCosts = itemCosts, | ||
} | } | ||
Line 53: | Line 67: | ||
end | end | ||
function | 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 59: | Line 73: | ||
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 70: | Line 84: | ||
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 _, | for _, obstacle in pairs(courseObstacles) do | ||
for skill, level in pairs( | -- 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 83: | Line 110: | ||
end | end | ||
for item, amount in pairs( | -- Pool together total item costs to build the entire course course. | ||
local obstacleCosts = obstacle.ItemCosts | |||
if obstacleCosts['GP'] then funcPoolCosts(courseItemCosts, 'GP', obstacleCosts['GP']) end | |||
if obstacleCosts['SC'] then funcPoolCosts(courseItemCosts, 'SC', obstacleCosts['SC']) end | |||
for item, amount in pairs(obstacleCosts['Items']) do | |||
funcPoolCosts(courseItemCosts['Items'], item, amount) | |||
end | end | ||
end | end | ||
Line 121: | Line 150: | ||
end | end | ||
function p.getCourseList( | function p.getCourseList(frame) | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
return p._getCourseList(args) | return p._getCourseList(args) | ||
Line 127: | Line 156: | ||
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 html = mw.html.create() | local html = mw.html.create() | ||
local div = html:tag('div') | local div = html:tag('div') | ||
if | if yesno(args['includeObstacles'], true) == true then | ||
div:tag(' | div:tag('b'):wikitext('Obstacles') | ||
local tbl = mw.html.create("table") | |||
:addClass("wikitable stickyHeader text-align-left") | |||
tbl :tag("tr") | |||
:tag("th"):wikitext("Slot") | |||
:tag("th"):wikitext("Obstacle") | |||
for _, v in pairs(courseRequirements.Obstacles) do | |||
tbl :tag('tr') | |||
:tag('td') | |||
:css('text-align', 'right') | |||
:wikitext(v.Slot) | |||
:tag('td') | |||
:wikitext(getObstacleIcon(v)) | |||
end | |||
div:node(tbl) | |||
div:wikitext('<br>') | |||
end | |||
if yesno(args['includeitems'], true) then | |||
div:tag('b'):wikitext('Items Required') | |||
local ul = div:tag('ul') | local ul = div:tag('ul') | ||
local itemList = Shared.sortDictionary( | 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) | ||
for _, v in pairs(itemList) do | for _, v in pairs(itemList) do | ||
ul:tag('li'):wikitext(getItemIcon(item, amount)) | ul:tag('li'):wikitext(getItemIcon(v.item, v.amount)) | ||
end | |||
div:wikitext('<br>') | |||
end | end | ||
div:tag(' | if yesno(args['includeskills'], true) then | ||
div:tag('b'):wikitext('Skills Required') | |||
local ul2 = div:tag('ul') | |||
local skillList = Shared.sortDictionary(courseRequirements.CourseLevelRequirements, | |||
function(a, b) return a.skill < b.skill end, | |||
function(a, b) return {skill = a, level = b} end) | |||
for _, v in pairs(skillList) do | |||
ul2:tag('li'):wikitext(Icons._SkillReq(v.skill, v.level)) | |||
end | |||
div:wikitext('<br>') | |||
end | |||
return tostring(html) | return tostring(html) | ||
end | end | ||
function p. | function p.getCourseTable(frame) | ||
local | local args = frame:getParent().args | ||
return p._getCourseTable(args) | |||
end | end | ||
function p._getCourseTable( | function p._getCourseTable(args) | ||
--== Local Functions for formatting Obstacle MetaData ==-- | |||
local function getBonusses(obstacle) | |||
if obstacle.modifiers == nil then | |||
return '<span style="color:red">None :(</span>' | |||
if | |||
else | else | ||
return Modifiers.getModifiersText(obstacle.modifiers, true, false, 10) | |||
end | end | ||
end | end | ||
local function getRequirements(requirementsTable) | |||
local skillList = Shared.sortDictionary(requirementsTable, | |||
function(a, b) return a.skill < b.skill end, | |||
function(a, b) return {skill = a, level = b} end) | |||
local res = {} | |||
for _, v in pairs(skillList) do | |||
table.insert(res, Icons._SkillReq(v.skill, v.level)) | |||
end | end | ||
return table.concat(res, '<br/>') | |||
end | end | ||
local function getCosts(costsTable) | |||
local res = {} | |||
-- Order table with GP, SC first, then the other items. | |||
if costsTable['GP'] then table.insert(res, getItemIcon('GP', costsTable['GP'])) end | |||
if costsTable['SC'] then table.insert(res, getItemIcon('SC', costsTable['SC'])) end | |||
local sortedCosts = Shared.sortDictionary(costsTable['Items'], | |||
function(a, b) return a.item < b.item end, | |||
function(a, b) return {item = a, amount = b} end) | |||
for _, v in pairs(sortedCosts) do | |||
table.insert(res, getItemIcon(v.item, v.amount)) | |||
end | |||
return table.concat(res, '<br/>') | |||
end | |||
local function getTotalBonuses(obstacles) | |||
--[==[ | |||
local bonuses = {} | |||
for _, obstacle in pairs(obstacles) do | |||
for bonusName, bonusValue in pairs(obstacle.Obstacle.modifiers) do | |||
table.insert(bonuses, {name = bonusName, value = bonusValue}) | |||
end | |||
end | end | ||
local | table.sort(bonuses, function(a, b) return a.name < b.name end) | ||
for | local ret = {} | ||
table.insert( | for _, bonus in pairs(bonuses) do | ||
table.insert(ret, Constants._getModifierText(bonus.name, bonus.value)) | |||
end | end | ||
if Shared.tableIsEmpty( | if Shared.tableIsEmpty(ret) then | ||
table.insert( | table.insert(ret, '<span style="color:red">None :(</span>') | ||
end | end | ||
return table.concat(ret, '<br/>') | |||
--]==] | |||
return '' | |||
end | |||
--== Parse optional parameters==-- | |||
local showTotals = yesno(args['showtotals'], false) | |||
local showbonus = yesno(args['showbonus'], true) | |||
local showrequirements = yesno(args['showrequirements'], true) | |||
local showcosts = yesno(args['showcosts'], 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 courseRequirements = p.calculateCourse(obstacleNames, true, costReduction) | |||
--== Start of table formatting ==-- | |||
local tbl = mw.html.create("table") | |||
:addClass("wikitable stickyheader") | |||
local thr = tbl:tag('tr') | |||
thr:tag('th'):wikitext('Slot') | |||
thr:tag('th'):wikitext('Obstacle') | |||
if showbonus then | |||
thr:tag('th'):wikitext('Bonuses') | |||
end | |||
if showrequirements then | |||
thr:tag('th'):wikitext('Requirements') | |||
end | |||
if showcosts then | |||
thr:tag('th'):wikitext('Costs') | |||
end | end | ||
for _, obstacle in pairs(courseRequirements.Obstacles) do | |||
local tr = tbl:tag('tr') | |||
tr :tag('td') | |||
:css('text-align', 'right') | |||
:wikitext(obstacle.Slot) | |||
:tag('td'):wikitext(getObstacleIcon(obstacle)) | |||
if showbonus then | |||
tr:tag('td'):wikitext(getBonusses(obstacle.Obstacle)) | |||
end | |||
if showrequirements then | |||
tr:tag('td'):wikitext(getRequirements(obstacle.LevelRequirements)) | |||
end | |||
if showcosts then | |||
tr:tag('td'):wikitext(getCosts(obstacle.ItemCosts)) | |||
end | |||
end | |||
if showTotals == true then | |||
local tr = tbl:tag('tr') | |||
tr :tag('th') | |||
:attr('colspan', 2) | |||
:wikitext('Totals') | |||
if showbonus then | |||
tr :tag('td') | |||
:wikitext(getTotalBonuses(courseRequirements.Obstacles)) | |||
end | |||
if showrequirements then | |||
tr :tag('td') | |||
:wikitext(getRequirements(courseRequirements.CourseLevelRequirements)) | |||
end | |||
if showcosts then | |||
tr :tag('td') | |||
:wikitext(getCosts(courseRequirements.CourseItemCosts)) | |||
end | |||
end | |||
return | return tostring(tbl) | ||
end | end | ||
function p. | function p.test() | ||
local | --local obstacles = {"Rope Climb","Monkey Bars"," Balance Seesaw","Elite Pillar of Conflict"} | ||
local | --local obs = p.calculateCourse(obstacles) | ||
mw.logObject(getObstacle('rope trap')) | |||
end | end | ||
return p | return p |
edits