Module:Archive/MatchMaps

From Liquipedia Rocket League Wiki

local p = {}

local getArgs = require('Module:Arguments').getArgs
local args
local countdown = require('Module:Countdown').create
local game_number_max = 19

function p.dateAndMatch(frame)
	args = getArgs(frame)  
	--Make a row for each
	return p.dateRow(frame) .. p.MatchRow(frame)
end

function p.dateRow(frame)
	if args.date then
		local row = mw.html.create('tr')
		
		local timezone = mw.text.split(args.date, 'data-tz="', true)
		timezone = timezone[2]
		if timezone then
	  		timezone = mw.text.split(timezone, '"', true)
	  		timezone = timezone[1]
	  	end
	
		local matchDate = mw.text.split(args.date, '<', true)
		matchDate = string.gsub( matchDate[1], '%-', '')
	
		local rawDate = mw.text.split(args.date, '<', true)
		rawDate = rawDate[1]
	
		mw.ext.VariablesLua.vardefine('timezone', timezone or '')
		mw.ext.VariablesLua.vardefine('Match_date', matchDate)
		mw.ext.VariablesLua.vardefine('Raw_Match_date', rawDate)
	
		local cell = row:tag('td')
			:attr('colspan', '4')
			:css('font-weight', 'bold')
			:css('font-size', '85%')
			:css('line-height', '90%')
			:css('height', '13px')
		cell:tag('center')
			:wikitext(countdown{
				date = args.date,
				finished = args.finished,
				stream = args.stream,
				twitch = args.twitch,
				afreeca = args.afreeca,
				afreecatv = args.afreecatv,
				dailymotion = args.dailymotion,
				douyu = args.douyu,
				huomao = args.huomao,
				smashcast = args.smashcast,
				youtube = args.youtube,
				facebook = args.facebook,
				pandatv = args.pandatv,
			})
	
		return tostring(row:done())
	else
		return ''
	end
end

function p.MatchRow(frame)
	args = getArgs(frame)
  	local player1 = args.player1 or args.team1 or ''
  	local player2 = args.player2 or args.team2 or ''
  	local matchmaps_player1 = mw.ext.VariablesLua.var('team1')
  	local matchmaps_player2 = mw.ext.VariablesLua.var('team2')
  	local matchmaps_date = mw.ext.VariablesLua.var('Match_date')
