Module:Squad: Difference between revisions

From TwogPedia
(Created page with "local Class = require('Module:Class') local Arguments = require('Module:Arguments') local String = require('Module:StringUtils') local Squad = Class.new() Squad.TYPE_ACTIVE = 0 Squad.TYPE_INACTIVE = 1 Squad.TYPE_FORMER = 2 Squad.TYPE_FORMER_INACTIVE = 3 function Squad:init(frame) self.frame = frame self.args = Arguments.getArgs(frame) self.root = mw.html.create('div') self.root :addClass('table-responsive') -- TODO: is this needed? :css('margin-bottom', '1...")
 
mNo edit summary
 
(40 intermediate revisions by the same user not shown)
Line 1: Line 1:
local Class = require('Module:Class')
local getArgs = require('Module:Arguments').getArgs
local Arguments = require('Module:Arguments')
local cargo = mw.ext.cargo
local String = require('Module:StringUtils')
local Flags = require('Module:Flags')
local Squad = {}
local stringifyDate = require('Module:Functions').stringifyDate


local Squad = Class.new()
local VariablesLua = mw.ext.VariablesLua


Squad.TYPE_ACTIVE = 0
function Squad.main(frame)
Squad.TYPE_INACTIVE = 1
local args = getArgs(frame)
Squad.TYPE_FORMER = 2
Squad.TYPE_FORMER_INACTIVE = 3


function Squad:init(frame)
if VariablesLua.varexists('Members') == false then return '' end
self.frame = frame
local tables = mw.text.split(VariablesLua.var( 'Members' ), '&&&')
self.args = Arguments.getArgs(frame)
self.root = mw.html.create('div')
self.root :addClass('table-responsive')
-- TODO: is this needed?
:css('margin-bottom', '10px')
-- TODO: is this needed?
:css('padding-bottom', '0px')


self.content = mw.html.create('table')
-- Get all unique properties from Member templates
self.content:addClass('wikitable wikitable-striped roster-card')
properties = {}
 
for i = 1, #tables do
if not String.isEmpty(self.args.team) then
local values = mw.text.jsonDecode(tables[i])
self.args.isLoan = true
for key2, value2 in pairs ( values ) do
if  string.find(mw.text.jsonEncode(properties), '"' .. key2 .. '"') then
else
table.insert(properties, key2)
end
end
end
end
 
local status = (self.args.status or 'active'):lower()
properties = mw.text.jsonEncode(properties)
 
if status == 'inactive' then
local tableContainer = mw.html.create('table')
self.type = Squad.TYPE_INACTIVE
tableContainer:addClass('striped-table')
elseif status == 'former' then
self.type = Squad.TYPE_FORMER
if args.title then
else
local title = mw.html.create('tr')
self.type = Squad.TYPE_ACTIVE
:addClass('headerRow'):node(mw.html.create('th'):addClass('table-title'):attr('colspan', 10):wikitext(args.title))
tableContainer:node(title):node(mw.html.create('tr'))
end
end
local titleRow = mw.html.create('tr')
titleRow:addClass('headerRow')


return self
local idCell = mw.html.create('th')
end
idCell:wikitext('ID')
 
local nameCell = mw.html.create('th')
function Squad:title()
nameCell:wikitext('Name')
local defaultTitle
local positionCell = mw.html.create('th')
if self.type == Squad.TYPE_FORMER then
positionCell:addClass('position')
defaultTitle = 'Former Squad'
positionCell:wikitext('Position')
elseif self.type == Squad.TYPE_INACTIVE then
local joindateCell = mw.html.create('th')
defaultTitle = 'Inactive Squad'
joindateCell:wikitext('Join Date')
else
defaultTitle = 'Active Squad'
titleRow:node(idCell):node(nameCell):node(positionCell):node(joindateCell)
-- If status is not active, then see what other th tags need to be added
if ( args.status ~= 'active' ) then
if ( string.find(properties, '"leavedate"') ) then
local leaveDate = mw.html.create('th')
leaveDate:wikitext('Leave Date')
titleRow:node(leaveDate)
end
if ( string.find(properties, '"inactivedate"') ) then
local inactiveDate = mw.html.create('th')
inactiveDate:wikitext('Inactive Since')
titleRow:node(inactiveDate)
end
if ( string.find(properties, '"newteam"') ) then
local newTeam = mw.html.create('th')
newTeam:wikitext('New team')
titleRow:node(newTeam)
end
end
end
titleRow:node(titleHeader)
tableContainer:node(titleRow)
local currentTitle = mw.title.getCurrentTitle().text
local gameCategory = mw.text.split(currentTitle, '/')[1]
local cargoCaptain = ''
local cargoCoach = ''
local cargoManager = ''
    local cargoAnalyst = ''
    local orgStaff = {}
