Module:TeamCard

local p = {}

local placementCell = require('Module:Placement')._placement local getArgs = require('Module:Arguments').getArgs local getFlag = require('Module:Localisation').getCountryName local args

local DEFAULT_TABLE_HEIGHT = 28 local HEIGHT_ROW = 26 local HEIGHT_ROW_PLACEMENT = 30 local prefix local prefixVar local pCount = 1 local cCount = 1 local team local teampage local maxTCheight

function p.draw( frame ) -- Get all arguments args = getArgs(frame) -- Check for a link if args.link then mw.ext.VariablesLua.vardefine('team', args.link) mw.ext.VariablesLua.vardefine('teamsubobj', args.link) else args.link = args.link or args.team if args.link then if mw.ext.TeamTemplate.teamexists( string.lower(args.link) ) then teampage = mw.ext.TeamTemplate.teampage( string.lower(args.link) ) mw.ext.VariablesLua.vardefine('team', teampage) mw.ext.VariablesLua.vardefine('teamsubobj', teampage) else mw.ext.VariablesLua.vardefine('team', args.link) mw.ext.VariablesLua.vardefine('teamsubobj', args.link) end else mw.ext.VariablesLua.vardefine('team', 'TBD') mw.ext.VariablesLua.vardefine('teamsubobj', 'TBD') end end if args.team then team = mw.ext.TeamLiquidIntegration.resolve_redirect(mw.ext.VariablesLua.var('team')) mw.ext.VariablesLua.vardefine('teamRR', team) mw.log('TeamCard ' .. args.team, mw.ext.VariablesLua.var('teamRR')) else team = 'TBD' mw.ext.VariablesLua.vardefine('teamRR', team) end -- Figure out what image to use local teamcardimage if args.image then teamcardimage = 'File:' .. args.image -- Check for a team page elseif args.link then if not teampage then if mw.ext.TeamTemplate.teamexists(string.lower(args.link)) then teampage = frame:expandTemplate{ title = 'Template:TeamPage', args = {string.lower(args.link)}} else teampage = args.link end end -- Get the date local date = mw.ext.VariablesLua.var('date') if not(date and date ~= '') then date = mw.ext.VariablesLua.var('edate') if not(date and date ~= '') then date = mw.ext.VariablesLua.var('sdate') if not(date and date ~= '') then date = os.date( '%F' ) end end end -- Attempt to retrieve image from TeamCard/HistoricImage local historic = frame:expandTemplate{ title = 'TeamCard/HistoricImage', args = { team = teampage, date = date, }}		-- Strip tags from historic image teamcardimage = string.gsub( string.gsub( historic, '%[%[SMW::on%]%]',  ), '%[%[SMW::off%]%]',  )

-- If there still isn't an image, look in LPDB if teamcardimage == nil or teamcardimage == '' then local data = mw.ext.LiquipediaDB.lpdb('team', {				conditions = 'pagename::' .. string.gsub(team, ' ', '_') .. '',				query = 'logo',			})

-- Look at the first (and hopefully only) result data = data[1] if data and data.logo and data.logo ~= '' then teamcardimage = 'File:' .. data.logo else -- Need a module to define default images in Module:TeamCard/default local defaultImages = mw.loadData('Module:TeamCard/default') local game = mw.ext.VariablesLua.var('tournament_game') or 'default' teamcardimage = defaultImages[game] if not teamcardimage then error( 'Add an entry for ' .. game .. ' to Module:TeamCard/default') end end end else -- Need a module to define default images in Module:TeamCard/default local defaultImages = mw.loadData('Module:TeamCard/default') local game = mw.ext.VariablesLua.var('tournament_game') or 'default' teamcardimage = defaultImages[game] if not teamcardimage then error( 'Add an entry for ' .. game .. ' to Module:TeamCard/default') end end -- Hack to remove double file: teamcardimage = string.gsub(teamcardimage, 'File:File:', 'File:') mw.ext.VariablesLua.vardefine('teamcardimage', teamcardimage) -- Wrapper Div local wrapper = mw.html.create('div') :attr('data-toggle-area','1') if args.showroster == 'true' then wrapper:addClass('teamcard teamcardmix toggle-area toggle-area-1 teamcard-opened') else wrapper:addClass('teamcard teamcardmix toggle-area toggle-area-1') end

-- Header/Team Name local header = wrapper:tag('center') if args.team then local link = args.link or args.team header:wikitext( .. args.team .. ) else header:wikitext('TBD') end if args.notes then header:wikitext( .. args.notes .. ) end if args.ref then header:wikitext(args.ref .. '') end -- Inner teamcard local inner = wrapper:tag('div') :addClass('teamcard-inner') -- Set first teamcard table (for main roster) local teamcard1Height = DEFAULT_TABLE_HEIGHT local teamcard1 = inner:tag('table') :addClass('table table-bordered list') :attr('data-toggle-area-content','1') if args.hideroster == 'true' then teamcard1:css('display','none') end -- Show the default number of player rows local t1type = args.t1type or 'default' for i=1, tonumber(args.defaultRowNumber) do prefix = 'p' .. tostring(i) newRow(frame, teamcard1, i, prefix, team, t1type) teamcard1Height = teamcard1Height + HEIGHT_ROW end -- Show the rest of the player rows (up to max limit) if they're filled in 	local compressedRow = false args.maxPlayers = args.maxPlayers or '10' for i=tonumber(args.defaultRowNumber)+1, tonumber(args.maxPlayers) do if args['p' .. tostring(i)] then prefix = 'p' .. tostring(i) newRow(frame, teamcard1, i, prefix, team, t1type) if args.favorDefaultRowNumber and i == tonumber(args.defaultRowNumber)+1 then compressedRow = true else teamcard1Height = teamcard1Height + HEIGHT_ROW end end end

-- Show coaches if filled in 	args.c1 = args.c1 or args.c  	args.c1link = args.c1link or args.clink args.c1flag = args.c1flag or args.cflag if mw.ext.VariablesLua.var('teamcard_coaches') ~= '' or args.c1 then if args.c2 then if args.c2OnNewLine then -- two coaches two rowes for i=1,2 do prefix = 'c' .. tostring(i) newRow(frame, teamcard1, i, prefix, team, t1type, 'coach') if args.favorDefaultRowNumber and compressedRow == false then compressedRow = true else teamcard1Height = teamcard1Height + HEIGHT_ROW end end else -- two coaches one row prefix = 'c'					newRowDouble(frame, teamcard1, prefix, team, t1type, 'coach') if args.favorDefaultRowNumber == nil then teamcard1Height = teamcard1Height + HEIGHT_ROW end end else -- one coach prefix = 'c1' newRow(frame, teamcard1, 1, prefix, team, t1type, 'coach') if args.favorDefaultRowNumber and compressedRow == false then compressedRow = true else teamcard1Height = teamcard1Height + HEIGHT_ROW end end end

-- Show toggle button for second/third teamcard if filled in 	if ((args.t2p1 or args.t2c1 or args.t2c) and (args.t3p1 or args.t3c1 or args.t3c)) then teamcard1Height = teamcard1Height + 27 local teamcardtitle2 = args.t2title or 'Substitutes' local teamcardtitle3 = args.t3title or 'Former' local toggleButtonRow = teamcard1:tag('tr') local toggleButtonRow = toggleButtonRow:tag('td') :attr( 'colspan', '2' ) :css('height','27px') :css('padding','0px !important') :wikitext('' .. teamcardtitle2 .. ' ' .. teamcardtitle3 .. ' ') elseif (args.t2p1 or args.t2c1 or args.t2c) then teamcard1Height = teamcard1Height + 27 local teamcardtitle2 = args.t2title or 'Substitutes' local toggleButtonRow = teamcard1:tag('tr') local toggleButtonRow = toggleButtonRow:tag('td') :attr( 'colspan', '2' ) :css('height','27px') :css('padding','0px !important') :wikitext('' .. teamcardtitle2 .. ' ') end -- Additional Information if args.placement then teamcard1Height = teamcard1Height + HEIGHT_ROW_PLACEMENT

