GFX47 / Gladiabots team colors

// ==UserScript==
// @name				 Gladiabots team colors
// @namespace		http://tampermonkey.net/
// @version			0.15
// @description	Mark player names with their team color
// @author			 GFX47
// @match				https://*.toornament.com/*
// @grant				none
// @license			MIT
// ==/UserScript==

let teamPlayerRanks = { 'Red': [], 'Green': [], 'Blue': [] };
let teamPlayerNames = { 'Red': [], 'Green': [], 'Blue': [] };

function GetTeamColor(playerName)
{
	let color = null;
	if (teamPlayerNames.Red.includes(playerName))
	{
		color = 'Red';
	}
	else if (teamPlayerNames.Green.includes(playerName))
	{
		color = 'Green';
	}
	else if (teamPlayerNames.Blue.includes(playerName))
	{
		color = 'Blue';
	}

	return color;
}

function GetPlayerRank(playerName)
{
	let playerIndex = teamPlayerNames.Red.indexOf(playerName, 0);
	if (playerIndex >= 0)
	{
		return teamPlayerRanks.Red[playerIndex];
	}

	playerIndex = teamPlayerNames.Green.indexOf(playerName, 0);
	if (playerIndex >= 0)
	{
		return teamPlayerRanks.Green[playerIndex];
	}

	playerIndex = teamPlayerNames.Blue.indexOf(playerName, 0);
	if (playerIndex >= 0)
	{
		return teamPlayerRanks.Blue[playerIndex];
	}

	return null;
}

function GetTeamPlayers(teamID, teamName)
{
	let xhttp = new XMLHttpRequest();
	xhttp.onreadystatechange = function()
	{
		if (this.readyState == 4 && this.status == 200)
		{
            let players = this.responseText.split('\n');
            for (let playerIndex in players)
            {
                let player = players[playerIndex];
                let separatorIndex = player.indexOf(' ');
                let playerRank = player.substring(0, separatorIndex);
                let playerName = player.substring(separatorIndex + 1);
                teamPlayerRanks[teamName].push(playerRank);
                teamPlayerNames[teamName].push(playerName);
            }
		}
	};

	xhttp.open("GET", "https://api.gladiabots.com/teams.php?apiKey=428rpM4Ryf7gL8kzZEPytYfj87LRJV7E&guild="+teamID, false);
	xhttp.send();
}

window.onload = function()
{
    GetTeamPlayers(1, 'Red');
    GetTeamPlayers(2, 'Green');
    GetTeamPlayers(3, 'Blue');

    let teamScores = { 'Red': 0, 'Green': 0, 'Blue': 0 };
    let teamPlayers = { 'Red': [], 'Green': [], 'Blue': [] };
	let maxTeamScore = 0;
	let rankingElements = document.querySelectorAll('.ranking-item');
	for (let rankingIndex in rankingElements)
	{
		let rankingElement = rankingElements[rankingIndex];
		if (!rankingElement.textContent)
		{
			continue;
		}

		//console.log(rankingElement);

		let rankingPlayerNameElement = rankingElement.querySelector('.name');
		if (!rankingPlayerNameElement.textContent)
		{
			continue;
		}

		let rankingPlayerName = rankingPlayerNameElement.textContent.trim();
		//console.log(rankingPlayerName);

		let rankingTeamColor = GetTeamColor(rankingPlayerName);
		if (!rankingTeamColor)
		{
			continue;
		}

    	teamPlayers[rankingTeamColor].push(rankingPlayerName);
		//console.log(teamPlayers[rankingTeamColor]);

        let scoreElement = rankingElement.querySelector('.points');
		if (!scoreElement.textContent)
		{
			continue;
		}

		scoreElement.innerHTML = '<span style="color: ' + rankingTeamColor + ';">' + scoreElement.innerHTML + '</span>';

		let scoreStr = scoreElement.textContent.trim();
		if (!scoreStr || isNaN(scoreStr))
		{
			continue;
		}

		let score = parseInt(scoreStr);
		//console.log(score);

		teamScores[rankingTeamColor] += score;
		//console.log(rankingTeamColor);
		//console.log(teamScores[rankingTeamColor]);
		if (teamScores[rankingTeamColor] > maxTeamScore)
		{
			maxTeamScore = teamScores[rankingTeamColor];
		}
    }

    let teamCount = 0;
    for (let teamPlayersColor in teamPlayers)
    {
        let teamPlayerCount = teamPlayers[teamPlayersColor].length;
        if (teamPlayerCount > 0)
        {
            ++teamCount;
        }
    }
    console.log(teamCount);

    // Team bars
    if (teamCount > 1)
    {
        let rankingsElement = document.querySelector('.ranking');
        if (rankingsElement != null)
        {
            for (let teamScoresColor in teamScores)
            {
                let teamScore = teamScores[teamScoresColor];

                let teamPlayerCount = teamPlayers[teamScoresColor].length;
                if (teamPlayerCount < 1)
                {
                    continue;
                }

                let barSize = (100 * teamScore / maxTeamScore);
                rankingsElement.innerHTML +=
                    '<div style="background-color: ' + teamScoresColor + '; width: ' + barSize + '%; float: left; height: 20px;"></div>' +
                    '<span style="color: white; text-shadow: black 2px 2px; float: left; margin-left: -' + barSize + '%;">' + teamScoresColor +' Team (' + teamPlayerCount + '): ' + teamScore + '</span>' +
                    '<div style="clear: both;"></div>';
            }
        }
    }

	let playerNameElements = document.querySelectorAll('.name');
	for (let playerIndex in playerNameElements)
	{
		let playerNameElement = playerNameElements[playerIndex];
		if (!playerNameElement.textContent)
		{
			continue;
		}

		let playerName = playerNameElement.textContent.trim();
		if (!playerName)
		{
			continue;
		}
		//console.log(playerName);

		let teamColor = GetTeamColor(playerName);
		if (teamColor != null)
		{
			let playerRank = GetPlayerRank(playerName);
			playerNameElement.innerHTML =
                '<span style="color: ' + teamColor + ';">' +
                playerNameElement.innerHTML +
                //' ('+teamColor+(playerRank > 0 ? ' #'+playerRank : '')+')' +
                (playerRank > 0 ? ' (#'+playerRank+')' : '') +
                '</span>';
		}
	}
};