Module:TeamRoster

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_esports = require('Module:EsportsUtil') local util_game = require('Module:GameUtil') local util_html = require('Module:HtmlUtil') local util_table = require('Module:TableUtil') local util_text = require('Module:TextUtil') local util_toggle = require('Module:ToggleUtil') local util_vars = require('Module:VarsUtil') local m_country = require('Module:Country') local m_team = require('Module:Team') local Sprite = require('Module:Sprite').sprite local lang = mw.getLanguage('en') local s = {}

function s.FlagSprite(id) return Sprite{ id, type = 'Flag', notext = true, nolink = true, } end

local PLAYER_FIELD_LIST = { 'Link', 'Flag', 'FootnoteN' }

local h = {}

local p = {} function p.main(frame) local args = util_args.merge(true) for k, _ in pairs(args) do		if tostring(k):match('^[a-z]+%d+$') then return k .. ''		end end util_vars.setGlobalIndex('rosterN') local playerData = h.getPlayerData(args) local teamData = h.getTeamData(args, playerData) h.storeCargo(teamData, args.nocargo) return h.makeOutput(playerData, teamData, args) end

function h.getPlayerData(args) local i = 1 local playerData = {} while i <= util_game.players_per_team or args[i] do		playerData[i] = args[i] and util_args.splitArgs(args[i], PLAYER_FIELD_LIST) or {} h.castPlayerRow(playerData[i], i)		i = i + 1 end h.cropPlayerResults(playerData) h.addTeamDataToPlayers(playerData, args) return playerData end

function h.castPlayerRow(row, i)	row.N = i	row.Flag = h.getFlag(row.Flag) row.LinkInput = row.Link row.Link = util_esports.playerLink(row.Link) end

function h.getFlag(flag) if flag then return m_country.name(flag) end return nil end

function h.cropPlayerResults(playerData) for k, v in ipairs(playerData) do		if k > util_game.players_per_team and not v.Link then playerData[k] = nil end end end

function h.addTeamDataToPlayers(playerData, args) for i, row in ipairs(playerData) do		h.addTeamDataToOnePlayer(i, row, args) end end

function h.addTeamDataToOnePlayer(i, row, args) row.TeamLink = m_team.teamlinkname(args.team) end

function h.getTeamData(args, playerData) local teamData = { _table = 'TournamentRosters', Team = m_team.teamlinkname(args.team), TeamLink = m_team.teamlinkname(args.team), Tournament = util_vars.getVar('Event Name'), UniqueLine = h.getUniqueLine(args), IsComplete = util_args.castAsBool(args.isincomplete), Team_Markup = m_team.rightlonglinked(args.team), }	h.addPageAndTeam(teamData) h.addPlayerDataToTeam(teamData, playerData) return teamData end

function h.getUniqueLine(args) if args.place then return util_cargo.getUniqueLine(tonumber(args.place)) end util_vars.setGlobalIndex('positionIndex') return util_cargo.getUniqueLine('rosterN', 'positionIndex') end

function h.addPageAndTeam(teamData) teamData.PageAndTeam = mw.title.getCurrentTitle.text .. '_' .. teamData.Team end

function h.addPlayerDataToTeam(teamData, playerData) teamData.Roster = h.concatPlayerParam(playerData, 'LinkInput', util_esports.playersDisplay) teamData.RosterLinks = h.concatPlayerParam(playerData, 'LinkInput', util_esports.playersLink) teamData.Flags = h.concatPlayerParam(playerData, 'Flag') end

function h.concatPlayerParam(playerData, param, f)	local ret = {} for _, player in ipairs(playerData) do		ret[#ret+1] = f and f(player[param]) or player[param] end return util_table.concat(ret, ';;') end

function h.concatPlayerLinked(playerData) local ret = {} for _, player in ipairs(playerData) do		ret[#ret+1] = util_esports.playerLinked(player.Link) end return util_table.concat(ret, ';;') end

function h.storeCargo(teamData, nocargo) if util_cargo.doWeStoreCargo(nocargo, '') then util_cargo.store(teamData) end end

function h.makeOutput(playerData, teamData, args) util_toggle.initSectionToggler local tbl = h.initTable h.printCaption(tbl, args.seed) h.printHeader(tbl, teamData.Team, args.footnoteteamn) h.printLogo(tbl, teamData.Team) h.printPlayers(tbl, playerData) return tbl end function h.initTable local tbl = mw.html.create('table') :addClass('wikitable tournament-roster') return tbl end

function h.printCaption(tbl, seed) tbl:tag('caption') :addClass('tournament-roster-span') :wikitext(seed) end

function h.printHeader(tbl, team, footnoten) local th = tbl:tag('tr'):tag('th') :addClass('tournament-roster-header') local outerdiv = th:tag('div') :css('position','relative') h.printNameAndFootnote(outerdiv, team, footnoten) local innerdiv = outerdiv:tag('div') :css({ position = "absolute", right = "2px", top = '0' }) h.printOneToggler(innerdiv, 'Show', 'RosterLogos', false) h.printOneToggler(innerdiv, 'Hide', 'RosterPlayers', true) end

function h.printNameAndFootnote(div, team, footnoten) div:wikitext(m_team.mediumplainlinked(team)) :wikitext(util_html.makeFootnoteN(footnoten)) end

function h.printOneToggler(div, image, class, isHidden) local span = div:tag('span') :addClass(class) :wikitext((''):format(image)) util_toggle.tagSectionToggler(span, isHidden) end

function h.printLogo(tbl, team) local logo_tr = tbl:tag('tr') :addClass('RosterLogos') local logo = logo_tr:tag('td') :addClass('tournament-roster-logo-cell') :wikitext(m_team.square(team, { size = 123, title = "Click to Show Roster" })) util_toggle.tagSectionToggler(logo_tr) end

function h.printPlayers(tbl, playerData) for _, player in ipairs(playerData) do		h.printOnePlayer(tbl, player) end end

function h.printOnePlayer(tbl, player) local tr = h.initPlayerRow(tbl) h.printPlayerCell(tr, player) end

function h.initPlayerRow(tbl) local tr = tbl:tag('tr') :addClass('RosterPlayers') util_toggle.tagSectionToggle(tr, true) return tr end

function h.printPlayerCell(tr, player) local td = tr:tag('td') :addClass('tournament-roster-player-cell') h.printFlagAndName(td, player) end

function h.printFlagAndName(td, player) td:tag('span') :addClass('tournament-roster-player') :wikitext(s.FlagSprite(player.Flag)) :wikitext(' ') :wikitext(util_esports.playerLinked(player.Link)) if player.FootnoteN then td:wikitext(util_html.makeFootnoteN(player.FootnoteN)) end end

return p