Difference between revisions of "Module:Rank"

From Pandorabox
Jump to navigation Jump to search
m
m (disable debug)
 
(4 intermediate revisions by the same user not shown)
Line 115: Line 115:
 
local function to_yn(letter, default) -- letter to yes/no
 
local function to_yn(letter, default) -- letter to yes/no
 
if type(letter) ~= "string" then return default end
 
if type(letter) ~= "string" then return default end
if letter ~= "y" or letter ~= "n" then return default end
+
if letter ~= "y" and letter ~= "n" then return default end
 
return letter
 
return letter
 
end
 
end

Latest revision as of 22:08, 9 April 2024

This is a module to make getting ranks out of XP values easier.

toRank

You can do
{{#invoke:Rank|toRank|<xp value>|[<options>]}}
to generate a rank. The options are not required, but they allow some customization.

Example: Xp cobble.png Cobble God -> rank with 1234 xp

Options

The options are a string of 1 to 3 width, consisting entirely out of "y" (denoting yes) and "n" (denoting no) characters. The first character controls whether the icon appears, the second controls whether the major rank appears, and the last character controls whether the minor rank appears. For any ommited or invalid characters, the default of "yyy" is used.


Example: Gold Peasant -> rank with 314159 xp, and no icon (options are n, but effectively is nyy)

Example: Xp copper block.png -> rank with 123456 xp, but only icon (options are ynn)

Example: Xp steel block.png Lord -> rank with 44444 xp, but no major rank (options are yn, but effectively is yny)

toMajorRank

You can do
{{#invoke:Rank|toMajorRank|<xp value>}}
to generate a rank without a minor rank. This is only used as an alias to
{{#invoke:Rank|toRank|<xp value>|yyn}}

Example: Xp uranium.png Uranium -> rank with 17363925 xp, icon and major only


-- bird's number -> rank converter
-- use this wherever you want, i guess?


--- THIS IS DIRECTLY COPIED FROM xp_redo_ranks_ores/ranks.lua
local prefixes = {
	{
		name="Cobble",
		icon="[[File:Xp_cobble.png]]",
		base_xp=0,
		xp_modifier=100
	}, -- 0 - 1k
	{
		name="Stone",
		icon="[[File:Xp_stone_block.png]]",
		base_xp=2000,
		xp_modifier=200
	}, -- 2k - 4k
	{
		name="Coal",
		icon="[[File:Xp_coal_block.png]]",
		base_xp=5000,
		xp_modifier=500
	}, -- 5k - 10k
	{
		name="Iron",
		icon="[[File:Xp_steel_block.png]]",
		base_xp=20*1000,
		xp_modifier=6000
	}, -- 20k - 80k
	{
		name="Copper",
		icon="[[File:Xp_copper_block.png]]",
		base_xp=100*1000,
		xp_modifier=10*1000
	}, -- 100k - 200k
	{
		name="Gold",
		icon="[[File:Xp_gold_block.png]]",
		base_xp=300*1000,
		xp_modifier=50*1000
	}, -- 300k - 800k
	{
		name="Diamond",
		icon="[[File:Xp_diamond_block.png]]",
		base_xp=1*1000*1000,
		xp_modifier=80*1000
	}, -- 1M - 1.8M
	{
		name="Mese",
		icon="[[File:Xp_mese_block.png]]",
		base_xp=2*1000*1000,
		xp_modifier=200*1000
	}, -- 2M - 4M
	{
		name="Lava",
		icon="[[File:Xp_lava.png]]",
		base_xp=5*1000*1000,
		xp_modifier=300*1000
	}, -- 5M - 8M
	{
		name="Sulfur",
		icon="[[File:Xp_sulfur.png]]",
		base_xp=9*1000*1000,
		xp_modifier=300*1000
	}, -- 9M - 12M
		{
		name="Uranium",
		icon="[[File:Xp_uranium.png]]",
		base_xp=13*1000*1000,
		xp_modifier=400*1000
	}, -- 13M - 17M
	{
		name="Chernobylite",
		icon="[[File:Xp_chernobylite.png]]",
		base_xp=18*1000*1000,
		xp_modifier=400*1000
	}, -- 18M - 22M
	{
		name="Corium",
		icon="[[File:Xp_corium.png]]",
		base_xp=23*1000*1000,
		xp_modifier=500*1000
	}, -- 23M - 28M
	{
		name="Mithril",
		icon="[[File:Xp_mithril.png]]",
		base_xp=29*1000*1000,
		xp_modifier=600*1000
	}, -- 29M - 35M
}

local suffixes = {
	-- 7 suffixes
	{name="Peasant", xp_modifier=0},
	{name="Merchant", xp_modifier=1},
	{name="Knight", xp_modifier=2},
	{name="Lord", xp_modifier=3},
	{name="Mayor", xp_modifier=5},
	{name="Master", xp_modifier=7},
	{name="God", xp_modifier=10}
}

local rnumbers = {}
local rnames = {}
-- #suffixes x #prefixes
for i,prefix in pairs(prefixes) do
	for j,suffix in pairs(suffixes) do
		table.insert(rnumbers,prefix.base_xp + (prefix.xp_modifier * suffix.xp_modifier))
		table.insert(rnames, {i,j})

	end
end

local function to_yn(letter, default) -- letter to yes/no
	if type(letter) ~= "string" then return default end
	if letter ~= "y" and letter ~= "n" then return default end
	return letter
end
local function to_bool(letter) -- returns true if letter=="y", false if letter=="n" and nil otherwise
	if letter == "y" then return true
	elseif letter == "n" then return false end
end

local module = {}
function module.toRank(frame)
	frame = frame or {}
	local xp = tonumber((frame).args[1] or 0) -- edge case hell
	frame.args[2] = frame.args[2] or ""
	local r = #rnames
	for i,rxp in pairs(rnumbers) do
		if xp >= (rxp or 0) and xp < (rnumbers[i+1] or math.huge) then
			r = i
			break
		end
	end
	
	local x = rnames[r]
	
	local out = ""
	if to_bool(to_yn(frame.args[2]:sub(1,1),"y")) then out = out..prefixes[x[1]].icon end
	if to_bool(to_yn(frame.args[2]:sub(2,2),"y")) then out = out.." '''"..prefixes[x[1]].name.."'''" end
	if to_bool(to_yn(frame.args[2]:sub(3,3),"y")) then out = out.." '''"..suffixes[x[2]].name.."'''" end
	return out
end
function module.toMajorRank(frame) -- shorthand?
	frame = frame or {}
	frame.args[2] = "yyn"
	return module.toRank(frame)
end

return module