Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ldhertert/a32dc0e5086d28eb8c89ed0187480a6d to your computer and use it in GitHub Desktop.
Save ldhertert/a32dc0e5086d28eb8c89ed0187480a6d to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Show R300 Win% on Players
// @description Shows players Rolling 300 Win% on their ball (and/or in a table)
// Works for registered (green) names only (and only if they appear on tagpro-stats.com)
// @version 0.1.5
// @include http://tagpro-*.koalabeast.com:*
// @connect tagpro-stats.com
// @connect koalabeast.com
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_deleteValue
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// @updateURL https://gist.github.com/nabbynz/6133ab322f42a92edd1b/raw/TagPro_Show_R300WinPC_On_Players.user.js
// @downloadURL https://gist.github.com/nabbynz/6133ab322f42a92edd1b/raw/TagPro_Show_R300WinPC_On_Players.user.js
// @license GPL
// @author nabby (using code by ProfessorTag)
// ==/UserScript==
console.log('START: ' + GM_info.script.name + ' (v' + GM_info.script.version + ' by ' + GM_info.script.author + ')');
tagpro.ready(function() {
var showOnBall = true;
var showTable = true;
var textColorOnBall = '#FDD017';
var textPositionOnBall = {x:20, y:30};
function getAllPlayers(auth) {
auth = auth || false;
var players = [];
for (var playerId in tagpro.players) {
if ( (tagpro.players.hasOwnProperty(playerId)) && (!auth || (auth && tagpro.players[playerId].auth)) ) {
players.push(tagpro.players[playerId]);
}
}
return players;
}
function addTextToBall(player) {
if (!player.sprites.R3PWP) {
var R3PWP_PrettyText = tagpro.renderer.prettyText(player.R3PWP.R300WinP, textColorOnBall);
R3PWP_PrettyText.x = textPositionOnBall.x;
R3PWP_PrettyText.y = textPositionOnBall.y;
R3PWP_PrettyText.alpha = 0.4;
player.sprites.R3PWP = R3PWP_PrettyText;
player.sprites.name.addChild(player.sprites.R3PWP);
}
}
function getProfileId(player) {
GM_xmlhttpRequest({
method: 'GET',
url: 'http://www.tagpro-stats.com/autocomplete.php?string=' + encodeURIComponent(player.name) + '+',
onload: function(response) {
if (!response.responseText) {
return;
}
var tpStatsId = response.responseText.match(new RegExp('(\\d+)\'>' + player.name + '<', 'i'))[1];
if (!tpStatsId) return;
GM_xmlhttpRequest({
method: 'GET',
url: 'http://www.tagpro-stats.com/profile.php?userid=' + tpStatsId,
onload: function(response) {
if (!response.responseText) {
return;
}
var profileId = response.responseText.match(/profile\/([^"]+)/)[1];
if (!profileId) return;
knownPlayerIds[player.name] = {}; //add to our known players so faster next time
knownPlayerIds[player.name].profileId = profileId;
GM_setValue('knownPlayerIds', knownPlayerIds);
getWinPercentage(player);
}
});
}
});
}
function showPlayersTable() {
var players = getAllPlayers(false);
$('#R3PWP_Players').remove();
$('#R3PWP_Mini').append('<table id="R3PWP_Players" style="text-align:center; line-height:9px"></div>');
$('#R3PWP_Players').append('<tr><th id="R3PWP_Refresh">P</th><th title="Rolling 300 Win %">R</th><th title="Today\'s Win %">T</th><th title="Weekly Win %">W</th><th title="Monthly Win %">M</th></tr>');
$.each(players, function(key, player) {
if (player.auth && player.hasOwnProperty('R3PWP') && knownPlayerIds.hasOwnProperty(player.name)) {
var profileURL = 'http://' + window.location.hostname + "/profile/" + knownPlayerIds[player.name].profileId;
$('#R3PWP_Players').append('<tr style="color:'+(player.team == 1 ? '#ffb0b0' : '#c0c0ff')+'"><td><a href="'+profileURL+'" target="_blank" style="color:'+(player.team == 1 ? '#ffb0b0' : '#c0c0ff')+'; text-decoration:underline">'+player.name+'</a></td><td>'+player.R3PWP.R300WinP+'</td><td>'+player.R3PWP.TodayWinP+'</td><td>'+player.R3PWP.WeekWinP+'</td><td>'+player.R3PWP.MonthWinP+'</td></tr>');
} else {
$('#R3PWP_Players').append('<tr style="color:'+(player.team == 1 ? '#ffb0b0' : '#c0c0ff')+'"><td>'+player.name+'</td><td>-</td><td>-</td><td>-</td><td>-</td></tr>');
}
});
}
function getWinPercentage(player) {
if (!player.hasOwnProperty('R3PWP')) {
player.R3PWP = {};
}
if (!player.R3PWP.hasOwnProperty('R300WinP')) {
var profileURL = 'http://' + window.location.hostname + "/profile/" + knownPlayerIds[player.name].profileId;
GM_xmlhttpRequest({
method: 'GET',
url: profileURL,
onload: function(response) {
if (!response.responseText) {
return;
}
player.R3PWP.profileId = knownPlayerIds[player.name].profileId;
player.R3PWP.R300WinP = $(response.responseText).find('#rolling').find('tbody').find('tr').eq(0).find('td').eq(1).text().trim(); //this cell contains the R300 Win% value
player.R3PWP.TodayWinP = $(response.responseText).find('#all-stats').find('tbody').find('tr').eq(0).find('td').eq(1).text().trim(); //"Today" Win% value
player.R3PWP.WeekWinP = $(response.responseText).find('#all-stats').find('tbody').find('tr').eq(0).find('td').eq(2).text().trim(); //"Week" Win% value
player.R3PWP.MonthWinP = $(response.responseText).find('#all-stats').find('tbody').find('tr').eq(0).find('td').eq(3).text().trim(); //"Month" Win% value
if ((showOnBall === true) && (!player.sprites.R3PWP)) {
addTextToBall(player);
}
if (showTable === true) {
showPlayersTable();
}
},
onerror: function(response) {
console.log('Win% on Players Error: '+player.name);
console.log(response);
}
});
} else {
if (showTable === true) {
showPlayersTable();
}
}
}
if (showTable === true) {
GM_addStyle("#R3PWP_Players th { text-align:center; cursor:default; background:#fff; color:#000; }");
GM_addStyle("#R3PWP_Players td { text-align:center; cursor:default; padding:1px 1px 0 1px }");
}
$('#exit').after('<div id="R3PWP_Mini" style="display:inline-block; position:absolute; top:300px; font-size:10px; background:#333; opacity:0.5"></table>');
//GM_deleteValue('knownPlayerIds');
var knownPlayerIds = GM_getValue('knownPlayerIds', {});
tagpro.socket.on('time', function() {
setTimeout(function() {
var authPlayers = getAllPlayers(true);
$.each(authPlayers, function(key, player) {
if (knownPlayerIds.hasOwnProperty(player.name)) {
getWinPercentage(player);
} else {
getProfileId(player);
}
});
}, 500);
});
tagpro.socket.on('chat', function(data) {
if ((data.from === null) && (tagpro.state === 1)) { //system message
if (data.message.indexOf('has joined the') >= 0) {
var authPlayers = getAllPlayers(true);
$.each(authPlayers, function(key, player) {
if (knownPlayerIds.hasOwnProperty(player.name)) {
getWinPercentage(player);
} else {
getProfileId(player);
}
});
} else if (data.message.indexOf('has left the') >= 0) {
if (showTable === true) {
showPlayersTable();
}
}
}
});
$('#R3PWP_Mini').on('click', '#R3PWP_Refresh', function() {
var authPlayers = getAllPlayers(true);
$.each(authPlayers, function(key, player) {
if (knownPlayerIds.hasOwnProperty(player.name)) {
getWinPercentage(player);
} else {
getProfileId(player);
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment