Module:Game/People

From TwogPedia
Revision as of 22:10, 4 October 2023 by Couchor (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

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

function p.main(frame)
	local game = mw.text.split(mw.title.getCurrentTitle().text, '/')[1]
	local tables = 'People'
	local fields = '_pageName, nationality, name, romanized, games, active_roles, past_roles'
	local cargoArgs = {
		where = 'games HOLDS "' .. game .. '"',
		orderBy = 'nationality ASC'
	}
	local results = cargo.query(tables, fields, cargoArgs)
	
	local container = mw.html.create('div')
	if #results > 0 then
		local nat = mw.text.split(results[1].nationality, ',')[1]
		
		local tableContainer = mw.html.create('table'):addClass('players__table')
		local flagRow = mw.html.create('tr'):node(mw.html.create('th'):attr('colspan', 4):node(mw.html.create('div'):addClass('players__header'):node(Flags.icon(nat)):node(mw.html.create('div'):wikitext(Flags.CountryName(nat)))))
		
		local headerRow = mw.html.create('tr'):addClass('tc'):node(mw.html.create('th'):wikitext('ID')):node(mw.html.create('th'):wikitext('Name')):node(mw.html.create('th'):wikitext('Role')):node(mw.html.create('th'):wikitext('Team'))
		tableContainer:node(flagRow):node(headerRow)
		
		for i = 1, #results do
			local result = results[i]
			local playerNat = mw.text.split(result.nationality, ',')[1]
			if playerNat ~= nat then
				local clone = mw.clone(tableContainer)
				container:node(clone)
				nat = playerNat
				tableContainer = mw.html.create('table'):addClass('players__table')
				flagRow = mw.html.create('tr'):node(mw.html.create('th'):attr('colspan', 4):node(mw.html.create('div'):addClass('players__header'):node(Flags.icon(nat)):node(mw.html.create('div'):wikitext(Flags.CountryName(nat)))))
				headerRow = mw.html.create('tr'):addClass('tc'):node(mw.html.create('th'):wikitext('ID')):node(mw.html.create('th'):wikitext('Name')):node(mw.html.create('th'):wikitext('Role')):node(mw.html.create('th'):wikitext('Team'))
				tableContainer:node(flagRow):node(headerRow)
			end
			local flag = Flags.icon(playerNat)
			
			local id = mw.html.create('td'):wikitext('[[' .. result._pageName .. ']]')
			local name = mw.html.create('td'):wikitext(result.romanized or result.name)
			local role = mw.html.create('td'):wikitext(result.active_roles)
			local team = mw.html.create('td')
			
			local currentDate = os.date('%Y-%m-%d')
			
		    tables = "Transfers, Logos"
		    fields = "Transfers._pageName, Logos.logoAll, Logos.logoLight, Logos.logoDark, Logos.iconAll, Logos.iconLight, Logos.iconDark"
		    cargoArgs = {
		    	join = "Transfers._pageName = Logos._pageName",
		    	where = "Transfers.id='" .. result._pageName .. "' AND (Transfers.leavedate IS NULL) AND (Transfers.inactivedate IS NULL) AND ((Logos.start IS NULL OR Logos.start < '" .. currentDate .. "') AND (Logos.end IS NULL OR Logos.end > '" .. currentDate .. "'))"
		    }
			local teamResults = cargo.query(tables, fields, cargoArgs)

			if #teamResults > 0 then
                local logoResults = teamResults[1]
                -- if no images under team
                if not teamResults[1]['Logos.logoAll'] and not teamResults[1]['Logos.logoLight'] and not teamResults[1]['Logos.logoDark'] and not teamResults[1]['Logos.iconAll'] and not teamResults[1]['Logos.iconLight'] and not teamResults[1]['Logos.iconDark'] then
                    tables = "Logos, Teams"
		            fields = "Logos.logoAll, Logos.logoLight, Logos.logoDark, Logos.iconAll, Logos.iconLight, Logos.iconDark"
		            cargoArgs = {
		    	        join = "Logos._pageName = Teams.org",
		    	        where = "Teams._pageName='" .. teamResults[1]['Transfers._pageName'] .. "' AND ((Logos.start IS NULL OR Logos.start < '" .. currentDate .. "') AND (Logos.end IS NULL OR Logos.end > '" .. currentDate .. "'))"
		           }
                   local orgLogos = cargo.query(tables, fields, cargoArgs)

                   if #orgLogos > 0 then logoResults = orgLogos[1] end
                end
				local teamName = teamResults[1]['Transfers._pageName']
				local logoAll = logoResults['Logos.logoAll']
				local logoLight = logoResults['Logos.logoLight']
				local logoDark = logoResults['Logos.logoDark']
				local iconAll = logoResults['Logos.iconAll']
				local iconLight = logoResults['Logos.iconLight']
				local iconDark = logoResults['Logos.iconDark']
				
				if iconAll then
					team:node(p.createLogo(iconAll, teamName, nil, game, size))
				elseif iconLight and iconDark then
					team:node(mw.html.create('div'):node(p.createLogo(iconLight, teamName, 'light', game, size)):node(p.createLogo(iconDark, teamName, 'dark', game, size)))
				elseif logoAll then
					team:node(p.createLogo(logoAll, teamName, nil, game, size))
				elseif logoLight and logoDark then
					team:node(mw.html.create('div'):node(p.createLogo(logoLight, teamName, 'light', game, size)):node(p.createLogo(logoDark, teamName, 'dark', game, size)))
				else 
					local logo = iconLight or iconDark or logoLight or logoDark
					team:node(p.createLogo(logo, teamName, nil, game, size))
				end
			else
				team:wikitext('None')
			end
			
			local playerRow = mw.html.create('tr'):node(id):node(name):node(role):node(team)

			tableContainer:node(playerRow)
		end
		container:node(tableContainer)
	end
	

	return container
end

function p.createLogo(url, teamPage, class, game, size)
	size = size or '20px'
    class = class and class or 'light'
	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