// ==UserScript==

// @name TagPro Server Count

// @version 0

// @description The 2nd best userscript to ever grace the TagPro community.

// @include http://*.koalabeast.com:*

// @author Zagd

// ==/UserScript==

//README

//Sends an in-game message with server player/game count information, when someone (including yourself) send '!sc' into the game chat.

var timeout; // Global timeout function reference.

tagpro.ready(function() {

var now;

var lastMessage = 0;

var timeDiff;

tagpro.socket.on('chat', function(data) {

if (data.message == "!sc") {

var myId = tagpro.playerId;

// 20 second cooldown.

now = new Date();

timeDiff = now - lastMessage;

if (timeDiff > 20000 || lastMessage === 0) {

server = window.location.href.substring(window.location.href.indexOf('tagpro-')+7, window.location.href.indexOf('.koalabeast.com'));

$.ajax({

url: "http://tagpro-" + server + ".koalabeast.com/stats",

type: "get",

dataType: "jsonp",

success: function(stats) {

theMessage = "On " + server.charAt(0).toUpperCase() + server.slice(1) + ", there are currently " + stats.players + " players and " + stats.games + " games.";

}

});

// Delay the sending by 500 + 100 * myId, so that each player in game with this script will send at different times.

timeout = setTimeout(function(){tagpro.socket.emit("chat",{message:theMessage,toAll:true});}, 500 + 100 * myId);

lastMessage = new Date();

}

}

if (/^On .*, there are currently .* players and .* games.$/.test(data.message)) {

clearTimeout(timeout);

}

});