Cookie clicker tools

Cookie clicker tools (visual)

当前为 2017-03-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Cookie clicker tools
  3. // @namespace orteil.dashnet.org
  4. // @version 2.091
  5. // @description Cookie clicker tools (visual)
  6. // @author Anton
  7. // @match http://orteil.dashnet.org/cookieclicker/*
  8. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13. var log = [], logTitle = '<div class="section">Cookie clicker tools 2.091 is here!</div>', newLogs = 0;
  14.  
  15. var started = false, t2, t3, tClick, tClickFrenzy, oldTitle = '', tLog,
  16. tPriority;
  17.  
  18. var _history = function() {
  19. var hist = [];
  20. hist.push('2.091 - priority table refactor');
  21. hist.push('2.090 - buy very cheap items first');
  22. hist.push('2.089 - fix pool toggle buy');
  23. hist.push('2.088 - fix');
  24. hist.push('2.087 - fix buy unknown upgrades');
  25. hist.push('2.086 - clear log button');
  26. hist.push('2.085 - fix');
  27. hist.push('2.084 - buy unknown upgrades if their price is 0.1% of cookie storage');
  28. hist.push('2.083 - mouse upgrades is processed as Upgrades');
  29. hist.push('2.082 - start from scratch');
  30. hist.push('2.081 - fix');
  31. hist.push('2.080 - add history, add option "showTimeCompensate"');
  32. hist.push('2.079 - hide donate box');
  33. hist.push('2.078 - Kittens now is processed as Upgrades');
  34. hist.push('');
  35. hist.push('');
  36. return hist;
  37. }
  38.  
  39. var _getKittenPercentByUpgradeName = function(kName) {
  40. if (kName == 'Kitten helpers') return Game.milkProgress*0.1;
  41. if (kName == 'Kitten workers') return Game.milkProgress*0.125;
  42. if (kName == 'Kitten engineers') return Game.milkProgress*0.15;
  43. if (kName == 'Kitten overseers') return Game.milkProgress*0.175;
  44. if (kName == 'Kitten managers') return Game.milkProgress*0.2;
  45. if (kName == 'Kitten accountants') return Game.milkProgress*0.2;
  46. if (kName == 'Kitten specialists') return Game.milkProgress*0.2;
  47. if (kName == 'Kitten experts') return Game.milkProgress*0.2;
  48. if (kName == 'Kitten angels') return Game.milkProgress*0.1;
  49. return 0;
  50. }
  51.  
  52. var options = {
  53. bankStoragePercent: 100,
  54. showTimeCompensate: false
  55. };
  56. var _store = function(name, value) {
  57. if (typeof(Storage) !== "undefined") localStorage.setItem(name, value);
  58. };
  59. var _restore = function(name) {
  60. if (typeof(Storage) !== "undefined") return localStorage.getItem(name);
  61. else return undefined;
  62. };
  63.  
  64. var _getBuffMult = function () {
  65. var buffMult = 1;
  66. for (var buff in Game.buffs) {
  67. if (Game.buffs[buff] !== 0 && typeof Game.buffs[buff].multCpS !== 'undefined') {
  68. buffMult = buffMult * Game.buffs[buff].multCpS;
  69. }
  70. }
  71. return buffMult === 0 ? 1 : buffMult;
  72. };
  73.  
  74. var _getNormalCookiesPs = function () {
  75. return Game.cookiesPs / _getBuffMult();
  76. };
  77.  
  78. var _getMinimalMoney = function () {
  79. return Math.floor(_getNormalCookiesPs() * 42100 * options.bankStoragePercent / 100);
  80. };
  81.  
  82. var _getMoneyCanSpend = function () {
  83. var moneyCanSpend = Game.cookies - _getMinimalMoney();
  84. return moneyCanSpend;
  85. };
  86.  
  87. var priorityListObj = {}, priorityList = [], timeCompensateObj = {}, timeCompensateList = [];
  88. var _createPriorityList = function() {
  89. var moneyCanSpend = _getMoneyCanSpend();
  90. var money = Game.cookies;
  91. priorityList = [];
  92. priorityListObj = {};
  93. for (var g in Game.UpgradesById) {
  94. if (Game.UpgradesById[g].bought === 0 && Game.UpgradesById[g].unlocked == 1) {
  95. var isMultiplier = Game.UpgradesById[g].desc.indexOf('production multiplier') >= 0;
  96. var isDouble = Game.UpgradesById[g].desc.indexOf('<b>twice</b>') >= 0;
  97. var isDoubleGrandma = Game.UpgradesById[g].desc.indexOf('Grandmas are <b>twice</b>') >= 0;
  98. var isKitten = Game.UpgradesById[g].name.indexOf('Kitten ') >= 0;
  99. var isDoubleCursor = Game.UpgradesById[g].desc.indexOf('The mouse and cursors are <b>twice</b>') >= 0;
  100. var increasedMoney = 0;
  101. if (isDoubleCursor) {
  102. increasedMoney = Game.Objects['Cursor'].storedTotalCps;
  103. } else if (isKitten) {
  104. var kittenPercent = _getKittenPercentByUpgradeName(Game.UpgradesById[g].name);
  105. increasedMoney = _getNormalCookiesPs() * kittenPercent;
  106. } else if (isDoubleGrandma) {
  107. increasedMoney = Game.Objects['Grandma'].storedTotalCps;
  108. } else if (isMultiplier) {
  109. increasedMoney = _getNormalCookiesPs() * Game.UpgradesById[g].power / 100;
  110. } else if (isDouble) {
  111. increasedMoney = Game.UpgradesById[g].buildingTie.storedTotalCps;
  112. }
  113. var interest = increasedMoney > 0 ? Game.UpgradesById[g].basePrice / increasedMoney : '-';
  114. if (interest != '-') {
  115. if (typeof priorityListObj[Math.floor(interest)] === 'undefined') {
  116. priorityListObj[Math.floor(interest)] = {
  117. name: Game.UpgradesById[g].name,
  118. title: Game.UpgradesById[g].name + ' - ' + Beautify(Game.UpgradesById[g].basePrice),
  119. price: Game.UpgradesById[g].basePrice,
  120. cps: 1,
  121. type: 'upgrade',
  122. id: g
  123. };
  124. }
  125. } else if (Game.UpgradesById[g].type == 'upgrade' &&
  126. Game.UpgradesById[g].basePrice < moneyCanSpend &&
  127. Game.UpgradesById[g].pool != 'toggle')
  128. {
  129. interest = Game.UpgradesById[g].basePrice / moneyCanSpend * 1000;
  130. if (typeof priorityListObj[Math.floor(interest)] === 'undefined') {
  131. priorityListObj[Math.floor(interest)] = {
  132. name: Game.UpgradesById[g].name,
  133. title: Game.UpgradesById[g].name + ' - ' + Beautify(Game.UpgradesById[g].basePrice),
  134. price: Game.UpgradesById[g].basePrice,
  135. cps: 1,
  136. type: 'upgrade',
  137. id: g
  138. };
  139. }
  140. }
  141. //priorityList.push(Game.UpgradesById[g].name + ' - ' + Beautify(Game.UpgradesById[g].basePrice) + ' (INTEREST: ' + Beautify(interest) + ')');
  142. }
  143. }
  144. for (var i in Game.ObjectsById) {
  145. if (typeof i !== 'undefined' && i != 'undefined' && Game.ObjectsById.hasOwnProperty(i)) {
  146. if (Game.ObjectsById[i].locked === 0) {
  147. interest = Game.ObjectsById[i].price / Game.ObjectsById[i].storedCps;
  148. var pp = moneyCanSpend / Game.ObjectsById[i].price; // percent of cheapness
  149. if (pp > 50) interest /= pp * 50;
  150. priorityListObj[Math.floor(interest)] = {
  151. name: Game.ObjectsById[i].name,
  152. title: Game.ObjectsById[i].name + ' - ' + Beautify(Game.ObjectsById[i].price),
  153. price: Game.ObjectsById[i].price,
  154. cps: Game.ObjectsById[i].storedCps,
  155. type: 'buy',
  156. id: i
  157. };
  158. //priorityList.push(Game.ObjectsById[i].name + ' - ' + Beautify(Game.ObjectsById[i].price) + ' (INTEREST: ' + Beautify(interest) + ')');
  159. }
  160. }
  161. }
  162. for (i in priorityListObj) {
  163. var prefix = priorityListObj[i].price < moneyCanSpend ? '!!! ' : (priorityListObj[i].price < money ? '! ' : '');
  164. var pp = Math.floor(moneyCanSpend / priorityListObj[i].price * 100);
  165. priorityList.push(prefix + priorityListObj[i].title + ' : ' + i + ' (' + pp + '%)');
  166. }
  167.  
  168. timeCompensateObj = {};
  169. timeCompensateList = [];
  170. for (i in priorityListObj) {
  171. var o = priorityListObj[i];
  172. if (o.type == 'buy') {
  173. var cpsWithPercent = (o.cps / _getNormalCookiesPs() + 1) * o.cps;
  174. var interestNew = o.price / cpsWithPercent;
  175. timeCompensateObj[Math.floor(interestNew)] = {
  176. name: o.name,
  177. title: o.title,
  178. price: o.price,
  179. cps: cpsWithPercent,
  180. type: 'buy',
  181. id: o.id
  182. };
  183. } else {
  184. timeCompensateObj[i] = o;
  185. }
  186. }
  187. for (i in timeCompensateObj) {
  188. prefix = timeCompensateObj[i].price < moneyCanSpend ? '!!! ' : (timeCompensateObj[i].price < money ? '! ' : '');
  189. timeCompensateList.push(prefix + timeCompensateObj[i].title + ' : ' + i);
  190. }
  191. };
  192.  
  193. var _caption = function(txt) {
  194. var $v = jQuery('#versionNumber');
  195. if ($v.text() != txt) $v.text(txt);
  196. };
  197.  
  198. var _title = function(txt) {
  199. var newTitle = (newLogs > 0 ? '(' + newLogs + ') ' : '') + txt;
  200. if (document.title != newTitle) {
  201. oldTitle = txt;
  202. document.title = newTitle;
  203. }
  204. };
  205.  
  206. var _titlePercent = function(have, need, txt) {
  207. var percent = Math.ceil(have / (need !== 0 ? need : 1) * 100);
  208. _title(String(percent) + "% - " + txt);
  209. };
  210.  
  211. var _logSectionTitle = function (text) {
  212. return '<div class="title">' + text + '</div>';
  213. };
  214.  
  215. var _logSection = function (title, items) {
  216. return '<div class="subsection">' + _logSectionTitle(title) +
  217. '<div class="listing">' + items.join('</div><div class="listing">') + '</div>' +
  218. '</div>';
  219. };
  220. var clearLogButton = '<button data-id="clearLog" onclick="document.getElementById(\'logButton\').onbuttonclick(this,event)">Clear Log</button>';
  221.  
  222. var _logPriorityTable = function() {
  223. var t = '<table style="width:100%"><tr><th>Type</th><th>Name</th><th>Price</th><th>Interest</th><th>% bank</th></tr>';
  224. for (i in priorityListObj) {
  225. var o = priorityListObj[i];
  226. var pp = Math.floor(moneyCanSpend / o.price * 100);
  227. t += '<tr><td>' + o.type + '</td><td>' + o.name + '</td><td>' + Beautify(o.price) + '</td><td>' + i + '</td><td>' + pp + '</td></tr>';
  228. }
  229. t += '</table>';
  230. return '<div class="subsection">' + _logSectionTitle('Priority ' + clearLogButton) + '<div class="listing">' + t + '</div></div>';
  231. }
  232. var _optionsSection = function() {
  233. var content = [];
  234. content.push('<label for="bankStoragePercent" id="bankStoragePercentLabel">Storage (' + Beautify(_getMinimalMoney()) + ') percent: ' + options.bankStoragePercent + '%</label> <input name="bankStoragePercent" id="bankStoragePercent" type="range" min="0" max="100" value="' + options.bankStoragePercent + '" onchange="document.getElementById(\'logButton\').onsliderchange(this)" />');
  235. content.push('<button data-id="showTimeCompensate" onclick="document.getElementById(\'logButton\').onbuttonclick(this,event)">showTimeCompensate: ' + (options.showTimeCompensate ? 'TRUE' : 'FALSE') + '</button>');
  236. content.push(clearLogButton);
  237. return content;
  238. };
  239.  
  240. var _updateLog = function () {
  241. Game.updateLog = logTitle +
  242. //_logSection('Priority ' + clearLogButton, priorityList) +
  243. _logPriorityTable() +
  244. (options.showTimeCompensate ? _logSection('Priority (time compensate)', timeCompensateList) : '') +
  245. _logSection('Log', log) +
  246. _logSection('Options', _optionsSection()) +
  247. _logSection('History', _history());
  248. var newButtonText = newLogs > 0 ? 'Log (' + newLogs + ')' : 'Log';
  249. if (jQuery('#logButton').text() != newButtonText) jQuery('#logButton').text(newButtonText);
  250. _title(oldTitle);
  251. };
  252.  
  253. document.getElementById('logButton').onsliderchange = function(el) {
  254. var val = $(el).val();
  255. _store('bankStoragePercent', val);
  256. options.bankStoragePercent = val;
  257. jQuery('#bankStoragePercentLabel').text('Storage (' + Beautify(_getMinimalMoney()) + ') percent: ' + val + '%');
  258. _updateLog();
  259. };
  260. document.getElementById('logButton').onbuttonclick = function(el,e) {
  261. e.preventDefault();
  262. var id = jQuery(el).attr('data-id');
  263. if (id == 'showTimeCompensate') {
  264. options.showTimeCompensate = !options.showTimeCompensate;
  265. } else if (id == 'clearLog') {
  266. log = [];
  267. newLogs = 0;
  268. }
  269. jQuery(el).attr('disabled', 'disabled');
  270. _updateLog();
  271. }
  272.  
  273. tPriority = setInterval(function () {
  274. _createPriorityList();
  275. _updateLog();
  276. }, 1000);
  277.  
  278. var _addLog = function(text) {
  279. var t = new Date();
  280. log.push(t.toUTCString() + ': ' + text);
  281. newLogs++;
  282. _updateLog();
  283. };
  284.  
  285. tLog = setInterval(function () {
  286. _updateLog();
  287. }, 600);
  288.  
  289. jQuery('#logButton').text('Log').on("click", function() {
  290. newLogs = 0;
  291. jQuery('#logButton').text('Log');
  292. });
  293.  
  294. var _getLongTimeUpgrade = function () {
  295. var moneyCanSpend = _getMoneyCanSpend();
  296. var longTime = (Game.cookiesPs < 100) ? 1 : (
  297. (Game.cookiesPs < 1000) ? 2 : (
  298. (Game.cookiesPs < 10000) ? 3 : (
  299. (Game.cookiesPs < 1E5) ? 4 : (
  300. (Game.cookiesPs < 1E6) ? 5 : (
  301. (Game.cookiesPs < 1E9) ? 10 : 15
  302. )
  303. )
  304. )
  305. )
  306. );
  307. return longTime;
  308. };
  309.  
  310. var _getWrinklerCount = function () {
  311. var wrinklersCount = 0;
  312. for (var i in Game.wrinklers) {
  313. if (Game.wrinklers[i].sucked > 0) {
  314. wrinklersCount++;
  315. }
  316. }
  317. return wrinklersCount;
  318. };
  319.  
  320. var _killAllWrinklers = function () {
  321. var wrinklersCount = _getWrinklerCount();
  322. if (wrinklersCount >= 10 || Game.hasBuff('Elder frenzy')) {
  323. var wrinklesIncome = 0;
  324. for (var i in Game.wrinklers) {
  325. wrinklesIncome += Game.wrinklers[i].sucked;
  326. Game.wrinklers[i].hp = 0; // kill ALL
  327. }
  328. if (wrinklesIncome > 0) {
  329. _addLog('Killed all Wrinkles for ' + Beautify(wrinklesIncome) + ' because of ' + (Game.hasBuff('Elder frenzy') ? '"Elder frenzy"' : 'count 10.'));
  330. }
  331. }
  332. };
  333.  
  334. var _getMinimalPriceForNewObject = function () {
  335. var minNewObjectPrice = null;
  336. for (var i in Game.ObjectsById) {
  337. if (typeof i !== 'undefined' && i != 'undefined' && Game.ObjectsById.hasOwnProperty(i)) {
  338. if (Game.ObjectsById[i].locked === 0 && Game.ObjectsById[i].amount === 0) {
  339. var p = Game.ObjectsById[i].price;
  340. if (minNewObjectPrice === null || p < minNewObjectPrice) minNewObjectPrice = p;
  341. }
  342. }
  343. }
  344. return minNewObjectPrice;
  345. };
  346.  
  347. var startT = function() {
  348. t2 = setInterval(function() {
  349.  
  350. _killAllWrinklers();
  351.  
  352. var moneyCanSpend = _getMoneyCanSpend();
  353.  
  354. if (moneyCanSpend < 0) {
  355. var minimalMoney = _getMinimalMoney();
  356. _caption('Collecting minimum ' + Beautify(minimalMoney));
  357. _titlePercent(Game.cookies, minimalMoney, 'minimum');
  358. return;
  359. }
  360. if (priorityList.length > 0) {
  361. for (var idx in priorityListObj) {
  362. var needToBuy = priorityListObj[idx];
  363. if (needToBuy.type == 'upgrade') {
  364. _caption('Upgrading ' + needToBuy.name + ' for ' + Beautify(needToBuy.price));
  365. if (needToBuy.price < moneyCanSpend) {
  366. if (Game.UpgradesById[needToBuy.id].canBuy()) {
  367. Game.UpgradesById[needToBuy.id].buy();
  368. _addLog('Buy upgrade ' + Game.UpgradesById[needToBuy.id].name + ' for ' + Beautify(needToBuy.price));
  369. _createPriorityList();
  370. }
  371. } else {
  372. _titlePercent(moneyCanSpend, needToBuy.price, Game.UpgradesById[needToBuy.id].name);
  373. }
  374. } else {
  375. _caption('Collecting ' + Beautify(needToBuy.price) + ' for ' + needToBuy.name);
  376. if (Game.ObjectsById[needToBuy.id].price < moneyCanSpend) {
  377. Game.ObjectsById[needToBuy.id].buy(1);
  378. _addLog('Buy object ' + Game.ObjectsById[needToBuy.id].name + ' for ' + Beautify(Game.ObjectsById[needToBuy.id].price));
  379. _createPriorityList();
  380. } else {
  381. _titlePercent(moneyCanSpend, needToBuy.price, Game.ObjectsById[needToBuy.id].name);
  382. }
  383. }
  384. break;
  385. }
  386. return;
  387. }
  388.  
  389. var minNewObjectPrice = _getMinimalPriceForNewObject();
  390.  
  391. var u = null; // upgrade can buy
  392. var minUpgradePrice = null;
  393. for (var g in Game.UpgradesById) {
  394. if (Game.UpgradesById[g].bought === 0 && Game.UpgradesById[g].unlocked == 1) {
  395. //u.push(Game.UpgradesById[g]);
  396. if (minUpgradePrice === null) {
  397. minUpgradePrice = Game.UpgradesById[g].basePrice;
  398. u = g;
  399. } else if (Game.UpgradesById[g].basePrice < minUpgradePrice) {
  400. minUpgradePrice = Game.UpgradesById[g].basePrice;
  401. u = g;
  402. }
  403. }
  404. }
  405.  
  406. var longTime = _getLongTimeUpgrade();
  407. var needUpgrade = (minNewObjectPrice > minUpgradePrice || minUpgradePrice === null) &&
  408. u !== null && typeof Game.UpgradesById[u] !== 'undefined' &&
  409. (moneyCanSpend >= minUpgradePrice || ((minUpgradePrice - moneyCanSpend) / _getNormalCookiesPs()) <= (longTime*60));
  410.  
  411. if (needUpgrade)
  412. {
  413. if (Game.UpgradesById[u].canBuy()) {
  414. Game.UpgradesById[u].buy();
  415. _addLog('Buy upgrade ' + Game.UpgradesById[u].name + ' for ' + Beautify(minUpgradePrice));
  416. } else {
  417. _caption('Upgrading ' + Game.UpgradesById[u].name + ' for ' + Beautify(minUpgradePrice));
  418. _titlePercent(moneyCanSpend, minUpgradePrice, Game.UpgradesById[u].name);
  419. }
  420. } else {
  421. var minInvert = null, minObj = null;
  422. for (var i in Game.ObjectsById) {
  423. if (typeof i !== 'undefined' && i != 'undefined' && Game.ObjectsById.hasOwnProperty(i)) {
  424. if (Game.ObjectsById[i].locked === 0) {
  425. var interest = Game.ObjectsById[i].price / Game.ObjectsById[i].storedCps;
  426. if (minInvert === null) {
  427. minInvert = interest;
  428. minObj = i;
  429. } else if (interest < minInvert) {
  430. minInvert = interest;
  431. minObj = i;
  432. }
  433. }
  434. }
  435. }
  436. if (minObj !== null) {
  437. if (Game.ObjectsById[minObj].price < moneyCanSpend) {
  438. Game.ObjectsById[minObj].buy(1);
  439. _addLog('Buy object ' + Game.ObjectsById[minObj].name + ' for ' + Beautify(Game.ObjectsById[minObj].price));
  440. } else {
  441. _caption('Collecting ' + Beautify(Game.ObjectsById[minObj].price) + ' for ' + Game.ObjectsById[minObj].name);
  442. _titlePercent(moneyCanSpend, Game.ObjectsById[minObj].price, Game.ObjectsById[minObj].name);
  443. }
  444. }
  445.  
  446. }
  447.  
  448. }, 1000);
  449.  
  450. started = true;
  451. _caption('Started');
  452. _addLog('Autobuy start!');
  453. };
  454.  
  455. var stopT = function() {
  456. clearInterval(t2);
  457. started = false;
  458. _caption('Collecting gold...');
  459. _addLog('Autobuy stop.');
  460. };
  461.  
  462. jQuery('#versionNumber').on("click", function() {
  463. if (!started)
  464. startT();
  465. else
  466. stopT();
  467. });
  468.  
  469. options.bankStoragePercent = _restore('bankStoragePercent');
  470. if (typeof options.bankStoragePercent === 'undefined' || options.bankStoragePercent === null) {
  471. options.bankStoragePercent = 100;
  472. }
  473.  
  474. setTimeout(function() {
  475. jQuery('#donateBox').hide();
  476. t3 = setInterval(function() {
  477. var golden = Game.shimmers;
  478. if (golden.length > 0) {
  479. for (var i in golden) {
  480. golden[i].pop();
  481. }
  482. }
  483. }, 500);
  484.  
  485. tClickFrenzy = setInterval(function() {
  486. if (Game.hasBuff('Click frenzy') || Game.hasBuff('Cursed finger') || Game.cookiesPs < 1000000) {
  487. Game.ClickCookie();
  488. }
  489. }, 75);
  490.  
  491. if (Game.cookiesPs < 1E6) {
  492. tClick = setInterval(function() {
  493. Game.mouseX = jQuery('#bigCookie').width() / 2 + jQuery('#bigCookie').offset().left;
  494. Game.mouseY = jQuery('#bigCookie').height() / 2 + jQuery('#bigCookie').offset().top;
  495. Game.ClickCookie();
  496. if (Game.cookiesPs > 1E8) clearInterval(tClick);
  497. }, 300);
  498. }
  499.  
  500. _caption('Collecting gold...');
  501. }, 5000);
  502. })();