goreae

AGame boss timer

Mar 18th, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.90 KB | None | 0 0
  1. /*
  2.  
  3. A GAME Mod by Barry
  4.  
  5. Info Page: http://www.rscharts.com/forum/index.php/topic,113
  6.  
  7. Create bookmark with following URL:
  8.  
  9. javascript:var s=document.createElement('script');s.type='text/javascript';document.body.appendChild(s);s.src='http://pastebin.com/raw.php?i=LC7HpwUT';void(0);
  10.  
  11. Like this: http://i.imgur.com/bCVT1nY.png
  12.  
  13. Then click on the bookmark while in the game to activate the mod
  14.  
  15. == Bugs ==
  16. - None
  17.  
  18. == ToDo ==
  19. - Audio notice
  20.  
  21. */
  22.  
  23. 'use strict';
  24.  
  25. function Mod(){
  26. jQuery.fn.outerHTML = function(s) { return s ? this.before(s).remove() : jQuery("<p>").append(this.eq(0).clone()).html(); };
  27. this.MO = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  28. this.observers = [];
  29. this.GBStatus = "waiting";
  30. this.workers = ['steve','miner','miner2', 'heavenlyminer','hellminer','enderminer'];
  31. this.workerValue = {steve: 29.25, miner: 46.77, miner2: 46.77, heavenlyminer: 267.09, hellminer: 356.58, enderminer: 423.01};
  32. this.timers = {
  33. update: false,
  34. lastUpdate: +new Date,
  35. joinedGbDelay: 0,
  36. oneSecondUpdate: 0,
  37. };
  38. this.settings = {
  39. updateTitle: {type: 'bool', label: 'Update title', value: true, func: this.toggleUpdateTitle.bind(this)},
  40. gbAlert: {type: 'bool', label: 'GB Alert', value: true},
  41. numberFormat: {type: 'bool', label: 'Format Numbers', value: false, func: this.toggleReplaceNumbers.bind(this)},
  42. showWorkerValues: {type: 'bool', label: 'Show worker values', value: false, func: this.toggleRenderWorkerProfit.bind(this)},
  43. };
  44. // Updater
  45. this.timers.update = setInterval(function(){ this.update(); }.bind(this), 100);
  46. // Elements
  47. this.elements = {
  48. aboutBox: $('<div style="float:left;"><div>Mod by Barry</div><div style="display:inline-block;margin-left:3px;vertical-align:top;"><button onclick="mod.showSettings();">Settings</button></div></div>'),
  49. workerProfits: [],
  50. };
  51. $('<script src="//cdnjs.buttflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js">').appendTo('body')
  52. $('#gamecontainer').prepend(this.elements.aboutBox);
  53. }
  54.  
  55. Mod.prototype.showSettings = function(){
  56. var settings = $('<div id="mod-settings"><table style="width:100%;"><tr><th>Setting</th><th>Value</th></tr></table></div>');
  57. var table = settings.find('table');
  58. var self = this;
  59. _.each(this.settings, function(setting, key){
  60. var row = '<tr><td>'+setting.label+'</td><td>';
  61. if(setting.type == "bool"){
  62. row += '<input type="checkbox" onclick="mod.onSettingChange(\''+key+'\', this);" '+(setting.value ? 'checked ' : '')+'/>';
  63. }else if(setting.type == "number"){
  64. row += '<input type="text" onchange="mod.onSettingChange(\''+key+'\', this);" value="'+setting.value+'" />';
  65. if(setting.suffix) row += ' '+setting.suffix;
  66. }
  67. row += '</td></tr>';
  68. table.append(row);
  69. });
  70. table.append('<tr><td>Remove Mod</td><td><button style="float:none;background-color:rgb(221, 221, 221);;font-weight:normal;color:#000;border:2px outset buttonface;" onclick="mod.terminate();">Remove</button></td></tr>');
  71. popup('Mod Settings', settings.outerHTML(), [{text: 'close', func: function(){}}], 0);
  72. }
  73.  
  74. Mod.prototype.onSettingChange = function(key, element){
  75. if(!_.has(this.settings, key)) return;
  76. var setting = this.settings[key];
  77. element = $(element);
  78. if(setting.type == "bool"){
  79. // Boolean
  80. setting.value = element.is(':checked');
  81. }
  82. else if(setting.type == "number"){
  83. // Number
  84. var val = parseFloat(element.val());
  85. if(null !== val && (!setting.minValue || val >= setting.minValue) ){
  86. setting.value = val;
  87. element.css('border-color', '');
  88. }else{
  89. element.css('border-color', 'red');
  90. }
  91. }
  92.  
  93. if(setting.func)
  94. setting.func(setting, element);
  95. }
  96.  
  97. Mod.prototype.update = function(){
  98. if("undefined" === typeof _) return;
  99. var delta = (+new Date - this.timers.lastUpdate);
  100.  
  101. this.timers.joinedGbDelay -= delta;
  102. this.timers.oneSecondUpdate += delta;
  103. if(this.timers.oneSecondUpdate > 1000){
  104. this.timers.oneSecondUpdate = 0;
  105.  
  106. if(this.settings.showWorkerValues.value){
  107. this.renderWorkerProfit();
  108. }
  109.  
  110. if(this.settings.updateTitle.value || this.settings.gbAlert.value){
  111. this.updateTitle();
  112. }
  113.  
  114. }
  115.  
  116. this.timers.lastUpdate = +new Date;
  117. }
  118.  
  119. Mod.prototype.updateTitle = function(){
  120.  
  121. var RBTime = (game.bossCooldown-(+new Date-game.bossLastGenerated))/1000;
  122. var RBActive = game.bossScenario.active;
  123. var GBTime = $('span[name="timeRemaining"]').text();
  124. var GBActive = $('img[name="globalBossImg"]:visible').length;
  125. var GBStartIn = $('span[name="lobbyCountdown"]').text();
  126. var GBLobby = (GBTime == "JOIN" || GBTime == "LEAVE");
  127.  
  128. if(this.GBStatus == "waiting" && !GBActive && GBLobby){
  129. this.GBStatus = "inlobby";
  130. if(this.settings.gbAlert.value && this.timers.joinedGbDelay <= 0){
  131. this.timers.joinedGbDelay = 300 * 1000;
  132. alert("You can join the GB now.");
  133. }
  134. }
  135. if(this.GBStatus == "inlobby" && (GBActive || !GBLobby)){
  136. this.GBStatus = "waiting";
  137. }
  138.  
  139. if(this.settings.updateTitle.value){
  140. window.document.title = (GBTime == "JOIN" ? (GBActive ? 'In Fight' : 'Start in '+GBStartIn) : GBTime)
  141. + ' | ' + (RBTime > 0 ? Math.round(RBTime)+' sec' : RBActive ? 'RB Active' : 'RB Available')
  142. + ' | A GAME';
  143. }
  144.  
  145. }
  146.  
  147. Mod.prototype.toggleUpdateTitle = function(setting){
  148. if(setting.value){
  149. this.updateTitle();
  150. }else{
  151. window.document.title = 'A GAME';
  152. }
  153. }
  154.  
  155. Mod.prototype.renderWorkerProfit = function(){
  156. var self = this;
  157. _.each(this.workers, function(name){
  158. var worker = self.calcWorkerProfit(name);
  159. if(!worker) return;
  160. // Use toPrecision for possible arbitary numbers
  161. var value = self.numberFormat(worker.opmValue);
  162. if(worker.el.find('td:first .worker-profit').length){
  163. worker.el.find('td:first .worker-profit').html(value);
  164. }else{
  165. var el = $('<br/><span class="worker-profit" style="color:green;font-weight:bold;">'+value+'</span>');
  166. worker.el.find('td:first').append(el);
  167. self.elements.workerProfits.push(el);
  168. }
  169. });
  170. var best = this.getBestWorker();
  171. best.el.find('.worker-profit').append(' (Best)');
  172. }
  173.  
  174. Mod.prototype.toggleRenderWorkerProfit = function(setting){
  175. if(!setting.value){
  176. _.each(this.elements.workerProfits, function(el){
  177. el.remove();
  178. });
  179. }
  180. }
  181.  
  182. Mod.prototype.toggleReplaceNumbers = function(setting){
  183. if(setting.value)
  184. this.enableReplaceNumbers();
  185. else
  186. this.removeAllObservers();
  187. }
  188.  
  189. Mod.prototype.enableReplaceNumbers = function(){
  190. var self = this;
  191. var elements = [];
  192.  
  193. elements.push(document.querySelector('#money_display'));
  194. elements.push(_.toArray(document.querySelectorAll('table[name="workers"] span[name="price"]')));
  195.  
  196. _.each(_.flatten(elements), function(el){ self.addNumberFormatObserver(el); });
  197. }
  198.  
  199. Mod.prototype.addNumberFormatObserver = function(element){
  200. var self = this;
  201. var observer = new MutationObserver(function(mutations) {
  202. mutations.forEach(function(mutation) {
  203. if (mutation.type === 'childList' && mutation.addedNodes[0]) {
  204. mutation.target.innerHTML = self.numberFormat(mutation.addedNodes[0].nodeValue);
  205. }
  206. });
  207. });
  208. observer.observe(element, {childList: true});
  209. this.observers.push(observer);
  210. }
  211.  
  212. Mod.prototype.removeAllObservers = function(){
  213. _.each(this.observers, function(observer){
  214. observer.disconnect();
  215. });
  216. this.observers = [];
  217. }
  218.  
  219. Mod.prototype.numberFormat = function(num) {
  220. var prefix = '';
  221. // Get rid of scientific notation numbers
  222. /*if("number" === typeof num && ~String(num).indexOf('e')){
  223. num = parseFloat(String(num).split('e')[0]);
  224. }*/
  225. if("string" === typeof num){
  226. if(num.replace(/\s/g,'').substr(0,1) == '$') prefix = '$';
  227. num = parseInt(num.replace(/(\$|\,|\.|\s)/g,''));
  228. }
  229. var formats = {'K': 1e3, 'M': 1e6, 'B': 1e9, 'T': 1e12, 'Q': 1e15};
  230. return prefix + _.reduce(formats, function(result, amount, name){
  231. if(num >= amount) result = (num / amount).toFixed(1).replace(/\.0$/, '') + name;
  232. return String(result);
  233. }, num);
  234. }
  235.  
  236. Mod.prototype.calcWorkerProfit = function(name){
  237. var worker = {el: $('body table[name="workers"] tr[name="'+name+'"]')};
  238. if(!worker.el.length) return false;
  239. worker.name = name;
  240. var cost = Math.round(items.workers[name].price * (Math.pow(1.025, game.employed[name][0])));
  241. worker.cost = cost ? cost : items.workers[name].price;
  242. worker.opm = items.workers[name].opm + items.workers[name].opmModifier;
  243. worker.oreValue = this.workerValue[worker.name];
  244. worker.opmValue = worker.opm * worker.oreValue;
  245. worker.profit = worker.opmValue / worker.cost;
  246. return worker;
  247.  
  248. /* -- In case I ever need to re-calculate worker's ore values --
  249. var oresTotal = 0, oresTotalWorth = 0;
  250. _.each(this.game.employed[worker.name][1], function(amount, type){
  251. oresTotal += amount;
  252. oresTotalWorth += items.ores[type].worth * amount;
  253. });
  254. worker.oreValue = oresTotal > 0 ? oresTotalWorth / oresTotal : 1;
  255. */
  256. }
  257.  
  258. Mod.prototype.getBestWorker = function(){
  259. var self = this;
  260. var sort_list = [];
  261. $.each(this.workers, function(i,val){
  262. var worker = self.calcWorkerProfit(val);
  263. if(worker) sort_list.push(worker);
  264. });
  265. sort_list.sort(function(a,b){return b.profit-a.profit});
  266. return sort_list[0];
  267. }
  268.  
  269. Mod.prototype.terminate = function(){
  270. _.each(this.elements, function(element){
  271. if(_.isArray(element)){
  272. _.each(element, function(el){ el.remove(); });
  273. }else{
  274. element.remove();
  275. }
  276. });
  277. this.removeAllObservers();
  278. clearInterval(this.timers.update);
  279. if($('#popup:visible #mod-settings')){
  280. popupHandleStack();
  281. }
  282. window.mod = undefined;
  283. window.document.title = "A GAME";
  284. }
  285.  
  286. function popup(c,a,b,d){if(popupActive)popupStack.push([0,c,a,b,d]);else{popupActive=!0;$('#popup p[name="title"]').text(c);$('#popup div[name="content"]').html(a);$('#popup span[name="buttons"] :not(button[name="continue"])').remove();$('#popup span[name="buttons"] button').hide();if(0==d)if("object"==typeof b){a=(new Date).getTime();for(var e in b)a=a+"-"+e,$('#popup span[name="buttons"]').append('<button name="'+a+'">'+b[e].text+"</button> "),$(document).one("click",'#popup button[name="'+a+'"]',
  287. function(a){b[a].func();"undefined"!=typeof b[a].response?(popupActive=!1,popup(c,b[a].response,"",0)):"undefined"!=typeof b[a].delay?popupHandleStack(!0):popupHandleStack()}.bind(this,e))}else $('#popup button[name="continue"]').show();else setTimeout(function(){popupHandleStack()},d+750);$("#popup").show();$("html, body").animate({scrollTop:0},"slow")}}
  288. function popupHandleStack(c){popupActive=!1;"undefined"!=typeof popupStack[0]&&1==popupStack[0][0]&&popupStack.shift();"undefined"!=typeof popupStack[0]?(popupStack[0][0]=1,popup(popupStack[0][1],popupStack[0][2],popupStack[0][3],popupStack[0][4])):c||$("#popup").hide(0)};
  289.  
  290. // Start
  291. if(window.mod) window.mod.terminate();
  292. window.mod = new Mod();
Advertisement
Add Comment
Please, Sign In to add comment