Module:GameLanding/Transfers: Difference between revisions

From TwogPedia
mNo edit summary
mNo edit summary
 
(3 intermediate revisions by the same user not shown)
Line 9: Line 9:
local cargoArgs = {
local cargoArgs = {
where = '_pageName LIKE "' .. game .. '/%"',
where = '_pageName LIKE "' .. game .. '/%"',
orderBy = 'leavedate DESC, joindate DESC',
orderBy = 'maxdate DESC, _pageName ASC',
limit = 10
limit = 10
}
}
Line 16: Line 16:
if #results > 0 then
if #results > 0 then
        local prevTransfers = {}
local list = ''
local list = ''
Line 30: Line 31:
for i = 1, #results do
for i = 1, #results do
local result = results[i]
local result = results[i]
local year, month, day = result.joindate:match("(%d+)-(%d+)-(%d+)")
            -- Prevent duplicate if joined a new team on same date as they left the previous one
local monthName = monthNames[tonumber(month)]
            if not tableContains(prevTransfers, result.id, result.date) then
local joinDate = monthName .. " " .. day .. ", " .. year
            table.insert(prevTransfers, {id = result.id, date = result.date})
 
local year, month, day = result.joindate:match("(%d+)-(%d+)-(%d+)")
local leaveDate = nil
local monthName = monthNames[tonumber(month)]
if result.leavedate then
local joinDate = monthName .. " " .. day .. ", " .. year
year, month, day = result.leavedate:match("(%d+)-(%d+)-(%d+)")
monthName = monthNames[tonumber(month)]
local leaveDate = nil
leaveDate = monthName .. " " .. day .. ", " .. year
if result.leavedate then
end
year, month, day = result.leavedate:match("(%d+)-(%d+)-(%d+)")
 
monthName = monthNames[tonumber(month)]
local transfer = mw.html.create('div'):addClass('transfer__row')
leaveDate = monthName .. " " .. day .. ", " .. year
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
end
 
local transfer = mw.html.create('div'):addClass('transfer__row')
local player = mw.html.create('div'):addClass('transfer__player'):node(flag):node('[[' .. result.id .. '|' .. string.gsub(result.id, 'People/', '') .. ']]')
local transferDate = mw.html.create('div'):wikitext(leaveDate or joinDate)
local from = mw.html.create('div')
local flag = ''
local to = mw.html.create('div')
if result.flag then
-- 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
flag = Flags.icon(result.flag, game)
if leaveDate then
else  
tables = 'Transfers'
tables = 'People'
fields = '_pageName, id, flag, joindate, leavedate, inactivedate'
fields = '_pageName, nationality, name, romanized'
cargoArgs = {
cargoArgs = {
where = '_pageName LIKE "' .. game .. '/%" AND id="' .. result.id .. '" AND joindate = "' .. leaveDate .. '"',
where = '_pageName="' .. result.id .. '"'
orderBy = 'joindate ASC',
}
limit = 10
local personResult = cargo.query(tables, fields, cargoArgs)
}
if #personResult > 0 then
local newTeamResult = cargo.query(tables, fields, cargoArgs)
flag = Flags.icon(personResult[1].nationality, game)
 
end
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
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 = "' .. result.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
end
transfer:node(transferDate):node(player):node(from):node(to)
transfersContainer:node(transfer)
end
end
return transfersWrapper
return transfersWrapper
       
end
end
return nil
return nil
end
function tableContains(tbl, targetId, targetDate)
    for _, transfer in ipairs(tbl) do
        if transfer.id == targetId and transfer.date == targetDate then
            return true  -- The table with the specified id and date exists
        end
    end
    return false
end
end


return p
return p

Latest revision as of 21:44, 12 February 2024

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

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

function p.latestTransfers(game)
	local tables = 'Transfers'
	local fields = '_pageName, id, flag, joindate, leavedate, inactivedate'
	local cargoArgs = {
		where = '_pageName LIKE "' .. game .. '/%"',
		orderBy = 'maxdate DESC, _pageName ASC',
		limit = 10
	}
	local size = '25px'
	local results = cargo.query(tables, fields, cargoArgs)
	
	if #results > 0 then
        local prevTransfers = {}
		local list = ''
		
		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]
            -- Prevent duplicate if joined a new team on same date as they left the previous one
            if not tableContains(prevTransfers, result.id, result.date) then
	            table.insert(prevTransfers, {id = result.id, date = result.date})
				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 = "' .. result.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
		end
		return transfersWrapper
        
	end
	return nil
end

function tableContains(tbl, targetId, targetDate)
    for _, transfer in ipairs(tbl) do
        if transfer.id == targetId and transfer.date == targetDate then
            return true  -- The table with the specified id and date exists
        end
    end
    return false
end

return p