CowboySoberano

FARMBOT AE

Jun 12th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Farmbotty_ae
  3. // @namespace   Tribalwars
  4. // @description Farm from the Farm-Assistant
  5. // @version     2.3
  6. // @author      Bassem
  7. // @copyright   (C) 2014 Bassem
  8. // @grant       GM_log
  9. // @grant       GM_getValue
  10. // @grant       GM_setValue
  11. // @grant       GM_deleteValue
  12. // @include     http://ae*.tribalwars.ae/game.php?*screen=am_farm*
  13. // ==/UserScript==
  14.  
  15. (function() {'use strict';
  16.     if (!(/http:\/\/(\w{2,3}\d+|hp|www)\./).test(location.href)) {
  17.         return;
  18.     }
  19.  
  20.     document.addEventListener('DOMContentLoaded', ready, false);
  21. })();
  22.  
  23. function ready() {'use strict';
  24.  
  25.     /* Variables to modify */
  26.  
  27.     /*========= Wall Level =============*/
  28.     var wall = 15;
  29.     /*== the number of the page the script will stop at ====*/
  30.     var page = 3;
  31.     /*===== activate\deactivate resources function =====*/
  32.     /* false or true */
  33.     var initResFunc = true;
  34.     /*===== hide\show Low Resource Rows =====*/
  35.     /* false or true */
  36.     var hideLowResRows = true;
  37.     /*===== the minimum amount of resources =====*/
  38.     var minResValue = 1000;
  39.     /*===== Hide/show Attacks =====*/
  40.     /* false or true */
  41.     var hide_attacks = true;
  42.     /*===== skip/reattack already attacked villages =====*/
  43.     /* false or true */
  44.     var fa_skipAttackedVillages = true;
  45.     /*===== the maximum distance to send units =====*/
  46.     var distance = 42;
  47.  
  48.     var nullFunction = function() {
  49.     };
  50.     var doc = document;
  51.     var win = unsafeWindow || window;
  52.     var texts, game_data, screen;
  53.     var jQuery = win.jQuery;
  54.     var image_base = win.image_base;
  55.  
  56.     /*===== for catching the spys number =====*/
  57.     var spys, distanceFar, lc;
  58.     /*=============================================*/
  59.  
  60.     /* >>>>> Function for storage & get/set values to localstorage >>>>>>>*/
  61.  
  62.     var lib = (function($) {
  63.         // Hypix's storage-class; thanks for providing!
  64.         var prefix = 'dsfbot', debug, storage, getGameData, alert, success, error, fireEvent, server, lang, world, worldString, hasPA;
  65.  
  66.         debug = (function() {
  67.             function dumpPHP(obj, lnbr) {
  68.                 var lineEnd = lnbr ? '\n' : '', ret = typeof (obj) + ' ' + obj + ( lnbr ? '\n' : '');
  69.                 if ( typeof (obj) == 'number') {
  70.                     ret = 'Number(' + obj + ')' + lineEnd;
  71.                 } else if ( typeof (obj) == 'string') {
  72.                     ret = 'String(' + obj.length + ') "' + obj + '"' + lineEnd;
  73.                 } else if (is_array(obj)) {
  74.                     ret = 'array(' + obj.length + ') { ' + lineEnd;
  75.                 } else if ( typeof (obj) == 'object') {
  76.                     ret = 'object { ' + lineEnd;
  77.                 }
  78.                 if ( typeof (obj) == 'object') {
  79.                     for (var key in obj) {
  80.                         ret += '[' + ( typeof (key) == 'string' ? '"' : '') + key + ( typeof (key) == 'string' ? '"' : '') + '] => ' + dumpPHP(obj[key], lnbr) + ( lnbr ? '' : ' ');
  81.                     }
  82.                     ret += '}' + lineEnd;
  83.                 }
  84.                 return ret;
  85.             }
  86.  
  87.             function dumpStr(obj, tabs) {
  88.                 var str;
  89.                 if (!tabs) {
  90.                     tabs = '';
  91.                     str = '\nDumpObj:\n{';
  92.                 } else {
  93.                     str = '\n' + tabs + '{';
  94.                 }
  95.                 var ntabs = tabs + '\t';
  96.                 for (var key in obj) {
  97.                     if ( typeof (obj[key]) == 'object') {
  98.                         str += '\n' + ntabs + key + ':';
  99.                         str += dumpStr(obj[key], ntabs);
  100.                     } else if ( typeof (obj[key]) != 'function') {
  101.                         if ( typeof (obj[key]) == 'string') {
  102.                             str += '\n' + ntabs + key + ': "' + obj[key] + '"';
  103.                         } else {
  104.                             str += '\n' + ntabs + key + ': ' + obj[key];
  105.                         }
  106.                     }
  107.                 }
  108.                 str += '\n' + tabs + '}';
  109.                 return str;
  110.             }
  111.  
  112.             // public methods
  113.             function log() {
  114.                 /* jshint boss:true */
  115.                 for (var i = 0, msg; ( msg = arguments[i]) !== undefined; i++) {
  116.                     if ( typeof (console) != 'undefined') {
  117.                         console.log(msg);
  118.                     } else if ( typeof (GM_log) != 'undefined')
  119.                     /* jshint newcap:false */
  120.                     {
  121.                         GM_log(msg);
  122.                     } else if ( typeof (opera) != 'undefined') {
  123.                         opera.postError(msg);
  124.                     }
  125.                 }
  126.             }
  127.  
  128.             function dumpObj(obj) {
  129.                 var str = dumpStr(obj);
  130.                 log(str);
  131.                 return str;
  132.             }
  133.  
  134.             function dumpVar(obj, lnbr) {
  135.                 log(dumpPHP(obj, lnbr));
  136.             }
  137.  
  138.             return {
  139.                 log : log,
  140.                 dumpObj : dumpObj,
  141.                 dumpVar : dumpVar
  142.             };
  143.         })();
  144.         storage = (function(prefix) {
  145.             var gm = typeof (unsafeWindow) != 'undefined' && navigator.userAgent.indexOf('Firefox') > -1, ls = false, intGetValue, intSetValue, intDeleteValue;
  146.             try {
  147.                 ls = typeof (win.localStorage) != 'undefined';
  148.             } catch(e) {
  149.                 debug.log(e.message);
  150.             }
  151.             if (gm) {
  152.                 prefix = prefix + '_' + document.location.host.split('.')[0];
  153.                 intSetValue = function(key, value) {
  154.                     /* jshint newcap:false */
  155.                     GM_setValue(prefix + '_' + key, value);
  156.                 };
  157.                 intGetValue = function(key, defaultValue) {
  158.                     /* jshint newcap:false */
  159.                     return GM_getValue(prefix + '_' + key, defaultValue);
  160.                 };
  161.                 intDeleteValue = function(key) {
  162.                     /* jshint newcap:false */
  163.                     GM_deleteValue(prefix + '_' + key);
  164.                 };
  165.             } else if (ls) {
  166.                 intSetValue = function(key, value) {
  167.                     localStorage.setItem(prefix + '_' + key, value);
  168.                 };
  169.                 intGetValue = function(key, defaultValue) {
  170.                     var value = localStorage.getItem(prefix + '_' + key);
  171.                     if (value) {
  172.                         return value;
  173.                     } else {
  174.                         return defaultValue;
  175.                     }
  176.                 };
  177.                 intDeleteValue = function(key) {
  178.                     localStorage.removeItem(prefix + '_' + key);
  179.                 };
  180.             } else {
  181.                 throw new Error('No suitable storage capability found!');
  182.             }
  183.  
  184.             function setValue(key, value) {
  185.                 switch (typeof(value)) {
  186.                     case 'object':
  187.                     case 'function':
  188.                         intSetValue(key, 'j' + JSON.stringify(value));
  189.                         break;
  190.                     case 'number':
  191.                         intSetValue(key, 'n' + value);
  192.                         break;
  193.                     case 'boolean':
  194.                         intSetValue(key, 'b' + ( value ? 1 : 0));
  195.                         break;
  196.                     case 'string':
  197.                         intSetValue(key, 's' + value);
  198.                         break;
  199.                     case 'undefined':
  200.                         intSetValue(key, 'u');
  201.                         break;
  202.                 }
  203.             }
  204.  
  205.             function getValue(key, defaultValue) {
  206.                 var str = intGetValue(key);
  207.                 if ( typeof (str) != 'undefined') {
  208.                     switch (str[0]) {
  209.                         case 'j':
  210.                             try {
  211.                                 return JSON.parse(str.substring(1));
  212.                             } catch(e) {
  213.                                 alert(key + ': ' + texts.gui.valueError);
  214.                             }
  215.                             return defaultValue;
  216.                         case 'n':
  217.                             return parseFloat(str.substring(1));
  218.                         case 'b':
  219.                             return str[1] == '1';
  220.                         case 's':
  221.                             return str.substring(1);
  222.                         default:
  223.                             intDeleteValue(key);
  224.                     }
  225.                 }
  226.                 return defaultValue;
  227.             }
  228.  
  229.             function deleteValue(key) {
  230.                 intDeleteValue(key);
  231.             }
  232.  
  233.             function getString(key) {
  234.                 return intGetValue(key);
  235.             }
  236.  
  237.             function setString(key, value) {
  238.                 intSetValue(key, value);
  239.             }
  240.  
  241.             return {
  242.                 setValue : setValue,
  243.                 getValue : getValue,
  244.                 deleteValue : deleteValue,
  245.                 getString : getString,
  246.                 setString : setString
  247.             };
  248.         })(prefix);
  249.  
  250.         function getGameData() {
  251.             var game_data = win.game_data || null;
  252.             if (!game_data && document && document.body) {
  253.                 var script = document.body.appendChild(document.createElement('script'));
  254.                 script.type = 'application/javascript';
  255.                 script.textContent = 'var input=document.body.appendChild(document.createElement("input"));' + 'input.type="hidden";' + 'input.value=JSON.stringify(game_data);' + 'input.id="game_data";';
  256.                 var input = document.getElementById('game_data');
  257.                 if (input) {
  258.                     eval('game_data=' + input.value + ';');
  259.                 }
  260.                 document.body.removeChild(script);
  261.             }
  262.             if (!game_data) {
  263.                 throw new Error('No GameData found!');
  264.             }
  265.             return game_data;
  266.         }
  267.  
  268.         function fireEvent(node, evt) {
  269.             if (node.nodeName.toUpperCase() == 'INPUT' && node.type.toUpperCase() == 'CHECKBOX') {
  270.                 node.checked = !node.checked;
  271.             }
  272.             var evObj = document.createEvent('HTMLEvents');
  273.             evObj.initEvent(evt, true, true);
  274.             var ok = node.dispatchEvent(evObj);
  275.             if (ok && node.nodeName == 'A' && node.href) {
  276.                 location.href = node.href;
  277.             }
  278.         }
  279.  
  280.         function alert(message, fadeOutTime, mclass) {
  281.             var script = document.body.appendChild(document.createElement('script'));
  282.             script.type = 'text/javascript';
  283.             if (arguments.length < 2) {
  284.                 script.innerHTML = 'jQuery(document).ready(function(){ UI.InfoMessage("' + message + '"); });';
  285.             } else if (arguments.length > 2) {
  286.                 script.innerHTML = 'jQuery(document).ready(function(){ UI.InfoMessage("' + message + '",' + fadeOutTime + ',"' + mclass + '"); });';
  287.             }
  288.         }
  289.  
  290.         function success(message, fadeOutTime, mclass) {
  291.             fadeOutTime = fadeOutTime || 2000;
  292.             mclass = mclass || '';
  293.             alert(message, fadeOutTime, mclass + ' success');
  294.         }
  295.  
  296.         function error(message, fadeOutTime, mclass) {
  297.             fadeOutTime = fadeOutTime || 2000;
  298.             mclass = mclass || '';
  299.             alert(message, fadeOutTime, mclass + ' error');
  300.         }
  301.  
  302.         game_data = getGameData();
  303.         hasPA = game_data.player.premium;
  304.  
  305.         return {
  306.             debug : debug,
  307.             storage : storage,
  308.             alert : alert,
  309.             success : success,
  310.             error : error,
  311.             fireEvent : fireEvent,
  312.             hasPA : hasPA
  313.         };
  314.     })(jQuery), state = lib.storage.getValue('z', 0), startVillage = lib.storage.getValue('s', 0), mOver = false;
  315.  
  316.     /* >>>>> End >>>>>>>*/
  317.  
  318.     /*
  319.      * >>>>>>>>>>>>>>>>>>> start of
  320.      *--------------
  321.      * DOM functions
  322.      * --------------
  323.      */
  324.     function __(id) {
  325.         return doc.getElementById(id);
  326.     }
  327.  
  328.     function _T(context, tagName, num) {
  329.         var sel = [];
  330.         if (context)
  331.             sel = context.getElementsByTagName(tagName);
  332.         return _select(sel, num);
  333.     }
  334.  
  335.     function _C(context, className, num) {
  336.         var sel = [];
  337.         if (context)
  338.             sel = context.getElementsByClassName(className);
  339.         return _select(sel, num);
  340.     }
  341.  
  342.     function _TC(context, tagName, className, num) {
  343.         var sel = [];
  344.         if (context)
  345.             sel = context.getElementsByTagName(tagName);
  346.         for (var i = 0, el; el = sel[i]; i++)
  347.             if (!el.classList.contains(className))
  348.                 sel = _splice(sel, i--, 1);
  349.         return _select(sel, num);
  350.     }
  351.  
  352.     function _select(sel, num) {
  353.         if ( typeof (num) == 'number') {
  354.             if (sel.length <= num)
  355.                 return null;
  356.             sel = sel[num];
  357.         } else if (num == 'last') {
  358.             if (!sel.length)
  359.                 return null;
  360.             sel = sel[sel.length - 1];
  361.         }
  362.         return sel;
  363.     }
  364.  
  365.     function _splice(els, start, num) {
  366.         if (!els)
  367.             return [];
  368.         var left = [], right = [];
  369.         if (start > 0)
  370.             left = _slice(els, 0, start - 1);
  371.         if (num < els.length - start)
  372.             right = _slice(els, start + num);
  373.         return left.concat(right);
  374.     }
  375.  
  376.     function _next(el) {
  377.         if (!el)
  378.             return null;
  379.         return el.nextElementSibling;
  380.     }
  381.  
  382.     function _slice(els, start, end) {
  383.         if (!els)
  384.             return [];
  385.         if ( typeof (end) == 'number')
  386.             return Array.prototype.slice.call(els, start, end);
  387.         return Array.prototype.slice.call(els, start);
  388.     }
  389.  
  390.     /*
  391.      * Modifier
  392.      */
  393.     function _ce(element, html, options) {
  394.         if ( typeof (html) == 'object') {
  395.             options = html;
  396.             html = null;
  397.         }
  398.         var el = doc.createElement(element);
  399.         // html is not undefined and if it is a string, not empty:
  400.         if (html != undefined && ( typeof (html) != 'string' || html))
  401.             el.innerHTML = html;
  402.         if ( typeof (options) == 'object') {
  403.             for (var key in options)
  404.             el.setAttribute(key, options[key]);
  405.         }
  406.         return el;
  407.     }
  408.  
  409.     // return: newEl
  410.     function _append(newEl, parent) {
  411.         if (!newEl || !parent)
  412.             return null;
  413.         return parent.appendChild(newEl);
  414.     }
  415.  
  416.     // return: newEl
  417.     function _before(newEl, beforeEl) {
  418.         if (!newEl || !beforeEl.parentNode)
  419.             return null;
  420.         return beforeEl.parentNode.insertBefore(newEl, beforeEl);
  421.     }
  422.  
  423.     // return: newEl
  424.     function _after(newEl, afterEl) {
  425.         var nextEl = _next(afterEl);
  426.         if (!nextEl)
  427.             return _append(newEl, afterEl.parentNode);
  428.         return _before(newEl, nextEl);
  429.     }
  430.  
  431.     /*
  432.      * Sonstiges
  433.      */
  434.     function _show(el) {
  435.         if (!el)
  436.             return null;
  437.         if (el.oldDisplay)
  438.             el.style.display = el.oldDisplay;
  439.         else if (el.tagName == 'DIV' || el.tagName == 'IMG')
  440.             el.style.display = 'block';
  441.         else
  442.             el.style.display = '';
  443.         return el;
  444.     }
  445.  
  446.     function _hide(el) {
  447.         if (!el)
  448.             return null;
  449.         if (el.style.display == 'none')
  450.             return el;
  451.         el.oldDisplay = el.style.display;
  452.         el.style.display = 'none';
  453.         return el;
  454.     }
  455.  
  456.     /*
  457.      * >>>>>>>>>>>>>>>>>>> end of
  458.      *--------------
  459.      * DOM functions
  460.      * --------------
  461.      */
  462.  
  463.     /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  464.  
  465.     /*
  466.      * >>>>>>>>>>>>>>>>>>> start of
  467.      *--------------
  468.      * Functions
  469.      * --------------
  470.      */
  471.  
  472.     var regex = {
  473.         // isNaN returns true for floating numbers... this one not:
  474.         NaN : /[^\d]/,
  475.         viewID : /view=(\d+)/,
  476.         ID : /[&\?]id=(\d+)/,
  477.         targetID : /target=(\d+)/,
  478.         captcha : /\/human\.php/,
  479.         villageChangeReplacer : /^.+\/game\.php\?(.*)([\?&]?)village=\d+/,
  480.         coords : /\((\d{1,3}\|\d{1,3})\)/,
  481.         unitKey : /unit\/unit_(.+)\.png/,
  482.         attackImgPath : /command\/attack(_small|_medium|_large)?\.png/,
  483.         attackImg : /<img[^>]*src="[^"]+command\/attack(_small|_medium|_large)?\.png/,
  484.         red_blue : /graphic\/dots\/red_blue.png?/,
  485.         red_yellow : /graphic\/dots\/red_yellow.png?/
  486.     };
  487.  
  488.     var Style = (function() {
  489.  
  490.         function add(name) {
  491.             var newStyle = name && __('dsplusStyle_' + name);
  492.             if (!newStyle) {
  493.                 newStyle = document.createElement('style');
  494.                 newStyle.type = 'text/css';
  495.                 if (name)
  496.                     newStyle.id = 'dsplusStyle_' + name;
  497.                 document.head.appendChild(newStyle);
  498.             }
  499.             function setInnerHTML(css) {
  500.                 newStyle.innerHTML = css;
  501.                 return newStyle;
  502.             }
  503.  
  504.             return {
  505.                 element : newStyle,
  506.                 set : setInnerHTML
  507.             };
  508.         }
  509.  
  510.         function am_farm() {
  511.             add('am_farm').set('tr.attack > td { background-color: #f0727e !important; }');
  512.         }
  513.  
  514.         function filter() {
  515.             add('filter').set('#dsplus_filterLink { vertical-align: middle; }');
  516.         }
  517.  
  518.         return {
  519.             add : add,
  520.             am_farm : am_farm,
  521.             filter : filter
  522.         };
  523.     })();
  524.  
  525.     var PA_Features = {
  526.         fa_initGUI : function() {
  527.             var am_widget_Farm = __('am_widget_Farm');
  528.             var amFarmTable = _T(am_widget_Farm, 'table', 0);
  529.             if (initResFunc) {
  530.                 if (!__('stone_header')) {
  531.                     var resourceHeader = _T(amFarmTable, 'th', 5);
  532.                     resourceHeader.id = 'wood_header';
  533.                     resourceHeader.style.textAlign = 'center';
  534.                     var resourceImg = _T(resourceHeader, 'img', 0);
  535.                     resourceImg.src = image_base + 'holz.png';
  536.  
  537.                     var stoneHeader = _ce('th', '', {
  538.                         'id' : 'stone_header',
  539.                         'style' : 'text-align: center;'
  540.                     });
  541.                     var stoneImg = resourceImg.cloneNode(true);
  542.                     stoneImg.src = image_base + 'lehm.png';
  543.                     stoneHeader.appendChild(stoneImg);
  544.                     _after(stoneHeader, resourceHeader);
  545.  
  546.                     var ironHeader = _ce('th', '', {
  547.                         'id' : 'iron_header',
  548.                         'style' : 'text-align: center;'
  549.                     });
  550.                     var ironImg = resourceImg.cloneNode(true);
  551.                     ironImg.src = image_base + 'eisen.png';
  552.                     ironHeader.appendChild(ironImg);
  553.                     _after(ironHeader, stoneHeader);
  554.                     var rows = amFarmTable.rows;
  555.                     for (var i = 0, len = rows.length; i < len; i++) {
  556.                         var row = rows[i];
  557.                         var numTds = _T(row, 'td').length;
  558.                         if (numTds > 1) {
  559.                             var resTypes = ['wood', 'stone', 'iron'];
  560.                             var resTypeIdx = 0;
  561.                             var res = {};
  562.                             var resClass = {};
  563.                             var resAmount = {};
  564.                             /*get the wood cells*/
  565.                             var resourceCell = row.cells[5];
  566.                             //console.log(resourceCell);
  567.  
  568.                             var spans = _T(row.cells[5], 'span');
  569.                             //console.log(spans);
  570.                             //console.log(resTypeIdx);
  571.                             for (var j = 0, span; ( span = spans[j]) && resTypeIdx < 3; j++) {
  572.                                 //console.log(resTypeIdx);
  573.                                 if (span.classList.contains('res') || span.classList.contains('warn') || span.classList.contains('warn_90')) {
  574.  
  575.                                     var resType = resTypes[resTypeIdx++];
  576.                                     //console.log(resType);
  577.                                     /*the amount number of resources*/
  578.                                     var html = span.innerHTML;
  579.                                     //console.log(html);
  580.                                     res[resType] = html;
  581.                                     //console.log(res[resType]);
  582.                                     resClass[resType] = span.className;
  583.                                     //console.log(resClass[resType]);
  584.                                     var value = parseInt(span.textContent.replace(/\./g, ''), 10);
  585.                                     //console.log(value);
  586.                                     resAmount[resType] = value;
  587.                                 }
  588.                             }
  589.  
  590.                             resourceCell.innerHTML = res.wood || '?';
  591.                             //console.log(resourceCell.innerHTML);
  592.                             resourceCell.className = resClass.wood || 'res';
  593.                             resourceCell.style.textAlign = 'right';
  594.  
  595.                             var stoneCell = _ce('td', (res.stone || '?'), {
  596.                                 'class' : resClass.stone || 'res',
  597.                                 'style' : 'text-align: right;'
  598.                             });
  599.                             _after(stoneCell, resourceCell);
  600.  
  601.                             var ironCell = _ce('td', (res.iron || '?'), {
  602.                                 'class' : resClass.iron || 'res',
  603.                                 'style' : 'text-align: right;'
  604.                             });
  605.                             _after(ironCell, stoneCell);
  606.  
  607.                             if (hideLowResRows) {
  608.                                 var resSum = resAmount.wood + resAmount.stone + resAmount.iron;
  609.                                 if (resSum < minResValue)
  610.                                     _hide(row);
  611.                             }
  612.                         } else if (numTds > 0) {
  613.                             row.cells[0].colSpan += 2;
  614.                         }
  615.                     }
  616.                 }
  617.             }
  618.             PA_Features.fa_hideAttacks();
  619.             Style.am_farm();
  620.             PA_Features.fa_colorAttackedRows();
  621.             PA_Features.fa_addControl();
  622.         },
  623.         fa_addControl : function() {
  624.             var str = '&lt;&#8225; Start Farmbotty &#8225;&gt;', color = 'green';
  625.             if (state) {
  626.                 str = '&lt;&#8225; Stop Farmbotty &#8225;&gt;';
  627.                 color = 'red';
  628.             }
  629.             jQuery("div#am_widget_Farm").find('h4').eq(0).append('<a id="farmbotty_start" href="javascript:;" style="float: right; color: ' + color + ';">' + str + '</a>');
  630.             jQuery('#farmbotty_start').get(0).addEventListener('click', function() {
  631.                 state = state ? 0 : 1;
  632.                 saveState();
  633.  
  634.                 if (state) {
  635.                     setStartVill();
  636.                     /*
  637.                      if (sortByDistance()) {
  638.                      return;
  639.                      }
  640.                      */
  641.                 }
  642.                 location.reload();
  643.             }, false);
  644.             jQuery('#farmbotty_start').get(0).addEventListener('mouseover', function() {
  645.                 mOver = true;
  646.             }, false);
  647.             jQuery('#farmbotty_start').get(0).addEventListener('mouseout', function() {
  648.                 mOver = false;
  649.             }, false);
  650.         },
  651.         fa_colorAttackedRows : function() {
  652.             var widget = __('am_widget_Farm');
  653.             var rows = _T(widget, 'tr');
  654.             //  var shownRows = 0;
  655.             for (var i = 0, row; row = rows[i]; i++) {
  656.                 var classList = row.classList;
  657.                 if (classList.contains('row_a') || classList.contains('row_b')) {
  658.                     if (regex.attackImgPath.test(row.cells[3].innerHTML)) {
  659.                         classList.add('attack');
  660.                     }
  661.                 }
  662.             }
  663.         },
  664.         fa_hideAttacks : function() {
  665.             var $rows = jQuery('tr.row_a, tr.row_b');
  666.             $rows.each(function() {
  667.                 if (hide_attacks && this.cells.length > 3 && regex.attackImg.test(this.cells[3].innerHTML)) {
  668.                     _hide(this);
  669.                 } else if (!hide_attacks && !this.classList.contains('inactive') && regex.attackImg.test(this.cells[3].innerHTML)) {
  670.                     _show(this);
  671.                 }
  672.             });
  673.         },
  674.         fa_clickButton : function() {
  675.             if (jQuery('.autoHideBox.error').length) {
  676.                 nextVillage();
  677.                 return;
  678.             }
  679.  
  680.             var clicked = false;
  681.             jQuery('div#am_widget_Farm a.farm_icon_c:visible').each(function() {
  682.                 spys = parseInt(jQuery('#units_home').find('td[id="spy"]').text().match(/\d+/)[0], 10);
  683.                 lc = parseInt(jQuery('#units_home').find('td[id="light"]').text().match(/\d+/)[0], 10);
  684.  
  685.                 var wallHTML = jQuery(this).parents('td').eq(0).prev('td').prev('td').prev('td').prev('td').text();
  686.                 var wallTest = parseInt(wallHTML, 10);
  687.                 var distanceFar = parseInt(jQuery(this).parents('td').eq(0).prev('td').prev('td').prev('td').text(), 10);
  688.                 var reportDotCell = jQuery(this).parents('tr').eq(0).find('td').eq(0).next('td').html();
  689.  
  690.                 if (clicked || spys == 0 || wallTest > wall || wallHTML == '?' || distanceFar > distance || jQuery(this).hasClass('farm_icon_disabled') || regex.red_blue.test(reportDotCell) || regex.red_yellow.test(reportDotCell)) {
  691.                     return;
  692.                 }
  693.                 var attackImg = (regex.attackImg).test(jQuery(this).parents('tr').eq(0).find('td').eq(3).html());
  694.                 var runningAttack = attackImg && regex.attackImgPath.test(attackImg.src);
  695.                 if (runningAttack && fa_skipAttackedVillages) {
  696.                     return;
  697.                 }
  698.                 this.click();
  699.                 jQuery(this).addClass('farm_icon_disabled');
  700.                 clicked = true;
  701.                 //console.log('Remaning spys in clickButtonC() =' + spys);
  702.             });
  703.             if (clicked) {
  704.                 var max = 800;
  705.                 if (!Math.floor(Math.random() * 50)) {
  706.                     max += Math.floor(Math.random() * 1000);
  707.                 }
  708.                 setRandom(PA_Features.fa_clickButton, 400, max);
  709.             } else {
  710.                 changePage();
  711.             }
  712.         }
  713.     };
  714.  
  715.     // at Captcha:
  716.     if (jQuery('#bot_check').length || jQuery('#bot_check_error').length || jQuery('#bot_check_image').length || jQuery('#bot_check_form').length || jQuery('#bot_check_code').length || jQuery('#bot_check_submit').length || (/<[Hh]2>\s*Botschutz\s*<\/[Hh]2>/).test(jQuery('body').html()) || (/\/game\.php\?captcha/).test(jQuery('body').html()) || (/&test=sound/).test(location.href)) {
  717.  
  718.         var sound = function() {
  719.             var vol = 50, audio = new Audio();
  720.             audio.src = 'http://fileface.de/sound/voy-neelix-donotdisturb.ogg';
  721.             audio.volume = vol / 100;
  722.             audio.play();
  723.         };
  724.         window.setInterval(sound, 120000);
  725.         sound();
  726.         return;
  727.     }
  728.  
  729.     var $tBody = jQuery('#all_village_checkbox').parents('tbody').eq(0);
  730.     function saveState() {
  731.         lib.storage.setValue('z', state);
  732.     }
  733.  
  734.     function setStartVill() {
  735.         startVillage = game_data.village.id;
  736.         lib.storage.setValue('s', startVillage);
  737.     }
  738.  
  739.     function setRandom(func, min, max) {
  740.         window.setTimeout(function() {
  741.             function f() {
  742.                 if (!mOver) {
  743.                     window.clearInterval(fInt);
  744.                     func.apply(null);
  745.                 }
  746.             }
  747.  
  748.             if (mOver) {
  749.                 var fInt = window.setInterval(f, 100);
  750.                 jQuery('body').prepend('<div style="width: ' + jQuery(window).width() + 'px; height: ' + jQuery(window).height() + 'px; position: fixed; top: 0px; left: 0px; z-index: -1; background-color: #002; opacity: 0.5;"></div>');
  751.             } else {
  752.                 f();
  753.             }
  754.         }, Math.floor(min + Math.random() * (max - min)));
  755.  
  756.     }
  757.  
  758.     function sortByDistance() {
  759.         var $link, i = -3, $ths = $tBody.find('th');
  760.         while (i > -5) {
  761.             $link = $ths.eq(i).find('a');
  762.             if ($link.length) {
  763.                 if ($link.attr('href').indexOf('dir=desc') > -1) {
  764.                     state = -1;
  765.                     saveState();
  766.                 }
  767.                 $link.get(0).click();
  768.                 return true;
  769.             }
  770.             i--;
  771.         }
  772.         return false;
  773.     }
  774.  
  775.     function nextVillage() {
  776.         state = 4;
  777.         saveState();
  778.  
  779.         var $link = jQuery('#village_switch_right');
  780.         setRandom(function() {
  781.             $link.get(0).click();
  782.         }, 500, 1000);
  783.     }
  784.  
  785.     function changePage() {
  786.         var $nextLink = $tBody.children().last().find('strong').next('a');
  787.         var $allLink = $tBody.children().last().find('a');
  788.         var stopChangePage = parseInt($tBody.children().last().find('strong').text().match(/\d+/)[0], 10);
  789.         if ($nextLink.length && stopChangePage < page && spys > 0 && distanceFar < distance && lc !== 0) {
  790.             //console.log('Remaning spys in changePage() =' + spys);
  791.             //console.log('Remaning light in changePage() =' + lc);
  792.             setRandom(function() {
  793.                 $nextLink.get(0).click();
  794.             }, 500, 1000);
  795.         } else {
  796.             nextVillage();
  797.         }
  798.     }
  799.  
  800.     function Go_to_the_first_page() {
  801.         var $nextLink = $tBody.children().last().find('strong').prevAll('a');
  802.         if ($nextLink.length) {
  803.             setRandom(function() {
  804.                 $nextLink.last().get(0).click();
  805.             }, 300, 800);
  806.         } else {
  807.             run();
  808.         }
  809.     }
  810.  
  811.     /*
  812.     * >>>>>>>>>>>>>>>>>>> end of
  813.     *--------------
  814.     * Functions
  815.     * --------------
  816.     */
  817.  
  818.     /*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&*/
  819.     /* ===== Excute the Fiunctions =====*/
  820.     /* Checking whether everything is correct */
  821.     //*
  822.     if (game_data.screen != 'am_farm') {
  823.         return;
  824.     }
  825.     if (!lib.hasPA || !game_data.player.farm_manager) {
  826.         lib.error('PA and Farm-Assistent is needed!');
  827.         return;
  828.     }
  829.  
  830.     PA_Features.fa_initGUI();
  831.  
  832.     /* >>>>>>>>>>>> main function >>>>>>>>>>>>>>> */
  833.  
  834.     function run() {
  835.         /*
  836.          if (state == -1) {
  837.          // Sort by distance
  838.          state = 1;
  839.          saveState();
  840.          setRandom(function() {
  841.          sortByDistance();
  842.          }, 300, 700);
  843.          } else
  844.          */
  845.         if (state == 1) {
  846.             // Go to the first page: the Start Village
  847.             state = 2;
  848.             saveState();
  849.  
  850.             Go_to_the_first_page();
  851.         } else if (state == 2) {
  852.             // the button click C: in the start village
  853.             PA_Features.fa_clickButton();
  854.         } else if (state == 3) {
  855.             // Click the button C, if the start village is not reached yet
  856.             if (game_data.village.id == startVillage) {
  857.                 state = 5;
  858.                 saveState();
  859.                 run();
  860.             } else {
  861.                 PA_Features.fa_clickButton();
  862.             }
  863.         } else if (state == 4) {
  864.             // Village change: go to the first page
  865.             state = 3;
  866.             saveState();
  867.  
  868.             Go_to_the_first_page();
  869.         } else if (state == 5) {
  870.             // once: wait and then start all over again
  871.             state = 1;
  872.             saveState();
  873.             setRandom(function() {
  874.                 location.reload();
  875.             }, 10 * 60000, 20 * 60000);
  876.         }
  877.     }
  878.  
  879.     //*
  880.     if (state) {
  881.         setRandom(function() {
  882.             run();
  883.         }, 10000, 20000);
  884.     }
  885.     //*/
  886.  
  887. }
Add Comment
Please, Sign In to add comment