Module:Arceuus library book calculator

From Old School RuneScape Wiki
Jump to: navigation, search
Module documentation
This documentation is transcluded from Module:Arceuus library book calculator/doc. [edit] [purge]
Module:Arceuus library book calculator's function invoke_main is invoked by Calculator:Arceuus library book/Template.
Module:Arceuus library book calculator requires Module:Addcommas.
Module:Arceuus library book calculator requires Module:Experience.
Module:Arceuus library book calculator requires Module:Paramtest.
Module:Arceuus library book calculator requires Module:ScaledExperience.
Module:Arceuus library book calculator requires Module:Yesno.
Module:Arceuus library book calculator is invoked by Calculator:Arceuus_library_book/Template.
Function Type Use
invoke_main(frame) table The main function to run if invoked from a template that is the results template for a calculator.

Valid arguments from the calculator: skill = 'Magic/Runecraft', startingMagicXP = 0 - 200000000, startingRunecraftXP = 0 - 200000000, targetType = 'Level/XP, target = 0 - 127, targetXP = 0 - 200000000, twistedLeague = 'yes/no', tlTier2 = 'None/Gift of the Gatherer/Way of the Warrior', tlTier5 = 'None/Xerics Wisdom'

main(arg) table Figures out the Level multiplier and the number of books needed to reach a target level or target XP.

Valid arguments: { skill = 'Magic/Runecraft', startingMagicXP = 0 - 200000000, startingRunecraftXP = 0 - 200000000, targetType = 'Level/XP, target = 0 - 127, targetXP = 0 - 200000000, twistedLeague = 'yes/no', tlTier2 = 'None/Gift of the Gatherer/Way of the Warrior', tlTier5 = 'None/Xerics Wisdom' }

getLevelMultiplier(args) table Returns the level multiplier for the provided Skill with the additional modifiers.

Valid arguments: { skill = 'Magic/Runecraft', twistedLeague = 'yes/no', tlRelicGatherer = 'yes/no', tlTier2 = 'None/Gift of the Gatherer/Way of the Warrior', tlTier5 = 'None/Xerics Wisdom' }

calculateBooksToLevel(startingXP, targetXP, levelMultiplier) String, Number, Number, Number, Number Calculates the number of books needed to reach a targetXP, with the provided startingXP and levelMultiplier.
renderResultsTable(results) table Renders the results of calculateBooksToLevel() to a table.

Valid arguments: { books = number, startingXP = number, endXP = number, earnedXP = number, startingLevel = number, endLevel = number, earnedLevels = number }


local Addcommas = require( 'Module:Addcommas' )
local Paramtest = require( 'Module:Paramtest' )
local Experience = require( 'Module:Experience' )
local ScaledExperience = require( 'Module:ScaledExperience' )
local Yesno = require( 'Module:Yesno' )

local p = {}

--
-- The main function to run if invoked from another page.
-- Example: {{#invoke:Arceuus library book calculator|invoke_main}}
--
function p.invoke_main( frame )
	local results = p.main( frame:getParent().args )
	
	return p.renderResultsTable( results )
end

--
-- The main function to run if being used by another module.
--
function p.main( args )	
	-- Extract all parameters and set their defauls if needed.
	local skill, startingMagicXP, startingRunecraftXP, targetType, target, targetXP, twistedLeague, tlTier2, tlTier5 = Paramtest.defaults{
		{ args.skill, 'Magic' },
		{ args.startingMagicXP, 0 },
		{ args.startingRunecraftXP, 0 },
		{ args.targetType, 'Level' },
		{ args.target, 0 },
		{ args.targetXP, 0 },
		{ args.twistedLeague, 'no' },
		{ args.tlTier2, 'None' },
		{ args.tlTier5, 'None' }
	}
	
	-- Make sure targetType is either 'Level' or 'XP'.
	if not (targetType == 'Level' or targetType == 'XP') then
		-- Just default to Level if not one of the valid types.
		targetType = 'Level'
	end
	
	-- Convert stringified numbers to numbers.
	startingMagicXP = tonumber( startingMagicXP )
	startingRunecraftXP = tonumber( startingRunecraftXP )
	target = tonumber( target )
	targetXP = tonumber( targetXP )
	
	local startingXP = 0
	
	if skill == 'Magic' then
		startingXP = startingMagicXP
	else
		startingXP = startingRunecraftXP
	end
	
	local levelMultiplier = p.getLevelMultiplier( {
		skill = skill,
		twistedLeague = twistedLeague,
		tlTier2 = tlTier2,
		tlTier5 = tlTier5
	} )
	if (targetType == 'XP') then
		return p.calculateBooksToLevel( startingXP, targetXP, levelMultiplier )
	else
		return p.calculateBooksToLevel( startingXP, Experience.xp_at_level_unr( { args = { target } } ), levelMultiplier )
	end
end

--
-- Calculates the book of arcane knowledge level multiplier.
--
function p.getLevelMultiplier( args )
	-- Extract all parameters and set their defauls if needed.
	local skill, twistedLeague, tlTier2, tlTier5 = Paramtest.defaults{
		{ args.skill, 'Magic' },
		{ args.twistedLeague, 'no' },
		{ args.tlTier2, 'None' },
		{ args.tlTier5, 'None' }
	}
	
	-- Convert yes/no to boolean.
	-- Note: I don't like that this function accepts yes/no instead of
	-- true/false, but Paramtest.defaults doesn't like booleans.
	twistedLeague = Yesno( twistedLeague, false )
	
	 -- Make sure the Twisted League Relic params are valid
	 if not (tlTier2 == 'Gift of the Gatherer' or tlTier2 == 'Way of the Warrior') then
	 	tlTier2 = 'None'
 	end
	 if not (tlTier5 == 'Xerics Wisdom') then
	 	tlTier5 = 'None'
 	end
	
	local levelMultiplier = 0
	local isMagic = skill == 'Magic'
	local isRunecraft = skill == 'Runecraft'
	
	if isMagic then
		levelMultiplier = 11
	elseif isRunecraft then
		levelMultiplier = 4
	end
	
	if twistedLeague then
		levelMultiplier = levelMultiplier * 5;
		
		if tlTier2 == 'Gift of the Gatherer' and isRunecraft then
			levelMultiplier = levelMultiplier * 2;
		end
		
		if tlTier2 == 'Way of the Warrior' and isMagic then
			levelMultiplier = levelMultiplier * 2;
		end
		
		if tlTier5 == 'Xerics Wisdom' then
			levelMultiplier = levelMultiplier * 2;
		end
	end
	
	return levelMultiplier
end

--
-- Calculates the books needed to reach a level with the provided levelMultiplier.
-- @TODO: extract duplicate code that calculates number of books to xp diff.
--
function p.calculateBooksToLevel( startingXP, targetXP, levelMultiplier )
	
	local actions = ScaledExperience.xp_to_xp_actions(startingXP, targetXP, levelMultiplier)
	
	local results = {
		books = actions.endActions,
		startingXP = actions.startingExp,
		endXP = actions.endExp,
		earnedXP = actions.endExp - actions.startingExp,
		startingLevel = actions.startingLevel,
		endLevel = actions.endLevel,
		earnedLevels = actions.endLevel - actions.startingLevel
	}
	
	return results
end

--
-- Renders the results in a table.
--
function p.renderResultsTable( results )
	local resultsTable = mw.html.create( 'div' )
	
	resultsTable:tag( 'table' )
		:addClass( 'wikitable' )
		:addClass( 'align-center-1' )
		:tag( 'tr' )
			:tag( 'th' )
				:wikitext( 'Results' )
				:attr( 'colspan', 5 )
			:done()
		:done()
		:tag( 'tr' )
			:tag( 'td' )
				:wikitext( '[[File:Book_of_arcane_knowledge.png|link=Book_of_arcane_knowledge]]' )
			:done()
			:tag( 'td' )
				:wikitext( 'Books needed' )
				:attr( 'colspan', 3 )
			:done()
			:tag( 'td' )
				:wikitext( Addcommas._add( results['books'] ) )
			:done()
		:done()
		:tag( 'tr' )
			:tag( 'th' )
				:wikitext( ' ' )
				:attr( 'colspan', 2 )
			:done()
			:tag( 'th' )
				:wikitext( 'Start' )
			:done()
			:tag( 'th' )
				:wikitext( 'End' )
			:done()
			:tag( 'th' )
				:wikitext( 'Earned' )
			:done()
		:done()
		:tag( 'tr' )
			:tag( 'td' )
				:wikitext( '[[File:Stats_icon.png|link=Experience]]' )
			:done()
			:tag( 'td' )
				:wikitext( 'Experience' )
			:done()
			:tag( 'td' )
				:wikitext( Addcommas._add( results['startingXP'] ) )
			:done()
			:tag( 'td' )
				:wikitext( Addcommas._add( results['endXP'] ) )
			:done()
			:tag( 'td' )
				:wikitext(Addcommas._add( results['earnedXP'] ) )
			:done()
		:done()
		:tag( 'tr' )
			:tag( 'td' )
				:wikitext( '[[File:Stats_icon.png|link=Experience]]' )
			:done()
			:tag( 'td' )
				:wikitext( 'Level' )
			:done()
			:tag( 'td' )
				:wikitext( results['startingLevel'] )
			:done()
			:tag('td')
				:wikitext( results['endLevel'] )
			:done()
			:tag('td')
				:wikitext( results['earnedLevels'] )
			:done()
		:done()
		:tag( 'tr' )
			:tag( 'th' )
				:wikitext( ' ' )
				:attr( 'colspan', 5 )
			:done()
		:done()
	:done()
	
	return resultsTable
end

return p