Module:Game/Tournaments

From TwogPedia
Revision as of 11:57, 29 September 2023 by Couchor (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local Flags = require('Module:Flags')
local p = {}
local VariablesLua = mw.ext.VariablesLua
local prizeToString = require('Module:Functions').prizeToString
local cargo = mw.ext.cargo

local currentDate = os.date('%Y-%m-%d')
local game = mw.text.split(mw.title.getCurrentTitle().text, '/')[1]

function p.main(frame)
	-- local container = mw.html.create('div'):attr('id', 'tournament__list')
	local container = mw.html.create('div')
	
	-- ONGOING QUERY
	local tables = 'Tournaments'
	local fields = '_pageName, start, end, type, prize, logoAll, logoLight, logoDark, iconAll, iconLight, iconDark, venue, female_only, formatFilter, educational, participants, country'

	local cargoArgs = {
		where = '_pageName LIKE "' .. game .. '/%" AND start < "' .. currentDate .. '" AND (end is NULL OR end >= "' .. currentDate .. '")',
		limit = 10
	}
	local results = cargo.query(tables, fields, cargoArgs)
	
	p.createTable(results, container, 'Ongoing')
	
	-- UPCOMING QUERY 
	tables = 'Tournaments'
	fields = '_pageName, start, end, type, prize, logoAll, logoLight, logoDark, iconAll, iconLight, iconDark, venue, female_only, formatFilter, educational, participants, country'
	cargoArgs = {
		where = '_pageName LIKE "' .. game .. '/%" AND start > "' .. currentDate .. '"',
		limit = 10
	}
	results = cargo.query(tables, fields, cargoArgs)
	p.createTable(results, container, 'Upcoming')
	
	-- FInISHED QUERY 
	tables = 'Tournaments'
	fields = '_pageName, start, end, type, prize, logoAll, logoLight, logoDark, iconAll, iconLight, iconDark, venue, female_only, formatFilter, educational, participants, country'
	cargoArgs = {
		where = '_pageName LIKE "' .. game .. '/%" AND end < "' .. currentDate .. '"',
		limit = 10
	}
	results = cargo.query(tables, fields, cargoArgs)
	p.createTable(results, container, 'Finished', true)
	
	return container
end

function p.createTable(results, container, title, winner)
	if #results > 0 then
		container:node(mw.html.create('h2'):addClass('tc'):wikitext(title))
		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 winnerTh = mw.html.create('th'):wikitext('Winner')
		local tableHeader = mw.html.create('tr'):addClass('tournament__header'):node(typeTh):node(nameTh):node(dateTh):node(prizeTh):node(locationTh):node(participantsTh):node(formatTh):node(winnerTh)
		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 winnerTd = 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(winnerTd)
			
			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
			
			-- Winner
			if winner then
				tables = "Prizes"
			    fields = "teams"
			    cargoArgs = {
			    	where = 'placement="1" AND _pageName="' .. result._pageName .. '"'
			    }
			    local prizeResults = cargo.query(tables, fields, cargoArgs)
			    
			    if #prizeResults > 0 then
			    	tables = "Logos"
				    fields = "_pageName, logoAll, logoLight, logoDark, iconAll, iconLight, iconDark"
				    cargoArgs = {
				    	where = '_pageName="' .. prizeResults[1].teams .. '" AND ((Logos.start IS NULL OR Logos.start < "' .. currentDate .. '") AND (Logos.end IS NULL OR Logos.end > "' .. currentDate .. '"))'
				    }
				    local logoResults = cargo.query(tables, fields, cargoArgs)
				    if #logoResults > 0 then
				    	local teamName = logoResults[1]._pageName
						local logoAll = logoResults[1].logoAll
						local logoLight = logoResults[1].logoLight
						local logoDark = logoResults[1].logoDark
						local iconAll = logoResults[1].iconAll
						local iconLight = logoResults[1].iconLight
						local iconDark = logoResults[1].iconDark
						
						if iconAll then
							winnerTd:node(p.createLogo(iconAll, teamName, nil, game, size))
						elseif iconLight and iconDark then
							winnerTd:node(mw.html.create('div'):node(p.createLogo(iconLight, teamName, 'light', game, size)):node(p.createLogo(iconDark, teamName, 'dark', game, size)))
						elseif logoAll then
							winnerTd:node(p.createLogo(logoAll, teamName, nil, game, size))
						elseif logoLight and logoDark then
							winnerTd:node(mw.html.create('div'):node(p.createLogo(logoLight, teamName, 'light', game, size)):node(p.createLogo(logoDark, teamName, 'dark', game, size)))
						else 
							local logo = iconLight or iconDark or logoLight or logoDark
							winnerTd:node(p.createLogo(logo, teamName, nil, game, size))
						end
				    else
				    	winnerTd:wikitext('TBD')	
				    end
			    else 
			    	winnerTd:wikitext('TBD')	
			    end
			else
				winnerTd:wikitext('TBD')	
			end
			
			listTable:node(tournamentRow)
		end
		
		return container:node(tournamentContainer)
	end
end

function p.createLogo(url, teamPage, class, game, size)
	size = size or '20px'
	local className = class and class .. ' h-100' or 'h-100'
	local logoURL = url and url or 'Team_placeholder_' .. class .. '.png'
	local logoContainer = mw.html.create('div'):addClass(className)
	local logo = mw.html.create('div')
		:addClass('team-list-logo')
		:node('[[File:' .. logoURL .. '|' .. size .. '|link=' .. teamPage .. '|' .. string.gsub(teamPage, game .. '/', '') .. ']]')

	logoContainer:node(logo)
	return logoContainer
end

return p