Created
November 24, 2017 00:01
-
-
Save MarksCode/b1f1906f69cd97f141fa4542b76efab8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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(newHeader, firstTr.cells[numberCellsInRow - 1]); // add new cell | |
firstTr.insertBefore(newHeader2, 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 numberOfCellsInRow = currentRow.cells.length; // get number of cells in that row | |
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, '{"totalGames": 0, "winsWith": "N/A", "winsAgainst": "N/A"}');// get data stored for that player | |
var formattedData = JSON.parse(playerData); | |
var gamesPlayed = formattedData["totalGames"]; | |
var winsWithPlayer = formattedData["winsWith"]; | |
var winsAgainst = formattedData["winsAgainst"]; | |
var winPercent = "N/A"; | |
var percentAgainst = "N/A"; | |
if (gamesPlayed == 0) { | |
winPercent = "N/A"; | |
percentAgainst = "N/A"; | |
} else { | |
if (winsWithPlayer != "N/A") { | |
winPercent = winsWithPlayer / gamesPlayed * 100; | |
winPercent = Math.round(winPercent * 100) / 100; // round percents to nearest .01 | |
} | |
if (winsAgainst != "N/A") { | |
percentAgainst = winsAgainst / gamesPlayed * 100; | |
percentAgainst = Math.round(percentAgainst * 100) / 100; | |
} | |
} | |
var newCell = currentRow.insertCell(numberOfCellsInRow - 1); // insert a new cell inside of the current row | |
newCell.innerText = winPercent; // set inner text of new cell | |
var newCell2 = currentRow.insertCell(numberOfCellsInRow - 1); | |
newCell2.innerText = percentAgainst; | |
} | |
} | |
}); | |
/* 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]; | |
// values are stored as an object (same thing as dictionary in python) | |
// format of data is dictionary["personsName"] = {"totalGames": 0, "wins": 0} | |
var pastData = GM_getValue(player.name, '{"totalGames": 0, "winsWith": 0, "winsAgainst": "N/A"}'); | |
var formattedData = JSON.parse(pastData); // Need to format data correctly | |
var gamesPlayed = formattedData["totalGames"]; | |
var winsWithPlayer = formattedData["winsWith"]; | |
var winsAgainst = formattedData["winsAgainst"]; | |
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 = { | |
"totalGames": gamesPlayed + 1, | |
"winsWith": winsWithPlayer, | |
"winsAgainst": 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, '{"totalGames": 0, "winsWith": "N/A", "winsAgainst":0}'); | |
var formattedData = JSON.parse(pastData); // Need to format data correctly | |
var gamesPlayed = formattedData["totalGames"]; | |
var winsWith = formattedData["winsWith"]; | |
var winsAgainst = formattedData["winsAgainst"]; | |
if (didWin) { | |
winsAgainst++; | |
} | |
dataToStore = { | |
"totalGames": gamesPlayed + 1, | |
"winsWith": winsWith, | |
"winsAgainst": winsAgainst | |
}; | |
GM_setValue(player.name, JSON.stringify(dataToStore)); | |
} | |
} | |
}) | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment