Module:Calculator/AgilityObstacle: Difference between revisions

From Melvor Idle
No edit summary
No edit summary
(44 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


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')
local Shared = require('Module:Shared')
local Shared = require('Module:Shared')
local Skills = require('Module:Skills')
local Items = require('Module:Items')
local Icons = require('Module:Icons')
local Icons = require('Module:Icons')
local Debug = require('Module:Debug') -- Comment out when Module is finalised.
local Debug = require('Module:Debug') -- Comment out when Module is finalised.


-- Adds the the highest level to the dictionary, or the level, if the key isn't present.
local function getItemIcon(itemName, amount)
local function addHighestLevelRequirement(tbl, skill, level)
if itemName == 'GP' then
Shared.addOrUpdate(tbl, skill,  
return Icons.GP(amount)
function(x)
end
if x then
return math.max(x, level)
if itemName == 'SC' then
else
return Icons.SC(amount)
return level
end
return Icons.Icon({itemName, type='item', qty = amount, notext=true})
end
 
-- Gets all associated metadata from an obstacle based on its name.
local function getObstacle(name)
name = Shared.specialTitleCase(name)
local obstacle = Agility.getObstacle(name) or Agility.getPillar(name)
if obstacle == nil then
error("Unknown Agility obstacle or pillar name: " .. name)
end
 
-- Resolve obstacle slot.
local slot = 0
if string.find(name, 'Pillar') then
if string.find(name, 'Elite') then
slot = 'Elite Pillar'
else
slot = 'Pillar'
end
else
slot = obstacle.category + 1
end
local obstacleInfo = {
Name = obstacle.name,
Slot = slot,
Obstacle = obstacle,
LevelRequirements = Agility.getObstacleRequirements(obstacle),
ItemCosts = Agility.getObstacleCosts(obstacle),
}
 
return obstacleInfo
end
 
function p.calculateCourse(obstacleNames, checkDoubleSlots)
-- Collect all obstacles and filter out nill values.
local courseObstacles = {}
local courseSlots = {}
for _, v in pairs(obstacleNames) do
local currObstacle = getObstacle(v)
if currObstacle then
if checkDoubleSlots and courseSlots[currObstacle.Slot] == true then
error('There are two or more obstacles in the same slot. Obstacle: ' .. currObstacle.Name .. ' Slot: ' .. currObstacle.Slot)
end
end
courseSlots[currObstacle.Slot] = true
table.insert(courseObstacles, currObstacle)
end
end
)
end
-- Calculate the highest level requirements and the total amount of items
-- required to build this agility course.
local courseLevelRequirements = {}
local courseItemCosts = {}
for _, course in pairs(courseObstacles) do
for skill, level in pairs(course.LevelRequirements) do
Shared.addOrUpdate(courseLevelRequirements, skill,
function(x)
if x then return math.max(x, level) else return level end
end)
end
for item, amount in pairs(course.ItemCosts) do
Shared.addOrUpdate(courseItemCosts, item,
function(x)
x = x or 0 return x + amount
end)
end
end
 
-- Sort course on category
local sortFunc = function(a, b)
-- Special case to sort pillars AFTER all regular obstacles.
    local pillar = { ["Pillar"] = 99, ["Elite Pillar"] = 100 }
return (pillar[a.Slot] or a.Slot) < (pillar[b.Slot] or b.Slot)
end
table.sort(courseObstacles, sortFunc)
return {
Obstacles = courseObstacles,
CourseLevelRequirements = courseLevelRequirements,
CourseItemCosts = courseItemCosts
}
end
end


-- Adds the item amount to the dictionary, or just the amount if the key isn't present.
-- Stupid shenanigans to filter out numeric parameters.
local function concatItemRequirements(tbl, item, amount)
-- Because just iterating over #args doesn't work for some reason...
Shared.addOrUpdate(tbl, item,
function parseObstacleArgs(args)
function(x)
local obstacleNames = {}
x = x or 0
for k, v in pairs(args) do
return x + amount
k = tonumber(k)
if k then
table.insert(obstacleNames, v:match("^%s*(.-)%s*$"))
end
end
)
end
return obstacleNames
end
 
function p.getCourseList(frame)
local args = frame:getParent().args
return p._getCourseList(args)
end
end


--- Gets all required levels to build the given obstacle.
function p._getCourseList(args)
local function getLevelRequirements(obstacle)
local obstacleNames = parseObstacleArgs(args)
local levelRequirements = {}
local courseRequirements = p.calculateCourse(obstacleNames, true)
-- Add agility level requirement.
local includeItems = args['includeitems'] or true
addHighestLevelRequirement(levelRequirements, 'Agility', Skills.getRecipeLevel('Agility', obstacle))
local includeSkills = args['includeskills'] or true
local includeObstacles = args['includeObstacles'] or true
local html = mw.html.create()
    local div = html:tag('div')
   
    if includeObstacles then
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(Icons.Icon({v.Name, type='agility'}))
end
div:node(tbl)
div:wikitext('<br>')
    end
 
    if includeItems then
    div:tag('b'):wikitext('Items Required')
    local ul = div:tag('ul')
   
    local itemList = Shared.sortDictionary(courseRequirements.CourseItemCosts,
    function(a, b) return a.item < b.item end,
    function(a, b) return {item = a, amount = b} end)


-- Add other level requirements.
for _, v in pairs(itemList) do
if type(obstacle.skillRequirements) == 'table' then
    ul:tag('li'):wikitext(getItemIcon(v.item, v.amount))
for i, skillReq in ipairs(obstacle.skillRequirements) do
local skillName = Constants.getSkillName(skillReq.skillID)
if skillName ~= nil then
addHighestLevelRequirement(levelRequirements, skillName, skillReq.level)
end
end
end
div:wikitext('<br>')
    end
if includeSkills 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
end
return levelRequirements
    return tostring(html)
end
end


local function getItemRequirements(obstacle)
function p.getCourseTable(frame)
local itemRequirements = {}
local args = frame:getParent().args
return p._getCourseTable(args)
end


if obstacle.gpCost > 0 then
function p._getCourseTable(args)
concatItemRequirements(itemRequirements, 'GP', obstacle.gpCost)
--== Local Functions for formatting Obstacle MetaData ==--
local function getBonusses(obstacle)
local bonuses = {}
for bonusName, bonusValue in pairs(obstacle.modifiers) do
table.insert(bonuses, Constants._getModifierText(bonusName, bonusValue))
end
if Shared.tableIsEmpty(bonuses) then
table.insert(bonuses, '<span style="color:red">None :(</span>')
end
return table.concat(bonuses, '<br/>')
end
end
if obstacle.scCost > 0 then
concatItemRequirements(itemRequirements, 'GP', obstacle.scCost)
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
return table.concat(res, '<br/>')
end
end
for j, itemCost in ipairs(obstacle.itemCosts) do
local function getCosts(costsTable)
local item = Items.getItemByID(itemCost.id)
-- Order table with GP, SC first, then the other items.
concatItemRequirements(itemRequirements, item.name, itemCost.quantity)
    local sortedCosts = {}
    if costsTable['GP'] then table.insert(sortedCosts, {item = 'GP', amount = costsTable['GP']}) end
    if costsTable['SC'] then table.insert(sortedCosts, {item = 'SC', amount = costsTable['SC']}) end
   
    for k, v in pairs(costsTable) do
    if k ~= 'GP' and k ~= 'SC' then
    table.insert(sortedCosts, {item = k, amount = v})
    end
    end
   
    local res = {}
    for _, v in pairs(sortedCosts) do
    table.insert(res, getItemIcon(v.item, v.amount))
    end
return table.concat(res, '<br/>')
end
end
return itemRequirements
local function getTotalBonuses(obstacles)
end
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


local function getObstacle(name)
table.sort(bonuses, function(a, b) return a.name < b.name end)
name = Shared.specialTitleCase(name)
local obstacle = Agility.getObstacle(name)
local ret = {}
if obstacle == nil then
for _, bonus in pairs(bonuses) do
return nil
table.insert(ret, Constants._getModifierText(bonus.name, bonus.value))
end
if Shared.tableIsEmpty(ret) then
table.insert(ret, '<span style="color:red">None :(</span>')
end
return table.concat(ret, '<br/>')
end
end
-- TODO: Handle pillars and return the same format/table.
-- if obstacle == pillar.....
local obstacleInfo = {
Name = obstacle.name,
Slot = obstacle.category + 1,
LevelRequirements = getLevelRequirements(obstacle),
ItemCosts = getItemRequirements(obstacle),
}


return obstacleInfo
--== Start of table formatting ==--
end
local obstacleNames = parseObstacleArgs(args)
local courseRequirements = p.calculateCourse(obstacleNames, true)
 
-- Todo: Add cost reduction?
local showTotals = args['showtotals'] or true
local obstacleMastery = args['obstacleMastry'] or false


function p.main(frame)
local tbl = mw.html.create("table")
local args = frame:getParent().args
      :addClass("wikitable stickyHeader")
return p._main(args)
     
    tbl :tag('tr')
:tag('th'):wikitext('Slot')
:tag('th'):wikitext('Obstacle')
:tag('th'):wikitext('Bonuses')
:tag('th'):wikitext('Requirements')
:tag('th'):wikitext('Costs')


end
for _, obstacle in pairs(courseRequirements.Obstacles) do
tbl :tag('tr')
:tag('td')
:css('text-align', 'right')
:wikitext(obstacle.Slot)
:tag('td'):wikitext(Icons.Icon({obstacle.Name, type='agility'}))
:tag('td'):wikitext(getBonusses(obstacle.Obstacle))
:tag('td'):wikitext(getRequirements(obstacle.LevelRequirements))
:tag('td'):wikitext(getCosts(obstacle.ItemCosts))
end
if showTotals == true then
tbl :tag('tr')
:tag('th')
:attr('colspan', 2)
:wikitext('Totals')
:tag('td')
:wikitext(getTotalBonuses(courseRequirements.Obstacles))
:tag('td')
:wikitext(getRequirements(courseRequirements.CourseLevelRequirements))
:tag('td')
:wikitext(getCosts(courseRequirements.CourseItemCosts))
end


function p._main(args)
return tostring(tbl)
return Debug.toString(args)
end
end


function p.test()
function p.test()
Debug.log(getObstacle("a lovely jog"))
local obstacles = {"Rope Climb","Monkey Bars"," Balance Seesaw","Elite Pillar of Conflict"}
local obs = p._getCourseTable(obstacles)
 
 
Debug.log(obs)
end
end
return p
return p

Revision as of 22:34, 22 April 2024

Documentation for this module may be created at Module:Calculator/AgilityObstacle/doc

local p = {}

local Constants = require('Module:Constants')
local Agility = require('Module:Skills/Agility')
local Shared = require('Module:Shared')
local Icons = require('Module:Icons')
local Debug = require('Module:Debug') -- Comment out when Module is finalised.

local function getItemIcon(itemName, amount)
	if itemName == 'GP' then
		return Icons.GP(amount)
	end
	
	if itemName == 'SC' then
		return Icons.SC(amount)
	end
	
	return Icons.Icon({itemName, type='item', qty = amount, notext=true})
end

-- Gets all associated metadata from an obstacle based on its name.
local function getObstacle(name)
	name = Shared.specialTitleCase(name)
	local obstacle = Agility.getObstacle(name) or Agility.getPillar(name)
	if obstacle == nil then
		error("Unknown Agility obstacle or pillar name: " .. name)
	end

	-- Resolve obstacle slot.
	local slot = 0
	if string.find(name, 'Pillar') then
		if string.find(name, 'Elite') then
			slot = 'Elite Pillar'
		else
			slot = 'Pillar'
		end
	else
		slot = obstacle.category + 1
	end
	
	local obstacleInfo = {
		Name = obstacle.name,
		Slot = slot,
		Obstacle = obstacle,
		LevelRequirements = Agility.getObstacleRequirements(obstacle),
		ItemCosts = Agility.getObstacleCosts(obstacle),
	}

	return obstacleInfo
end

function p.calculateCourse(obstacleNames, checkDoubleSlots)
	-- Collect all obstacles and filter out nill values.
	local courseObstacles = {}
	local courseSlots = {}
	
	for _, v in pairs(obstacleNames) do
		local currObstacle = getObstacle(v)
		if currObstacle then
			if checkDoubleSlots and courseSlots[currObstacle.Slot] == true then
				error('There are two or more obstacles in the same slot. Obstacle: ' .. currObstacle.Name .. ' Slot: ' .. currObstacle.Slot)
			end
			
			courseSlots[currObstacle.Slot] = true
			table.insert(courseObstacles, currObstacle) 
		end
	end
	
	-- Calculate the highest level requirements and the total amount of items
	-- required to build this agility course.
	local courseLevelRequirements = {}
	local courseItemCosts = {}
	
	for _, course in pairs(courseObstacles) do
		for skill, level in pairs(course.LevelRequirements) do
				Shared.addOrUpdate(courseLevelRequirements, skill, 
					function(x)	
						if x then return math.max(x, level)	else return level end 
					end)
		end
		
		for item, amount in pairs(course.ItemCosts) do
				Shared.addOrUpdate(courseItemCosts, item, 
					function(x)
						x = x or 0 return x + amount
					end)
		end
	end

	-- Sort course on category
	local sortFunc = function(a, b)
		-- Special case to sort pillars AFTER all regular obstacles.
    	local pillar = { ["Pillar"] = 99, ["Elite Pillar"] = 100 }
		return (pillar[a.Slot] or a.Slot) < (pillar[b.Slot] or b.Slot)
	end
	
	table.sort(courseObstacles, sortFunc)
	
	return {
		Obstacles = courseObstacles,
		CourseLevelRequirements = courseLevelRequirements,
		CourseItemCosts = courseItemCosts
	}
end

-- Stupid shenanigans to filter out numeric parameters.
-- Because just iterating over #args doesn't work for some reason...
function parseObstacleArgs(args)
	local obstacleNames = {}
	for k, v in pairs(args) do
		k = tonumber(k)
		if k then 
			table.insert(obstacleNames, v:match("^%s*(.-)%s*$")) 
		end
	end
	
	return obstacleNames
end

function p.getCourseList(frame)
	local args = frame:getParent().args
	return p._getCourseList(args)
end

function p._getCourseList(args)
	local obstacleNames = parseObstacleArgs(args)
	local courseRequirements = p.calculateCourse(obstacleNames, true)
	
	local includeItems = args['includeitems'] or true
	local includeSkills = args['includeskills'] or true
	local includeObstacles = args['includeObstacles'] or true
	
	local html = mw.html.create()
    local div = html:tag('div')
    
    if includeObstacles then
		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(Icons.Icon({v.Name, type='agility'}))
		end
		
		div:node(tbl)
		div:wikitext('<br>')
    end

    if includeItems then
    	div:tag('b'):wikitext('Items Required')
    	local ul = div:tag('ul')
    	
    	local itemList = Shared.sortDictionary(courseRequirements.CourseItemCosts, 
    		function(a, b) return a.item < b.item end,
    		function(a, b) return {item = a, amount = b} end)

		for _, v in pairs(itemList) do
    		ul:tag('li'):wikitext(getItemIcon(v.item, v.amount))
		end
		div:wikitext('<br>')
    end
	
	if includeSkills 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)
end

function p.getCourseTable(frame)
	local args = frame:getParent().args
	return p._getCourseTable(args)
end

function p._getCourseTable(args)
	--== Local Functions for formatting Obstacle MetaData ==--
	local function getBonusses(obstacle)
		local bonuses = {}
		for bonusName, bonusValue in pairs(obstacle.modifiers) do
			table.insert(bonuses, Constants._getModifierText(bonusName, bonusValue))
		end
		if Shared.tableIsEmpty(bonuses) then
			table.insert(bonuses, '<span style="color:red">None :(</span>')
		end
		return table.concat(bonuses, '<br/>')
	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
		return table.concat(res, '<br/>')
	end
	
	local function getCosts(costsTable)
		-- Order table with GP, SC first, then the other items.
	    local sortedCosts = {}
    	if costsTable['GP'] then table.insert(sortedCosts, {item = 'GP', amount = costsTable['GP']}) end
    	if costsTable['SC'] then table.insert(sortedCosts, {item = 'SC', amount = costsTable['SC']}) end
    	
    	for k, v in pairs(costsTable) do
    		if k ~= 'GP' and k ~= 'SC' then
    			table.insert(sortedCosts, {item = k, amount = v})
    		end
    	end
    	
    	local res = {}
    	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

		table.sort(bonuses, function(a, b) return a.name < b.name end)
		
		local ret = {}
		for _, bonus in pairs(bonuses) do
			table.insert(ret, Constants._getModifierText(bonus.name, bonus.value))
		end
		if Shared.tableIsEmpty(ret) then
			table.insert(ret, '<span style="color:red">None :(</span>')
		end
		
		return table.concat(ret, '<br/>')
	end

	--== Start of table formatting ==--
	local obstacleNames = parseObstacleArgs(args)
	local courseRequirements = p.calculateCourse(obstacleNames, true)

	-- Todo: Add cost reduction?
	local showTotals = args['showtotals'] or true
	local obstacleMastery = args['obstacleMastry'] or false

	local tbl = mw.html.create("table")
       	:addClass("wikitable stickyHeader")
       	
    tbl :tag('tr')
			:tag('th'):wikitext('Slot')
			:tag('th'):wikitext('Obstacle')
			:tag('th'):wikitext('Bonuses')
			:tag('th'):wikitext('Requirements')
			:tag('th'):wikitext('Costs')

	for _, obstacle in pairs(courseRequirements.Obstacles) do
		tbl :tag('tr')
				:tag('td')
					:css('text-align', 'right')
					:wikitext(obstacle.Slot)
				:tag('td'):wikitext(Icons.Icon({obstacle.Name, type='agility'}))
				:tag('td'):wikitext(getBonusses(obstacle.Obstacle))
				:tag('td'):wikitext(getRequirements(obstacle.LevelRequirements))
				:tag('td'):wikitext(getCosts(obstacle.ItemCosts))
	end
	
	if showTotals == true then
		tbl :tag('tr')
				:tag('th')
					:attr('colspan', 2)
					:wikitext('Totals')
				:tag('td')
					:wikitext(getTotalBonuses(courseRequirements.Obstacles))
				:tag('td')
					:wikitext(getRequirements(courseRequirements.CourseLevelRequirements))
				:tag('td')
					:wikitext(getCosts(courseRequirements.CourseItemCosts))
	end

	return tostring(tbl)
end

function p.test()
	local obstacles = {"Rope Climb","Monkey Bars"," Balance Seesaw","Elite Pillar of Conflict"}
	local obs = p._getCourseTable(obstacles)


	Debug.log(obs)
end
return p