Skip to content

Instantly share code, notes, and snippets.

@MarksCode
Created November 24, 2017 06:54
Show Gist options
  • Save MarksCode/0d0d6781b208b71ab3eabe0b6e09a4e5 to your computer and use it in GitHub Desktop.
Save MarksCode/0d0d6781b208b71ab3eabe0b6e09a4e5 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Teammate Win Percents
// @author EphewSeakay (Feat. Capernicus aka u/StraightZlat)
// @version 0.1
// @include http://*.koalabeast.com:*
// @include http://tagpro-*.koalabeast.com*
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
(tagpro.ready(function() {
if (tagpro.state) { // make sure were actually in a game
/* Get the stats table */
var statsTable = document.getElementById("stats");
/* Insert new header */
var tableHeader = statsTable.tHead; // get header of table
var firstTr = tableHeader.rows[0]; // get first table row of header
var numberCellsInRow = firstTr.cells.length; // get number of children that row has
var newHeader = document.createElement("th"); // create a header cell element
newHeader.innerText = "Buddy %"; // set text of new cell
var newHeader2 = document.createElement("th");
newHeader2.innerText = "Against %";
firstTr.insertBefore(newHeader2, firstTr.cells[numberCellsInRow - 1]); // add new cell
firstTr.insertBefore(newHeader, firstTr.cells[numberCellsInRow - 1]);
/* Insert data */
var tableBody = statsTable.tBodies[0];
var rows = tableBody.rows; // array containing rows of table body (1 for each player)
/* This code will get ran every time the table updates (about once per second when table is open)
We need this because tagpro re-renders the table every second or so to reflect each players updated stats
So we need to add our Buddy % cell for each row every time it updates. */
tagpro.events.register({
modifyScoreUI: function() {
for (var i = 0; i < rows.length; i++) { // loop through every row of table body
var currentRow = rows[i]; // get current row (at i'th index)
var firstChild = currentRow.cells[0]; // cell of row with player's name
var nameSpan = firstChild.getElementsByClassName("scoreName")[0]; // part of cell that holds player's name
var name = nameSpan.innerText; // extract player's name from cell
if (!name) { // make sure name is actually there
continue; // if name is not there, go to next iteration of loop
}
var playerData = GM_getValue(name, '{"totalGamesWith": 0, "totalGamesAgainst": 0, "winsWith": 0, "winsAgainst": 0}'); // get data stored for that player
var formattedData = JSON.parse(playerData);
var gamesPlayedWith = formattedData["totalGamesWith"];
var gamesPlayedAgainst = formattedData["totalGamesAgainst"];
var winsWith = formattedData["winsWith"];
var winsAgainst = formattedData["winsAgainst"];
var winPercent, percentAgainst;
if (gamesPlayedWith > 0) {
winPercent = winsWith / gamesPlayedWith * 100;
winPercent = Math.round(winPercent * 100) / 100; // round percents to nearest .01
} else {
winPercent = "N/A";
}
if (gamesPlayedAgainst > 0) {
percentAgainst = winsAgainst / gamesPlayedAgainst * 100;
percentAgainst = Math.round(percentAgainst * 100) / 100;
} else {
percentAgainst = "N/A";
}
var newCell = currentRow.insertCell(numberCellsInRow - 1); // insert a new cell inside of the current row
if (typeof(percentAgainst) == "number") {
newCell.style.color = getColor(percentAgainst/100);
}
newCell.innerText = percentAgainst; // set inner text of new cell
var newCell2 = currentRow.insertCell(numberCellsInRow - 1);
if (typeof(winPercent) == "number") {
newCell2.style.color = getColor(winPercent/100);
}
newCell2.innerText = winPercent;
}
}
});
/* Wait for game to end, set data once it does */
if (tagpro.socket) {
tagpro.socket.on('end', function(e) {
if (!tagpro.spectator) {
var me = tagpro.players[tagpro.playerId]; // me (tagpro.playerId is my id)
var myTeam = me.team; // get my team number (1=red, 2=blue)
/* Get all players on my team */
var players = tagpro.players;
var playersOnOurTeam = [];
var playersAgainst = [];
for (var playerId in players) { // loop through each player
if (playerId != tagpro.playerId) { // check so we dont add ourselves to playersOnOurTeam
var playerData = players[playerId]; // get that player's data
var playerTeam = playerData.team;
if (playerTeam == myTeam) { // check if player is on our team
playersOnOurTeam.push(playerData); // add player's data to our team array
} else { // check if player is on the other team
playersAgainst.push(playerData); // add player's data to our against array
}
}
}
/* Check if I won */
var winner = e.winner;
var didWin = false;
// if red won, and my team is the red team, or if blue won and my team is the blue team, we won
if ((winner == 'red' && myTeam == 1) || (winner == 'blue' && myTeam == 2)) {
didWin = true;
}
/*
Loop through all players on my team
*/
for (var i = 0; i < playersOnOurTeam.length; i++) {
var player = playersOnOurTeam[i];
var pastData = GM_getValue(player.name, '{"totalGamesWith": 0, "totalGamesAgainst": 0, "winsWith": 0, "winsAgainst": 0}');
var formattedData = JSON.parse(pastData); // Need to format data correctly
var winsWithPlayer = formattedData["winsWith"];
if (didWin) { // if we won, then increment the number of wins we've won with the player
winsWithPlayer++;
}
// this is the object that we will store
dataToStore = {
"totalGamesWith": formattedData["totalGamesWith"] + 1,
"totalGamesAgainst": formattedData["totalGamesAgainst"],
"winsWith": winsWithPlayer,
"winsAgainst": formattedData["winsAgainst"]
};
// save data
GM_setValue(player.name, JSON.stringify(dataToStore)); // JSON.stringify encodes the data in a way that it can be stored
}
for (var i = 0; i < playersAgainst.length; i++) {
var player = playersAgainst[i];
var pastData = GM_getValue(player.name, '{"totalGamesWith": 0, "totalGamesAgainst": 0, "winsWith": 0, "winsAgainst": 0}');
var formattedData = JSON.parse(pastData); // Need to format data correctly
var winsAgainst = formattedData["winsAgainst"];
if (didWin) { // if we won, then increment the number of wins we've won against the player
winsAgainst++;
}
dataToStore = {
"totalGamesWith": formattedData["totalGamesWith"],
"totalGamesAgainst": formattedData["totalGamesAgainst"] + 1,
"winsWith": formattedData["winsWith"],
"winsAgainst": winsAgainst
};
// save data
GM_setValue(player.name, JSON.stringify(dataToStore)); // JSON.stringify encodes the data in a way that it can be stored
}
}
})
}
}
function getColor(val){
var hue=(val*120).toString(10);
return ["hsl(",hue,",100%,50%)"].join("");
}
}))();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment