Module:StoreTournamentRoster

From Call of Duty Esports Wiki
Jump to: navigation, search

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

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_map = require('Module:MapUtil')
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 Country = require('Module:Country')
local RoleList = require('Module:RoleList')
local m_team = require('Module:Team')
local Sprite = require('Module:Sprite').sprite

local NewsCargo = require('Module:TeamRoster/NewsCargo').main

local lang = mw.getLanguage('en')
local s = {}

function s.RoleSprite(id)
	return Sprite{
		id,
		size = '15',
		type = 'Role',
		notext = true,
		nolink = true,
	}
end

local PLAYER_FIELD_LIST = { 'Player', 'Flag', 'Role', 'FootnoteN', 'TeamOrder', 'NoDistribution' }

local h = {}
local p = {}

-- public
function p.init()
	util_vars.setGlobalIndex('rosterN')
end

function p.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 p.getTeamData(args, playerData)
	local teamData = {
		_table = 'TournamentRosters',
		Team = m_team.teamlinkname(args.team),
		Tournament = util_vars.getVar('Event Name'),
		UniqueLine = util_cargo.getUniqueLine('rosterN'),
		IsComplete = util_args.castAsBool(args.isincomplete),
		Team_Markup = m_team.rightlonglinked(args.team),
		Region = args.region,
		Short = args.short,
	}
	teamData.PageAndTeam = h.getPageAndTeam(teamData)
	teamData.OverviewPage = h.getOverviewPage()
	h.addPlayerDataToTeam(teamData, playerData)
	return teamData
end

function p.store(playerData, teamData, args)
	if util_vars.getBool('is_data') then
		h.storeRosterCargo(playerData, teamData, args, 'Data')
		h.storeArgsCargo(args, 'Data')
		NewsCargo(playerData, teamData, args, 'Data')
	else
		h.storeRosterCargo(playerData, teamData, args, '')
	end
end

-- private
-- get player data
function h.castPlayerRow(row, i)
	row.N_PlayerInTeam = i
	row.Flag = Country(row.Flag)
	row.Role = RoleList(row.Role, { sep = ',' })
	row.Link = row.Player
	row.UniqueLine = util_cargo.getUniqueLine('rosterN', i)
	row._table = 'TournamentPlayers'
	row.OverviewPage = h.getOverviewPage()
	row.TeamOrder = row.TeamOrder or 1
	row.IsDistribution = not util_args.castAsBool(row.NoDistribution)
end

function h.cropPlayerResults(playerData)
	for k, v in ipairs(playerData) do
		if k > util_game.players_per_team and not v.Player 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.Team = m_team.teamlinkname(args.team)
	row.PageAndTeam = h.getPageAndTeam(row)
end

-- get team data
function h.getPageAndTeam(teamData)
	return h.getOverviewPage() .. '_' .. teamData.Team
end

function h.addPlayerDataToTeam(teamData, playerData)
	teamData.RosterLinks = h.concatPlayerParam(playerData, 'Link')
	teamData.Roles = h.concatPlayerParam(playerData, 'Role')
	teamData.Flags = h.concatPlayerParam(playerData, 'Flag')
end

function h.concatPlayerParam(playerData, param)
	local ret = {}
	for _, player in ipairs(playerData) do
		if player[param] then
			ret[#ret+1] = tostring(player[param])
		end
	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

-- store cargo
function h.storeRosterCargo(playerData, teamData, args, ns)
	teamData.RosterLinks = tostring(teamData.RosterLinks)
	if util_cargo.doWeStoreCargo(args.nocargo, ns or '') then
		h.storePlayers(playerData)
		util_cargo.store(teamData)
	end
end

function h.storePlayers(playerData)
	for _, row in ipairs(playerData) do
		if row.Link then
			util_cargo.store(row)
		end
	end
end

function h.storeArgsCargo(args, ns)
	local toStore = {
		_table = 'ParticipantsArgs',
		N_TeamInPage = util_vars.getGlobalIndex('rosterN'),
		Args = util_cargo.concatArgsForStore(args),
		OverviewPage = h.getOverviewPage(),
	}
	if util_cargo.doWeStoreCargo(args.nocargo, ns or 'Data') then
		util_cargo.store(toStore)
	end
end

function h.getOverviewPage()
	return mw.title.getCurrentTitle().text:gsub('/Participants', '')
end

return p