Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.12 KB | None | 0 0
  1. /*
  2. Author : Dale McKay
  3. Email : dalesmckay@gmail.com
  4. Notes : thanks to slowtarget for some initial ideas/functions
  5.  
  6. TODO :
  7. ____________________________________________________________
  8.  
  9. Copyright (C) 2010 Dale McKay, all rights reserved
  10. version 1.0, 9 April 2010
  11.  
  12. This software is provided 'as-is', without any express or implied warranty. In no event will the author be held liable for any damages arising from the use of this software.
  13.  
  14. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  15. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  16. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  17. This notice may not be removed or altered from any source distribution.
  18. ____________________________________________________________
  19.  
  20. */
  21.  
  22. function AttackCounter(){
  23. try{
  24. var _name='Attack Counter';
  25. var _version=7.03;
  26. var _min_game_version=7.00;
  27. var _author={
  28. name:'dalesmckay',
  29. email:'dalesmckay@gmail.com'
  30. };
  31. var _debugID = _name.replace(/\s/g,'');
  32. var _debugEnabled=true;
  33. var _win=(window.main||self),$=_win.$;
  34.  
  35.  
  36. // Check Permissions.
  37. if(_win.game_data.world=='zz2'){
  38. if([16467].indexOf(parseInt(_win.game_data.player.id,10))<0){
  39. alert('Hi '+_win.game_data.player.name+'!\n\nYour scripts have been disabled by dalesmckay\nSend him a mail if you wish to help with testing');
  40. return false;
  41. }
  42. }
  43.  
  44.  
  45. if($('#' + _debugID + '_done').length > 0){
  46. throw("This script has already been executed.\nPlease refresh the browser\nand try again.");
  47. }
  48.  
  49. var _gameVersion = _win.game_data.version.match(/[\d|\.]+/g);
  50. _gameVersion = (_gameVersion?parseFloat(_gameVersion[1]):-1);
  51.  
  52. /* HACK: fix null mode */
  53. if(!_win.game_data.mode){
  54. var vmode=$('#overview_menu td[class="selected"] a').attr('href');
  55. vmode=vmode?vmode.match(/mode\=(\w*)/i):null;
  56. _win.game_data.mode=vmode?vmode[1]:null;
  57. }
  58.  
  59. var _worldConfig = $(fnAjax('/interface.php','GET',{'func':'get_config'},'xml',false)).find('config');
  60. var _unitConfig = $(fnAjax('/interface.php','GET',{'func':'get_unit_info'},'xml',false)).find('config');
  61. /*
  62. var _buildingConfig = $(fnAjax('/interface.php','GET',{'func':'get_building_info'},'xml',false)).find('config');
  63. */
  64.  
  65. var _isPremium = ($('#quickbar_outer').length>0);
  66. var _hasChurch = (parseInt(_worldConfig.find('game church').text()||'0',10)>0);
  67. var _hasPaladin = (parseInt(_worldConfig.find('game knight').text()||'0',10)>0);
  68. var _hasArchers = (parseInt(_worldConfig.find('game archer').text()||'0',10)>0);
  69. var _hasMilitia = (_unitConfig.find('militia').length>0);
  70. var _hasVillageNotes = ($('[src*="note.png"],[class*="note-icon"]').length>0);
  71.  
  72. fnPrintVersion();
  73.  
  74. if(!fnHasMinVersion(_min_game_version)){
  75. throw('This script requires v'+_min_game_version.toFixed(2)+' or higher.\nYou are running: v'+_gameVersion.toFixed(2));
  76. }
  77.  
  78. if(_win.game_data.mode != 'incomings'){
  79. throw("This script must be run from\nthe Overviews->Incoming page");
  80. }
  81.  
  82. return{
  83. execute:function(){fnQueryIncomingAttacks();}
  84. };
  85. }
  86. catch(objError){
  87. fnHandleError(objError);
  88. }
  89.  
  90.  
  91.  
  92. function fnPrint(msg,id){
  93. if($('#' + _debugID).length <= 0){
  94. $('body').append('<div id="' + _debugID + '"></div>');
  95. }
  96.  
  97. $('#' + _debugID).append('<span id="'+((typeof(id)=='undefined')?'':id)+'">'+msg+'</span><br/>');
  98. }
  99.  
  100. function fnDebug(msg){
  101. if((typeof(_debugEnabled) != "undefined") && _debugEnabled){
  102. fnPrint(msg);
  103. }
  104. }
  105.  
  106. /* TODO: possibly add a max_version parameter */
  107. function fnHasMinVersion(min_version){
  108. return (_gameVersion >= min_version);
  109. }
  110.  
  111. function fnHandleError(objError){
  112. var errMsg=String(objError.message||objError||'');
  113. if(errMsg){
  114. fnPrint('Error: ' + errMsg);
  115. alert('Error: ' + errMsg);
  116. }
  117. }
  118.  
  119. function fnAjax(url,method,params,type,isAsync){
  120. var error = null;
  121. var payload = null;
  122.  
  123. $.ajax({
  124. 'async':isAsync,
  125. 'url':url,
  126. 'data':params,
  127. 'dataType':type,
  128. 'type':String(method||'GET').toUpperCase(),
  129. 'error':function(req,status,err){error='ajax: ' + status;},
  130. 'success':function(data,status,req){payload=data;}
  131. });
  132.  
  133. if(error){
  134. throw(error);
  135. }
  136.  
  137. return payload;
  138. }
  139.  
  140. function fnPrintVersion(){
  141. fnPrint('<span style="color:red;">Provide the following info when asking for help:</span>');
  142. fnPrint("=========================");
  143. fnPrint(_author.name + "'s " + _name + ": v" + _version.toFixed(2));
  144. fnPrint("=========================");
  145. fnPrint("Premium: "+(_isPremium?"yes":"no"));
  146. fnPrint("Church : "+(_hasChurch?"yes":"no"));
  147. fnPrint("Statue : "+(_hasPaladin?"yes":"no"));
  148. fnPrint("Archer : "+(_hasArchers?"yes":"no"));
  149. fnPrint("Militia: "+(_hasMilitia?"yes":"no"));
  150. fnPrint("Notes : "+(_hasVillageNotes?"yes":"no"));
  151. fnPrint("Sitter : "+(_win.location.href.match(/t\=\d+/i)?"yes":"no"));
  152. fnPrint("=========================");
  153. fnPrint("Version: "+_win.game_data.version);
  154. fnPrint("World : "+_win.game_data.world);
  155. fnPrint("Screen : "+_win.game_data.screen);
  156. fnPrint("Mode : "+_win.game_data.mode);
  157. fnPrint("URL : "+_win.location.href);
  158. fnPrint("Browser: "+navigator.userAgent);
  159. fnPrint("=========================");
  160. }
  161.  
  162. function fnQueryIncomingAttacks(){
  163. fnDebug('CALL: '+_debugID+'::fnQueryIncomingAttacks');
  164.  
  165. try{
  166. var summary = [];
  167.  
  168. /* Count incoming attacks for each village */
  169. $('#incomings_table tr:has(.rename-icon) td:nth-child(3)').each(function(i,e){
  170. var coords=$(e).text().match(/\d+\|\d+/g);
  171. coords = coords?coords[coords.length - 1]:null;
  172. if(typeof(summary[coords])=="undefined")
  173. summary[coords] = {tally:0,html:$(e).closest('tr').find('td:eq(2)').html()};
  174.  
  175. summary[coords].tally++;
  176. });
  177.  
  178. $('#incomings_table tr:has(.rename-icon) td:nth-child(3)').each(function(i,e){
  179. var coords=$(e).text().match(/\d+\|\d+/g);
  180. coords = coords?coords[coords.length - 1]:null;
  181.  
  182. /* Inject the attack tally */
  183. var cell=$(e).closest('tr').find('td:eq(0)');
  184. cell.html('(' + summary[coords].tally + ') ' + cell.html());
  185. });
  186.  
  187. /* Convert Associative Array to Indexed Array for sorting */
  188. var count=0;
  189. var attackSummary=[];
  190. for(var coords in summary){
  191. if(summary.hasOwnProperty(coords))
  192. attackSummary[count++]=summary[coords];
  193. }
  194.  
  195. /* Sort the Attack Summary in descending order by tally */
  196. attackSummary.sort(function(a,b){return b.tally - a.tally;});
  197.  
  198. /* Display results in a new Popup Window */
  199. var docSource='\
  200. <html>\
  201. <head>\
  202. <title>'+_author.name + "'s " + _name + ": v" + _version.toFixed(2)+'</title>\
  203. <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>\
  204. <link rel="stylesheet" type="text/css" href="/style.php"/>\
  205. </head>\
  206. <body>\
  207. <h3>Attack Summary:</h3><hr>\
  208. </body>\
  209. </html>\
  210. ';
  211.  
  212. var popup=window.open('about:blank','dalesmckay_tw_'+_debugID,'width=400,height=480,scrollbars=yes');
  213. popup.document.open('text/html','replace');
  214. popup.document.write(docSource);
  215. popup.document.close();
  216. void(0);
  217. popup.focus();
  218.  
  219. var eleDIV=popup.document.createElement("div");
  220. eleDIV.setAttribute("id","div_attack_summary");
  221. popup.document.body.appendChild(eleDIV);
  222.  
  223. var resultTable=popup.document.createElement("table");
  224. resultTable.setAttribute("id","table_attack_summary");
  225. resultTable.setAttribute("class","vis");
  226. resultTable.setAttribute("width","100%");
  227. eleDIV.appendChild(resultTable);
  228.  
  229. /* Create the Table Header */
  230. curRow=resultTable.insertRow(0);
  231. curRow.setAttribute("style","white-space:nowrap");
  232. curTH=popup.document.createElement("th");
  233. curTH.appendChild(popup.document.createTextNode("Village"));
  234. curRow.appendChild(curTH);
  235. curTH=popup.document.createElement("th");
  236. curTH.appendChild(popup.document.createTextNode("Attack Count"));
  237. curRow.appendChild(curTH);
  238.  
  239. /* Create Table Content */
  240. for(var ii=0; ii < attackSummary.length; ii++){
  241. curRow=resultTable.insertRow(-1);
  242. curRow.setAttribute("class",(ii%2)?"row_b":"row_a");
  243. curRow.setAttribute("style","white-space:nowrap");
  244.  
  245. curTD=popup.document.createElement("td");
  246. curTD.innerHTML=attackSummary[ii].html;
  247. curRow.appendChild(curTD);
  248.  
  249. /* Force the link target to the main window */
  250. curTD.getElementsByTagName("a")[0].setAttribute("target","tw.main");
  251.  
  252. curTD=popup.document.createElement("td");
  253. curTD.setAttribute("align","right");
  254. curTD.innerHTML=attackSummary[ii].tally;
  255. curRow.appendChild(curTD);
  256. }
  257.  
  258. fnPrint('<br/>execution completed',_debugID + '_done');
  259. }
  260. catch(objError){
  261. fnHandleError(objError);
  262. }
  263. }
  264. }
  265.  
  266.  
  267. if(typeof(scriptOb)=="undefined"){
  268. var scriptObj = new AttackCounter;
  269. scriptObj.execute();
  270. }
  271.  
  272. void(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement