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

--Written by BryghtShadow
-- <nowiki>
local getArgs = require("Dev:Arguments").getArgs
local p = {}
 
local function Nth(n)
	local function getSuffix (n)
		local lastTwo, lastOne = n % 100, n % 10
		if lastTwo > 3 and lastTwo < 21 then return "th" end
		if lastOne == 1 then return "st" end
		if lastOne == 2 then return "nd" end
		if lastOne == 3 then return "rd" end
		return "th"
	end
	if tonumber(n) then
		return n .. getSuffix(tonumber(n))
	end
	return n
end
 
local types = {
	['moves'] = "Moves",
	['jelly'] = "Jelly",
	['ingredients'] = "Ingredients",
	['timed'] = "Timed",
	['candy order'] = "Candy Order",
	['mixed'] = "Mixed",
	['rainbow rapids'] = "Rainbow Rapids",
}
 
local function _lookupTypeStatus( levelName, isDreamworld )
    if not levelName or levelName == '' then return nil end
	local level = levelName:match('^Level (%d+)')
	if not level then return nil end
	local lookupTable = "Module:Leveltype/List"
	if isDreamworld then
		lookupTable = "Module:LeveltypeDW/List"
	end
	local data = mw.loadData( lookupTable )
	return data[tonumber(level)]
end
 
function p.type(frame)
	local args = getArgs(frame)
	local PAGENAME = args[1]
	local titleparts = mw.text.split(PAGENAME, '/')
	local isDreamworld = titleparts[2] == "Dreamworld" or titleparts[2] == "Dreamworld (Mobile)"
	local typeStatus = _lookupTypeStatus(PAGENAME, isDreamworld) or ''
	local text = types[typeStatus:lower()] or "''Unknown''"
	return text
end
 
-- usage:	{{#invoke:Leveltype|numberInType|Level 1999}}
-- returns:  170th [[Moves levels|moves level]]
-- usage:	{{#invoke:Leveltype|numberInType|Level 1999/Dreamworld}}
-- returns:  170th [[Moves levels|moves level]] in [[Dreamworld]]
function p.numberInType(frame)
	local args = getArgs(frame)
	local PAGENAME = args[1]
	local level = tonumber(PAGENAME:match("Level (%d+)"))
	if not level then
		return "??? (pagename did not contain a number)" -- pagename didn't contain a number.
	end
	local titleparts = mw.text.split(PAGENAME, '/')
	local isDreamworld = titleparts[2] == "Dreamworld" or titleparts[2] == "Dreamworld (Mobile)"
 
	local lookupTable ='Module:Leveltype/List'
	if isDreamworld then
		lookupTable = 'Module:LeveltypeDW/List'
	end
	local data = mw.loadData( lookupTable )
	local leveltype = data[tonumber(level)]
	local tallies = {}
	for i=1, level do
		local key = data[i]
		tallies[key] = (tallies[key] or 0) + 1
	end
	local tally = tallies[leveltype]
	local levelTypes = {
		['moves']	   = "[[Moves levels|moves level]]",
		['jelly']	   = "[[Jelly levels|jelly level]]",
		['ingredients'] = "[[Ingredients levels|ingredients level]]",
		['timed']	   = "[[Timed levels|timed level]]",
		['candy order'] = "[[Candy Order levels|candy order level]]",
		['mixed']	   = "[[Mixed Mode levels|mixed mode level]]",
		['rainbow rapids']	   = "[[Rainbow Rapids levels|rainbow rapids level]]",
	}
	local wikitext = Nth(tally) .. " " .. levelTypes[leveltype:lower()] .. (isDreamworld and " in [[Dreamworld]]" or "")
	return wikitext
end
 
function p._test()
	mw.log(p.numberInType{"Level 1999"})
	mw.log(p.type{"Level 1999"})
end
 
return p
-- </nowiki>
Community content is available under CC-BY-SA unless otherwise noted.