置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:VG Awards

贴贴♀百科,万娘皆可贴的百科全书!转载请标注来源页面的网页链接,并声明引自贴贴百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
local p = {}
local data = require('Module:VG Awards/data')
local yesno = require('Module:Yesno')
local getArgs = require('Module:Arguments').getArgs

local function isWonOrNominated(val)
	val = type(val) == 'string' and val:lower() or val
	if val == 'won'
		or val == 'w'
		or val == '获奖'
		or val == '獲獎'
		or val == '得奖'
		or val == '得獎'
	then
		return true
	elseif val == 'nominated'
		or val == 'n'
		or val == '提名'
		or val == '未获奖'
		or val == '未獲獎'
		or val == '未得奖'
		or val == '未得獎'
	then
		return false
	else
		return yesno(val)
	end
end

local function getArgKeyTables(args)
	local awardsData, hasReferences, hasRows = {
		_years = {}
	}, false, false
	for k, v in pairs(args) do
		if string.match(k, '^[^_]+_[^_]+_.+$') then
			local pos1 = string.find(k, '_')
			local pos2 = string.find(k, '_', pos1 + 1)
			local year = string.sub(k, 1, pos1 - 1)
			local award = string.sub(k, pos1 + 1, pos2 - 1)
			local category = string.sub(k, pos2 + 1)
			if not awardsData[year] then
				awardsData[year] = {
					_awards = {},
					_row = 0
				}
				table.insert(awardsData['_years'], year)
			end
			if not awardsData[year][award] then
				awardsData[year][award] = {}
				table.insert(awardsData[year]['_awards'], award)
			end
			if category == 'Name' then
				awardsData[year][award]['_name'] = v
			elseif category == 'Ref' then
				awardsData[year][award]['_ref'] = v
				hasReferences = true
			else
				table.insert(awardsData[year][award], {category, isWonOrNominated(v)})
				awardsData[year]["_row"] = awardsData[year]["_row"] + 1
				hasRows = true
			end
		end
	end
	table.sort(awardsData['_years'], function (a, b)
		return tonumber(a) < tonumber(b)
	end)
	return awardsData, hasReferences, hasRows
end

local function renderHeadingRow(builder, hasReferences)
	local line = builder:tag('tr')
	line:tag('th')
				:wikitext(data.i18n.year)
			:done()
			:tag('th')
				:wikitext(data.i18n.award)
			:done()
			:tag('th')
				:wikitext(data.i18n.awardCategory)
			:done()
			:tag('th')
				:wikitext(data.i18n.result)
	if hasReferences then
		line:tag('th')
				:wikitext(data.i18n.references)
	end
end

local function renderYearAwards(builder, year, yearAwards, hasReferences, args)
	local isYearRendered = false
	table.sort(yearAwards['_awards'], function (a, b)
		if data.awards[a] then
			if data.awards[b] then
				return data.awards[a].sortkey < data.awards[b].sortkey
			else
				return true
			end
		else
			if data.awards[b] then
				return false
			else
				return a < b
			end
		end
	end)
	for _, v in ipairs(yearAwards['_awards']) do
		table.sort(yearAwards[v], function (a, b)
			if a[2] then
				if b[2] then
					return a[1] < b[1]
				else
					return true
				end
			else
				if b[2] then
					return false
				else
					return a[1] < b[1]
				end
			end
		end)
		local isAwardRendered = false
		for _2, v2 in ipairs(yearAwards[v]) do
			local line = builder:tag('tr')
			if not isYearRendered then
				line:tag('th')
						:attr('rowspan', yearAwards['_row'])
						:wikitext(year)
				isYearRendered = true
			end
			if not isAwardRendered then
				line:tag('th')
						:attr('rowspan', #yearAwards[v])
						:wikitext(yearAwards[v]['_name'] or (data.awards[v] and data.awards[v].name) or v)
			end
			line:tag('td')
					:wikitext(v2[1])
				:done()
				:tag('td')
					:css('background-color', (v2[2] and data.i18n.wonBackground) or data.i18n.nominatedBackground)
					:wikitext((v2[2] and data.i18n.won) or data.i18n.nominated)
			if not isAwardRendered then
				if hasReferences then
					line:tag('td')
							:attr('rowspan', #yearAwards[v])
							:wikitext(yearAwards[v]['_ref'])
				end
				isAwardRendered = true
			end
		end
	end
end

local function renderAwards(builder, awardsData, hasReferences, args)
	renderHeadingRow(builder, hasReferences)
	for _, v in ipairs(awardsData['_years']) do
		local yearAwards = awardsData[v]
		renderYearAwards(builder, v, yearAwards, hasReferences, args)
	end
end

local function renderMainTable(awardsData, hasReferences, args)
	local tbl = mw.html.create('table')
		:addClass('wikitable')
		:addClass('no-min-width-on-mobile')
		:css('text-align', 'center')
		:css('display', 'table')

	if args.align then
		tbl.addClass('float' .. args.align)
	end

	if args.width then
		tbl
			:css('width', args.width)
	else
		tbl
			:css('min-width', '30em')
	end

	if args.title and args.state and (args.state == 'autocollapse'
			or args.state == 'collapsed' or args.state == 'expanded') then
		tbl
			:addClass('collapsible')
			:addClass(args.state)
	end
	tbl:tag('caption')
			:wikitext(args.title or data.i18n.awardTitle)
			:css('display', 'table-caption')

	if args.subtitle then
		tbl:tag('tr'):tag('th')
				:attr('colspan', (hasReferences and 5) or 4)
				:wikitext(args.subtitle)
	end

	renderAwards(tbl, awardsData, hasReferences, args)

	local style = mw.getCurrentFrame():extensionTag('templatestyles', '', {src = 'Template:VG Reviews/styles.css'})

	return tostring(tbl) .. style
end

function p._main(frame, args)
	local awardsData, hasReferences, hasRows = getArgKeyTables(args)
	if hasRows ~= 0 then
		return renderMainTable(awardsData, hasReferences, args)
	elseif mw.title.getCurrentTitle().namespace == 0 then
		return data.i18n.emptyCategory
	end
end

function p.main(frame)
	return p._main(frame, getArgs(frame, {wrappers = "Template:VG Awards"}))
end

return p