Module:AllBroadcasts

From TwogPedia
Revision as of 20:10, 12 November 2023 by Couchor (talk | contribs) (Created page with "local cargo = mw.ext.cargo local Func = require('Module:Functions') local getTeamLogo = Func.getTeamLogo local stringifyDate = Func.stringifyDate local prizeToString = Func.prizeToString local getTeamDisplaytitle = Func.getTeamDisplaytitle local makeFlag = require('Module:Person/Flag').makeFlag local p = {} function p.main(frame) -- TOURNAMENTS AS TALENT local currentTitle = mw.title.getCurrentTitle().text local split = mw.text.split(currentTitle, '/') local person...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local cargo = mw.ext.cargo
local Func = require('Module:Functions')
local getTeamLogo = Func.getTeamLogo
local stringifyDate = Func.stringifyDate
local prizeToString = Func.prizeToString
local getTeamDisplaytitle = Func.getTeamDisplaytitle
local makeFlag = require('Module:Person/Flag').makeFlag

local p = {}

function p.main(frame)
	-- TOURNAMENTS AS TALENT
	local currentTitle = mw.title.getCurrentTitle().text
	local split = mw.text.split(currentTitle, '/')
	local person = split[1] .. '/' .. split[2]
	
	local parentDT = mw.ext.displaytitle.get(person)
	local newDT = parentDT .. ' - Broadcasts'
	local seoArgs = {
		title_mode = 'replace',
		title = newDT .. ' {{!}} TwogPedia',
		description = 'Detailed broadcast info of tournaments and matches for ' .. parentDT,
		keywords = parentDT .. ',broadcasts,matches,tournaments'
	}
	
	local tables = 'Talent, Tournaments'
	local fields = 'Talent.role=role, Talent.lang=lang, Tournaments.prize=prize, Tournaments._pageName=_pageName, Tournaments.type=type, Tournaments.start=start, Tournaments.end=end, 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 cargoArgs = {
		join = 'Talent._pageName=Tournaments._pageName',
		where = 'Talent.people HOLDS "' .. person .. '"',
		orderBy = 'Tournaments.end desc'
	}
	local tournamentResults = cargo.query(tables, fields, cargoArgs)
	
	if #tournamentResults > 0 then
		return createTable(tournamentResults, person)
	end
end

function createTable(results, person)
	local container = mw.html.create('div')

	if #results > 0 then
		local monthNames = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
		
		local roleTh = mw.html.create('th'):wikitext('Role')
		local typeTh = mw.html.create('th'):wikitext('Type')
		local nameTh = mw.html.create('th'):wikitext('Tournament')
		local dateTh = mw.html.create('th'):wikitext('Date')
		local prizeTh = mw.html.create('th'):wikitext('Prize')
		local placementTh = mw.html.create('th'):wikitext('Winner')
		local tableHeader = mw.html.create('tr'):addClass('tournament__header'):node(roleTh):node(nameTh):node(dateTh):node(prizeTh):node(placementTh)

		local listTable = mw.html.create('table'):addClass('tournament__table'):attr('style', 'width: -webkit-fill-available;'):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 roleTd = mw.html.create('td'):wikitext(result.lang .. ' ' .. string.sub(result.role, 1, -2))
			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 placementTd = mw.html.create('td'):addClass('tc')
			
		
			-- Check if tournament has a winner
			tables = "Prizes"
		    fields = "teams"
		    cargoArgs = {
		    	where = 'placement="1" AND _pageName="' .. result._pageName .. '"'
		    }
		    local prizeResults = cargo.query(tables, fields, cargoArgs)
		    
		    if #prizeResults > 0 then
		    	placementTd:node(getTeamLogo(prizeResults[1].teams, mw.text.split(result._pageName, '/')[1], '30x30px'))
			else
				placementTd:wikitext('TBD')	
		    end

			local tournamentType = result.type and mw.text.split(result.type, ',') or {}
			for j = 1, #tournamentType do
				typeTd:node(mw.html.create('div'):wikitext(tournamentType[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(roleTd):node(nameTd):node(dateTd):node(prizeTd):node(placementTd)

			local currentTime = os.time()
			
			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
		    
			listTable:node(tournamentRow)
			-- Get matches of tournament
			local matches = getMatches(result._pageName, person)
			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
	end
end

function getMatches(tournament, person)
	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 (casters HOLDS "' .. person .. '" OR observers HOLDS "' .. person .. '") 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 team = not string.find(results[1].p1, 'People/')
		local dateTh = mw.html.create('th'):wikitext('Date')
		local vsTh = mw.html.create('th'):wikitext('Match')
		local vodTh = mw.html.create('th'):wikitext('VODs')
		local headerRow = mw.html.create('tr'):addClass('headerRow'):node(dateTh):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 dateTd = mw.html.create('td'):wikitext(stringifyDate(result.date) .. ' '):node(mw.html.create('abbr'):attr('title', 'Coordinated Universal Time'):wikitext('UTC'))
			local vsTd = mw.html.create('td')
			if team then 
				vsTd:node(mw.html.create('div'):attr('style', 'display: flex; gap: 0.2rem; align-items: center; justify-content: center;'):node(getTeamLogo(result.p1, game, '20x20px')):wikitext('[[' .. result.p1 .. '|' .. getTeamDisplaytitle(result.p1, result.date) ..']]'):wikitext(result.p1score .. ':' .. result.p2score):node(getTeamLogo(result.p2, game, '20x20px')):wikitext('[[' .. result.p2 .. '|' .. getTeamDisplaytitle(result.p2, result.date) ..']]'))
			else
				vsTd:node(mw.html.create('div'):attr('style', 'display: flex; gap: 0.2rem; align-items: center; justify-content: center;'):wikitext(makeFlag(nil, result.p1)):wikitext('[[' .. result.p1 .. ']]'):wikitext(result.p1score .. ':' .. result.p2score):wikitext(makeFlag(nil, result.p2)):wikitext('[[' .. result.p2 .. ']]'))
			end
			local vodTd = mw.html.create('td')
			local resultRow = mw.html.create('tr'):addClass('bodyRow'):node(dateTd):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|link=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