--mw.log('MatchRow para: ', player1, player2, matchmaps_player1, matchmaps_player2, matchmaps_date)

	if matchmaps_player1 ~= '' then
		player1 = tostring(safeExpandTeamShort(mw.getCurrentFrame(), matchmaps_player1, matchmaps_date, 2))
	elseif player1 ~= '' then
		player1 = tostring(safeExpandTeamShort(mw.getCurrentFrame(), player1, matchmaps_date, 2, true))
	elseif args.team1dota then
		player1 = args.team1dota
	else
		player1 = tostring(safeExpandTeamShort(mw.getCurrentFrame(), 'TBD', matchmaps_date, 2))
	end
	if matchmaps_player2 ~= '' then
		player2 = tostring(safeExpandTeamShort(mw.getCurrentFrame(), matchmaps_player2, matchmaps_date))
	elseif player2 ~= '' then
		player2 = tostring(safeExpandTeamShort(mw.getCurrentFrame(), player2, matchmaps_date, '', true))
	elseif args.team2dota then
		player2 = args.team2dota
	else
		player2 = tostring(safeExpandTeamShort(mw.getCurrentFrame(), 'TBD', matchmaps_date))
	end
  
  	local score1, score2 = 0, 0
  	local resulttype = ''
	if args.walkover == '0' then
		score1 = '-'
		score2 = '-'
	elseif args.walkover == '1' then
		resulttype = args.resulttype or 'FF'
		score1 = 'W'
		score2 = resulttype
		args.winner = '1'
	elseif args.walkover == '2' then
		resulttype = args.resulttype or 'FF'
		score1 = resulttype
		score2 = 'W'
		args.winner = '2'
	elseif args.score1 and args.score2 then
		score1 = args.score1
		score2 = args.score2
	elseif args.map1win then
		for i = 1, game_number_max do
			score1 = score1 + (args['map' .. i .. 'win'] == '1' and 1 or 0)
			score2 = score2 + (args['map' .. i .. 'win'] == '2' and 1 or 0)
		end
	else
		score1 = '-'
		score2 = '-'
	end

	local matchRow = mw.html.create('tr')
		:addClass('match-row')
	local leftSide = matchRow:tag('td')
		:addClass('matchlistslot')
		:attr('width', '42%')
		:attr('align', 'right')
		:wikitext(player1)
	local leftScore = matchRow:tag('td')
		:attr('width', '8%')
		:attr('align', 'center')
  		:wikitext(score1)
	local rightScore = matchRow:tag('td')
		:attr('width', '8%')
		:attr('align', 'center')
  		:wikitext(score2)
	local rightSide = matchRow:tag('td')
		:addClass('matchlistslot')
		:attr('width', '42%')
		:wikitext(player2)
  
	if args.player1flag then
		leftSide:wikitext('&nbsp;' .. safeExpand(frame, 'Flag/' .. string.lower(args.player1flag), {}))
	end
  	if args.player2flag then
		rightSide:wikitext('&nbsp;' .. safeExpand(frame, 'Flag/' .. string.lower(args.player2flag), {}))
	end

	if args.winner == '1' then
		leftScore:css('font-weight', 'bold')
		leftSide:addClass('bg-win')
	elseif args.winner == '2' then
		rightScore:css('font-weight', 'bold')
		rightSide:addClass('bg-win')
	elseif args.winner == 'draw' then
		leftScore:css('font-weight', 'bold')
		rightScore:css('font-weight', 'bold')
		leftSide:addClass('bg-draw')
		rightSide:addClass('bg-draw')
	end 

	if args.details then
		local width = args.width or 300
		width = (width-640)/2
	
		local popupWrapper = leftScore:tag('div')
			:addClass('bracket-popup-wrapper')
			:addClass('bracket-popup-team')
			:css('margin-left', tostring(width) .. 'px')
		local popup = popupWrapper:tag('div')
			:addClass('bracket-popup')
		local popupHeader = popup:tag('div')
			:addClass('bracket-popup-header')
		local popupHeaderLeft = popupHeader:tag('div')
			:addClass('bracket-popup-header-left')
			:wikitext(player1)
		local popupHeaderRight = popupHeader:tag('div')
			:addClass('bracket-popup-header-right')
			:wikitext(player2)
		popup:wikitext(args.details)
	end
  
  	-- Preparing vars
  	score1 = string.match(string.gsub(score1, "%b<>", ""),'%d+') or '0'
  	score2 = string.match(string.gsub(score2, "%b<>", ""),'%d+') or '0'
	mw.ext.VariablesLua.vardefine('winner', (args.winner or ''))
	mw.ext.VariablesLua.vardefine('team1wins_matchmaps', score1)
	mw.ext.VariablesLua.vardefine('team2wins_matchmaps', score2)
	mw.ext.VariablesLua.vardefine('resulttype', string.lower(args.resulttype or resulttype))
  
	return tostring(matchRow:done())
end

function safeExpand(frame, templateTitle, templateArgs)
	local result, value = pcall(frame.expandTemplate, frame, {title = templateTitle, args = templateArgs})
	if result then
		return value
	else
		return '[[Template:' .. templateTitle .. ']]'
	end
end

function safeExpandTeamShort(frame, team, date, i, category)
	local result, value = pcall(frame.expandTemplate, frame, {title = 'Team' .. tostring(i or '') .. 'Short', args = {team:lower(), date}})
	if result then
		return value
	elseif category and mw.title.getCurrentTitle().namespace == 0 then
		mw.log('Missing TeamShort: ', team)
		return team .. '[[Category:Pages using unrecognised TeamShort]]'
	else
		return team
	end
end

return p