TW Auto Farm Assistant

auto farm script for tribalwars.

  1. // ==UserScript==
  2. // @name TW Auto Farm Assistant
  3. // @author ismail.yankayis
  4. // @version 2.0.1
  5. // @grant Public
  6. // @description auto farm script for tribalwars.
  7. // @include http*://*.tribalwars.net/*screen=am_farm*
  8. // @include https*://*.klanlar.org/*screen=am_farm*
  9. // @namespace https://greasyfork.org/users/1019070
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. var botProtect = $('body').data('bot-protect');
  14. if (document.URL.indexOf('screen=am_farm') == -1)
  15. console.log('Você deve executar o script no assistente de farm!');
  16. else if (botProtect !== undefined) {
  17. alert('Alerta Captcha!');
  18. }
  19. else {
  20. var // menu = $('#am_widget_Farm a.farm_icon_a'),
  21. menu = '#plunder_list a.farm_icon_a',
  22. menu_a = '#plunder_list a.farm_icon_a',
  23. menu_b = '#plunder_list a.farm_icon_b',
  24. attackInterval = 300,
  25. minhaVar = "",
  26. changeVillage = true,
  27. changeVillageInterval = 360000,
  28. refreshPage = false,
  29. refreshPageInterval = 25000,
  30. lootAssistantPageSize = 100,
  31. minUnitNumber = 6,
  32. boxCaptcha = $("#bot_check");
  33.  
  34. var randomTime = function (superior, inferior) {
  35. var numPosibilidades = superior - inferior,
  36. aleat = Math.random() * numPosibilidades;
  37.  
  38. return Math.round(parseInt(inferior) + aleat);
  39. };
  40.  
  41. if (attackInterval === "random") {
  42. attackInterval = randomTime(5000, 10000);
  43. }
  44. if (refreshPageInterval === "random") {
  45. refreshPageInterval = randomTime(700000, 800000);
  46. }
  47.  
  48. $('img').each(function () {
  49. var tempStr = $(this).attr('src');
  50. if (tempStr.indexOf('attack') != -1)
  51. $(this).addClass('tooltip');
  52. });
  53.  
  54. if (refreshPage === true) {
  55. setInterval(function () {
  56. window.location.reload();
  57. }, refreshPageInterval);
  58. }
  59.  
  60.  
  61. if (changeVillageInterval !== false) {
  62. if (changeVillageInterval === true)
  63. if ($('#light').text() >= 1)
  64. changeVillageInterval = randomTime(100000, 150000);
  65. else
  66. changeVillageInterval = randomTime(3000, 5000);
  67. else
  68. changeVillageInterval = parseInt(changeVillageInterval) + parseInt(randomTime(500, 1000));
  69. }
  70.  
  71. showAllBarbs();
  72. if (parseInt($('#light').text()) < parseInt($('#heavy').text())) {
  73. menu = menu_b;
  74. }
  75. console.log("menu: ", menu);
  76. }
  77.  
  78. startFarming();
  79.  
  80. if (changeVillage === true) {
  81. var altVillage = setInterval(function () {
  82. $('.arrowRight, .groupRight').click();
  83.  
  84. clearInterval(altVillage);
  85. }, changeVillageInterval);
  86. }
  87.  
  88.  
  89. var checkCaptcha = setInterval(function () {
  90. if (boxCaptcha.length) {
  91. alert('Captcha found!');
  92. clearInterval(checkCaptcha);
  93. clearInterval(altVillage);
  94. }
  95. }, 100);
  96.  
  97. function startFarming() {
  98. var count = 1;
  99. for (i = 0; i < lootAssistantPageSize; i++) {
  100. if(checkWall(10)) {
  101. $(menu).eq(i).each(function () {
  102. var intervalOfNexAttack = attackInterval * count;
  103. setTimeout(function (minhaVar) {
  104. if (menu === menu_a && $('#light').text() >= minUnitNumber) {
  105. $(minhaVar).click();
  106. } else if (menu === menu_b && $('#heavy').text() >= minUnitNumber) {
  107. $(minhaVar).click();
  108. }
  109. }, intervalOfNexAttack, this);
  110. ++count;
  111. });
  112. }
  113. }
  114. }
  115.  
  116. function checkWall(maxWallLv) {
  117. return parseInt($('#plunder_list tr:not(:first)').eq(i + 1).find('td').eq(6).text()) < maxWallLv || isNaN($('#plunder_list tr:not(:first)').eq(i + 1).find('td').eq(6).text()) == true;
  118. }
  119.  
  120. function showAllBarbs() {
  121. if (game_data.screen == 'am_farm') {
  122. function modify_table(data) {
  123. var result = $('<div>').html(data).contents();
  124. var rows = result.find('#plunder_list tr:not(:first-child)');
  125. $('#plunder_list').append(rows);
  126. }
  127.  
  128. $('.paged-nav-item:not(:first-child)').each(function () {
  129. $.ajax({
  130. url: $(this).attr('href'), type: "get", async: false, success: function (data) {
  131. modify_table(data);
  132. }, error: function () {
  133. UI.ErrorMessage('An error occurred while downloading data. Refresh the page to try again', 5000);
  134. throw new Error('interrupted script');
  135. }
  136. });
  137. $(this).remove();
  138. });
  139. window.scrollTo(0, 0);
  140. } else {
  141. UI.InfoMessage('The script should be used from the farm assistant view.', 2000, 'error');
  142. }
  143. lootAssistantPageSize = document.querySelectorAll("#plunder_list a.farm_icon_a").length;
  144. console.log("lootAssistantPageSize: ", lootAssistantPageSize);
  145. }