-- Create row for placements local placeRow = teamcard1:tag('tr') local placeCell = placeRow:tag('td') :attr( 'colspan', '2' ) :css('height','30px') :addClass('teamcard-placement') placementCell{parent = placeCell, placement = args.placement} -- Icon (if desired) if args.placementicon then placeCell:wikitext(' ' .. args.placementicon) end end if args.preview then teamcard1Height = teamcard1Height + HEIGHT_ROW

local previewRow = teamcard1:tag('tr') local previewCell = previewRow:tag('td') :attr( 'colspan', '2' ) :addClass('teamcard-preview') :css('height','26px') :wikitext('[' .. args.preview .. ' Preview]') end -- Set second teamcard table (for substitutes) local teamcard2Height = DEFAULT_TABLE_HEIGHT local teamcard2 if (args.t2p1 or args.t2c1 or args.t2c) then teamcard2 = inner:tag('table') :addClass('table table-bordered list') :attr('data-toggle-area-content','2')

-- Show the substitute player rows if they're filled in		local compressedRow = false local t2type = args.t2type or 'sub' for i=1, tonumber(args.maxPlayers) do if args['t2p' .. tostring(i)] then prefix = 't2p' .. tostring(i) newRow(frame, teamcard2, i, prefix, team, t2type) if args.favorDefaultRowNumber and i == tonumber(args.defaultRowNumber)+1 then compressedRow = true else teamcard2Height = teamcard2Height + HEIGHT_ROW end end end

-- Show coaches if filled in		args.t2c1 = args.t2c1 or args.t2c args.t2c1link = args.t2c1link or args.t2clink args.t2c1flag = args.t2c1flag or args.t2cflag if args.t2c1 then for i=1,2 do if args['t2c' .. tostring(i)] then prefix = 't2c' .. tostring(i) newRow(frame, teamcard2, i, prefix, team, t2type, 'coach') if args.favorDefaultRowNumber and compressedRow == false then compressedRow = true else teamcard2Height = teamcard2Height + HEIGHT_ROW end end end end

-- Show toggle button for first/third teamcard if (args.t3p1 or args.t3c1 or args.t3c) then teamcard2Height = teamcard2Height + 27 local teamcardtitle1 = args.t1title or 'Main Roster' local teamcardtitle3 = args.t3title or 'Former' local toggleButtonRow = teamcard2:tag('tr') local toggleButtonRow = toggleButtonRow:tag('td') :attr( 'colspan', '2' ) :css('height','27px') :css('padding','0px !important') :wikitext('' .. teamcardtitle1 .. ' ' .. teamcardtitle3 .. ' ') else teamcard2Height = teamcard2Height + 27 local teamcardtitle1 = args.t1title or 'Main Roster' local toggleButtonRow = teamcard2:tag('tr') local toggleButtonRow = toggleButtonRow:tag('td') :attr( 'colspan', '2' ) :css('height','27px') :css('padding','0px !important') :wikitext('' .. teamcardtitle1 .. ' ') end

-- Additional Information if args.placement then teamcard2Height = teamcard2Height + HEIGHT_ROW_PLACEMENT

-- Create row for placements local placeRow = teamcard2:tag('tr') local placeCell = placeRow:tag('td') :attr( 'colspan', '2' ) :css('height','30px') :addClass('teamcard-placement') placementCell{parent = placeCell, placement = args.placement} -- Icon (if desired) if args.placementicon then placeCell:wikitext(' ' .. args.placementicon) end end

if args.preview then teamcard2Height = teamcard2Height + HEIGHT_ROW

local previewRow = teamcard2:tag('tr') local previewCell = previewRow:tag('td') :attr( 'colspan', '2' ) :addClass('teamcard-preview') :css('height','26px') :wikitext('[' .. args.preview .. ' Preview]') end end -- Set third teamcard table (for former roster) local teamcard3Height = DEFAULT_TABLE_HEIGHT local teamcard3 if (args.t3p1 or args.t3c1 or args.t3c) then teamcard3 = inner:tag('table') :addClass('table table-bordered list') :attr('data-toggle-area-content','3')

