Module:Placement

From Call of Duty Esports Wiki
Jump to: navigation, search

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 = {}
local p = require('Module:LuaClassSystem').class()

function p:init(place)
	if not place then
		self.is_nil = true
		return
	end
	self.vars = h.processInput(place)
end

function p:number()
	if self.is_nil then return end
	if not self.vars.range then return end
	return self.vars.range[1]
end

function p:sortValue()
	if self.is_nil then return end
	return self.vars.sort
end

function p:display()
	if self.is_nil then return end
	return self.vars.display
end

function p:cssClass()
	if self.is_nil then return end
	return self.vars.classes
end

function p:string()
	if self.is_nil then return end
	return ('class="%s"|<span style="display:none;">%s</span>%s'):format(
		self.vars.classes,
		self.vars.sort,
		self.vars.display
	)
end

function p:flair(place)
	if self.is_nil then return end
	if not self.vars.lower then return self.vars.display end
	if self.vars.lower >= 4 then return table.concat(self.vars.range, '-') end
	return ('[[File:PlacementIcon%s.png|25px|link=]] %s'):format(
		self.vars.lower or '',
		table.concat(self.vars.range, '-')
	)
end

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),
		range = range,
		lower = tonumber(range[1]),
	}
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

return p