Module:MatchInterviewQuery

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

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

local util_args = require('Module:ArgsUtil')
local util_cargo = require("Module:CargoUtil")
local util_html = require("Module:HtmlUtil")
local util_map = require('Module:MapUtil')
local util_table = require("Module:TableUtil")
local util_text = require("Module:TextUtil")
local util_time = require("Module:TimeUtil")
local util_vars = require("Module:VarsUtil")
local i18n = require("Module:I18nUtil")
local lang = mw.getLanguage('en')
local m_team = require('Module:Team')

local COLUMNS = {
	'Date', 'Tournament', 'TeamDisplay', 'Link',
	classes = { TeamDisplay = 'postmatch-interviews-teams' }
}

local h = {}
local p = {}
function p.main(frame)
	i18n.init('MatchInterviewQuery')
	local args = util_args.merge()
	local player = args[1] or mw.title.getCurrentTitle().text
	local data = h.makeAndRunQuery(player)
	if #data == 0 then return '<!-- -->' end
	util_map.rowsInPlace(data, h.formatOneRow)
	return h.makeOutput(data)
end

function h.makeAndRunQuery(player)
	local query = {
		tables = {
			'MatchSchedule=MS',
			'MatchSchedule__InterviewWith=MSInt',
			'PlayerRedirects=PRMS',
			'Tournaments=T',
		},
		join = {
			'MS._ID=MSInt._rowID',
			'MSInt._value=PRMS.AllName',
			'MS.OverviewPage=T.OverviewPage',
		},
		fields = {
			'T.Name=Tournament',
			'T.OverviewPage=OverviewPage',
			'MS.VodInterview=MSInterview',
			'MS.Winner=MSWinner',
			'MS.DateTime_UTC=Date',
			'MS.Team1=Team1',
			'MS.Team2=Team2',
		},
		types = {
			MSWinner = 'number',
		},
		where = {
			('PRMS.OverviewPage="%s"'):format(player)
		},
		orderBy = 'MS.DateTime_UTC DESC',
		groupBy = 'MSInterview',
	}
	util_cargo.logQuery(query)
	return util_cargo.queryAndCast(query)
end

function h.formatOneRow(row)
	row.Tournament = h.makeTournamentDisplay(row)
	row.TeamDisplay = tostring(util_html.vsAlign(
		m_team.leftshort(row.Team1),
		m_team.rightshort(row.Team2)
		))
	row.Link = util_text.extLink(row.MSInterview)
	row.Date = util_time.strToDateStr(row.Date)
end

function h.makeTournamentDisplay(row)
	row.Link = util_text.intLink(row.OverviewPage, row.Tournament)
	return row.Link
end

function h.makeOutput(data)
	local output = mw.html.create()
	output:tag('h3')
		:wikitext(i18n.default('heading'))
	h.printTable(output, data)
	return output
end

function h.printTable(output, data)
	local tbl = output:tag('table')
		:addClass('wikitable')
		:addClass('postmatch-interviews')
	util_html.printHeaderFromI18n(tbl, COLUMNS)
	util_html.printRowsByList(tbl, data, COLUMNS)
end

return p