Module:Functions: Difference between revisions

From TwogPedia
mNo edit summary
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 40: Line 40:


function p.getTeamDisplaytitle(name, date)
function p.getTeamDisplaytitle(name, date)
if not date then return mw.ext.displaytitle.get(name) end
    local title = mw.title.new(name)
if not date then return (title and title.exists) and mw.ext.displaytitle.get(name) or mw.text.split(name, '/')[2] end
local tables = 'DisplayTitles'
local tables = 'DisplayTitles'
local fields = 'title'
local fields = 'title'
Line 47: Line 48:
}
}
local results = cargo.query(tables, fields, cargoArgs)
local results = cargo.query(tables, fields, cargoArgs)
if #results > 0 then return results[1].title else return mw.ext.displaytitle.get(name) end
 
if #results > 0 then return results[1].title else return (title and title.exists) and mw.ext.displaytitle.get(name) or mw.text.split(name, '/')[2] end
end
end



Latest revision as of 07:30, 21 November 2023

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

local cargo = mw.ext.cargo
local p = {}

function p.getTeamPage(team, game)
	local currentTitle = mw.title.getCurrentTitle().text
	local gameCategory = game or mw.text.split(currentTitle, '/')[1]
	-- Check if shorthand of a team was used
	local tables = 'Teams'
	local fields = '_pageName, shorthand'
	local cargoArgs = {
		where = '_pageName LIKE "' .. gameCategory .. '/%" AND shorthand = "' .. team .. '"'
	}
	local results = cargo.query(tables, fields, cargoArgs)
	if #results > 0 then return results[1]._pageName else return gameCategory .. '/' .. team end	
end

