Модуль:KVN (Bk;rl,&KVN)

Перейти к навигации Перейти к поиску
Документация

Функции

[править код]

Реализация {{Высшая лига КВН}}

Реализация {{Игра КВН}}

local p = {}
local categoryHandler = require('Module:Category handler').main
local prefix = 'Высшая лига КВН '
local T10 = 'Т10'

local function seacon_article(year)
	if year == T10 then
		return '[[' .. prefix .. '2000|Турнир десяти-2000]]'
	end
	return '[[' .. prefix .. tostring(year) .. '|' .. tostring(year) .. ']]'
end

local function seacon_cat(year, nc)
	if year == T10 then
		return seacon_cat(2000, nc)
	end
	return categoryHandler{main = '[[К:' .. prefix .. tostring(year) .. ' года]]', nocat=nc} or ''
end

local function is_empty(t)
	for _,_ in pairs(t) do
		return false
	end
	return true
end

local function main_cat(nc)
	return categoryHandler{main = '[[К:Команды Высшей лиги КВН]]', nocat=nc} or ''
end

function p.seasons(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	local args = getArgs(frame)

	if is_empty(args) then
		return '<!--empty-->'
	end

	local count = 0
	local nocat = args.nocat
	local list = {}
	for _, y in ipairs(args) do
		list[#list + 1] = seacon_article(y) .. seacon_cat(y, nocat)
		count = count + 1
	end
	return tostring(count) .. ' (' .. table.concat(list, ', ') .. ')' .. main_cat(nocat)
end

local function param_to_num(v)
	local n = tonumber(v)
	if n == nil then
		n = mw.ustring.gsub(v, ',', '.')
		n = tonumber(n)
	end
	return n
end

local function num_to_print(n)
	if n == nil or n == '-' then 
		return '-'
	end
	local s = mw.ustring.gsub(tostring(n), '%.', ',')
	return s
end

local team = 'команда'
local city = 'город'
local theme = 'тема'
local contest = 'конкурс'
local short = 'кр'
local mark = 'оценка'

local function get_param_num(args, prefix)
	local num = 0
	while args[prefix .. tostring(num+1)] ~= nil do
		num = num + 1
	end
	return num
end

local function bullet(prefix, str)
	if str == nil then
		return ''
	end
	return '* ' .. prefix .. ': ' .. str .. '\n'
end

local function team_with_city(args, i)
	local teamName = args[team .. tostring(i)]
	local cityName = args[city .. tostring(i)]
	if cityName ~= nil then
		return teamName .. ' (' .. cityName .. ')'
	end
	return teamName
end

local function contest_with_theme(args, i)
	local contestName = args[contest .. tostring(i)]
	local themeName = args[theme .. tostring(i)]
	if themeName ~= nil then
		return contestName .. ' («' .. themeName .. '»)'
	end
	return contestName
end

local function join(t)
	return mw.text.listToText(t, ', ', ', ')
end

local function contest_header(args, i)
	return mw.getContentLanguage():ucfirst(args[contest .. tostring(i) .. short] or args[contest .. tostring(i)])
end

function p.game(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	local args = getArgs(frame)

	local res = ''
	res = res .. bullet('Дата игры', args['дата'])
	res = res .. bullet('Тема игры', args['тема'])
	local team_num = get_param_num(args, team)
	local teams = {}
	for i = 1, team_num do
		table.insert(teams, team_with_city(args, i))
	end
	res = res .. bullet('Команды', join(teams))
	res = res .. bullet('Жюри', args['жюри'])
	local contest_num = get_param_num(args, contest)
	local contests = {}
	for i = 1, contest_num do
		table.insert(contests, contest_with_theme(args, i))
	end
	res = res .. bullet('Конкурсы', join(contests))
	if args[mark .. '11'] ~= nil then
		local mt = mw.html.create('table')
			:addClass('standard sortable')
			:css('text-align', 'left')
		mt:tag('caption'):wikitext('оценки')
		local headers = mt:tag('tr'):css('text-align', 'center')
		headers:tag('th'):css('width', '150'):wikitext('Команда')
		headers:tag('th'):css('width', '50'):wikitext(contest_header(args, 1))
		for i = 2, contest_num - 1 do
			headers:tag('th'):css('width', '50'):wikitext(contest_header(args, i))
			headers:tag('th'):css('width', '50'):wikitext('общий')
		end
		if contest_num > 1 then
			headers:tag('th'):css('width', '50'):wikitext(contest_header(args, contest_num))
			headers:tag('th'):css('width', '50'):wikitext('Итого')
		end
		for i = 1, team_num do 
			local teamRow = mt:tag('tr')
			teamRow:tag('td'):wikitext(args[team .. tostring(i) .. short] or args[team .. tostring(i)])
			local sum = 0
			for j = 1, contest_num do 
				local m = param_to_num(args[mark .. tostring(i) .. tostring(j)])
				if m == nil then
					sum = nil
					m = nil
				elseif sum ~= nil then
					sum = sum + m
				end
				teamRow:tag('td'):attr('align', 'center'):wikitext(num_to_print(m))
				if j > 1 and j < contest_num then
					teamRow:tag('td'):attr('align', 'center'):wikitext(num_to_print(sum))
				end
			end
			if contest_num > 1 then
				teamRow:tag('td'):attr('align', 'center'):wikitext(args['итого' .. tostring(i)] or  num_to_print(sum))
			end
		end
		res = res .. tostring(mt)
	end
	return res
end

return p