TW Friends

Friend Management for The West Events

当前为 2014-07-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name TW Friends
  3. // @version 0.08
  4. // @description Friend Management for The West Events
  5. // @author hiroaki
  6. // @include http://*.the-west.*/game.php*
  7. // @grant none
  8. // @namespace https://greasyfork.org/users/3197
  9. // ==/UserScript==
  10.  
  11. function hiroFriendsScript(fn) {
  12. var script = document.createElement('script');
  13. script.setAttribute("type", "application/javascript");
  14. script.textContent = '(' + fn + ')();';
  15. document.body.appendChild(script);
  16. document.body.removeChild(script);
  17. }
  18. hiroFriendsScript (function() {
  19. var VERSION = 0.08;
  20. var installURL = "https://greasyfork.org/scripts/2992-tw-friends";
  21. var eventName = 'Independence';
  22. var refreshMs = 2 * 60 * 1000; // 2 minutes
  23.  
  24. HiroFriends = {
  25. version: VERSION,
  26. storageItem: "HiroFriends.version",
  27. cdnBase: '',
  28. eventName : '',
  29. eventInfo : {},
  30. eventEndStamp : 0,
  31. friends : [],
  32. timeLeft : 0,
  33. total : 0,
  34. avail: 0,
  35. spanCounter: $("<span />", { id: "hiro_friends_counter", style: "position: absolute; right: 5px; color: #f8c57c; font-size: 13pt; height: 25px; line-height: 25px; bottom: 0px" }),
  36. spanTimeLeft: $("<span />", { id: "hiro_event_timeleft", style: "position: absolute; left: 5px; color: #d3d3d3; font-size: 11px; height: 25px; line-height: 25px" })
  37. };
  38. HiroFriends.init = function(eventName) {
  39. if (undefined === Game.sesData[eventName] || undefined === Game.sesData[eventName].friendsbar) return false;
  40. HiroFriends.eventName = eventName;
  41. HiroFriends.eventInfo = Game.sesData[eventName].friendsbar;
  42. HiroFriends.eventEndStamp = (buildTimestamp(Game.sesData[HiroFriends.eventName].meta.end) - Game.serverTimeDifference) / 1000;
  43. HiroFriends.timeLeft = HiroFriends.eventEndStamp - Game.getServerTime();
  44. if (HiroFriends.timeLeft <= 0) return false;
  45. HiroFriends.cdnBase = (undefined === Game.cdnURL) ? "http://westzz.innogamescdn.com" : Game.cdnURL;
  46. var eventImage = HiroFriends.cdnBase + "/images/interface/friendsbar/events/Independence.png"; // to be based on the event
  47. 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;" })
  48. 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;" });
  49. var divRefresh = $("<div />", { style: "width: 24px; height: 24px; position: absolute; left: 8px; top: 3px; z-index: 3; padding: 4px 0px 0px 4px;" });
  50. 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;" });
  51. var spanSend = $("<span />", { style: "width: 26px; height: 26px; left: auto; position: absolute; right: 7px; top: 2px; z-index: 3;" });
  52. var imageSend = $("<img />", { src: eventImage, title: HiroFriends.eventInfo.label, style: "width: 26px; height: 26px; cursor: pointer" });
  53. divContainer.append(divRefresh.append(spanRefresh), spanSend.append(imageSend), divCounter.append(HiroFriends.spanTimeLeft, HiroFriends.spanCounter)).appendTo("#user-interface");;
  54. spanRefresh.hover(function() { $(this).css("background-position", ""); }, function() { $(this).css("background-position", "0px -20px"); });
  55. spanRefresh.click(function() { HiroFriends.spanCounter.slideUp(500, function() { HiroFriends.fetch(); }).slideDown(1500); });
  56. imageSend.click(function() { HiroFriends.open(); });
  57. HiroFriends.updateTimer();
  58. if(typeof(Storage) !== "undefined") {
  59. var previousVersion = (localStorage.getItem(HiroFriends.storageItem) === null) ? 0 : parseFloat(localStorage.getItem(HiroFriends.storageItem));
  60. localStorage.setItem(HiroFriends.storageItem, HiroFriends.version);
  61. // 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();
  62. }
  63. return true;
  64. }
  65. HiroFriends.fetch = function() {
  66. if (interval !== false) clearInterval (interval);
  67. var event_times = {};
  68. var friends = [], total = 0, avail = 0;
  69. var server_time = Game.getServerTime(), activation_time, friend_time;
  70. if (HiroFriends.timeLeft < 0) {
  71. $("#hiro_friends_container").slideUp(5000);
  72. throw "Event is over";
  73. }
  74. return $.post( "/game.php?window=friendsbar&mode=search", { search_type: "friends" } , function (data) {
  75. $.each(data.eventActivations, function (key, val) {
  76. if (val.event_name == HiroFriends.eventName) event_times[val.friend_id] = val.activation_time;
  77. });
  78. $.each(data.players, function (key, val) {
  79. if (val.name !== Character.name) {
  80. activation_time = (event_times[val.player_id] !== undefined) ? event_times[val.player_id]: 0;
  81. friend_time = activation_time + HiroFriends.eventInfo.cooldown - server_time;
  82. friends.push ({ id: val.player_id, name: val.name, time: friend_time });
  83. ++ total;
  84. if (friend_time <= 0) ++ avail;
  85. }
  86. });
  87. if (total) friends.sort(HiroFriends.timeCompare);
  88. HiroFriends.friends = friends;
  89. HiroFriends.avail = avail;
  90. HiroFriends.total = total;
  91. interval = setInterval(function() { HiroFriends.fetch(); }, refreshMs);
  92. HiroFriends.update();
  93. });
  94. }
  95. HiroFriends.open = function() {
  96. if (!WestUi.FriendsBar.hidden) WestUi.FriendsBar.toggle();
  97. return HiroFriends.fetch().done(function() {
  98. var td;
  99. var idx = 1;
  100. var tbl = $('<table style="width: 100%" border="0" cellpadding="0" cellspacing="0">');
  101. $.each(HiroFriends.friends, function (key, val) {
  102. if (val.time > HiroFriends.timeLeft) td = $('<td style="vertical-align: middle;">(Next Year)</td>');
  103. else if (val.time > 0) td = $('<td style="vertical-align: middle;">(' + val.time.formatDurationBuffWay() + ')</td>');
  104. else {
  105. td = $('<td style="vertical-align: middle;"><a href="#">' + HiroFriends.eventInfo.label + "</a></td>").click({ id: val.id, ev: HiroFriends.eventName }, function (e) {
  106. $(this).parent().remove();
  107. Ajax.remoteCall("friendsbar", "event", { player_id: val.id, event: HiroFriends.eventName }, function (response) {
  108. if (response.error) return MessageError(response.msg).show();
  109. MessageSuccess(response.msg).show();
  110. if (HiroFriends.avail) -- HiroFriends.avail;
  111. HiroFriends.update();
  112. if (WestUi.FriendsBar.friendsBarUi !== null)
  113. WestUi.FriendsBar.friendsBarUi.friendsBar.eventActivations[val.id][HiroFriends.eventName] = response.activationTime;
  114. });
  115. });
  116. }
  117. tbl.append($('<tr style="background-image: url(\''+HiroFriends.cdnBase+'/images/tw2gui/table/table_row_you.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));
  118. ++ idx;
  119. });
  120. tbl.append($('<tr style="background-image: url(\''+HiroFriends.cdnBase+'/images/tw2gui/table/table_row.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> v<b>' + HiroFriends.version + '</b></td>')));
  121. var hiroWindow = wman.open("HiroFriends_"+HiroFriends.eventName, null, "noreload").setMiniTitle(HiroFriends.eventInfo.label).setTitle(HiroFriends.eventInfo.label);
  122. var hiroPane = new west.gui.Scrollpane;
  123. hiroPane.appendContent(tbl);
  124. hiroWindow.appendToContentPane(hiroPane.getMainDiv())
  125. });
  126. }
  127. HiroFriends.timeCompare = function (a,b) {
  128. if (a.time < b.time) return -1;
  129. if (a.time > b.time) return 1;
  130. return 0;
  131. }
  132. HiroFriends.updateTimer = function() {
  133. HiroFriends.timeLeft = HiroFriends.eventEndStamp - Game.getServerTime();
  134. if (HiroFriends.timeLeft <= 0) {
  135. HiroFriends.spanTimeLeft.html('Finito');
  136. HiroFriends.fetch();
  137. return;
  138. }
  139. HiroFriends.spanTimeLeft.html(HiroFriends.timeLeft.formatDurationBuffWay());
  140. var seconds = 0;
  141. if (HiroFriends.timeLeft < 70) seconds = 1;
  142. else if (HiroFriends.timeLeft < 3660) seconds = 10;
  143. else if (HiroFriends.timeLeft < 86520) seconds = 60;
  144. else seconds = 120;
  145. setTimeout (function() { HiroFriends.updateTimer(); }, seconds * 1000);
  146. }
  147. HiroFriends.update = function() {
  148. HiroFriends.spanCounter.html(HiroFriends.avail+' <span style="color: #d3d3d3; font-size: 11px;">/ '+HiroFriends.total+'</span>');
  149. }
  150. var interval = false;
  151. if (HiroFriends.init(eventName)) HiroFriends.fetch();
  152. });