Module:Game/Transfers: Difference between revisions

From TwogPedia
(Created page with "local Flags = require('Module:Flags') local p = {} local VariablesLua = mw.ext.VariablesLua local getTeamLogo = require('Module:Functions').getTeamLogo local cargo = mw.ext.cargo function p.main(frame) local game = mw.text.split(mw.title.getCurrentTitle().text, '/')[1] local tables = 'Transfers' local fields = '_pageName, id, flag, joindate, leavedate, inactivedate' local cargoArgs = { where = '_pageName LIKE "' .. game .. '/%"', orderBy = 'joindate DESC', lim...")
 
mNo edit summary
Line 34: Line 34:
local joinDate = monthName .. " " .. day .. ", " .. year
local joinDate = monthName .. " " .. day .. ", " .. year


local leavedate = nil
local leaveDate = nil
if result.leavedate then
if result.leavedate then
year, month, day = result.leavedate:match("(%d+)-(%d+)-(%d+)")
year, month, day = result.leavedate:match("(%d+)-(%d+)-(%d+)")
Line 68: Line 68:
fields = '_pageName, id, flag, joindate, leavedate, inactivedate'
fields = '_pageName, id, flag, joindate, leavedate, inactivedate'
cargoArgs = {
cargoArgs = {
where = '_pageName LIKE "' .. game .. '/%" AND id="' .. result.id .. '" AND joindate = "' .. result.leavedate .. '"',
where = '_pageName LIKE "' .. game .. '/%" AND id="' .. result.id .. '" AND joindate = "' .. leaveDate .. '"',
orderBy = 'joindate ASC',
orderBy = 'joindate ASC',
limit = 10
limit = 10

Revision as of 08:56, 2 November 2023

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

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

function p.main(frame)
	local game = mw.text.split(mw.title.getCurrentTitle().text, '/')[1]
	local tables = 'Transfers'
	local fields = '_pageName, id, flag, joindate, leavedate, inactivedate'
	local cargoArgs = {
		where = '_pageName LIKE "' .. game .. '/%"',
		orderBy = 'joindate DESC',
		limit = 50
	}
	local results = cargo.query(tables, fields, cargoArgs)
	
	local container = mw.html.create('div')
	if #results > 0 then
		local transfersContainer = mw.html.create('div'):addClass('transfer__container')
		local transfersWrapper = mw.html.create('div'):addClass('box__container'):node(mw.html.create('div'):addClass('box__title'):wikitext('Latest transfers')):node(transfersContainer)
		
		-- Header row
		local transferHeader = mw.html.create('div'):addClass('transfer__row transfer__header')
		transferHeader:node(mw.html.create('div'):wikitext('Date')):node(mw.html.create('div'):wikitext('Player')):node(mw.html.create('div'):wikitext('From')):node(mw.html.create('div'):wikitext('To'))
		transfersContainer:node(transferHeader)
		
		local monthNames = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
		-- Loop through all transfers
		for i = 1, #results do
			local result = results[i]
			local year, month, day = result.joindate:match("(%d+)-(%d+)-(%d+)")
			local monthName = monthNames[tonumber(month)]
			local joinDate = monthName .. " " .. day .. ", " .. year

			local leaveDate = nil
			if result.leavedate then
				year, month, day = result.leavedate:match("(%d+)-(%d+)-(%d+)")
				monthName = monthNames[tonumber(month)]
				leaveDate = monthName .. " " .. day .. ", " .. year
			end

			local transfer = mw.html.create('div'):addClass('transfer__row')
			local transferDate = mw.html.create('div'):wikitext(leaveDate or joinDate)
			
			local flag = ''
			if result.flag then
				flag = Flags.icon(result.flag, game)
			else 
				tables = 'People'
				fields = '_pageName, nationality, name, romanized'
				cargoArgs = {
					where = '_pageName="' .. result.id .. '"'
				}
				local personResult = cargo.query(tables, fields, cargoArgs)
				if #personResult > 0 then
					flag = Flags.icon(personResult[1].nationality, game)
				end
			end

			local player = mw.html.create('div'):addClass('transfer__player'):node(flag):node('[[' .. result.id .. '|' .. string.gsub(result.id, 'People/', '') .. ']]')
			
			local from = mw.html.create('div')
			local to = mw.html.create('div')
			-- If left the current team then check if has joined a new one, else check for older entries from the same player to see which team he joined from
			if leaveDate then
				tables = 'Transfers'
				fields = '_pageName, id, flag, joindate, leavedate, inactivedate'
				cargoArgs = {
					where = '_pageName LIKE "' .. game .. '/%" AND id="' .. result.id .. '" AND joindate = "' .. leaveDate .. '"',
					orderBy = 'joindate ASC',
					limit = 10
				}
				local newTeamResult = cargo.query(tables, fields, cargoArgs)

				from:node(getTeamLogo(result._pageName, game, size))	
				if #newTeamResult > 0 then to:node(getTeamLogo(newTeamResult[1]._pageName, game, size))	
				else to:wikitext('X') end
			else 
				tables = 'Transfers'
				fields = '_pageName, id, flag, joindate, leavedate, inactivedate'
				cargoArgs = {
					where = '_pageName LIKE "' .. game .. '/%" AND id="' .. result.id .. '" AND joindate < "' .. result.joindate .. '" AND leavedate="' .. result.joindate .. '"',
					orderBy = 'joindate DESC',
					limit = 10
				}
				local fromResults = cargo.query(tables, fields, cargoArgs)

				-- If had previous teams
				if #fromResults > 0 then
					from:node(getTeamLogo(fromResults[1]._pageName, game, size))
					to:node(getTeamLogo(result._pageName, game, size))
				else
					from:wikitext('X')
					to:node(getTeamLogo(result._pageName, game, size))
				end
				
			end
			transfer:node(transferDate):node(player):node(from):node(to)

			transfersContainer:node(transfer)
		end
		return transfersWrapper
	end
	
	return container
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