Halo Esports Wiki
Halo Esports Wiki

To edit the documentation or categories for this module, click here.


local util_args = require('Module:ArgsUtil')
local util_text = require('Module:TextUtil')
local util_vars = require('Module:VarsUtil')

local lang = mw.getLanguage('en')

local CONSTANTS = {
	q = {
		display = 'Q',
		sort = 'aaaaaa'
	},
	nq = {
		display = 'NQ',
		sort = 'bbbbbb'
	},
	dq = {
		display = 'DQ',
		sort = 'cccccc'
	}
}

local h = {}

function h.processInput(place)
	local lc = lang:lc(place)
	if CONSTANTS[lc] then
		return h.processConstant(lc)
	end
	local range = util_text.split(place,'%s*%-%s*')
	range[1] = h.getFirstNumber(range[1])
	if not range[1] then error('Invalid place number -' .. place) end
	range[2] = h.getFirstNumber(range[2])
	local lessThan = (range[1] and range[1]:sub(1,1)) == '<'
	return {
		classes = h.getClassesFromNumber(range[1]),
		sort = h.makeSort(range[1], range[2]),
		display = h.makeDisplay(range, lessThan),
	}
end

function h.processConstant(lc)
	local tbl = CONSTANTS[lc]
	tbl.classes = 'placement placement-' .. lc
	return tbl
end

function h.getFirstNumber(str)
	if not str then return nil end
	return str:match('^.-(%d*)')
end

function h.getClassesFromNumber(num)
	if not tonumber(num) or tonumber(num) > 8 then
		return 'placement placement-9'
	else
		return 'placement placement-' .. num
	end
end

function h.makeSort(first, second)
	local tbl = {}
	h.addSortParts(tbl, first)
	h.addSortParts(tbl, second)
	return table.concat(tbl, '')
end

function h.addSortParts(tbl, str)
	local splitStr
	if not str then
		splitStr = util_text.split('000','')
	elseif #str == 1 then
		splitStr = util_text.split('00' .. str,'')
	elseif #str == 2 then
		splitStr = util_text.split('0' .. str, '')
	else
		splitStr = util_text.split(str, '')
	end
	for _, v in ipairs(splitStr) do
		tbl[#tbl+1] = v
	end
end

function h.makeDisplay(range, lessThan)
	if lessThan then
		range[1] = '<' .. range[1]
	end
	return table.concat(range, ' - ')
end

function h.string(place)
	local namevars = h.processInput(place)
	return ('class="%s"|<span style="display:none;">%s</span>%s'):format(
		namevars.classes,
		namevars.sort,
		namevars.display
	)
end

local p = {}
function p.makeNode(place)
	local namevars = h.processInput(place)
	local tbl = mw.html.create('td')
		:addClass(namevars.classes)
		:tag('span')
			:css('display','none')
			:wikitext(namevars.sort)
		:done()
		:wikitext(namevars.display)
	return tbl
end

function p.main(frame)
	local args = util_args.merge(true)
	return h.string(args[1] or '', args)
end

return p