TW Friends

Friend Management for The West Events

目前為 2014-10-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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

function hiroFriendsScript(fn) {
	var script = document.createElement('script');
	script.setAttribute("type", "application/javascript");
	script.textContent = '(' + fn + ')();';
	document.body.appendChild(script);
	document.body.removeChild(script);
}
hiroFriendsScript (function() {
	var VERSION = 0.12;
	var installURL = "https://greasyfork.org/scripts/2992-tw-friends";
	var refreshMs = 2 * 60 * 1000;	// 2 minutes

	HiroFriends = {
		version: VERSION,
		storageItem: "HiroFriends.version",
		cdnBase: '',
		eventName : '',
		eventInfo : {},
		eventEndStamp : 0,
		friends : [],
		interval: false,
		timeLeft : 0,
		total : 0,
		avail: 0,
		spanCounter: $("<span />", { id: "hiro_friends_counter", style: "position: absolute; right: 5px; color: #f8c57c; font-size: 13pt; height: 25px; line-height: 25px; bottom: 0px" }),
		spanTimeLeft: $("<span />", { id: "hiro_event_timeleft", style: "position: absolute; left: 5px; color: #d3d3d3; font-size: 11px; height: 25px; line-height: 25px" })
	};
	HiroFriends.init = function(eventName) {
		if (undefined === Game.sesData[eventName] || undefined === Game.sesData[eventName].friendsbar) return false;
		HiroFriends.eventName = eventName;
		HiroFriends.eventInfo = Game.sesData[eventName].friendsbar;
		if (undefined === Game.sesData[HiroFriends.eventName].meta.end) return false;
		HiroFriends.eventEndStamp = (buildTimestamp(Game.sesData[HiroFriends.eventName].meta.end) - Game.serverTimeDifference) / 1000;
		HiroFriends.timeLeft = HiroFriends.eventEndStamp - Game.getServerTime();
		if (HiroFriends.timeLeft < 0) return false;
		HiroFriends.cdnBase = (undefined === Game.cdnURL) ? "http://westzz.innogamescdn.com" : Game.cdnURL;
		var eventImage = HiroFriends.cdnBase + "/images/interface/friendsbar/events/" + HiroFriends.eventName + ".png";	// event based
		var divContainer = $("<div />", { id: "hiro_friends_container", 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('"+HiroFriends.cdnBase+"/images/interface/custom_unit_counter_sprite.png?2') no-repeat scroll 50% 0px transparent;" })
		var divCounter = $("<div />", { id: "hiro_friends", style: "background: url('"+HiroFriends.cdnBase+"/images/interface/custom_unit_counter_sprite.png?2') no-repeat scroll 0 -36px rgba(0, 0, 0, 0); height: 25px; left: 32px; line-height: 25px; padding: 0 5px; position: absolute; top: 3px; width: 105px; z-index: 1; text-shadow: 1px 1px 1px #000;" });
		var divRefresh = $("<div />", { style: "width: 24px; height: 24px; position: absolute; left: 8px; top: 3px; z-index: 3; padding: 4px 0px 0px 4px;" });
		var spanRefresh = $("<span >", { style: "display: inline-block; width: 20px; height: 20px; cursor: pointer; background: url('"+HiroFriends.cdnBase+"/images/tw2gui/window/window2_buttons.png?5') repeat scroll 0px -20px transparent;" });
		var spanSend = $("<span />", { style: "width: 26px; height: 26px; left: auto; position: absolute; right: 7px; top: 2px; z-index: 3;" });
		var imageSend = $("<img />", { src: eventImage, title: HiroFriends.eventInfo.label, style: "width: 26px; height: 26px; cursor: pointer" });
		divContainer.append(divRefresh.append(spanRefresh), spanSend.append(imageSend), divCounter.append(HiroFriends.spanTimeLeft, HiroFriends.spanCounter)).appendTo("#user-interface");;
		spanRefresh.hover(function() { $(this).css("background-position", ""); }, function() { $(this).css("background-position", "0px -20px"); });
		spanRefresh.click(function() { HiroFriends.spanCounter.slideUp(500, function() { HiroFriends.fetch(); }).slideDown(1500); });
		imageSend.click(function() { HiroFriends.open(); });
		HiroFriends.updateTimer();
		if(typeof(Storage) !== "undefined") {
			var previousVersion = (localStorage.getItem(HiroFriends.storageItem) === null) ? 0 : parseFloat(localStorage.getItem(HiroFriends.storageItem));
			localStorage.setItem(HiroFriends.storageItem, HiroFriends.version);
			// if (previousVersion && HiroFriends.version > previousVersion) var msg=new west.gui.Dialog("TW Friends", "Script upgraded to version "+HiroFriends.version, west.gui.Dialog.SYS_WARNING).addButton("OK").show();
		}
		return true;
	}
	HiroFriends.fetch = function() {
		if (HiroFriends.interval !== false) clearInterval (HiroFriends.interval);
		var event_times = {};
		var friends = [], total = 0, avail = 0;
		var server_time = Game.getServerTime(), activation_time, friend_time;
		if (HiroFriends.timeLeft < 0) {
			$("#hiro_friends_container").slideUp(5000);
			throw "Event is over";
		}
		return $.post( "/game.php?window=friendsbar&mode=search", { search_type: "friends" } , function (data) {
			$.each(data.eventActivations, function (key, val) {
				if (val.event_name == HiroFriends.eventName) 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.eventInfo.cooldown - server_time;
					friends.push ({ id: val.player_id, name: val.name, time: friend_time });
					++ total;
					if (friend_time <= 0) ++ avail;
				}
			});
			if (total) friends.sort(HiroFriends.timeCompare);
			HiroFriends.friends = friends;
			HiroFriends.avail = avail;
			HiroFriends.total = total;
			HiroFriends.interval = setInterval(function() { HiroFriends.fetch(); }, refreshMs);
			HiroFriends.update();
		});
	}
	HiroFriends.open = function() {
		if (!WestUi.FriendsBar.hidden) WestUi.FriendsBar.toggle();
		return HiroFriends.fetch().done(function() {
			var td;
			var idx = 1;
			var tbl = $('<table style="width: 100%" border="0" cellpadding="0" cellspacing="0">');
			$.each(HiroFriends.friends, function (key, val) {
				if (val.time > HiroFriends.timeLeft) td = $('<td style="vertical-align: middle;">(Next Year)</td>');
				else if (val.time > 0) td = $('<td style="vertical-align: middle;">(' + val.time.formatDurationBuffWay() + ')</td>');
				else {
					td = $('<td style="vertical-align: middle;"><a href="#">' + HiroFriends.eventInfo.label + "</a></td>").click({ id: val.id, ev: HiroFriends.eventName }, function (e) {
						$(this).parent().remove();
						Ajax.remoteCall("friendsbar", "event", { player_id: val.id, event: HiroFriends.eventName }, function (response) {
							if (response.error) return MessageError(response.msg).show();
							MessageSuccess(response.msg).show();
							if (HiroFriends.avail) -- HiroFriends.avail;
							HiroFriends.update();
							if (WestUi.FriendsBar.friendsBarUi !== null)
								WestUi.FriendsBar.friendsBarUi.friendsBar.eventActivations[val.id][HiroFriends.eventName] = response.activationTime;
						});
					});
				}
				tbl.append($('<tr style="background-image: url(\''+HiroFriends.cdnBase+'/images/tw2gui/table/table_row.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;"><a href="javascript:void(PlayerProfileWindow.open('+val.id+'));">' + val.name + '</a></td>'), td));
				++ idx;
			});
			tbl.append($('<tr style="background-image: url(\''+HiroFriends.cdnBase+'/images/tw2gui/table/table_row_you.png\'); height: 29px;">)').append($('<td style="vertical-align: middle; text-align: right; padding-right: 8px" colspan="3"><a target="_blank" href="'+installURL+'">TW Friends</a> version <b>' + HiroFriends.version.toFixed(2) + '</b></td>')));
			var hiroWindow = wman.open("HiroFriends_"+HiroFriends.eventName, null, "noreload").setMiniTitle(HiroFriends.eventInfo.label).setTitle(HiroFriends.eventInfo.label);
			var hiroPane = new west.gui.Scrollpane;
			hiroPane.appendContent(tbl);
			hiroWindow.appendToContentPane(hiroPane.getMainDiv())
		});
	}
	HiroFriends.timeCompare = function(a, b) {
		if (a.time < b.time) return -1;
		if (a.time > b.time) return 1;
		return 0;
	}
	HiroFriends.updateTimer = function() {
		HiroFriends.timeLeft = HiroFriends.eventEndStamp - Game.getServerTime();
		if (HiroFriends.timeLeft <= 0) {
			HiroFriends.spanTimeLeft.html('Finito');
			HiroFriends.fetch();
			return;
		}
		HiroFriends.spanTimeLeft.html(HiroFriends.timeLeft.formatDurationBuffWay());
		var seconds = 0;
		if (HiroFriends.timeLeft < 70) seconds = 1;
		else if (HiroFriends.timeLeft < 3660) seconds = 10;
		else if (HiroFriends.timeLeft < 86520) seconds = 60;
		else seconds = 120;
		setTimeout (function() { HiroFriends.updateTimer(); }, seconds * 1000);
	}
	HiroFriends.update = function() {
		HiroFriends.spanCounter.html(HiroFriends.avail+' <span style="color: #d3d3d3; font-size: 11px;">/ '+HiroFriends.total+'</span>');
	}
	HiroFriends.scriptInit = function(tries, maxTries) {
		if (tries >= maxTries) return false;
		if (Game && Game.loaded && undefined !== west.events && undefined != west.events.Manager) {
			var westEvents = west.events.Manager.getRunningEvents();
			var eventName;
			if ("object" == typeof westEvents) $.each (westEvents, function (key, val) {
				if (undefined !== val['id']) {
					eventName = val['id'];
					if ('SES: ' == eventName.substr(0,5)) eventName = eventName.substr(5);
					if ('Hearts' == eventName || 'Easter' == eventName || 'Independence' == eventName || 'DayOfDead' == eventName) {
						if (HiroFriends.init(eventName)) HiroFriends.fetch();
						return false;
					}
				}
			});
			return true;
		}
		++ tries;
		setTimeout(function() { HiroFriends.scriptInit(tries, maxTries); }, tries * 1000);
	}
	HiroFriends.scriptInit(0, 100);
});