Module:Functions: Difference between revisions

From TwogPedia
mNo edit summary
mNo edit summary
Line 33: Line 33:
fields = 'org'
fields = 'org'
cargoArgs = {
cargoArgs = {
where = '_pageName = "' .. team .. '"'
where = '_pageName = "' .. team .. '" AND org IS NOT NULL'
}
}
results = cargo.query(tables, fields, cargoArgs)
results = cargo.query(tables, fields, cargoArgs)
     if #results > 0 then org = results[1].org end
     if #results > 0 then org = results[1].org end



Revision as of 13:05, 5 October 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 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)
    -- 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 ' ' .. 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

return p