SophieDebleeckere

backline counter

Dec 9th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. javascript:
  2. var populationPerHC = 4;
  3. var packetSize = 1000;
  4. var villageIDs = [];
  5. var rallyPointURLS = [];
  6. var availableUnits = [];
  7. var backgroundColor = "#36393f";
  8. var borderColor = "#3e4147";
  9. var headerColor = "#202225";
  10. var titleColor = "#ffffdf";
  11. var totalPackets = 0;
  12. if ($("#popup")[0]) $("#popup")[0].remove();
  13. $.getAll = function (
  14.     urls, // array of URLs
  15.     onLoad, // called when any URL is loaded, params (index, data)
  16.     onDone, // called when all URLs successfully loaded, no params
  17.     onError // called when a URL load fails or if onLoad throws an exception, params (error)
  18. ) {
  19.     var numDone = 0;
  20.     var lastRequestTime = 0;
  21.     var minWaitTime = 200; // ms between requests
  22.     loadNext();
  23.     function loadNext() {
  24.         if (numDone == urls.length) {
  25.             onDone();
  26.             return;
  27.         }
  28.  
  29.         let now = Date.now();
  30.         let timeElapsed = now - lastRequestTime;
  31.         if (timeElapsed < minWaitTime) {
  32.             let timeRemaining = minWaitTime - timeElapsed;
  33.             setTimeout(loadNext, timeRemaining);
  34.             return;
  35.         }
  36.  
  37.         console.log('Getting ', urls[numDone]);
  38.         lastRequestTime = now;
  39.         $.get(urls[numDone])
  40.             .done((data) => {
  41.                 try {
  42.                     onLoad(numDone, data);
  43.                     ++numDone;
  44.                     loadNext();
  45.                 } catch (e) {
  46.                     onError(e);
  47.                 }
  48.             })
  49.             .fail((xhr) => {
  50.                 onError(xhr);
  51.             })
  52.     }
  53. };
  54.  
  55. if (window.location.href.indexOf('screen=overview_villages&mode=combined') < 0) {
  56.     //relocate
  57.     window.location.assign(game_data.link_base_pure + "overview_villages&mode=combined");
  58. }
  59. else {
  60.     //right page
  61.     console.log("Right");
  62.     villageIDs = grabVillageIDs();
  63.     grabVillageAvailableUnits(villageIDs);
  64.  
  65. }
  66.  
  67.  
  68. function popupSophie(html) {
  69.     var popup = `<div id="popup" class="ui-widget-content" style="position:absolute;height: 500px; width: 950px;overflow-y: auto;overflow-x: hidden;z-index:50;background-color:${backgroundColor};cursor:move">
  70.     <table id="tableBarbShaper" class="vis" border="1" style="width: 100%;background-color:${backgroundColor};border-color:${borderColor}">
  71.     <tr>
  72.         <tr>
  73.             <td colspan="10" id="BLpacketCounter" style="text-align:center; width:auto; background-color:${headerColor}">
  74.             <h2>
  75.                 <center style="margin:10px"><u>
  76.                         <font color="${titleColor}">Backline packet counter</font>
  77.                     </u>
  78.                 </center>
  79.             </h2>
  80.             </td>
  81.         </tr>
  82.         <tr>
  83.             <td colspan="10" id="titlePackets" style="text-align:center; width:auto; background-color:${headerColor}">
  84.             <h2>
  85.                 <font color="${titleColor}">Total packets found: ${totalPackets}</font>
  86.                 </u>
  87.             </h2>
  88.             </td>
  89.         </tr>
  90.         <tr style="background-color:${backgroundColor}">
  91.         <td id="pasteHere"></td>
  92.         </tr>
  93.     </table>
  94.     <hr>
  95.     <center><img class="tooltip-delayed" title="Sophie -Shinko to Kuma-" src="https://dl.dropboxusercontent.com/s/0do4be4rzef4j30/sophie2.gif" style="cursor:help; position: relative"></center>
  96.     <br>
  97.     <center>
  98.     <p><font color="${titleColor}">Creator: </font><a href="https://forum.tribalwars.net/index.php?members/shinko-to-kuma.121220/" style="text-shadow:-1px -1px 0 ${titleColor},1px -1px 0 ${titleColor},-1px 1px 0 ${titleColor},1px 1px 0 ${titleColor};" title="Sophie profile" target="_blank">Sophie "Shinko to Kuma"</a>
  99.     </p>
  100.     </center>
  101.     </div>`;
  102.     $("#contentContainer").before(popup);
  103.     $("#popup").draggable();
  104.     $("#pasteHere").eq(0).append(html);
  105.     //$("#totalPackets")[0].innerHTML=totalPackets;
  106. }
  107.  
  108. function grabVillageIDs() {
  109.     var IDs = [];
  110.     /*
  111.     for (var j = 0; j < villageIds.length; j++) {
  112.         targetUrls.push(`${window.location.origin}/game.php?village=${villageIds[j]}&screen=place`);
  113.     }*/
  114.     for (var j = 0; j < $(".quickedit-vn").length; j++) {
  115.         IDs.push($(".quickedit-vn").eq(j).attr("data-id"));
  116.     }
  117.     console.log(IDs);
  118.     return IDs;
  119. }
  120.  
  121. function grabVillageAvailableUnits(id) {
  122.     //grab rally point for each village /game.php?village=id&screen=place
  123.  
  124.     for (var i = 0; i < id.length; i++) {
  125.         if (get_sitter_player() == false) {
  126.             rallyPointURLS.push("/game.php?village=" + id[i] + "&screen=place");
  127.         }
  128.         else {
  129.             rallyPointURLS.push("/game.php?t=" + get_sitter_player() + "&village=" + id[i] + "&screen=place");
  130.         }
  131.     }
  132.     console.log(rallyPointURLS);
  133.     $.getAll(rallyPointURLS,
  134.         (i, blabla) => {
  135.             var troopCounts = {};
  136.             $(blabla).find(('a[id^=units_entry_all_]')).each(function (i, el) {
  137.                 var id = $(el).attr('id');
  138.                 var unit = id.match(/units_entry_all_(\w+)/)[1];
  139.                 var count = $(el).text();
  140.                 count = count.match(/\((\d+)\)/)[1];
  141.                 troopCounts[unit] = parseInt(count);
  142.             });
  143.             availableUnits.push(troopCounts);
  144.         },
  145.         () => {
  146.             console.log("Total available");
  147.             console.table(availableUnits);
  148.             //header section
  149.             var html = `<font color="${titleColor}"><table id="troops" class="vis" border="1" style="width: 100%;background-color:${backgroundColor};border-color:${borderColor}">
  150.         <tr><td style="text-align:center; width:auto; background-color:${headerColor}">ID</td><td align="center" style="background-color:${headerColor}">Village link</td><td align="center" style="background-color:${headerColor}">Packets</td>`;
  151.             //header for each unit available
  152.             for (var o = 0; o < game_data.units.length - 1; o++) {
  153.                 html += `<td align="center" style="background-color:${headerColor}">${game_data.units[o]}</td>`
  154.             }
  155.             html += `</tr>`;
  156.  
  157.             //troops section
  158.             for (var k = 0; k < id.length; k++) {
  159.                 html += `<tr><td align="center" style="background-color:${backgroundColor}">${id[k]}</td><td align="center" style="background-color:${backgroundColor}"><a href=${$(".quickedit-content")[k].children[0].href} style="color:#40D0E0;"><div style="height:100%;width:100%">${$(".quickedit-content")[k].innerText}</div></td>`;
  160.                 //calc population of def units
  161.                 localDefPackets = availableUnits[k]["spear"] + availableUnits[k]["sword"];
  162.                 if (game_data.units[3] == "archer") {
  163.                     localDefPackets += availableUnits[k]["archer"]
  164.                 }
  165.                 heavyNr = game_data.units.indexOf("heavy");
  166.                 localDefPackets += availableUnits[k]["heavy"] * populationPerHC;
  167.                 localDefPackets = Math.floor(localDefPackets / packetSize);
  168.                 totalPackets+=localDefPackets;
  169.                 html += `<td align="center" style="background-color:${backgroundColor}">${localDefPackets}</td>`
  170.                 for (var unit = 0; unit < game_data.units.length - 1; unit++) {
  171.                     html += `<td align="center" style="background-color:${backgroundColor}">${availableUnits[k][game_data.units[unit]]}</td>`
  172.                 }
  173.                 html += `</tr>`
  174.             }
  175.             html += `</table></font>`
  176.             popupSophie(html);
  177.         },
  178.         (error) => {
  179.             console.error(error);
  180.         });
  181. }
Add Comment
Please, Sign In to add comment