Module:AllResults

From TwogPedia
Revision as of 22:06, 24 October 2023 by Couchor (talk | contribs)

Documentation for this module may be created at Module:AllResults/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 prizeToString = Func.prizeToString
local Flags = require('Module:Flags')

local p = {}

local currentDate = os.date('%Y-%m-%d')

function p.main(frame)
	local args = getArgs(frame)
	if not args.page then return end
	local currentPage = args.page
	local team = args.team
	
	local tables = 'Participants, Tournaments'
	local fields = 'Tournaments._pageName=_pageName, Tournaments.type=type, Tournaments.start=start, Tournaments.end=end, Tournaments.prize=prize, Tournaments.country=country, Tournaments.participants=participants, Tournaments.formatFilter=formatFilter, Tournaments.logoAll=logoAll, Tournaments.logoLight=logoLight, Tournaments.logoDark=logoDark, Tournaments.iconAll=iconAll, Tournaments.iconLight=iconLight, Tournaments.iconDark=iconDark'
	local where = team and 'Participants.team="' .. currentPage .. '"' or 'Participants.players HOLDS "' .. currentPage .. '"'
	local cargoArgs = {
		join = 'Participants._pageName=Tournaments._pageName',
		where = where,
		orderBy = 'Tournaments.end desc'
	}
	
	local results = cargo.query(tables, fields, cargoArgs)
	local container = mw.html.create('div')
	if #results > 0 then
		local tournament = results[1]
		local icon = tournament.iconAll or tournament.iconLight or tournament.iconDark or tournament.logoAll or tournament.logoDark or tournament.logoLight
		createTable(results, container, currentPage, team)
	
		return container
	end
	
end

function createTable(results, container, page, team)
	local game = mw.text.split(page, '/')[1]
	if #results > 0 then
		local monthNames = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
		
		local typeTh = mw.html.create('th'):wikitext('Type')
		local nameTh = mw.html.create('th'):wikitext('Name')
		local dateTh = mw.html.create('th'):wikitext('Date')
		local prizeTh = mw.html.create('th'):wikitext('Prize')
		local locationTh = mw.html.create('th'):wikitext('Location')
		local participantsTh = mw.html.create('th'):wikitext('Participants')
		local formatTh = mw.html.create('th'):wikitext('Format')
		local placementTh = mw.html.create('th'):wikitext('Placement')
		local tableHeader = mw.html.create('tr'):addClass('tournament__header'):node(typeTh):node(nameTh):node(dateTh):node(prizeTh):node(locationTh):node(participantsTh):node(formatTh):node(placementTh)
		local listTable = mw.html.create('table'):addClass('tournament__table'):node(tableHeader)
		container:node(listTable)

		for i = 1, #results do
			local result = results[i]
			local icon = result.iconAll ~= '' and result.iconAll or result.iconLight ~= '' and result.iconLight or result.iconDark ~= '' and result.iconDark or result.logoAll ~= '' and result.logoAll or result.logoDark ~= '' and result.logoDark or result.logoLight ~= '' and result.logoLight
			if not icon then icon = 'Tournament_placeholder.png' end
			
			local typeTd = mw.html.create('td')
			local nameTd = mw.html.create('td'):node(mw.html.create('div'):addClass('tournament__name'):wikitext('[[File:' .. icon .. '|25px]]'):wikitext('[[' .. result._pageName .. ']]'))
			local dateTd = mw.html.create('td')
			local prizeTd = mw.html.create('td'):wikitext('$' .. prizeToString(result.prize))
			local locationTd = mw.html.create('td')
			local participantsTd = mw.html.create('td'):wikitext(result.participants)
			local formatTd = mw.html.create('td')
			local placementTd = mw.html.create('td')
			
			local tournamentType = result.type and mw.text.split(result.type, ',')
			for j = 1, #tournamentType do
				typeTd:node(mw.html.create('div'):wikitext(tournamentType[j]))
			end
			
			local formatList = result.formatFilter and mw.text.split(result.formatFilter, ',') or {}
			for j = 1, #formatList do
				formatTd:node(mw.html.create('div'):wikitext(formatList[j]))
			end
			
			local startDate = 'TBD'
			if result.start then
				local year, month, day = result.start:match("(%d+)-(%d+)-(%d+)")
				local monthName = monthNames[tonumber(month)]
				startDate = monthName .. " " .. day .. ", " .. year
			end
			local endDate = 'TBD'
			if result['end'] then
				year, month, day = result['end']:match("(%d+)-(%d+)-(%d+)")
				monthName = monthNames[tonumber(month)]
				endDate = monthName .. " " .. day .. ", " .. year
			end
			dateTd:node(mw.html.create('div'):wikitext(startDate)):node(mw.html.create('div'):wikitext('-')):node(mw.html.create('div'):wikitext(endDate))
			
			local tournamentRow = mw.html.create('tr'):node(typeTd):node(nameTd):node(dateTd):node(prizeTd):node(locationTd):node(participantsTd):node(formatTd):node(placementTd)
			
			local currentTime = os.time()
			
			if result.country then
				locationTd:node(mw.html.create('div'):addClass('tournament__location'):node(Flags.icon(result.country)):node(mw.html.create('div'):wikitext(Flags.CountryName(result.country))))
			end
			
			local startTime = nil
			if result.start then
				startTime = mw.ustring.gsub(result.start, '(%d+)-(%d+)-(%d+)', function(year, month, day)
				    return os.time({year=tonumber(year), month=tonumber(month), day=tonumber(day)})
				end)	
			end
			local endTime = nil
			if result['end'] then
				endTime = mw.ustring.gsub(result['end'], '(%d+)-(%d+)-(%d+)', function(year, month, day)
				    return os.time({year=tonumber(year), month=tonumber(month), day=tonumber(day)})
				end)
			end

			local date = mw.html.create('div')
			-- If live
			if (startTime ~= nil and os.difftime(currentTime, startTime) > 0) and (endTime == nil or os.difftime(currentTime, endTime) <= 0) then
				date:wikitext('LIVE')
			else 
				local startDate = 'TBD'
				if result.start then
					local year, month, day = result.start:match("(%d+)-(%d+)-(%d+)")
					local monthName = monthNames[tonumber(month)]
					startDate = monthName .. " " .. day .. ", " .. year
				end
				local endDate = 'TBD'
				if result['end'] then
					year, month, day = result['end']:match("(%d+)-(%d+)-(%d+)")
					monthName = monthNames[tonumber(month)]
					endDate = monthName .. " " .. day .. ", " .. year
				end
				date:wikitext(startDate .. ' - ' .. endDate)
			end
			
			-- Get placement of team/player
			tables = "Prizes"
		    fields = "placement, players, teams"
		    where =  team and 'teams HOLDS "' .. page .. '"' or 'players HOLDS "' .. page .. '"'
		    cargoArgs = {
		    	where = where
		    }
		    local prizeResults = cargo.query(tables, fields, cargoArgs)

		    if #prizeResults > 0 then
		    	placementTd:wikitext(prizeResults[1].placement)
		    else
		    	placementTd:wikitext('TBD')
		    end
		    
		    
			listTable:node(tournamentRow)
			-- Get matches of tournament
			local matches = tournamentMatches(page, result._pageName, team)
			if matches then
				local matchesTd = mw.html.create('td'):attr('colspan', 10):node(matches)
				local matchesRow = mw.html.create('tr'):node(matchesTd)
				listTable:node(matchesRow)
			end
		end
		
		return container:node(tournamentContainer)
	end
