Created
November 11, 2017 21:00
-
-
Save MarksCode/f04a1eca180461bfebb36cd16c355e01 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 % | |
// @author Ephew | |
// @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 x = firstTr.insertCell(numberCellsInRow - 2); // insert cell at second to last spot in row | |
x.innerText = "Buddy %"; // set text of new cell | |
/* | |
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, "wins": 0}');// get data stored for that player | |
var formattedData = JSON.parse(playerData); | |
var gamesPlayed = formattedData["totalGames"]; | |
var winsWithPlayer = formattedData["wins"]; | |
var winPercent = winsWithPlayer / gamesPlayed * 100; // calculate win percent for that player | |
if (gamesPlayed == 0) { | |
winPercent = "N/A"; | |
} | |
var newCell = currentRow.insertCell(numberOfCellsInRow - 2); // insert a new cell inside of the current row | |
newCell.innerText = winPercent; // set inner text of new cell | |
} | |
} | |
}) | |
/* | |
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 = []; | |
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 | |
} | |
} | |
} | |
/* | |
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 is dictionary in python) | |
// format of data is dictionary["personsName"] = {"totalGames": 0, "wins": 0} | |
var pastData = GM_getValue(player.name, '{"totalGames": 0, "wins": 0}'); | |
var formattedData = JSON.parse(pastData); // Need to format data correctly | |
var gamesPlayed = formattedData["totalGames"]; | |
var winsWithPlayer = formattedData["wins"]; | |
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, | |
"wins": winsWithPlayer | |
}; | |
// save data | |
GM_setValue(player.name, JSON.stringify(dataToStore)); // JSON.stringify encodes the data in a way that it can be stored | |
} | |
} | |
}); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment