Module:Infobox/Widget/Tournaments

From TwogPedia
Revision as of 12:09, 30 October 2023 by Couchor (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Infobox/Widget/Tournaments/doc

local getArgs = require('Module:Arguments').getArgs
local cargo = mw.ext.cargo
local func = require('Module:Functions')
local UpcomingMatches = require('Module:Infobox/Widget/Upcoming matches').get
local Subheader = require('Module:Infobox/Widget/Subheader')
local Functions = require('Module:Functions')
local stringifyDate = Functions.stringifyDate
local getTeamLogo = Functions.getTeamLogo
local timeUntil = Functions.timeUntil
local colorModeDiv = Functions.colorModeDiv
local p = {}

function p.get(frame)
    local args = getArgs(frame)
    local team = args.team
	local currentDate = os.date('%Y-%m-%d')
	-- Get live tournaments
	local tables = 'Tournaments, Participants'
	local fields = 'Tournaments._pageName=_pageName, Tournaments.start=start, Tournaments.end=end, Tournaments.logoAll=logoAll, Tournaments.logoLight=logoLight, Tournaments.logoDark=logoDark, Tournaments.iconAll=iconAll, Tournaments.iconLight=iconLight, Tournaments.iconDark=iconDark, Participants.team=team'
	local currentPage = mw.title.getCurrentTitle().text
    
	local cargoArgs = {
		join = 'Tournaments._pageName=Participants._pageName'
	}
	if not team then
    	cargoArgs.where = '(Participants.players HOLDS "' .. currentPage .. '" OR Participants.coaches HOLDS "' .. currentPage .. '" OR Participants.analysts HOLDS "' .. currentPage .. '" OR Participants.managers HOLDS "' .. currentPage .. '") AND (Tournaments.start is NOT NULL AND Tournaments.start <= "' .. currentDate .. '") AND (Tournaments.end IS NOT NULL AND Tournaments.end >= "' .. currentDate .. '")'
    else 
    	cargoArgs.where = 'Participants.team = "' .. currentPage .. '" AND (Tournaments.start is NOT NULL AND Tournaments.start <= "' .. currentDate .. '") AND (Tournaments.end IS NOT NULL AND Tournaments.end >= "' .. currentDate .. '")'
    end

	local playerLiveResults = cargo.query(tables, fields, cargoArgs)
    local container = mw.html.create('div'):addClass('ib')
    local hasContents = false
	if #playerLiveResults > 0 then
        hasContents = true
		container:node(Subheader.make('Live tournaments')):node(p.tournamentWrapper(playerLiveResults, true))
	end
	
	-- Get upcoming tournaments
	if not team then
    	cargoArgs.where = '(Participants.players HOLDS "' .. currentPage .. '" OR Participants.coaches HOLDS "' .. currentPage .. '" OR Participants.analysts HOLDS "' .. currentPage .. '" OR Participants.managers HOLDS "' .. currentPage .. '") AND (Tournaments.start is NOT NULL AND Tournaments.start > "' .. currentDate .. '")'
    else 
    	cargoArgs.where = 'Participants.team = "' .. currentPage .. '" AND (Tournaments.start is NOT NULL AND Tournaments.start > "' .. currentDate .. '")'
    end
	
	local playerUpcomingResults = cargo.query(tables, fields, cargoArgs)
	
	if #playerUpcomingResults > 0 then
        hasContents = true
		container:node(Subheader.make('Upcoming tournaments')):node(p.tournamentWrapper(playerUpcomingResults))
	end
	
	if not team and #playerUpcomingResults == 0 and #playerLiveResults == 0 then
		-- Get live tournaments for talent
		tables = 'Tournaments, Talent'
		fields = 'Tournaments._pageName=_pageName, Tournaments.start=start, Tournaments.end=end, Tournaments.logoAll=logoAll, Tournaments.logoLight=logoLight, Tournaments.logoDark=logoDark, Tournaments.iconAll=iconAll, Tournaments.iconLight=iconLight, Tournaments.iconDark=iconDark'
	
		cargoArgs = {
			join = 'Tournaments._pageName=Talent._pageName',
			where = 'Talent.people HOLDS "' .. currentPage .. '" AND (Tournaments.start is NOT NULL AND Tournaments.start <= "' .. currentDate .. '") AND (Tournaments.end IS NOT NULL AND Tournaments.end >= ' .. currentDate .. ')'
		}
		local talentLiveResults = cargo.query(tables, fields, cargoArgs)
		if #talentLiveResults > 0 then
			container:node(Subheader.make('Live tournaments')):node(p.tournamentWrapper(talentLiveResults, true))
		end
	end
    if hasContents then return container else return end
end

function p.tournamentWrapper(results, live)
	local wrapper = mw.html.create('div'):attr('style', 'padding: 10px;')
	for i = 1, #results do
		local result = results[i]
		
		local dateDiv = mw.html.create('div')
		if live then if result['end'] then dateDiv:wikitext('Until ' ..stringifyDate(result['end']) ) else dateDiv:wikitext('Until TBD') end else dateDiv:wikitext('Starts on ' .. stringifyDate(result.start)) end
		local logoDiv = mw.html.create('div')
		local nameDiv = mw.html.create('div'):wikitext('[[' .. result._pageName .. ']]')
		local tournamentInfo = mw.html.create('div'):attr('style', 'display: flex; gap: 0.5rem; justify-content: center; align-items: center;'):node(dateDiv):node(logoDiv):node(nameDiv)
		local matches = mw.html.create('div')
		local tournamentContainer = mw.html.create('div'):node(tournamentInfo):node(matches)
		local iconSize = '30px'
		if result.iconAll then
			logoDiv:node('[[File:' .. result.iconAll .. '|' .. iconSize .. '|link=' .. result._pageName .. ']]')
		elseif result.iconLight and result.iconDark then
			logoDiv:node(colorModeDiv('[[File:' .. result.iconLight .. '|' .. iconSize .. '|link=' .. result._pageName .. ']]', '[[File:' .. result.iconDark .. '|' .. iconSize .. '|link=' .. result._pageName .. ']]'))
		elseif result.logoAll then
			logoDiv:node('[[File:' .. result.logoAll .. '|' .. iconSize .. '|link=' .. result._pageName .. ']]')
		elseif result.logoLight and result.logoDark then
			logoDiv:node(colorModeDiv('[[File:' .. result.logoLight .. '|' .. iconSize .. '|link=' .. result._pageName .. ']]', '[[File:' .. result.logoDark .. '|' .. iconSize .. '|link=' .. result._pageName .. ']]'))
		else 
			local logo = result.iconLight or result.iconDark or result.logoLight or result.logoDark
			logoDiv:node('[[File:' .. logo .. '|' .. iconSize .. '|link=' .. result._pageName .. ']]')
		end
		
		-- Check if any upcoming or live matches for this tournament and player
		tournamentContainer:node(UpcomingMatches(result._pageName, result.team))
		wrapper:node(tournamentContainer)
	end
	return wrapper
end

return p