end

function tournamentMatches(page, tournament, team)
	local game = mw.text.split(tournament, '/')[1]
	local tables = 'AllMatches'
	local fields = '_ID, _pageName, p1, p2, p1score, p2score, winner, date, bestof, stage'
	local cargoArgs = {
		where = '_pageName LIKE "' .. tournament .. '%" AND (p1 = "' .. page .. '" OR p2="' .. page .. '") AND winner IS NOT NULL AND winner != "0"',
		limit =  1000,
		orderBy = 'date desc'
	}
	
	local results = cargo.query(tables, fields, cargoArgs)
	
	if #results > 0 then
		local dateTh = mw.html.create('th'):wikitext('Date')
		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(scoreTh):node(vsTh):node(vodTh)
		local tbl = mw.html.create('table'):addClass('striped-table w-100'):node(headerRow)
		
		for i = 1, #results do
			local result = results[i]
			local opponent
			local score
			local opponentScore
			if result.p1 == page then 
				score = result.p1score
				opponentScore = result.p2score
				opponent = result.p2
			else 
				score = result.p2score
				opponentScore = result.p1score
				opponent = result.p1
			end
			
			local dateTd = mw.html.create('td'):wikitext(stringifyDate(result.date) .. ' '):node(mw.html.create('abbr'):attr('title', 'Coordinated Universal Time'):wikitext('UTC'))
			local scoreTd = mw.html.create('td'):addClass('tc'):wikitext(score .. ':' .. opponentScore)
			local vsTd = mw.html.create('td'):addClass('tc')
			if team then vsTd:node(mw.html.create('div'):attr('style', 'display: flex; gap: 0.2rem; align-items: center;'):node(getTeamLogo(opponent, game, '20x20px')):wikitext('[[' .. opponent .. ']]')) else vsTd:wikitext('[[' .. opponent .. ']]') end
			local vodTd = mw.html.create('td')
			local resultRow = mw.html.create('tr'):addClass('bodyRow'):node(dateTd):node(scoreTd):node(vsTd):node(vodTd)
		
			
			-- 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

		return tbl
	else
		return nil
	end
end

return p