// ==UserScript==
// @name           RatedList
// @namespace      tag:katana@iaxs.net,2009-04-20:wwui
// @description    Show games on the headquarters web page if they are rated or unrated
// @include        http://weewar.com/headquarters/games
// ==/UserScript==

// ============================================================================================
// This script modifies the headquarters/games web page to identify if each game is Rated or Unrated.
// The result shows up after the Round number of the game, ie.
//      MyGame  			Round 29 (Rated)
//		Aruba On Steroids	Round 5 (Unrated)
// --------------------------------------------------------------------------------------------

//Alert the user if the GM_xmlhttpRequest function is not available
if (!GM_xmlhttpRequest) 
{
	alert('Please upgrade to the latest version of Greasemonkey.');
	return;
}

var gameList = new Array();
var gameRated = new Array();

var user = getElementsByAttribute(document, "a", "href", "\/user.*")[0].getAttribute("href").split("/")[2];

GM_xmlhttpRequest(
{
	method: 'GET',
	url: 'http://weewar.com/api1/user/' + user,
	headers: { 
		'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey/0.3',
		'Accept': 'application/atom+xml,application/xml,text/xml'},
	onload: function(responseDetails)
	{
			var parser = new DOMParser();
			var responseXML = parser.parseFromString(responseDetails.responseText, "application/xml");
			var games = responseXML.getElementsByTagName('game');
			for (var i = 0; i < games.length; i++)
			{
				gameList[i] = "http://weewar.com/api1/game/" + games[i].textContent;
			}
	}
});

window.setTimeout(function(){for (var i = 0; i < gameList.length; i++) { getGameRated(gameList[i]); }}, 1000);		// Delay until each request finishes

function getGameRated(gameURL)
{
	GM_xmlhttpRequest(
	{
		method: 'GET',
		url: gameURL,
		headers: {
			'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey/0.3',
			'Accept': 'application/atom+xml,application/xml,text/xml',},
		onload: function(responseDetails)
		{
			var parser = new DOMParser();
			var responseXML = parser.parseFromString(responseDetails.responseText, "application/xml");
			var ratedResponse = responseXML.getElementsByTagName('rated')[0].textContent == "true" ? "Rated" : "Unrated";
			var gameID = gameURL.substring(gameURL.indexOf("game/") + 5);
			var oElm = getElementsByAttribute(document, "a", "href", "../game/" + gameID);
			
			for (var i = 0; i < oElm.length; i++)
			{
				if (oElm[i].id == "gameInfoDetailLink") 
				{
					var textRound = oElm[i].parentNode.childNodes[1].innerHTML;
					textRound = textRound.substring(0, textRound.indexOf("</")) + " (" + ratedResponse + ")" + textRound.substring(textRound.indexOf("</"));
					oElm[i].parentNode.childNodes[1].innerHTML = textRound;
				}
			}
		}
	});
}

// ============================================================================================
// - Generic Functions -
// --------------------------------------------------------------------------------------------

function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue)
{
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
    var oCurrent;
    var oAttribute;
    for(var i=0; i<arrElements.length; i++){
        oCurrent = arrElements[i];
        oAttribute = oCurrent.getAttribute(strAttributeName);
        if(typeof oAttribute == "string" && oAttribute.length > 0){
            if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
                arrReturnElements.push(oCurrent);
            }
        }
    }
    return arrReturnElements;
}
