Module:Experience

This is the current revision of this page, as edited by Alex (talk | contribs) at 00:24, 16 October 2024 (Created page with "local p = {} local commas_strip = require('Module:Addcommas')._strip local XPAtLevel = mw.loadData('Module:Experience/data') local XPAtLevelUnr = mw.loadData('Module:Experience/dataunr') -- -- Returns lowest needed XP to have required level -- Template:XP -- function p._xp_at_level(level) level = tonumber(level) -- calling tonumber on a string returns nil if not level then return "You must pass numbers as arguments" end -- Level range is 1 to 99 if level..."). The present address (URL) is a permanent link to this version.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Module documentation
This documentation is transcluded from Template:No documentation/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Experience/doc. [edit]
Module:Experience requires Module:Addcommas.
Module:Experience loads data from Module:Experience/data.
Module:Experience loads data from Module:Experience/dataunr.
Module:Experience is required by Module:Arceuus library book calculator.
Module:Experience is required by Module:Sandbox/User:Jakesterwars/Skill calc/Helpers.
Module:Experience is required by Module:ScaledExperience.
Module:Experience is required by Module:Skill calc/Helpers.
Module:Experience is required by Module:User hiscores.

local p = {}
local commas_strip = require('Module:Addcommas')._strip
local XPAtLevel = mw.loadData('Module:Experience/data')
local XPAtLevelUnr = mw.loadData('Module:Experience/dataunr')

--
-- Returns lowest needed XP to have required level
-- [[Template:XP]]
-- 
function p._xp_at_level(level)
	level = tonumber(level)

	-- calling tonumber on a string returns nil
	if not level then
		 return "You must pass numbers as arguments"
	end

	-- Level range is 1 to 99
	if level < 0 or level > 99 then
		return "Value must be in the range 1 - 99"
	else
		return XPAtLevel[level]
	end
end

function p.xp_at_level(frame)
	return p._xp_at_level(frame.args[1])
end

-- Same as above, but with the 99 level cap removed
function p._xp_at_level_unr(level)
	level = tonumber(level)

	-- calling tonumber on a string returns nil
	if not level then
		 return "You must pass numbers as arguments"
	end

	-- Virtual level range is 1 to 126
	if level < 0 or level > 126 then
		return "Value must be in the range 1 - 126"
	else
		return XPAtLevelUnr[level]
	end
end

function p.xp_at_level_unr(frame)
	return p._xp_at_level_unr(frame.args[1])
end

--
-- The maximum amount of experience you can have at a given level
-- [[Template:Max XP for level]]
--
function p._max_xp_at_level(level)
	level = tonumber(level)

	-- calling tonumber on a string returns nil
	if not level then
		 return "You must pass numbers as arguments"
	end
	
	-- Just in case
	level = math.floor(level)

	if level == -1 then
		return 0
	elseif level <= 0 or level > 99 then
		return "Value must be in the range 1 - 99"
	-- Highest level is 99, highest XP is 200M
	elseif level == 99 then
		return 200000000
	end

	-- Return XP required for the *next* level minus 1
	local xp_at_next = XPAtLevel[level + 1]
	return xp_at_next - 1
end

function p.max_xp_at_level(frame)
	return p._max_xp_at_level(frame.args[1])
end

--
-- What level you have when at a given xp
-- [[Template:Level]]
--
function p._level_at_xp(xp)
	if type(xp) == 'string' then
		 xp = commas_strip(xp)
	end
	xp = tonumber(xp)

	-- calling tonumber on an empty string returns nil
	if not xp then
		 return "You must pass numbers as arguments"
	elseif xp < 0 or xp > 200000000 then
		return "Value must be in the range 0 - 200,000,000"
	end
	
	for i = 99, 1, -1 do
		if XPAtLevel[i] <= xp then
			return i
		end
	end
end

function p.level_at_xp(frame)
	return p._level_at_xp(frame.args[1])
end

-- Same as above, but with the 99 level cap removed
function p._level_at_xp_unr(xp)
	if type(xp) == 'string' then
		 xp = commas_strip(xp)
	end
	xp = tonumber(xp)

	-- calling tonumber on a string returns nil
	if not xp then
		 return "You must pass numbers as arguments"
	end

	if xp == -1 then
		return 0
	elseif xp < 0 or xp > 200000000 then
		return "Value must be in the range 0 - 200,000,000"
	end
	
	for i = 126, 1, -1 do
		if XPAtLevelUnr[i] <= xp then
			return i
		end
	end
end

function p.level_at_xp_unr(frame)
	return p._level_at_xp_unr(frame.args[1])
end

--
-- Experience needed to obtain a level from a given amount of xp
-- If current xp is greater than target, returns 0
-- [[Template:XP to Level]]
--
function p._xp_to_level(xp, target)
	target = tonumber(target)

	if type(xp) == 'string' then
		 xp = commas_strip(xp)
	end
	xp = tonumber(xp)
	
	-- calling tonumber on a string returns nil
	if not xp or not target then
		 return "You must pass numbers as arguments"
	elseif xp < 0 or xp > 200000000 then
		return "XP value must be in the range 0 - 200,000,000"
	elseif target < 2 or target > 99 then
		return "Level value must be in the range 2 - 99"
	end

	local target_xp = XPAtLevel[target]

	if xp >= target_xp then
		return 0
	else
		return target_xp - xp
	end
end

function p.xp_to_level(frame)
	return p._xp_to_level(frame.args[1], frame.args[2])
end

-- Same as above, but with the 99 level cap removed
function p._xp_to_level_unr(xp, target)
	target = tonumber(target)

	if type(xp) == 'string' then
		 xp = commas_strip(xp)
	end
	xp = tonumber(xp)
	
	-- calling tonumber on a string returns nil
	if not xp or not target then
		 return "You must pass numbers as arguments"
	elseif xp < 0 or xp > 200000000 then
		return "XP value must be in the range 0 - 200,000,000"
	elseif target < 2 or target > 126 then
		return "Level value must be in the range 2 - 99"
	end

	local target_xp = XPAtLevelUnr[target]

	if xp >= target_xp then
		return 0
	else
		return target_xp - xp
	end
end

function p.xp_to_level_unr(frame)
	return p._xp_to_level_unr(frame.args[1], frame.args[2])
end

--
-- Experience between two levels; will always return absolute value
-- [[Template:XP level to level]]
--
function p._level_to_level(level1, level2)
	if type(level1) == 'string' then
		 level1 = commas_strip(level1)
	end
	level1 = tonumber(level1)

	if type(level2) == 'string' then
		 level2 = commas_strip(level2)
	end
	level2 = tonumber(level2)

	if not level1 or not level2 then
		 return "You must pass numbers as arguments"
	elseif level1 < 1 or level1 > 99 or level2 < 1 or level2 > 99 then
		return "Level must be in the range 1 - 99"
	end

	local difference = XPAtLevel[level1] - XPAtLevel[level2]
	return math.abs(difference)
end

function p.level_to_level(frame)
	return p._level_to_level(frame.args[1], frame.args[2])
end

-- Same as above, but with the 99 level cap removed
function p._level_to_level_unr(level1, level2)
	if type(level1) == 'string' then
		 level1 = commas_strip(level1)
	end
	level1 = tonumber(level1)

	if type(level2) == 'string' then
		 level2 = commas_strip(level2)
	end
	level2 = tonumber(level2)

	if not level1 or not level2 then
		 return "You must pass numbers as arguments"
	elseif level1 < 1 or level1 > 126 or level2 < 1 or level2 > 126 then
		return "Level must be in the range 1 - 126"
	end

	local difference = XPAtLevelUnr[level1] - XPAtLevelUnr[level2]
	return math.abs(difference)
end

function p.level_to_level_unr(frame)
	return p._level_to_level_unr(frame.args[1], frame.args[2])
end

function p._exp_diff(startxp, startlevel, targetxp, targetlevel)
	if startxp then
		startxp = tonumber(startxp) or 0
		if startxp < 0 or startxp > 200000000 then
			return "Experience must be in the range 0 - 200,000,000"
		end
	elseif startlevel then
		startlevel = tonumber(startlevel)
		if not startlevel then 
			return "You must pass numbers as arguments"
		elseif startlevel < 1 or startlevel > 99 then
			return "Level must be in the range 1 - 99"
		end
		startxp = XPAtLevel[startlevel]
	end
	
	if targetxp then
		targetxp = tonumber(targetxp) or 0
		if targetxp < 1 or targetxp > 200000000 then
			return "Experience must be in the range 1 - 200,000,000"
		end
	elseif targetlevel then
			targetlevel = tonumber(targetlevel)
		if not targetlevel then
			return "You must pass numbers as arguments"
		elseif targetlevel < 1 or targetlevel > 99 then
			return "Level must be in the range 1 - 99"
		end
		targetxp = XPAtLevel[targetlevel]
	else
		return "You must pass either a target level or target xp."
	end
	
	if targetxp < startxp then
		return "Target must be higher than start."
	end
	return targetxp - startxp
end

function p.exp_diff(frame)
	local args = frame:getParent().args
	return p._exp_diff(args.startxp, args.startlevel, args.xp, args.level)
end

return p