function removeLastPart(inputString)
	local separator = '/'
    -- Find the position of the last occurrence of the separator
    local lastSeparatorIndex = inputString:match(".*" .. separator .. "([^" .. separator .. "]*)$")

    if lastSeparatorIndex then
        -- Extract the substring before the last separator
        return inputString:sub(1, #inputString - #lastSeparatorIndex - 1)
    else
        -- If separator is not found, return the input string as is
        return inputString
    end
end

function p.getTournamentEndDate(page)
	local tables = 'Tournaments'
	local fields = 'end'
	local cargoArgs = {
		where = '_pageName = "' .. page .. '" OR _pageName= "' .. removeLastPart(page) .. '"'
	}
	local results = cargo.query(tables, fields, cargoArgs)
	if #results > 0 then return results[1]['end'] else return nil end
end

function p.getTeamDisplaytitle(name, date)
    local title = mw.title.new(name)
	if not date then return (title and title.exists) and mw.ext.displaytitle.get(name) or mw.text.split(name, '/')[2] end
	local tables = 'DisplayTitles'
	local fields = 'title'
	local cargoArgs = {
		where = '_pageName = "' .. name .. '" AND  (start is NULL OR start < "' .. date .. '") AND (end is NULL OR end > "' .. date .. '")'
	}
	local results = cargo.query(tables, fields, cargoArgs)

	if #results > 0 then return results[1].title else return (title and title.exists) and mw.ext.displaytitle.get(name) or mw.text.split(name, '/')[2] end
end

function p.prizeToString(nr)
	if nr == nil then return 'TBD' end
	local prize = ''
    local reversed = string.reverse(tostring(nr))
    for k = 1, #reversed do
    	prize = reversed:sub(k, k) .. prize
    	if k % 3 == 0 and k < #reversed then prize =  ',' .. prize end
    	k = k + 1
    end
    return prize
end

function p.getTeamLogo(team, game, size)
    if not team then return mw.html.create('div'):node(mw.html.create('div'):addClass('light'):wikitext('[[File:Team_placeholder_light.png|' .. (size or '20x20px') .. ']]') ):node(mw.html.create('div'):addClass('dark'):wikitext('[[File:Team_placeholder_dark.png|' .. (size or '20x20px') .. ']]') ) end
    -- Check if team has org incase the logo is saved under org
    local org = ''
    tables = 'Teams'
	fields = 'org'
	cargoArgs = {
		where = '_pageName = "' .. team .. '" AND org IS NOT NULL'
	}
	results = cargo.query(tables, fields, cargoArgs)

    if #results > 0 then org = results[1].org end

	local tables = 'Logos'
	local fields = '_pageName, logoLight, logoDark, logoAll, iconAll, iconLight, iconDark, start, end'
	local currentDate = os.date('%Y-%m-%d')
	local cargoArgs = {
		where = '(_pageName = "' .. team .. '" OR _pageName="' .. org .. '") AND  (start is NULL OR start < "' .. currentDate .. '") AND (end is NULL OR end > "' .. currentDate .. '")'
	}
	local results = cargo.query(tables, fields, cargoArgs)
	if #results > 0 then
		if results[1].iconAll then
			return p.createLogo(results[1].iconAll, team, nil, game, size)
		elseif results[1].iconLight and results[1].iconDark then
			return mw.html.create('div'):node(p.createLogo(results[1].iconLight, team, 'light', game, size)):node(p.createLogo(results[1].iconDark, team, 'dark', game, size))
		elseif results[1].logoAll then
			return p.createLogo(results[1].logoAll, team, nil, game, size)
		elseif results[1].logoLight and results[1].logoDark then
			return mw.html.create('div'):node(p.createLogo(results[1].logoLight, team, 'light', game, size)):node(p.createLogo(results[1].logoDark, team, 'dark', game, size))
		else 
			local logo = results[1].iconLight or results[1].iconDark or results[1].logoLight or results[1].logoDark
			return p.createLogo(logo, team, nil, game, size)
		end
	else 
		return mw.html.create('div'):node(p.createLogo(nil, team, 'light', game, size)):node(p.createLogo(nil, team, 'dark', game, size))
	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

function p.stringifyDate(inputDate)
	if not inputDate then return nil end
    local dateTimeSplit = mw.text.split(inputDate, ' ')
    local date = dateTimeSplit[1]
    local time = dateTimeSplit[2] and ' ' .. timeAbbr(dateTimeSplit[2]) or ''
    
	local monthNames = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
    local dateSplit = mw.text.split(date, '-')
    local year = dateSplit[1] or ''
    local month = dateSplit[2] and monthNames[tonumber(dateSplit[2])] .. ' ' or ''
    local day = dateSplit[3] and dateSplit[3] .. ', ' or ''

	return month .. day .. year	.. time 
end

function timeAbbr(time)
	local split = mw.text.split(time, ':')
	local hours = split[1]
	local minutes = split[2]
	return hours .. ':' .. minutes
end

function p.timeUntil(inputDate)
	local currentDate = os.time() -- Get the current Unix timestamp
	-- Parse the date string into a table
	local year, month, day, hour, minute, second = inputDate:match("(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)")
	-- Create a table with the parsed date and time
	local targetDate = {
	    year = tonumber(year),
	    month = tonumber(month),
	    day = tonumber(day),
	    hour = tonumber(hour),
	    min = tonumber(minute),
	    sec = tonumber(second)
	}
	-- Convert the target date and time to a Unix timestamp
	local targetTimestamp = os.time(targetDate)
	
	-- Calculate the time difference in seconds
	local timeDifference = targetTimestamp - currentDate
	
	if timeDifference <= 0 then
		return 'LIVE'
	else
		-- Calculate days, hours, and minutes
		local days = math.floor(timeDifference / (24 * 3600))
		local hours = math.floor((timeDifference % (24 * 3600)) / 3600)
		local minutes = math.floor((timeDifference % 3600) / 60)

		local timeUntil = ""

	    if days > 0 then
	        timeUntil = timeUntil .. days .. "d "
	    end
	
	    if hours > 0 then
	        timeUntil = timeUntil .. hours .. "h "
	    end
	
	    if minutes > 0 or (days == 0 and hours == 0) then
	        timeUntil = timeUntil .. minutes .. "m"
	    end
		
		return timeUntil
	end
end

function p.colorModeDiv(light, dark)
	local light = mw.html.create('div'):addClass('light'):node(light)
	local dark = mw.html.create('div'):addClass('dark'):node(dark)
	return mw.html.create('div'):node(light):node(dark)
end

return p