Module:PointSystem

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search

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

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

local FIELDS = { 'Points' }
local PREFIXES = {}
local SUFFIXES = { Points = ' points' }

local h = {}

function h.getFields(arg)
	if not arg then return end
	FIELDS = util_text.split(arg)
end

function h.addHeading(tbl)
	local tr = tbl:tag('tr')
	tr:tag('th'):wikitext('Place')
	for _, v in ipairs(FIELDS) do
		tr:tag('th'):wikitext(v)
	end
end

function h.getPrefixes(args)
	for _, field in ipairs(FIELDS) do
		local prefix = args[field .. '_prefix']
		if prefix then
			PREFIXES[field] = prefix:gsub('_',' ')
		end
	end
end

function h.getSuffixes(args)
	for _, field in ipairs(FIELDS) do
		local suffix = args[field .. '_suffix']
		if suffix then
			SUFFIXES[field] = suffix:gsub('_',' ')
		end
	end
end

function h.getData(args)
	local tbl = {}
	for _, field in ipairs(FIELDS) do
		tbl[field] = util_text.split(args[field] or '')
	end
	tbl.Place = args.Place and util_text.split(args.Place) or {}
	return tbl
end

function h.printFields(tbl, args, data)
	for i, v in ipairs(data[FIELDS[1]]) do
		local tr = tbl:tag('tr')
		local place = data.Place[i] or i
		tr:tag('td'):wikitext(place):done()
		for _, field in ipairs(FIELDS) do
			local td = tr:tag('td')
			if data[field][i] then
				td:wikitext(PREFIXES[field])
					:wikitext(data[field][i])
					:wikitext(SUFFIXES[field])
			end
		end
	end
end

local p = {}
function p.main(frame)
	local args = util_args.merge(true)
	local tbl = mw.html.create('table')
		:addClass('wikitable')
		:addClass('zebra')
		:addClass('point-system')
	h.getFields(args.fields)
	h.addHeading(tbl)
	local data = h.getData(args)
	h.getPrefixes(args)
	h.getSuffixes(args)
	h.printFields(tbl, args, data)
	return tbl
end
return p