Module:RecentMatches

From TwogPedia
Revision as of 20:53, 24 October 2023 by Couchor (talk | contribs) (Created page with "local getArgs = require('Module:Arguments').getArgs local cargo = mw.ext.cargo local Func = require('Module:Functions') local getTeamLogo = Func.getTeamLogo local stringifyDate = Func.stringifyDate local p = {} function p.team(frame) local args = getArgs(frame) local currentPage = mw.title.getCurrentTitle().text local game = mw.text.split(currentPage, '/')[1] local tables = 'AllMatches' local fields = '_ID, _pageName, p1, p2, p1score, p2score, winner, date, besto...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local getArgs = require('Module:Arguments').getArgs
local cargo = mw.ext.cargo
local Func = require('Module:Functions')
local getTeamLogo = Func.getTeamLogo
local stringifyDate = Func.stringifyDate

local p = {}

function p.team(frame)
	local args = getArgs(frame)
	local currentPage = mw.title.getCurrentTitle().text

	local game = mw.text.split(currentPage, '/')[1]
	local tables = 'AllMatches'
	local fields = '_ID, _pageName, p1, p2, p1score, p2score, winner, date, bestof, stage'
	local cargoArgs = {
		where = '(p1 = "' .. currentPage .. '" OR p2="' .. currentPage .. '") AND winner IS NOT NULL AND winner != "0"',
		limit = args.limit or 1000,
		orderBy = 'date desc'
	}
	
	local results = cargo.query(tables, fields, cargoArgs)
	
	if #results > 0 then
		local tournamentTh = mw.html.create('th'):wikitext('Tournament')
		local dateTh = mw.html.create('th'):wikitext('Date')
		local typeTh = mw.html.create('th'):wikitext('Type')
		local scoreTh = mw.html.create('th'):wikitext('Score')
		local vsTh = mw.html.create('th'):wikitext('Opponent')
		local vodTh = mw.html.create('th'):wikitext('VODs')
		local headerRow = mw.html.create('tr'):addClass('headerRow'):node(dateTh):node(tournamentTh):node(typeTh):node(scoreTh):node(vsTh):node(vodTh)
		local tbl = mw.html.create('table'):addClass('striped-table'):node(headerRow)
		
		local container = mw.html.create('div')
		if args.title then container:node(mw.html.create('h2'):wikitext(args.title)) end
		container:node(tbl)
		
		for i = 1, #results do
			local result = results[i]
			local opponent
			local score
			local opponentScore
			if result.p1 == currentPage then 
				score = result.p1score
				opponentScore = result.p2score
				opponent = result.p2
			else 
				score = result.p2score
				opponentScore = result.p1score
				opponent = result.p1
			end
			tables = 'Tournaments'
			fields = '_pageName, type, logoAll, logoLight, logoDark, iconAll, iconLight, iconDark'
			cargoArgs = {
				where = '(_pageName = "' .. result._pageName  .. '" OR _pageName = "' .. removeLastPart(result._pageName) .. '")'
			}
			tournaments = cargo.query(tables, fields, cargoArgs)
			
			local dateTd = mw.html.create('td'):wikitext(stringifyDate(result.date) .. ' '):node(mw.html.create('abbr'):attr('title', 'Coordinated Universal Time'):wikitext('UTC'))
			local tournamentTd = mw.html.create('td')
			local typeTd = mw.html.create('td')
			local scoreTd = mw.html.create('td'):addClass('tc'):wikitext(score .. ':' .. opponentScore)
			local vsTd = mw.html.create('td'):node(mw.html.create('div'):attr('style', 'display: flex; gap: 0.2rem; align-items: center;'):node(getTeamLogo(opponent, game, '20x20px')):wikitext('[[' .. opponent .. ']]'))
			local vodTd = mw.html.create('td')
			local resultRow = mw.html.create('tr'):addClass('bodyRow'):node(dateTd):node(tournamentTd):node(typeTd):node(scoreTd):node(vsTd):node(vodTd)
			if #tournaments > 0 then
				local tournament = tournaments[1]
				local icon = tournament.iconAll or tournament.iconLight or tournament.iconDark or tournament.logoAll or tournament.logoDark or tournament.logoLight
				tournamentTd:wikitext('[[File:' .. icon .. '|25x25px]]'):wikitext('[[' .. result._pageName .. ']]')
				typeTd:wikitext(tournament.type)
			end
			
			-- Get VODs
			if game == 'Dota2' then
				tables = 'Maps_Dota'
				fields = 'yt, vod'
				cargoArgs = {
					where = 'matchID ="' .. result._ID .. '"',
					orderBy = 'map'
				}
				
				local vods = cargo.query(tables, fields, cargoArgs)
				if #vods > 0 then
					for j = 1, #vods do
						local vod = vods[j]
						if vod.yt then
							vodTd:wikitext('[[File:Youtube.png|20x20px|https://www.youtube.com/watch?v=' .. vod.yt .. '|Map ' .. j .. ']]')
						end
					end
				end
			end
			
			tbl:node(resultRow)
		end
		tbl:node(mw.html.create('tr'):node(mw.html.create('td'):addClass('tc'):attr('colspan', 10):wikitext('[[' .. currentPage .. '/Results|All results]]')))
		return container
	end
end

function removeLastPart(str)
	local parts = {}

	for part in str:gmatch("[^/]+") do
	    table.insert(parts, part)
	end
	
	table.remove(parts)
	
	return table.concat(parts, "/")
end

return p