Module:GameLanding/Upcoming matches

From TwogPedia
< Module:GameLanding
Revision as of 11:31, 29 September 2023 by Couchor (talk | contribs) (Created page with "local cargo = mw.ext.cargo local p = {} local getTeamLogo = require('Module:Functions').getTeamLogo local prizeToString = require('Module:Functions').prizeToString function p.upcomingMatches(game) local tables = 'AllMatches' local fields = '_pageName, date, p1, p2, winner' local currentDate = os.date('%Y-%m-%d') local cargoArgs = { where = '_pageName LIKE "' .. game .. '/%" AND ( date > "' .. currentDate .. '" OR winner=0)', orderBy = 'date ASC', limit = 10...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:GameLanding/Upcoming matches/doc

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


function p.upcomingMatches(game)
	local tables = 'AllMatches'
	local fields = '_pageName, date, p1, p2, winner'
	local currentDate = os.date('%Y-%m-%d')
	local cargoArgs = {
		where = '_pageName LIKE "' .. game .. '/%" AND ( date > "' .. currentDate .. '" OR winner=0)',
		orderBy = 'date ASC',
		limit = 10
	}
	local results = cargo.query(tables, fields, cargoArgs)

	if #results > 0 then
		local list = ''
		local header = mw.html.create('div'):addClass('transfer__header transfer__row'):node(mw.html.create('div'):wikitext('Time')):node(mw.html.create('div'):wikitext('Team 1')):node(mw.html.create('div'):wikitext('')):node(mw.html.create('div'):wikitext('Team 2'))
		local wrapper = mw.html.create('div'):addClass('transfer__container'):node(header)
		local header = mw.html.create('div'):addClass('transfer__header'):node(mw.html.create('div'):wikitext('Time')):node(mw.html.create('div'):wikitext('Team 1')):node(mw.html.create('div'):wikitext('')):node(mw.html.create('div'):wikitext('Team 2'))
		local container = mw.html.create('div'):addClass('box__container'):node(mw.html.create('div'):addClass('box__title'):wikitext('Upcoming matches')):node(wrapper)

		for i = 1, #results do
			local result = results[i]
			local row = mw.html.create('div'):addClass('transfer__row')
			local time = mw.html.create('div')
			local team1 = mw.html.create('div')
			local vs = mw.html.create('div'):wikitext('[[' .. result._pageName .. '|vs]]')
			local team2 = mw.html.create('div')
			
			local currentDate = os.time() -- Get the current Unix timestamp
			local dateString = result.date

			-- Parse the date string into a table
			local year, month, day, hour, minute, second = dateString: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
				time:wikitext('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 = string.format("%dd %dh %dm", days, hours, minutes)
				
				time:wikitext(timeUntil)
			end
			
			
			team1:node(mw.html.create('div'):addClass('transfer__player'):node(getTeamLogo(result.p1, game)):node('[[' .. result.p1 .. '|' .. string.gsub(result.p1, game .. '/', '') .. ']]'))
			team2:node(mw.html.create('div'):addClass('transfer__player'):node(getTeamLogo(result.p2, game)):node('[[' .. result.p2 .. '|' .. string.gsub(result.p2, game .. '/', '') .. ']]'))
			
			row:node(time):node(team1):node(vs):node(team2)
			
			wrapper:node(row)
		end
		
		
		return container
	end
	return nil
end

return p