Trimps tools

Trimps tools (visual)

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

  1. // ==UserScript==
  2. // @name Trimps tools
  3. // @namespace trimps.github.io
  4. // @version 1.033
  5. // @description Trimps tools (visual)
  6. // @author Anton
  7. // @match https://trimps.github.io
  8. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. var tMain, tAutoBuy, tStyleFix, tPassiveWatcher, $ = jQuery, isStarted = false;
  15.  
  16. var _getBarWidthPercent = function(barSelector) {
  17. return jQuery(barSelector).width() / jQuery(barSelector).parent().width() * 100;
  18. }
  19.  
  20. var _log = function(mes, type) {
  21. if (typeof type === 'undefined') type = "Story";
  22. message("BOT: " + mes, type);
  23. }
  24.  
  25. var _checkProgress = function() {
  26. var maxProgress = 0, maxResource = '';
  27. var food = _getBarWidthPercent('#foodBar');
  28. var wood = _getBarWidthPercent('#woodBar');
  29. var metal = _getBarWidthPercent('#metalBar');
  30. if (food > maxProgress) {maxProgress = food; maxResource = 'food';}
  31. if (wood > maxProgress) {maxProgress = wood; maxResource = 'wood';}
  32. if (metal > maxProgress) {maxProgress = metal; maxResource = 'metal';}
  33. document.title = '' + Math.floor(maxProgress) + '% ' + maxResource;
  34. };
  35.  
  36. var _start = function() {
  37. clearInterval(tPassiveWatcher);
  38. tAutoBuy = setInterval(function() {
  39. var cnt = _autoUpgrade() + _autoBuy() + _autoJobs();
  40. if (cnt > 0) {
  41. tooltip('hide');
  42. }
  43. }, 1000);
  44. $('#botStart').text('Bot stop');
  45. }
  46.  
  47. var _passiveWatcher = function() {
  48. var cnt = 0;
  49. var food = _getBarWidthPercent('#foodBar');
  50. var wood = _getBarWidthPercent('#woodBar');
  51. var metal = _getBarWidthPercent('#metalBar');
  52. if (food > 90) {
  53. $('#Barn').trigger('click');
  54. cnt++;
  55. }
  56. if (wood > 90) {
  57. $('#Shed').trigger('click');
  58. cnt++;
  59. }
  60. if (metal > 90) {
  61. $('#Forge').trigger('click');
  62. cnt++;
  63. }
  64. cnt += _autoJobs();
  65. return cnt;
  66. }
  67.  
  68. var _stop = function() {
  69. clearInterval(tAutoBuy);
  70. $('#botStart').text('Bot start');
  71. tPassiveWatcher = setInterval(_passiveWatcher, 1000);
  72. _log('Passive watcher started');
  73. }
  74. var _onStartButton = function() {
  75. if (isStarted) {
  76. _stop();
  77. _log('Stop.');
  78. } else {
  79. _start();
  80. _log('Started!');
  81. }
  82. }
  83.  
  84. var _styleUpdate = function() {
  85. // remove counts
  86. $('head').append('<style type="text/css">.queueItem,.btn{padding:0}.thingColorCanNotAfford.upgradeThing{background-color:#530053;}</style>');
  87. // remove tabs
  88. $('#buyTabs').hide();
  89. filterTabs('all');
  90. // remove captions
  91. $('#buildingsTitleDiv,#upgradesTitleDiv,#equipmentTitleDiv').hide();
  92. // fix height
  93. $('#topRow,#queueContainer').css('margin-bottom', '0');
  94. $('#jobsTitleDiv').css('padding', '0').css('font-size', 'smaller');
  95. $('#buyHere').css('margin', '0').css('padding', '0').css('overflow-x', 'hidden');
  96. $('#queueContainer').css('height', '70px');
  97. $('#numTabs').css('margin', '0');
  98. $('#buyContainer').css('height', 'calc(99vh - 20vw - 96px)');
  99. // add button
  100. $('#settingsTable tr').append('<td class="btn btn-info" id="botStart">Bot start</td>');
  101. $('#botStart').click(_onStartButton);
  102. }
  103.  
  104. var _autoUpgrade = function() {
  105. var $item = $('#upgradesHere').find('.thingColorCanAfford').first();
  106. if ($item.length > 0) {
  107. $item.trigger( "click" );
  108. _log('Upgrading ' + $item.attr('id'));
  109. return 1;
  110. } else {
  111. return 0;
  112. }
  113. }
  114.  
  115. var _autoBuy = function() {
  116. var $item = $('#buildingsHere').find('.thingColorCanAfford:not([id=Wormhole])').last();
  117. if ($item.length > 0 && $item.attr('id') != 'Trap') {
  118. $item.trigger("click");
  119. _log('Building ' + $item.attr('id'));
  120. return 1;
  121. } else {
  122. return 0;
  123. }
  124. }
  125.  
  126. var _buyJobs = function($obj, unemployed, objName) {
  127. if ($obj.length > 0) {
  128. var cnt = 1;
  129. if (unemployed > 61*10) {numTab(4);cnt=100;}
  130. else if (unemployed > 61*2.5) {numTab(3);cnt=25;}
  131. else if (unemployed > 61) {numTab(2);cnt=10;}
  132. $obj.trigger("click");
  133. if (unemployed > 61) numTab(1); // +1
  134. _log('New ' + objName + (cnt > 1 ? " x" + cnt : ''), 'Combat');
  135. return cnt;
  136. } else {
  137. return 0;
  138. }
  139. }
  140.  
  141. var _autoJobs = function() {
  142. var cnt = 0;
  143. var $trainer = $('#jobsHere').find('.thingColorCanAfford[id=Trainer]');
  144. if ($trainer.length > 0) {
  145. $trainer.trigger("click");
  146. _log('New trainer');
  147. cnt++;
  148. }
  149. var $explorer = $('#jobsHere').find('.thingColorCanAfford[id=Explorer]');
  150. if ($explorer.length > 0) {
  151. $explorer.trigger("click");
  152. _log('New explorer');
  153. cnt++;
  154. }
  155. var farmer = parseInt($('#jobsHere').find('#FarmerOwned').text());
  156. var lumber = parseInt($('#jobsHere').find('#LumberjackOwned').text());
  157. var miner = parseInt($('#jobsHere').find('#MinerOwned').text());
  158. var science = parseInt($('#jobsHere').find('#ScientistOwned').text());
  159. var jobsTotal = farmer + lumber + miner + science;
  160. var unemployed = parseInt($('#jobsTitleUnemployed').text());
  161. if (farmer < (jobsTotal / 3.05)) {
  162. var $farmer = $('#jobsHere').find('.thingColorCanAfford[id=Farmer]');
  163. cnt += _buyJobs($farmer, unemployed, 'farmer');
  164. } else if (lumber < (jobsTotal / 3.05)) {
  165. var $lumber = $('#jobsHere').find('.thingColorCanAfford[id=Lumberjack]');
  166. cnt += _buyJobs($lumber, unemployed, 'lumberjack');
  167. } else if (miner < (jobsTotal / 3.05)) {
  168. var $miner = $('#jobsHere').find('.thingColorCanAfford[id=Miner]');
  169. cnt += _buyJobs($miner, unemployed, 'miner');
  170. } else if (science < (jobsTotal / 61)) {
  171. var $science = $('#jobsHere').find('.thingColorCanAfford[id=Scientist]');
  172. cnt += _buyJobs($science, unemployed, 'scientist');
  173. }
  174. if (unemployed > 0 && cnt === 0) {
  175. $farmer = $('#jobsHere').find('.thingColorCanAfford[id=Farmer]');
  176. cnt += _buyJobs($farmer, unemployed, 'farmer');
  177. }
  178.  
  179. return cnt;
  180. }
  181.  
  182. var _styleFix = function() {
  183. $('.buyBox').find('.thing').find('br').remove();
  184. $('.buyBox').find('.thing').find('.thingOwned').css('margin-left','4px');
  185. $('#upgradesHere').find('.alert.badge').text('');
  186. }
  187.  
  188. setTimeout(function() {
  189. tMain = setInterval(_checkProgress, 500);
  190.  
  191. tStyleFix = setInterval(_styleFix, 2000);
  192. _styleUpdate();
  193. _styleFix();
  194. _stop(); // start passive watcher
  195. }, 1000);
  196.  
  197. })();