-- Show the former player rows if they're filled in		local compressedRow = false local t3type = args.t3type or 'former' for i=1, tonumber(args.maxPlayers) do if args['t3p' .. tostring(i)] then prefix = 't3p' .. tostring(i) newRow(frame, teamcard3, i, prefix, team, t3type) if args.favorDefaultRowNumber and i == tonumber(args.defaultRowNumber)+1 then compressedRow = true else teamcard3Height = teamcard3Height + HEIGHT_ROW end end end

-- Show coaches if filled in		args.t3c1 = args.t3c1 or args.t3c args.t3c1link = args.t3c1link or args.t3clink args.t3c1flag = args.t3c1flag or args.t3cflag if args.t3c1 then for i=1,2 do if args['t3c' .. tostring(i)] then prefix = 't3c' .. tostring(i) newRow(frame, teamcard3, i, prefix, team, t3type, 'coach') if args.favorDefaultRowNumber and compressedRow == false then compressedRow = true else teamcard3Height = teamcard3Height + HEIGHT_ROW end end end end

-- Show toggle button for first/second teamcard teamcard3Height = teamcard3Height + 27 local teamcardtitle1 = args.t1title or 'Main Roster' local teamcardtitle2 = args.t2title or 'Substitutes' local toggleButtonRow = teamcard3:tag('tr') local toggleButtonRow = toggleButtonRow:tag('td') :attr( 'colspan', '2' ) :css('height','27px') :css('padding','0px !important') :wikitext('' .. teamcardtitle1 .. ' ' .. teamcardtitle2 .. ' ')

-- Additional Information if args.placement then teamcard3Height = teamcard3Height + HEIGHT_ROW_PLACEMENT

-- Create row for placements local placeRow = teamcard3:tag('tr') local placeCell = placeRow:tag('td') :attr( 'colspan', '2' ) :css('height','30px') :addClass('teamcard-placement') placementCell{parent = placeCell, placement = args.placement} -- Icon (if desired) if args.placementicon then placeCell:wikitext(' ' .. args.placementicon) end end

if args.preview then teamcard3Height = teamcard3Height + HEIGHT_ROW

local previewRow = teamcard3:tag('tr') local previewCell = previewRow:tag('td') :attr( 'colspan', '2' ) :addClass('teamcard-preview') :css('height','26px') :wikitext('[' .. args.preview .. ' Preview]') end end -- Compare table heights, minimal height and predefined height then set tables to highest value local TCheight = mw.ext.VariablesLua.var('TCheight', '0') local tableHeightFinal = math.max(teamcard1Height, teamcard2Height, teamcard3Height, 121, tonumber(TCheight))

-- Indicator for the tallest card; use to standardized height if tonumber(mw.ext.VariablesLua.var('maxTCheight', '0')) - tonumber(tableHeightFinal) < 0 then maxTCheight = tableHeightFinal mw.ext.VariablesLua.vardefine('maxTCheight', tableHeightFinal) mw.log('TCheight', maxTCheight) end

teamcard1:css('height', tostring(tableHeightFinal) .. 'px;') if (args.t2p1 or args.t2c1 or args.t2c) then teamcard2:css('height', tostring(tableHeightFinal) .. 'px;') end if (args.t3p1 or args.t3c1 or args.t3c) then teamcard3:css('height', tostring(tableHeightFinal) .. 'px;') end -- Set image size local imageHeightFinal = math.max(tableHeightFinal-10, 105, tonumber(TCheight)-10) args.imagesize = args.imagesize or ('190x' .. tostring(imageHeightFinal) .. 'px') -- Create a new table for the logo local logoTable = inner:tag('table') :addClass('table table-bordered logo') local logoRow = logoTable:tag('tr') :css('height', tostring(tableHeightFinal-1) .. 'px;') local logoCell = logoRow:tag('td') :wikitext('' .. args.imagesize .. '|center|link=')

