TW Friends

Friend Management for The West Events

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

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