TW Fred Timer

Timer View for Event's free action

当前为 2015-06-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name TW Fred Timer
  3. // @name:ru TW Fred Timer
  4. // @namespace TW Fred Timer
  5. // @description Timer View for Event's free action
  6. // @description:ru Счетчик времени бесплатного события Праздника
  7. // @include http://*.the-west.*/game.php*
  8. // @include https://*.the-west.*/game.php*
  9.  
  10. // @version 0.04
  11. // @grant none
  12.  
  13. // ==/UserScript==
  14.  
  15.  
  16. function TWFredScript(fn) {
  17. var script = document.createElement('script');
  18. script.setAttribute("type", "application/javascript");
  19. script.textContent = '(' + fn + ')();';
  20. document.body.appendChild(script);
  21. document.body.removeChild(script);
  22. }
  23.  
  24. TWFredScript(function() {
  25. var VERSION = "0.04";
  26. var NAME = "TW Fred Timer";
  27. var installURL = '';
  28. fcContainer = $("<div />",
  29. {
  30. id: "twfred_container",
  31. style: "position: absolute; top: 64px; right: 50%; margin-right: 140px; z-index: 10; width: 142px; height: 16px; background: url('/images/interface/character/character.png') no-repeat scroll left -160px transparent;"
  32. }
  33. );
  34. fcContainer.appendTo("#user-interface");
  35. fcWrap = $('<div />',
  36. {
  37. id: "twfred_wrap",
  38. style:"padding-top:1px; font-size:10px; color:#FFF; text-align:center;",
  39. }
  40. );
  41. fcContainer.append(fcWrap);
  42. TWFred = {
  43. wofid:2, // don't know where find it
  44. diff: 0,
  45. timer:2, // seconds
  46. execCount:0,
  47. reInitVal:60, // execs to reinit data - on start
  48. reInitValN:60, // execs to reinit data - on normal work
  49. reInitValI:7, // execs to reinit data - on zero Timer
  50. clickObj: null,
  51. timerId: 0,
  52. };
  53. fcWrap.click(function(){TWFred.click()});
  54. TWFred.init = function()
  55. {
  56. TWFred.execCount = 0;
  57. TWFred.reInitVal = TWFred.reInitValN;
  58. TWFred.getClicker();
  59. $.post("/game.php?window=wheeloffortune&mode=init", {wofid:TWFred.wofid},
  60. function(data)
  61. {
  62. var diff = 0;
  63. var ctime = 0;
  64. var cdl = 0;
  65. //console.log(data);
  66. if (data.error) {TWFred.destroy(); return;}
  67. var diff = 0;
  68. var cd = data.mode;
  69. if (cd.cooldowns !== undefined) // FRED
  70. {
  71. cd = cd.cooldowns;
  72. cdl = cd.length;
  73. if (cdl) // cuz no data at end of Event
  74. {
  75. ctime = cd[0].cdstamp
  76. var stime = Game.getServerTime();
  77. diff = ctime-stime;
  78. }
  79. }
  80. else if (cd.states !== undefined) // IndDay
  81. {
  82. cd = cd.states;
  83. cdl = Object.keys(cd).length;
  84. if (cdl)
  85. {
  86. ctime = cd[0].finish_date;
  87. var stime = Game.getServerTime();
  88. if (!ctime) ctime = stime;
  89. diff = ctime-stime;
  90. }
  91. }
  92. else {console.log('No Fred data'); TWFred.destroy(); return;} // no data
  93. if (!cdl) {console.log('No Fred length'); TWFred.destroy(); return;}
  94. TWFred.diff = diff;
  95. TWFredTimer();
  96. }
  97. ); //post
  98. };
  99. TWFred.showTimer = function()
  100. {
  101. var str = TWFred.diff.formatDuration();
  102. fcWrap.html(str);
  103. };
  104. TWFred.doTimer = function()
  105. {
  106. //console.log('doTimer:' + TWFred.execCount + ',' + TWFred.reInitVal);
  107. TWFred.timerId = 0;
  108. if (undefined === TWFred) return;
  109. if (!TWFred.clickObj) TWFred.getClicker(); // init clicker
  110. var diff = TWFred.diff - TWFred.timer;
  111. if (diff < 0) diff = 0;
  112. if (!diff) TWFred.reInitVal = TWFred.reInitValI;
  113. TWFred.diff = diff;
  114. TWFred.showTimer();
  115. TWFred.execCount++;
  116. if (TWFred.execCount > TWFred.reInitVal)
  117. {
  118. TWFred.init();
  119. }
  120. else
  121. {
  122. TWFredTimer();
  123. }
  124. };
  125. TWFred.getClicker = function()
  126. {
  127. if (TWFred.clickObj) return;
  128. var bar = WestUi.NotiBar.getBar();
  129. if (!bar) return;
  130. bar = bar.list;
  131. TWFred.reInitVal = TWFred.reInitValN;
  132. $.each(bar, function(key,val)
  133. {
  134. //console.log(val);
  135. if ((val.uid == 'easter2015')||(val.uid == 'independencewof'))
  136. {
  137. TWFred.clickObj = val.element;
  138. }
  139. });
  140. if (TWFred.clickObj) fcWrap.css("cursor","pointer");
  141. };
  142. TWFred.click = function()
  143. {
  144. if (!TWFred.clickObj) return;
  145. TWFred.init();
  146. TWFred.clickObj.click();
  147. };
  148. TWFred.destroy = function()
  149. {
  150. console.log('Removing TWFred');
  151. fcContainer.remove();
  152. delete TWFred;
  153. TWFred = undefined;
  154. };
  155. function TWFredTimer()
  156. {
  157. if (undefined === TWFred) return;
  158. if (!TWFred.timerId) TWFred.timerId = setTimeout(TWFred.doTimer,TWFred.timer * 1000);
  159. };
  160. TWFred.init();
  161. }
  162. );