if args.qualifier then local qualRow = logoTable:tag('tr') qualRow:tag('td') :attr('colspan', '2') :addClass('teamcard-qualifier') :wikitext(args.qualifier) end

return wrapper end

function newRow(frame, root, rowNum, prefix, playerTeam, teamcardType, playerType) local prefixPos = args[prefix .. 'pos'] local prefixPosLegacy if playerType == nil then prefixPosLegacy = args['pos' .. tostring(rowNum)] end

-- Make a new row local row = root:tag('tr') if args[prefix ..'dnp'] then row:css('background-color', 'rgba(229, 229, 229, 0.5)') end -- Row identifier if (prefixPos or prefixPosLegacy) and teamcardType == 'default' then row:tag('th') :wikitext(prefixPos or prefixPosLegacy) elseif prefixPos then row:tag('th') :wikitext(prefixPos) elseif playerType == 'coach' and rowNum == 1 then row:tag('th') :css('border-top-width', '2px !important') :wikitext('C ') elseif playerType == 'coach' then row:tag('th') :wikitext('C ') elseif teamcardType == 'sub' then row:tag('th') :wikitext('S ') else row:tag('th') :wikitext(tostring(rowNum)) end -- Player local cell = row:tag('td')

if playerType == 'coach' and rowNum == 1 then cell:css('border-top-width', '2px !important') end

-- Did not play/DNP indicator if args[prefix ..'dnp'] then if args[prefix ..'team'] then cell:wikitext(' DNP ') else cell:wikitext(' DNP ') end end -- Team icon if args[prefix ..'team'] then cell:wikitext(safeExpand(frame, 'TeamPart', {args[prefix ..'team']:lower})) end -- Show flag local data local varflag args[prefix ..'link'] = args[prefix ..'link'] or args[prefix] if args[prefix ..'link'] then data = mw.ext.LiquipediaDB.lpdb('player', {			conditions = 'pagename::' .. string.gsub(mw.ext.TeamLiquidIntegration.resolve_redirect(args[prefix ..'link']), ' ', '_') .. '',			query = 'nationality',		})

data = data[1]	-- Take the only result end if data then cell:wikitext(safeExpand(frame, 'flag/' .. string.lower(data.nationality), {}) .. ' ') varflag = data.nationality elseif args[prefix ..'flag'] then cell:wikitext(safeExpand(frame, 'flag/' .. string.lower(args[prefix ..'flag']), {}) .. ' ') varflag = args[prefix ..'flag'] else cell:wikitext(safeExpand(frame, 'flag/filler flag', {}) .. ' ') end -- Show id 	local varname local vardisplayname if args[prefix] then cell:wikitext( .. args[prefix] .. ) varname = string.gsub(mw.ext.TeamLiquidIntegration.resolve_redirect(args[prefix ..'link']), ' ', '_') vardisplayname = args[prefix] else cell:wikitext('TBD ') end -- Player preview if args[prefix .. 'preview'] then cell:wikitext('') end

playervariables(playerTeam, prefix, varflag, varname, vardisplayname, teamcardType, playerType) end function newRowDouble(frame, root, prefix, playerTeam, teamcardType, playerType) local prefix1 = prefix .. '1'	local prefix2 = prefix .. '2'	-- Make a new row local row = root:tag('tr') -- Row identifier if playerType == 'coach' then row:tag('th') :css('border-top-width', '2px !important') :wikitext('C ') else row:tag('th') end -- Player local cell = row:tag('td')

