Module:Infobox/Widget/Tournaments: Difference between revisions

From TwogPedia
mNo edit summary
mNo edit summary
Line 1: Line 1:
local getArgs = require('Module:Arguments').getArgs
local cargo = mw.ext.cargo
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 Functions = require('Module:Functions')
local stringifyDate = Functions.stringifyDate
local getTeamLogo = Functions.getTeamLogo
local getTeamLogo = Functions.getTeamLogo
local prizeToString = Functions.prizeToString
local timeUntil = Functions.timeUntil
local timeUntil = Functions.timeUntil
local stringifyDate = Functions.stringifyDate
local colorModeDiv = Functions.colorModeDiv
local p = {}
local p = {}


function p.get(tournament, team)
function p.get(frame)
    local args = getArgs(frame)
    local team = args.team
local currentDate = os.date('%Y-%m-%d')
local currentDate = os.date('%Y-%m-%d')
-- Get live matches
-- Get live tournaments
local tables = 'AllMatches'
local tables = 'Tournaments, Participants'
local fields = '_pageName, p1, p2, date, bestof'
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 playerPage = mw.title.getCurrentTitle().text
local currentPage = mw.title.getCurrentTitle().text
team = team or ''
   
 
local cargoArgs = {
local cargoArgs = {
where = '_pageName LIKE "' .. tournament .. '/%" AND (date is NOT NULL AND date <= "' .. currentDate .. '") AND (winner IS NULL OR winner = 0) AND ((p1 = "' .. team .. '" OR p2 = "' .. team .. '") OR (p1 = "' .. playerPage .. '" OR p2 = "' .. playerPage .. '"))',
join = 'Tournaments._pageName=Participants._pageName'
orderBy = 'date'
}
}
 
if not team then
local results = cargo.query(tables, fields, cargoArgs)
    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')
if #playerLiveResults > 0 then
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)
    local container = mw.html.create('table'):addClass('w-100 tc')
if #playerUpcomingResults > 0 then
 
container:node(Subheader.make('Upcoming tournaments')):node(p.tournamentWrapper(playerUpcomingResults))
if #results > 0 then
p.matchHtml(container, results, 'true')
end
end
-- Check for upcoming matches
if not team and #playerUpcomingResults == 0 and #playerLiveResults == 0 then
cargoArgs.where = '_pageName LIKE "' .. tournament .. '/%" AND (date is NOT NULL AND date > "' .. currentDate .. '") AND (winner IS NULL OR winner = 0) AND ((p1 = "' .. team .. '" OR p2 = "' .. team .. '") OR (p1 = "' .. playerPage .. '" OR p2 = "' .. playerPage .. '"))'
-- Get live tournaments for talent
results = cargo.query(tables, fields, cargoArgs)
tables = 'Tournaments, Talent'
if #results > 0 then
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'
p.matchHtml(container, results)
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
end
return container
return container
end
end


function p.matchHtml(container, results, live)
function p.tournamentWrapper(results, live)
local game = mw.text.split(results[1]._pageName, '/')[1]
local wrapper = mw.html.create('div'):attr('style', 'padding: 10px;')
for i = 1, #results do
for i = 1, #results do
local result = results[i]
local result = results[i]
local row = mw.html.create('tr')
local time = mw.html.create('td'):attr('style', 'padding: 4px; font-size: 0.8rem;')
local team1 = mw.html.create('td'):attr('style', 'padding: 4px;')
local vs = mw.html.create('td'):attr('style', 'padding: 4px;'):node(mw.html.create('abbr'):attr('title', 'Best-of-' .. result.bestof):wikitext('Bo' .. result.bestof))
local team2 = mw.html.create('td'):attr('style', 'padding: 4px;')
if live then time:wikitext('LIVE') else time:wikitext(timeUntil(result.date)) end
local styleLeft = 'display: flex; column-gap: 0.2rem;justify-content: end; align-items: center;'
local styleRight = 'display: flex; column-gap: 0.2rem;justify-content: start; align-items: center;'
local size = '20x20px'
team1:node(mw.html.create('div'):attr('style', styleLeft):node('[[' .. result.p1 .. '|<div style="font-size: 0.8rem; text-align: right;">' .. string.gsub(result.p1, game .. '/', '') .. '</div>]]'):node(getTeamLogo(result.p1, game, size)))
team2:node(mw.html.create('div'):attr('style', styleRight):node(getTeamLogo(result.p2, game, size)):node('[[' .. result.p2 .. '|<div style="font-size: 0.8rem;">' .. string.gsub(result.p2, game .. '/', '') .. '</div>]]'))
row:node(time):node(team1):node(vs):node(team2)
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
container:node(row)
-- Check if any upcoming or live matches for this tournament and player
tournamentContainer:node(UpcomingMatches(result._pageName, result.team))
wrapper:node(tournamentContainer)
end
end
return wrapper
end
end


return p
return p

Revision as of 20:53, 22 October 2023

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')
	if #playerLiveResults > 0 then
		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
		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
	return container
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