TW Friends

Friend Management for The West Events

Od 04.07.2014.. Pogledajte najnovija verzija.

// ==UserScript==
// @name		TW Friends
// @version		0.01
// @description		Friend Management for The West Events
// @author		hiroaki
// @include		http://*.the-west.*/game.php*
// @namespace https://greasyfork.org/users/3197
// ==/UserScript==


function hf(fn) {
	var script = document.createElement('script');
	script.setAttribute("type", "application/javascript");
	script.textContent = '(' + fn + ')();';
	document.body.appendChild(script);
	document.body.removeChild(script);
}

hf (function() {
	HiroFriends = new Object ({
		event : Game.sesData['Independence'].friendsbar,
		friends : [],
		total : 0,
		avail: 0,
	});
	HiroFriends.init = function() {
		var hiro_div = document.createElement('div');
		hiro_div.setAttribute("id", "hiro_friends_container");
		hiro_div.setAttribute("style", "position: absolute; top: 32px; right: 50%; margin-right: 120px; z-index: 16; width: 180px; height: 36px; text-align: left; text-shadow: 1px 1px 1px #000; background: url('http://westzz.innogamescdn.com/images/interface/custom_unit_counter_sprite.png?2') no-repeat scroll 50% 0px transparent;");
		hiro_div.innerHTML='<div id="hiro_friends" style="background: url(\'http://westzz.innogamescdn.com/images/interface/custom_unit_counter_sprite.png?2\') no-repeat scroll 0 -36px rgba(0, 0, 0, 0); color: #f8c57c; font-size: 13pt; height: 25px; left: 32px; line-height: 25px; padding: 0 5px; position: absolute; text-align: right; top: 3px; width: 105px; z-index: 1; text-shadow: 1px 1px 1px #000;"></div><span style="width: 36px; height: 36px; position: absolute; left: 8px; top: 3px; z-index: 3"><a href="#" onClick="MessageSuccess(\'Fetching friends list\').show(); HiroFriends.fetch(); return false;"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYEAYAAACw5+G7AAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAACXZwQWcAAAAYAAAAGAB4TKWmAAAJiUlEQVRYw91XSWxb5Ro9d/IUDxk9pE7SpHGcpFM6OBQ1zSttUVoV2iIqSkFiwYIgFogNS9iDBIIFFBYgoYK6CRUqLQWCqqotJSVpmqSZ6thJE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64t9HCdKiRdHt9vk6O+/ejUTm5xcWnnnm5MnDh/fs4bjBwVAonQYyGeIq165NTq6tAfv2+f12O+mcHFfydV3LTU2kOPXRQ5fMwMD09Ooq8MknP/4YjwNra9msIOj5Jf3G7Qb6+vr6+vpyuVwum81kTp8mHXhiYh2Bh4mEQnZ7Xd3OnZs3UxTDcJzff+hQZ6fPxzBTU8THBYGk+NatmZlUChBFIjWbzWSiaSIZgMw6qgrEYslksQj094+MJBLAhQuDg4kEkM8Xi5JEbFzTgJMn9+51uYCJiYmJ8XFJmpycmpqePn9+dPTSpffff+edP+J95DRaVeX1trZeuRKL8fzS0ssvm81ms8lkND733BNP+P0MQ44jGZFlIJ3O5yUJmJ9fXi4UgFAoHs/ngWAwGs3ngXv3otFcjvSXYhHI5Uhx19VVVRmNwNNPd3bW1ABDQ8PDIyOiePPm9evXrycSqVQ8fu/eiRNra/F4MFgs6p75e+3+OXx95jAYqqsbGrZvr652u9va/vGP995zuZxOl+vw4VOnTp06edJsdjjsdqsVKBSIC1EURWkaYDKxLEDmfY4jDVFVSUYAgONIUadS6XQuB3zxxVdf9fUVCrEYz/N8f380evduf/+bb66uRqNTU8kkwaPXgE5Ekh5BgP1XcetObbGQ3WRqagoEjh8/dMhmc7mam999d8eOrVvb2oxGv7+lxeczGFpamppqa2m6udnrragADAYyfs/NxWKrq8D09OxsPK6q9+4Fg6GQKA4M3L49NiaK6XQ8PjPz1ltzc7dvf/ttf//DgPN5susvNKR1Apr2N9+JGUbPCNnNZofD7W5udjqrqurqNm/u7uY4k8lm27WLYczm8vJAgGVZluN8Pv0JsizLohgOy3KhsLY2NCRJ+Xw6fedOIhGJjI/fuJFOLy3NzfH8wwD1SP/ZEPN/sv4JWVxlk1jXyFkAAAAielRYdFNvZnR3YXJlAAB42isvL9fLzMsuTk4sSNXLL0oHADbYBlgQU8pcAAAAAElFTkSuQmCC" style="width: 24px; height: 24px;" alt="" title="Refresh"></a></span><span style="height: 26px; left: auto; position: absolute; right: 7px; top: 2px; width: 26px; z-index: 3;" title=""><a href="#" onclick="HiroFriends.open(); return false;"><img src="/images/interface/friendsbar/events/Independence.png" style="width: 26px; height: 26px;" alt="" title="'+HiroFriends.event.label+'"></a></span>';
		document.body.appendChild(hiro_div);
	};
	HiroFriends.compare = function (a,b) {
		if (a.time < b.time) return -1;
		if (a.time > b.time) return 1;
		return 0;
	};
	HiroFriends.open = function() {
		$.ajaxSetup({async:false});
		HiroFriends.fetch();
		$.ajaxSetup({async:true});
		var td;
		var title = "Independence";
		var idx = 1;
		var tbl = $('<table style="width: 100%" border="0" cellpadding="0" cellspacing="0">');
		$.each(HiroFriends.friends, function (key, val) {
			if (val.time > 0) td = $('<td style="vertical-align: middle;">(' + val.time.formatDurationBuffWay() + ')</td>');
			else {
				td = $('<td style="vertical-align: middle;"><a href="#">' + HiroFriends.event.label + "</a></td>").click({ id: val.id, ev: e }, function (e) {
					$(this).parent().remove();
					var t = e.handleObj.data;
					Ajax.remoteCall("friendsbar", "event", {
						player_id: t.id,
						event: t.ev
					}, function (e) {
						if (e.error) return MessageError(e.msg).show();
						MessageSuccess(e.msg).show();
						HiroFriends.friends[key].time = HiroFriends.event.cooldown;
					})
                		})
            		}
            		tbl.append($('<tr style="background-image: url(\'/images/tw2gui/table/table_row_mark.png\'); height: 29px;">)').append($('<td style="width: 10%; vertical-align: middle; text-align: right; padding-right: 8px">' + idx + '.</td><td style="width: 35%; vertical-align: middle;">' + val.name + "</td>"), td))
			++ idx;
        	});
		var hiroWindow = wman.open("HiroFriends"+title, null).setMiniTitle(title).setTitle(title);
		var hiroPane = new west.gui.Scrollpane;
		hiroPane.appendContent(tbl);
		hiroWindow.appendToContentPane(hiroPane.getMainDiv())
    	};
	HiroFriends.fetch = function() {
		var event_times = {};
		HiroFriends.friends = new Array();
		HiroFriends.total = HiroFriends.avail = 0;
		var server_time = (new ServerDate).getTime() / 1e3;
		$.post( "/game.php?window=friendsbar&mode=search", { search_type: "friends" } , function (data) {
			$.each(data.eventActivations, function (key, val) {
				if (val.event_name == 'Independence') event_times[val.friend_id] = val.activation_time;
			});
			$.each(data.players, function (key, val) {
				if (val.name !== Character.name) {
					activation_time = (event_times[val.player_id] !== undefined) ? event_times[val.player_id]: 0;
					friend_time = activation_time + HiroFriends.event.cooldown - server_time;
					HiroFriends.friends.push ({ id: val.player_id, name: val.name, time: friend_time });
					++ HiroFriends.total;
					if (friend_time <= 0) ++ HiroFriends.avail;
				}
			});
			HiroFriends.friends.sort(HiroFriends.compare);
			console.log (HiroFriends.friends);
			document.getElementById("hiro_friends").innerHTML = (HiroFriends.avail+" / "+HiroFriends.total);
		});
	};
	HiroFriends.init();
	HiroFriends.fetch();
	setInterval(function() { HiroFriends.fetch(); }, 120000);
});