Travian wave builder

make waves for travian4 and travian 3.6 (classic)

  1. // ==UserScript==
  2. // @name Travian wave builder
  3. // @namespace http://userscripts.org/scripts/show/132636
  4. // @description make waves for travian4 and travian 3.6 (classic)
  5. // @author Serj_LV
  6. // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  7.  
  8. // @include http://*.travian.*/build.php*
  9. // @include http://*/*.travian.*/build.php*
  10. // @include http://*.travian.*/a2b.php*
  11. // @include http://*/*.travian.*/a2b.php*
  12. // @include http://*.travianteam.*/build.php*
  13. // @include http://www.vfthis.net/*/build.php*
  14. // @include http://www.vfthis.net/*/a2b.php*
  15. // @grant GM_addStyle
  16. // @grant GM_getValue
  17. // @grant GM_setValue
  18. // @grant GM_deleteValue
  19.  
  20. // @version 0.5
  21. // ==/UserScript==
  22.  
  23. (function () {
  24.  
  25. function allInOneOpera () {
  26. var version = '0.5';
  27. var scriptURL = 'http://userscripts.org/scripts/show/132636';
  28. notRunYet = false;
  29. var defInterval = 200;
  30. full_Imitation = false;
  31.  
  32. /*********************** common library ****************************/
  33. function ajaxRequest(url, aMethod, param, onSuccess, onFailure) {
  34. var aR = new XMLHttpRequest();
  35. aR.onreadystatechange = function() {
  36. if( aR.readyState == 4 && (aR.status == 200 || aR.status == 304))
  37. onSuccess(aR);
  38. else if (aR.readyState == 4 && aR.status != 200) onFailure(aR);
  39. };
  40. aR.open(aMethod, url, true);
  41. if (aMethod == 'POST') aR.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
  42. aR.send(param);
  43. };
  44. function httpPost(url,data) {
  45. var xhttp = new XMLHttpRequest();
  46. data = encodeURI(data);
  47. xhttp.open("POST", url, false);
  48. xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
  49. xhttp.send(data);
  50. return xhttp.responseText;
  51. }
  52. Number.prototype.NaN0 = function(){return isNaN(this)?0:this;};
  53. String.prototype.trim = function(){return this.replace(/ /g,'').replace(/^\s+|\s+$/g,'');};
  54. String.prototype.onlyText = function(){return this.replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/<[\s\S]+?>/g,'');};
  55. function $g(aID) {return (aID != '' ? document.getElementById(aID) : null);};
  56. function $gn(aID) {return (aID != '' ? document.getElementsByName(aID) : null);};
  57. function $gt(str,m) { return (typeof m == 'undefined' ? document:m).getElementsByTagName(str); };
  58. function $gc(str,m) { return (typeof m == 'undefined' ? document:m).getElementsByClassName(str); };
  59. function $at(aElem, att) {if (att !== undefined) {for (var xi = 0; xi < att.length; xi++) {aElem.setAttribute(att[xi][0], att[xi][1]); if (att[xi][0].toUpperCase() == 'TITLE') aElem.setAttribute('alt', att[xi][1]);};};};
  60. function $t(iHTML) {return document.createTextNode(iHTML);};
  61. function $e(nElem, att) {var Elem = document.createElement(nElem); $at(Elem, att); return Elem;};
  62. function $ee(nElem, oElem, att) {var Elem = $e(nElem, att); if (oElem !== undefined) if( typeof(oElem) == 'object' ) Elem.appendChild(oElem); else Elem.innerHTML = oElem; return Elem;};
  63. function $c(iHTML, att) { return $ee('TD',iHTML,att); }
  64. function $a(iHTML, att) { return $ee('A',iHTML,att); }
  65. function $am(Elem, mElem) { if (mElem !== undefined) for(var i = 0; i < mElem.length; i++) { if( typeof(mElem[i]) == 'object' ) Elem.appendChild(mElem[i]); else Elem.appendChild($t(mElem[i])); } return Elem;};
  66. function $em(nElem, mElem, att) {var Elem = $e(nElem, att); return $am(Elem, mElem);};
  67. function dummy() {return;};
  68. jsNone = 'return false;';
  69.  
  70. function trImg ( cl, et ) {
  71. var ecl = [['class', cl],['src', 'img/x.gif']];
  72. if( typeof et != 'undefined' ) ecl.push(['title',et]);
  73. return $e('IMG',ecl);
  74. }
  75.  
  76. function getRandom ( x ) {
  77. x = Math.round(x*0.8);
  78. return x+Math.round(Math.random()*x*0.5);
  79. }
  80.  
  81. /********** begin of main code block ************/
  82.  
  83. function ok () {
  84. tFormFL = true;
  85. plus.innerHTML = '+';
  86. }
  87.  
  88. function addWave () {
  89. if( tFormFL ) {
  90. tFormFL = false;
  91. plus.innerHTML = 'x';
  92. } else return;
  93.  
  94. var tInputs = $gt('INPUT',tForm);
  95. var needC = true;
  96. var sParams = '';
  97. var cDescr = '';
  98.  
  99. for( var i=0; i< tInputs.length; i++ ) {
  100. t = tInputs[i].name;
  101. if( /redeployHero/.test(t) ) {
  102. sParams += "redeployHero=&";
  103. } else if ( /^t\d/.test(t) || /x|y/.test(t) ) {
  104. sParams += t + "=" + $gn(t)[0].value + "&";
  105. } else if ( t == "c" ) {
  106. if ( needC ) {
  107. var iAttackType = $gn('c');
  108. for (var q = 0; q < iAttackType.length; q++)
  109. if( iAttackType[q].checked ) {
  110. sParams += "c=" + (q+2) + "&";
  111. cDescr = iAttackType[q].parentNode.innerHTML.onlyText().trim();
  112. }
  113. needC = false;
  114. }
  115. } else {
  116. sParams += t + "=" + tInputs[i].value + "&";
  117. }
  118. }
  119. sParams = sParams.substring(0, sParams.length - 1);
  120. var rpPage = $ee('div',httpPost(a2bURL,sParams),[['style','display:none;']]);
  121. var err = $gc('error',rpPage);
  122. if( err.length > 0 && err[0].innerHTML.length > 1 ) {
  123. ok();
  124. alert( err[0].innerHTML.onlyText() );
  125. return;
  126. }
  127. err = $gc('alert',rpPage);
  128. if( err.length > 0 && err[0].innerHTML.length > 1 ) {
  129. ok();
  130. if( ! confirm(err[0].innerHTML.onlyText()) ) return;
  131. }
  132. tInputs = $gt('INPUT',rpPage);
  133. sParams = '';
  134. var tc = new Array(12);
  135. for( i=0; i< tInputs.length; i++ ) {
  136. t = tInputs[i].name;
  137. if( /^t\d/.test(t) ) {
  138. tc[t.match(/\d+/)[0]] = tInputs[i].value;
  139. } if( t == "c" ) {
  140. needC = tInputs[i].value;
  141. }
  142. sParams += t + "=" + tInputs[i].value + "&";
  143. }
  144. sParams = sParams.substring(0, sParams.length - 1);
  145. var remBtn = $c($a('-',[['href','#'],['onClick',jsNone]]),[['title','remove wave'],['rowspan',2]]);
  146. remBtn.appendChild($e('INPUT',[['type','hidden'],['value',sParams]]));
  147. remBtn.addEventListener('click',remWave,false);
  148. var nrow = $ee('TR',remBtn);
  149. for( i=1; i< 12; i++ ) {
  150. nrow.appendChild($c(tc[i]));
  151. }
  152. nrow.appendChild($c(needC,[['title',cDescr]]));
  153. var nbody = $ee('TBODY',nrow);
  154. tInputs = $gt('SELECT',rpPage);
  155. var nrow = $e('TR');
  156. nrow.appendChild($c(tInputs.length>0 ? tInputs[0]: '-',[['colspan',6]]));
  157. nrow.appendChild($c(tInputs.length>0 ? tInputs[0]: '-',[['colspan',6]]));
  158. nbody.appendChild(nrow);
  159. tbl.appendChild(nbody);
  160. setTimeout(newForm, getRandom(1200));
  161. }
  162.  
  163. function newForm () {
  164. if( full_Imitation ) {
  165. ajaxRequest(a2bURL, "GET", null, function(ajaxResp) {
  166. var rpPage = $ee('div',ajaxResp.responseText,[['style','display:none;']]);
  167. rpPage = document.evaluate('.//form[@name="snd"]', rpPage, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  168. if( rpPage ) tForm = rpPage;
  169. ok();
  170. }, ok );
  171. } else ok();
  172. }
  173.  
  174. function remWave () {
  175. var tb = this.parentNode.parentNode;
  176. tb.parentNode.removeChild(tb);
  177. }
  178.  
  179. function sendTroops (x) {
  180. var wBody = tbl.tBodies[x];
  181. var sParams = $gt('INPUT',wBody)[0].value;
  182. var tInputs = $gt('SELECT',wBody);
  183. sParams += tInputs.length>0 ? "&"+ tInputs[0].name +"="+ tInputs[0].value: '';
  184. sParams += tInputs.length>1 ? "&"+ tInputs[1].name +"="+ tInputs[1].value: '';
  185.  
  186. wlog += x+', ';
  187. if( x== wCount-1 ) {
  188. wlog += 'OK';
  189. setTimeout(function(){ document.location.href = fullName +'build.php?'+ (ver4FL?'tt=1&':'') +'id=39'; }, getRandom(1500));
  190. }
  191. cLog.innerHTML = wlog;
  192. ajaxRequest(a2bURL, "POST", sParams, dummy, dummy );
  193. }
  194.  
  195. function sendWaves () {
  196. cLog = $c(wlog,[['colspan',13]]);
  197. tbl.tFoot.appendChild($ee('TR',cLog));
  198. wCount = tbl.tBodies.length;
  199. var nextWave = 10;
  200. var intWave = parseInt(interval.value).NaN0();
  201. if( intWave < 100 ) intWave = defInterval;
  202. for( var i=0; i<wCount; i++ ) {
  203. setTimeout(function(x){return function(){ sendTroops(x); }}(i), nextWave);
  204. nextWave += getRandom(intWave);
  205. }
  206. }
  207.  
  208. var ver4FL = true;
  209. if( /a2b.php/.test(window.location.href) ) {
  210. var build = $g('content');
  211. ver4FL = false;
  212. } else {
  213. var build = $g('build');
  214. if( !(build) ) return;
  215. if( build.getAttribute('class').indexOf('gid16') == -1 ) return;
  216. }
  217. var snd = $gn('snd');
  218. if( $gn('snd').length == 0 ) return;
  219.  
  220. var nation = Math.floor(parseInt($gc('unit')[0].getAttribute('class').match(/\d+/)[0])/10);
  221. if( nation < 0 ) return;
  222.  
  223. var a2bURL = ver4FL ? "build.php?tt=2&id=39": "a2b.php";
  224. var wCount = 0;
  225. var wlog = '';
  226. var cLog;
  227. var tForm = snd[0];
  228. var tFormFL = true;
  229. var fullName = window.location.href.match(/^.*\/\/.+\/+?/)[0];
  230. // build table header
  231. var tbl = $e('TABLE',[['style','border:1px solid silver;']]);
  232. var plus = $a('+',[['href','#'],['onClick',jsNone]]);
  233. var addBtn = $c(plus,[['title','append wave']]);
  234. addBtn.addEventListener('click',addWave,false);
  235. var hrow = $ee('TR',addBtn);
  236. for( var i=1; i<11; i++ ) {
  237. hrow.appendChild($c(trImg('unit u'+(nation*10+i))));
  238. }
  239. $am(hrow,[$c(trImg('unit uhero')),$c('c')]);
  240. tbl.appendChild($ee('THEAD',hrow));
  241. if( ver4FL ) {
  242. var sendBtn = $g('btn_ok').cloneNode(true);
  243. sendBtn.removeAttribute('name');
  244. sendBtn.removeAttribute('id');
  245. } else {
  246. i = $g('btn_ok').getAttribute('alt');
  247. var sendBtn = $ee('BUTTON',(i?i:'Go!'));
  248. }
  249. sendBtn.addEventListener('click',sendWaves,false);
  250. var interval = $e('INPUT',[['type','text'],['value',defInterval],['size',4],['maxlength',4]]);
  251. tbl.appendChild($ee('TFOOT',$ee('TR',$em('TD',['interval ',interval,' ms ',sendBtn,
  252. $a(' (v'+version+') ',[['href',scriptURL],['target','_blank']])],
  253. [['colspan',13],['style','text-align:center !important;']]))));
  254.  
  255. build.appendChild(tbl);
  256.  
  257. /********** end of main code block ************/
  258. }
  259.  
  260. function backupStart () {
  261. if(notRunYet) {
  262. var l4 = document.getElementById('l4');
  263. if( l4 ) allInOneOpera();
  264. else setTimeout(backupStart, 500);
  265. }
  266. }
  267.  
  268. var notRunYet = true;
  269. if( /khtml/i.test(navigator.appVersion) ) allInOneOpera();
  270. else if (window.addEventListener) window.addEventListener("load",function () { if(notRunYet) allInOneOpera(); },false);
  271. setTimeout(backupStart, 500);
  272.  
  273. })();