-- Go through all the Person template instances
for i = 1, #tables do
local values = mw.text.jsonDecode(tables[i])
local tables = 'People'
local fields = '_pageName, nationality, name, romanized'
local cargoArgs = {
where = '_pageName="People/' .. values.id .. '"'
}
local results = cargo.query(tables, fields, cargoArgs)
local personRow = mw.html.create('tr')
personRow:addClass('bodyRow')
local id = mw.html.create('td')
id:addClass('linkID')
if values.flag  then
flag = Flags.icon(values.flag, gameCategory)
id:wikitext(flag)
elseif ( #results > 0 and results[1].nationality ) then
flag = Flags.icon(mw.text.split(results[1].nationality, ',')[1], gameCategory)
id:wikitext(flag)
end


local titleText = String.isEmpty(self.args.title) and defaultTitle or self.args.title
if ( #results > 0 ) then
id:wikitext('<span>[[People/' .. values.id .. ']]</span>')
else
id:wikitext('<span>'..values.id ..'</span>')
end
if ( values.captain == 'Yes' ) then
-- Add captain to cargo table if it's active roster
if args.status == 'active' then
cargoCaptain = 'People/' .. values.id
end


local titleContainer = mw.html.create('tr')
id:wikitext('<span class="captain light">[[File:Captain light.png|18px|link=|Captain]]</span>')
id:wikitext('<span class="captain dark-inline">[[File:Captain dark.png|18px|link=|Captain]]</span>')
end


local titleRow = mw.html.create('th')
local name = mw.html.create('td')
titleRow:addClass('large-only')
name:addClass('playerName')
:attr('colspan', '1')
if values.name then
:wikitext(titleText)
name:wikitext(values.name)
 
elseif ( #results > 0 and (results[1].name or results[1].romanized) ) then
local titleRow2 = mw.html.create('th')
name:wikitext(results[1].romanized or results[1].name)
titleRow2:addClass('large-only')
end
:attr('colspan', '10')
:addClass('roster-title-row2-border')
local role = ''
:wikitext(titleText)
if values.role then
role = ' (' .. values.role .. ')'
end
-- Add coach and manager to cargo table if exist
if string.find(string.upper(values.position), 'COACH')and args.status == 'active' then
                cargoCoach = cargoCoach  .. 'People/' .. values.id .. ','
end
if string.find(string.upper(values.position), 'MANAGER') and args.status == 'active' then
                cargoManager = cargoManager .. 'People/' .. values.id .. ','
end
        if string.find(string.upper(values.position), 'ANALYST') and args.status == 'active' then
                cargoAnalyst = cargoAnalyst .. 'People/' .. values.id .. ','
end
local position = mw.html.create('td')
:addClass('tc')
:wikitext(values.position .. role)
local joindate = mw.html.create('td')
joindate:wikitext(stringifyDate(values.joindate))
personRow:node(id):node(name):node(position):node(joindate)
-- If not active squad, then go add additional columns to table
if ( args.status ~= 'active' ) then
if ( string.find(properties, '"leavedate"') ) then
local leaveText = mw.html.create('td')
:addClass('tc')
:wikitext(stringifyDate(values.leavedate) or '-')
personRow:node(leaveText)
end
if ( string.find(properties, '"inactivedate"') ) then
local inactiveDate = mw.html.create('td')
:addClass('tc')
:wikitext(values.inactivedate or '-')
personRow:node(inactiveDate)
end


titleContainer:node(titleRow):node(titleRow2)
if ( string.find(properties, '"newteam"')) then
self.content:node(titleContainer)
local newTeam = mw.html.create('td')
 
if values.newteam then
return self
--Check if page newteam exists and if it does, then link to it, otherwise string only
end
local pageExists = frame:callParserFunction('#ifexist', gameCategory .. '/' .. values.newteam, 'yes', 'no' )
 
if ( pageExists == 'yes' ) then
function Squad:header()
newTeam:wikitext('<span>[[' .. gameCategory .. '/' .. values.newteam .. '|' .. values.newteam  .. ']]</span>')
local makeHeader = function(wikiText)
else
local headerCell = mw.html.create('th')
newTeam:wikitext(values.newteam)
 
end
if wikiText == nil then
else
return headerCell
newTeam:wikitext('')
end
personRow:node(newTeam)
end
end
end
 
        -- If position indicates being part of the org rather than a player
return headerCell:wikitext(wikiText):addClass('divCell')
        if string.find(string.upper(values.position), 'MANAGER') or string.find(string.upper(values.position), 'COACH') or string.find(string.upper(values.position), 'ANALYST') or string.find(string.upper(values.position), 'MANAGER') then
            table.insert(orgStaff, personRow)
        else
            tableContainer:node(personRow)
        end
end
end
 
 
local headerRow = mw.html.create('tr'):addClass('HeaderRow')
    -- If had non-players
 
    if #orgStaff > 0 then
headerRow :node(makeHeader('ID'))
        tableContainer:node(mw.html.create('tr'):addClass('headerRow'):node(mw.html.create('th'):attr('colspan', 10):addClass('table-title'):wikitext('Staff')))
:node(makeHeader()) -- "Team Icon" (most commmonly used for loans)
        for i = 1, #orgStaff do
:node(makeHeader('Name'))
            tableContainer:node(orgStaff[i])
:node(makeHeader()) -- "Role"
        end
:node(makeHeader('Join Date'))
    end
if self.type == Squad.TYPE_FORMER then
if args.status == 'active' then
headerRow :node(makeHeader('Leave Date'))
        -- remove excess comma from cargoCoach and cargoManager
:node(makeHeader('New Team'))
        local length = string.len(cargoCoach)
elseif self.type == Squad.TYPE_INACTIVE then
        if length > 0 and string.sub(cargoCoach, length) == "," then
headerRow:node(makeHeader('Inactive Date'))
            cargoCoach = string.sub(cargoCoach , 1, length - 1)
        end
        local length = string.len(cargoManager)
        if length > 0 and string.sub(cargoManager, length) == "," then
            cargoManager = string.sub(cargoManager, 1, length - 1)
        end
        local length = string.len(cargoAnalyst)
        if length > 0 and string.sub(cargoAnalyst, length) == "," then
            cargoAnalyst = string.sub(cargoAnalyst, 1, length - 1)
        end
frame:callParserFunction{name = '#cargo_store:', args = {_table = 'Squads', captain = cargoCaptain, coach = cargoCoach, manager = cargoManager, analyst = cargoAnalyst}}
end
end
 
VariablesLua.vardefine( 'Members', nil )
self.content:node(headerRow)
return tableContainer
 
return self
end
 
function Squad:row(row)
self.content:node(row)
return self
end
 
function Squad:create()
self.root:node(self.content)
 
return self.root
end
end


return Squad
return Squad

Latest revision as of 16:57, 9 April 2024

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

local getArgs = require('Module:Arguments').getArgs
local cargo = mw.ext.cargo
local Flags = require('Module:Flags')
local Squad = {}
local stringifyDate = require('Module:Functions').stringifyDate

local VariablesLua = mw.ext.VariablesLua

function Squad.main(frame)
	local args = getArgs(frame)

	if VariablesLua.varexists('Members') == false then return '' end
	local tables = mw.text.split(VariablesLua.var( 'Members' ), '&&&')

	-- Get all unique properties from Member templates
	properties = {}
	for i = 1, #tables do
		local values = mw.text.jsonDecode(tables[i])
		for key2, value2 in pairs ( values ) do
			if  string.find(mw.text.jsonEncode(properties), '"' .. key2 .. '"') then
			
			else
				table.insert(properties, key2)
			end
		end
	end
	
	properties = mw.text.jsonEncode(properties)
		
	local tableContainer = mw.html.create('table')
	tableContainer:addClass('striped-table')
	
	if args.title then
		local title = mw.html.create('tr')
			:addClass('headerRow'):node(mw.html.create('th'):addClass('table-title'):attr('colspan', 10):wikitext(args.title))
		tableContainer:node(title):node(mw.html.create('tr'))
	end
	
	local titleRow = mw.html.create('tr')
	titleRow:addClass('headerRow')

	local idCell = mw.html.create('th')
		idCell:wikitext('ID')
	local nameCell = mw.html.create('th')
		nameCell:wikitext('Name')
	local positionCell = mw.html.create('th')
		positionCell:addClass('position')
		positionCell:wikitext('Position')
	local joindateCell = mw.html.create('th')
		joindateCell:wikitext('Join Date')
		
	titleRow:node(idCell):node(nameCell):node(positionCell):node(joindateCell)
	
	-- If status is not active, then see what other th tags need to be added
	if ( args.status ~= 'active' ) then
		if ( string.find(properties, '"leavedate"') ) then
			local leaveDate = mw.html.create('th')
				leaveDate:wikitext('Leave Date')
				titleRow:node(leaveDate)
		end
		
		if ( string.find(properties, '"inactivedate"') ) then
			local inactiveDate = mw.html.create('th')
				inactiveDate:wikitext('Inactive Since')
				titleRow:node(inactiveDate)
		end
		
		if ( string.find(properties, '"newteam"') ) then
			local newTeam = mw.html.create('th')
				newTeam:wikitext('New team')
				titleRow:node(newTeam)
		end
	end
	
	titleRow:node(titleHeader)
	
	tableContainer:node(titleRow)
	
	local currentTitle = mw.title.getCurrentTitle().text
	local gameCategory = mw.text.split(currentTitle, '/')[1]
	
	local cargoCaptain = ''
	local cargoCoach = '' 
	local cargoManager = ''
    local cargoAnalyst = ''
	
    local orgStaff = {}
	-- Go through all the Person template instances
	for i = 1, #tables do
		local values = mw.text.jsonDecode(tables[i])
	
		local tables = 'People'
		local fields = '_pageName, nationality, name, romanized'
		local cargoArgs = {
			where = '_pageName="People/' .. values.id .. '"'
		}
		local results = cargo.query(tables, fields, cargoArgs)
		
		local personRow = mw.html.create('tr')
			personRow:addClass('bodyRow')
		local id = mw.html.create('td')
			id:addClass('linkID')
			
		if values.flag  then
			flag = Flags.icon(values.flag, gameCategory)
			id:wikitext(flag)
		elseif ( #results > 0 and results[1].nationality ) then 
			flag = Flags.icon(mw.text.split(results[1].nationality, ',')[1], gameCategory)
			id:wikitext(flag)
		end

		if ( #results > 0 ) then 
			id:wikitext('<span>[[People/' .. values.id .. ']]</span>')
		else
			id:wikitext('<span>'..values.id ..'</span>')
		end
		
		if ( values.captain == 'Yes' ) then
			-- Add captain to cargo table if it's active roster
			if args.status == 'active' then
				cargoCaptain = 'People/' .. values.id
			end

			id:wikitext('<span class="captain light">[[File:Captain light.png|18px|link=|Captain]]</span>')
			id:wikitext('<span class="captain dark-inline">[[File:Captain dark.png|18px|link=|Captain]]</span>')
		end
			

		local name = mw.html.create('td')
			name:addClass('playerName')
		if values.name then
			name:wikitext(values.name)
		elseif ( #results > 0 and (results[1].name or results[1].romanized) ) then
			name:wikitext(results[1].romanized or results[1].name)
		end
			
		local role = ''
		if values.role then
			role = ' (' .. values.role .. ')'
		end
		
		-- Add coach and manager to cargo table if exist
		if string.find(string.upper(values.position), 'COACH')and args.status == 'active' then
                cargoCoach = cargoCoach  .. 'People/' .. values.id .. ','
		end
		if string.find(string.upper(values.position), 'MANAGER') and args.status == 'active' then
                cargoManager = cargoManager .. 'People/' .. values.id .. ','
		end
        if string.find(string.upper(values.position), 'ANALYST') and args.status == 'active' then
                cargoAnalyst = cargoAnalyst .. 'People/' .. values.id .. ','
		end
		
		local position = mw.html.create('td')
			:addClass('tc')
			:wikitext(values.position .. role)
		local joindate = mw.html.create('td')
			joindate:wikitext(stringifyDate(values.joindate))
		
		personRow:node(id):node(name):node(position):node(joindate)
		
		-- If not active squad, then go add additional columns to table
		if ( args.status ~= 'active' ) then
			if ( string.find(properties, '"leavedate"') ) then
				local leaveText = mw.html.create('td')
					:addClass('tc')
					:wikitext(stringifyDate(values.leavedate) or '-')
				personRow:node(leaveText)
			end
			
			if ( string.find(properties, '"inactivedate"') ) then
				local inactiveDate = mw.html.create('td')
					:addClass('tc')
					:wikitext(values.inactivedate or '-')
				personRow:node(inactiveDate)
			end

			if ( string.find(properties, '"newteam"')) then
				local newTeam = mw.html.create('td')
				if values.newteam then
					--Check if page newteam exists and if it does, then link to it, otherwise string only
					local pageExists = frame:callParserFunction('#ifexist', gameCategory .. '/' .. values.newteam, 'yes', 'no' )
					if ( pageExists == 'yes' ) then 
						newTeam:wikitext('<span>[[' .. gameCategory .. '/' .. values.newteam .. '|' .. values.newteam  .. ']]</span>')
					else
						newTeam:wikitext(values.newteam)
					end
				else 
					newTeam:wikitext('')
				end
				
				personRow:node(newTeam)
			end
		end
        -- If position indicates being part of the org rather than a player
        if string.find(string.upper(values.position), 'MANAGER') or string.find(string.upper(values.position), 'COACH') or string.find(string.upper(values.position), 'ANALYST') or string.find(string.upper(values.position), 'MANAGER') then
            table.insert(orgStaff, personRow)
        else
            tableContainer:node(personRow)
        end
	end
   
    -- If had non-players
    if #orgStaff > 0 then
        tableContainer:node(mw.html.create('tr'):addClass('headerRow'):node(mw.html.create('th'):attr('colspan', 10):addClass('table-title'):wikitext('Staff')))
        for i = 1, #orgStaff do
            tableContainer:node(orgStaff[i])
        end
    end
	if args.status == 'active' then
        -- remove excess comma from cargoCoach and cargoManager
        local length = string.len(cargoCoach)
        if length > 0 and string.sub(cargoCoach, length) == "," then
            cargoCoach = string.sub(cargoCoach , 1, length - 1)
        end
        local length = string.len(cargoManager)
        if length > 0 and string.sub(cargoManager, length) == "," then
            cargoManager = string.sub(cargoManager, 1, length - 1)
        end
        local length = string.len(cargoAnalyst)
        if length > 0 and string.sub(cargoAnalyst, length) == "," then
            cargoAnalyst = string.sub(cargoAnalyst, 1, length - 1)
        end
		frame:callParserFunction{name = '#cargo_store:', args = {_table = 'Squads', captain = cargoCaptain, coach = cargoCoach, manager = cargoManager, analyst = cargoAnalyst}}
	end
	VariablesLua.vardefine( 'Members', nil )
	return tableContainer
end

return Squad