if playerType == 'coach' then cell:css('border-top-width', '2px !important') end -- first coach -- Show flag local data local varflag args[prefix1 ..'link'] = args[prefix1 ..'link'] or args[prefix1] if args[prefix1 ..'link'] then data = mw.ext.LiquipediaDB.lpdb('player', {			conditions = 'pagename::' .. string.gsub(mw.ext.TeamLiquidIntegration.resolve_redirect(args[prefix1 ..'link']), ' ', '_') .. '',			query = 'nationality',		}) data = data[1]	-- Take the only result end if data then cell:wikitext(safeExpand(frame, 'flag/' .. string.lower(data.nationality), {}) .. ' ') varflag = data.nationality elseif args[prefix1 ..'flag'] then cell:wikitext(safeExpand(frame, 'flag/' .. string.lower(args[prefix1 ..'flag']), {}) .. ' ') varflag = args[prefix ..'flag'] else cell:wikitext(safeExpand(frame, 'flag/filler flag', {}) .. ' ') end -- Show id 	local varname local vardisplayname cell:wikitext('' .. args[prefix1] .. ' ') varname = string.gsub(mw.ext.TeamLiquidIntegration.resolve_redirect(args[prefix1 ..'link']), ' ', '_') vardisplayname = args[prefix] playervariables(playerTeam, prefix1, varflag, varname, vardisplayname, teamcardType, playerType) -- second coach -- Show flag local data local varflag args[prefix2 ..'link'] = args[prefix2 ..'link'] or args[prefix2] if args[prefix2 ..'link'] then data = mw.ext.LiquipediaDB.lpdb('player', {			conditions = 'pagename::' .. string.gsub(mw.ext.TeamLiquidIntegration.resolve_redirect(args[prefix2 ..'link']), ' ', '_') .. '',			query = 'nationality',		}) data = data[1]	-- Take the only result end if data then cell:wikitext(safeExpand(frame, 'flag/' .. string.lower(data.nationality), {}) .. ' ') varflag = data.nationality elseif args[prefix2 ..'flag'] then cell:wikitext(safeExpand(frame, 'flag/' .. string.lower(args[prefix2 ..'flag']), {}) .. ' ') varflag = args[prefix ..'flag'] else cell:wikitext(safeExpand(frame, 'flag/filler flag', {}) .. ' ') end -- Show id 	local varname local vardisplayname cell:wikitext('' .. args[prefix2] .. ' ') varname = string.gsub(mw.ext.TeamLiquidIntegration.resolve_redirect(args[prefix2 ..'link']), ' ', '_') vardisplayname = args[prefix] playervariables(playerTeam, prefix2, varflag, varname, vardisplayname, teamcardType, playerType) end -- Safely expand dyanmically named templates without throwing errors every time it doesn't exist function safeExpand(frame, templateTitle, templateArgs) local result, value = pcall(frame.expandTemplate, frame, {title = templateTitle, args = templateArgs}) if result then return value else return 'Template:' .. templateTitle .. '' end end

function playervariables(playerTeam, prefix, flag, name, displayname, teamcardType, playerType) if playerType == 'coach' then prefixVar = playerTeam .. '_c' .. cCount else prefixVar = playerTeam .. '_p' .. pCount end --case for teamcard 2+3 noVar if args.noVarDefault and teamcardType ~= 'default' and args[prefix ..'played'] and args[prefix ..'dnp'] == nil then mw.ext.VariablesLua.vardefine(prefixVar .. 'flag', getFlag{flag, 'false'}) mw.ext.VariablesLua.vardefine(prefixVar, name) mw.ext.VariablesLua.vardefine(prefixVar .. 'dn', displayname) if playerType == 'coach' then cCount = cCount + 1 else pCount = pCount + 1 end --case for teamcard 2+3 elseif args.noVarDefault == nil and teamcardType ~= 'default' and args[prefix ..'dnp'] == nil then mw.ext.VariablesLua.vardefine(prefixVar .. 'flag', getFlag{flag, 'false'}) mw.ext.VariablesLua.vardefine(prefixVar, name) mw.ext.VariablesLua.vardefine(prefixVar .. 'dn', displayname) if playerType == 'coach' then cCount = cCount + 1 else pCount = pCount + 1 end --normal behaviour elseif teamcardType == 'default' and args[prefix ..'dnp'] == nil then mw.ext.VariablesLua.vardefine(prefixVar .. 'flag', getFlag{flag, 'false'}) mw.ext.VariablesLua.vardefine(prefixVar, name) mw.ext.VariablesLua.vardefine(prefixVar .. 'dn', displayname) if playerType == 'coach' then cCount = cCount + 1 else pCount = pCount + 1 end end end

return p