musaad-sa

Untitled

Apr 10th, 2021 (edited)
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. javascript:
  2. //FUNCTIONS
  3.  
  4. //"logic" functions
  5.  
  6. /*Functions used to obtain some data*/
  7.  
  8. //download the time window from a given url
  9. function getArrivalDate(url){
  10.     var request = new XMLHttpRequest();
  11.     request.open('GET', url, false);
  12.     request.send(null);
  13.     dates=request.response.split(';');
  14.     min=dates[0].split(':');
  15.     max=dates[1].split(':');
  16.     min_time=new Date(parseInt(min[0]),parseInt(min[1])-1,parseInt(min[2]),parseInt(min[3]),parseInt(min[4]),parseInt(min[5]),parseInt(min[6]));
  17.     max_time=new Date(parseInt(max[0]),parseInt(max[1])-1,parseInt(max[2]),parseInt(max[3]),parseInt(max[4]),parseInt(max[5]),parseInt(max[6]));
  18.     arrival=[min_time,max_time];
  19.     return arrival;
  20. }
  21.  
  22. //read a text file uploaded somewhere
  23. function getCoordsByUrl(url){
  24.     var request = new XMLHttpRequest();
  25.     request.open('GET', url, false);
  26.     request.send(null);
  27.     return request.response;
  28. }
  29.  
  30. //get unit infos (speed and pop)
  31. function fnAjaxRequest(url,sendMethod,params,type){
  32.     var error=null,payload=null;
  33.  
  34.     win.$.ajax({
  35.         "async":false,
  36.         "url":url,
  37.         "data":params,
  38.         "dataType":type,
  39.         "type":String(sendMethod||"GET").toUpperCase(),
  40.         "error":function(req,status,err){error="ajax: " + status;},
  41.         "success":function(data,status,req){payload=data;}
  42.     });
  43.  
  44.     if(error){
  45.         throw(error);
  46.     }
  47.  
  48.     return payload;
  49. }
  50. function fnCreateConfig(name){return win.$(fnAjaxRequest("/interface.php","GET",{"func":name},"xml")).find("config");}
  51. function fnCreateUnitConfig(){return fnCreateConfig("get_unit_info");}
  52.  
  53. //find speed of a specific unit (m/field)
  54. function getSpeed(unit){
  55.     return parseFloat(unitConfig.find(unit+" speed").text());
  56. }
  57.  
  58. //find pop of a specific unit
  59. function getPop(unit){
  60.     return parseFloat(unitConfig.find(unit+" pop").text());
  61. }
  62.  
  63. //find base build time of a specific unit
  64. function getBuildTime(unit){
  65.     return parseFloat(unitConfig.find(unit+" build_time").text());
  66. }
  67.  
  68. //get current village coord
  69. function currentCoord(){
  70.     return game_data.village.coord;
  71.     }
  72.  
  73.  
  74. /*simple calculation*/
  75.  
  76. // find the distance between two given coords
  77. function distance(source, target){
  78.     var fields = Math.sqrt(Math.pow(source[1]-target[1],2)+Math.pow(source[0]-target[0],2));
  79.     return fields;
  80.     }
  81.    
  82. //find travel time between two given coords for a specific unit
  83. function travelTime(source, target, unit){
  84.     var unitSpeed = getSpeed(unit);
  85.     var fields=distance(source,target);
  86.     var tt=unitSpeed*fields;
  87.     return tt;
  88. }
  89.  
  90. //add time (in minutes) to a date
  91. function addTime(date,time){
  92.     var date_ms=date.getTime();
  93.     time=time*1000*60;
  94.     newDate=date_ms+time;
  95.     newDate=new Date(newDate);
  96.     return newDate;
  97. }
  98.  
  99. /*Core functions*/
  100.  
  101. //get a list of coords whith the rigth traveltime
  102. function getGoodCoords(coords,unit,minTime,maxTime){
  103.     var doc = document;
  104.     var goodCoords=[];
  105.     var servertime = window.$("#serverTime").html().match(/\d+/g);
  106.     var serverDate = win.$("#serverDate").html().match(/\d+/g);
  107.     serverTime = new Date(serverDate[1]+"/"+serverDate[0]+"/"+serverDate[2]+" "+servertime.join(":"));
  108.     coords = coords.split(",");
  109.     closest=60*500;
  110.     far=0;
  111.    
  112.     for(i=0;i<coords.length-1;i++){
  113.             coordsSplit = coords[i].split('|');
  114.             travel=travelTime(coordsSplit, currentCoord().split("|"), unit);
  115.             arrival=addTime(serverTime,travel);
  116.             if (travel<closest){
  117.                 closest=travel;
  118.             }
  119.             else if (travel>far){
  120.                 far=travel;
  121.             }
  122.             if (arrival>minTime && arrival<maxTime)
  123.             {
  124.                 goodCoords.push(coords[i]);
  125.             }
  126.         }
  127.        
  128.         if(goodCoords.length>0){
  129.             console.log(goodCoords);
  130.             index=Math.round(Math.random() * (goodCoords.length - 1));
  131.             while(goodCoords.length>0 && alreadySent(currentCoord(),goodCoords[index])){
  132.                 goodCoords.splice(index,1);
  133.                 index=Math.round(Math.random() * (goodCoords.length - 1));
  134.             }
  135.             if(goodCoords.length>0){
  136.                 fillInCoords(goodCoords[index]);
  137.                 return goodCoords[index];
  138.                 }
  139.             else{
  140.                 UI.ErrorMessage("تم الارسال من كل القرى الذي بالمدى، حاول لاحقا او غير نوع الوحدة",5000);
  141.                 return null;
  142.             }
  143.         }
  144.         else{
  145.             UI.ErrorMessage("لا توجد قرى في المدى النطاق المطلوب."+"\n"+" ستحتاج استعمال السكربت بالوقت بين "+addTime(minTime,(-1)*far).toString()+" و "+addTime(maxTime,(-1)*closest).toString(),6000);
  146.             return null;
  147.         }
  148. }
  149.  
  150. //insert given coords as target
  151. function fillInCoords(coords){
  152.     coordsSplit = coords.split('|');
  153.     document.forms[0].x.value = coordsSplit[0];
  154.     document.forms[0].y.value = coordsSplit[1];
  155.     $('#place_target').find('input').val(coordsSplit[0] + '|' + coordsSplit[1]);
  156. }
  157.  
  158. //save sent coords
  159. function alreadySent(myCoords,target){
  160.     if(sessionStorage.alreadySent){
  161.         history=JSON.parse(sessionStorage.alreadySent);
  162.         if (myCoords in history){
  163.             if (history[myCoords].includes(target)){
  164.                 return true
  165.             }
  166.             else{
  167.                 history[myCoords].push(target);
  168.                 return false
  169.             }
  170.         }
  171.         else{
  172.             history[myCoords]=[target];
  173.             sessionStorage.alreadySent=JSON.stringify(history);
  174.             return false
  175.         }
  176.     }
  177.     else{
  178.         history={}
  179.         history[myCoords]=[target];
  180.         sessionStorage.alreadySent=JSON.stringify(history);
  181.         return false
  182.     }
  183.    
  184. }
  185.  
  186.  
  187. function fillInTroops(troopCount, troopPreferences){
  188.     troopArray = troopPreferences;
  189.     //find the slowest selected unit
  190.     var keys=Object.keys(troopArray);
  191.     slowest=["spy"];
  192.     for (i=0;i<keys.length;i++){
  193.         if(troopArray[keys[i]] && troopCount[keys[i]]>0){
  194.             if(getSpeed(keys[i])>getSpeed(slowest[0])){
  195.                 slowest=[];
  196.                 slowest.push(keys[i]);
  197.             }
  198.             else if(getSpeed(keys[i])==getSpeed(slowest[0])){
  199.                 slowest.push(keys[i]);
  200.             }
  201.         }
  202.     }
  203.     var fakePopNeeded = Math.ceil(game_data.village.points / 100);
  204.     troopsToSend=new Array(keys.length).fill(0);
  205.     troopsToSend[keys.indexOf(slowest[0])]++;
  206.     fakePopNeeded-=getPop(slowest[0]);
  207.  
  208.    
  209.     fasterBarrackTroops=findFasterBuild(troopArray)[0];
  210.     fasterStableTroops=findFasterBuild(troopArray)[1];
  211.     fasterWorkshopTroops=findFasterBuild(troopArray)[2];
  212.     k=1;
  213.     while (k>0){
  214.         if(fasterBarrackTroops.length>0 && troopCount[fasterBarrackTroops[0]]>troopsToSend[keys.indexOf(fasterBarrackTroops[0])]){
  215.             troopsToSend[keys.indexOf(fasterBarrackTroops[0])]++;
  216.             fakePopNeeded--;
  217.         }
  218.         else{
  219.             fasterBarrackTroops.shift();
  220.         }
  221.        
  222.         if(fakePopNeeded<=0){
  223.             break;
  224.         }
  225.        
  226.         if(fasterStableTroops.length>0 && troopCount[fasterStableTroops[0]]>troopsToSend[keys.indexOf(fasterStableTroops[0])]){
  227.             troopsToSend[keys.indexOf(fasterStableTroops[0])]++;
  228.             fakePopNeeded-=getPop(fasterStableTroops[0]);
  229.         }
  230.         else{
  231.             fasterStableTroops.shift();
  232.         }
  233.        
  234.        
  235.         if(fakePopNeeded<=0 || (fasterBarrackTroops.length<1 && fasterStableTroops.length<1)){
  236.             k=-1;
  237.         }
  238.        
  239.     }
  240.    if(fakePopNeeded<=0){
  241.     for(i=0;i<troopsToSend.length;i++){
  242.         document.forms[0][keys[i]].value = troopsToSend[i];
  243.     }
  244.     return slowest[0];
  245.    }
  246.    else{
  247.     alert("لا يوجد وحدات لارسال الوهميات");
  248.     return null;
  249.    }
  250.    
  251. }
  252.  
  253.  
  254. //find the unit whith the fastest base build time
  255. function findFasterBuild(troopArray){
  256.     var keys=Object.keys(troopArray);
  257.     var barracks=[];
  258.     var stable=[];
  259.     var workshop=[];
  260.     keys.sort(function sortByBuildTime(a,b){return getBuildTime(a)-getBuildTime(b);});
  261.     for(i=0;i<keys.length;i++){
  262.         if(troopArray[keys[i]]){
  263.             if(getPop(keys[i])==1){
  264.                 barracks.push(keys[i]);
  265.             }
  266.             else if(keys[i]=="ram" || keys[i]=="catapult"){
  267.                 workshop.push(keys[i]);
  268.             }
  269.             else {
  270.                 stable.push(keys[i]);
  271.             }
  272.         }
  273.     }
  274.     return [barracks,stable, workshop];
  275. }
  276.  
  277.  
  278. /*------------------------------------------------------------------------------------------------------------*/
  279. /*interface functions*/
  280.  
  281. function dateToIsoFormat(date){
  282.     offset= -(new Date().getTimezoneOffset() / 60);
  283.     date=new Date(date.getTime()+1000*60*60*offset);
  284.     return date.toISOString().split(".")[0];
  285. }
  286.  
  287. function setManual(){
  288.     mode="manual";
  289.     document.getElementById("coordsUrl").disabled=true;
  290.     document.getElementById("arrivalUrl").disabled=true;
  291.     document.getElementById("coords").disabled=false;
  292.     document.getElementById("minDate").disabled=false;
  293.     document.getElementById("maxDate").disabled=false;
  294. }
  295.  
  296. function setByUrl(){
  297.     mode="byUrl";
  298.     document.getElementById("coords").disabled=true;
  299.     document.getElementById("minDate").disabled=true;
  300.     document.getElementById("maxDate").disabled=true;
  301.     document.getElementById("coordsUrl").disabled=false;
  302.     document.getElementById("arrivalUrl").disabled=false;
  303. }
  304.  
  305. function getCoords(){
  306.     coords=document.getElementById("coords").value.match(/\d\d\d\|\d\d\d/g);
  307. }
  308.  
  309. function getCoordsUrl(){
  310.     coordsUrl=document.getElementById("coordsUrl").value;
  311. }
  312.  
  313. function getArrival(){
  314.     minArrival=new Date(document.getElementById("minDate").value);
  315.     maxArrival=new Date(document.getElementById("maxDate").value);
  316.     document.getElementById("maxDate").min=document.getElementById("minDate").value;
  317.     document.getElementById("minDate").max=document.getElementById("minDate").value;
  318. }
  319.  
  320. function getArrivalUrl(){
  321.     arrivalUrl=document.getElementById("arrivalUrl").value;
  322. }
  323.  
  324. function updateUnits(){
  325.     for(i=0;i<unitNames.length;i++){
  326.         unitPreference[unitNames[i]]=document.getElementById(unitNames[i]+"Check").checked
  327.     }
  328. }
  329.  
  330. function saveSettings(){
  331.     if (minArrival>maxArrival){
  332.         UI.ErrorMessage('وقت الوصول خطا يانوب');
  333.     }
  334.     else if (coords==null){
  335.         UI.ErrorMessage('ادخل على الاقل قرية هدف واحده ',5000);
  336.     }
  337.     else{
  338.         if(mode=="manual"){
  339.             localStorage.smartFakeSettings=mode+":::"+coords+":::"+minArrival+":::"+maxArrival+":::"+JSON.stringify(unitPreference);
  340.         }
  341.         else{
  342.             localStorage.smartFakeSettings=mode+":::"+coordsUrl+":::"+arrivalUrl+":::"+JSON.stringify(unitPreference);
  343.         }
  344.         UI.SuccessMessage('Settings saved', 3000);
  345.     }
  346.    
  347. }
  348.  
  349. function reset(){
  350.     localStorage.removeItem("smartFakeSettings");
  351.     coords=[];
  352.     coordsUrl="";
  353.     minArrival=new Date();
  354.     maxArrival=new Date(minArrival.getTime() + 1000*60*60);
  355.     arrivalUrl="";
  356.     unitPreference={};
  357.     mode="manual";
  358.     unitNames=[];
  359.     openUI();
  360. }
  361.  
  362.  
  363. function openUI(){
  364.     images="";
  365.     checkBoxes="";
  366.  
  367.     if(localStorage.smartFakeSettings){
  368.         savedSettings=localStorage.smartFakeSettings.split(":::");
  369.         if(savedSettings[0]=="manual"){
  370.             mode=savedSettings[0];
  371.             coords=savedSettings[1].replace(/,/g," ");
  372.             minArrival=new Date(savedSettings[2]);
  373.             maxArrival=new Date(savedSettings[3]);
  374.             unitPreference=JSON.parse(savedSettings[4]);
  375.             }
  376.         else{
  377.             mode=savedSettings[0];
  378.             coordsUrl=savedSettings[1];
  379.             arrivalUrl=savedSettings[2];
  380.             unitPreference=JSON.parse(savedSettings[3]);
  381.         }
  382.     }
  383.  
  384.  
  385.     for (var i = 0; i < game_data.units.length; i++) {
  386.         if (game_data.units[i] != "militia" && game_data.units[i] != "knigth") {
  387.             unitNames.push(game_data.units[i]);
  388.         }
  389.     }
  390.     for(i=0;i<unitNames.length;i++){
  391.         images=images+'<td style={border: 1px solid black; }><img src="https://dsen.innogamescdn.com/asset/cf2959e7/graphic/unit/unit_'+unitNames[i]+'.png" title='+unitNames[i]+' class="unitImage"></td>';
  392.         if(unitNames[i] in unitPreference && unitPreference[unitNames[i]]){
  393.             checkBoxes=checkBoxes+'<td><input type="checkbox" id='+unitNames[i]+"Check"+' onchange="updateUnits()" checked="true"></input></td>';
  394.         }
  395.         else{
  396.             checkBoxes=checkBoxes+'<td><input type="checkbox" id='+unitNames[i]+"Check"+' onchange="updateUnits()" ></input></td>';
  397.         }
  398.     }
  399.  
  400.     var html =     ' <head></head><body>    <h1>سكربت ارساليات لاوبات أوس</h1>    <form><fieldset><legend>Data Source</legend><h2>Select Data Source</h2><p><input type="radio" id="manual" name="source" value="manual" checked="true" onchange="setManual()">ادخل الاهداف والوقت بشكل يدوي</input></p><p><input type="radio" id="tribe" name="source" value="url" onchange="setByUrl()">ادخل الاهداف والوقت من رابط</input></p></fieldset>      <fieldset>        <legend>Manual settings</legend>         <p>         <h2>احداثيات الاهداف</h2> <label>الاحداثيات:</label>          <textarea id = "coords"                  rows = "5"                  cols = "70" placeholder="ادخل الاحداثيات للاهداف" onchange="getCoords()"></textarea>        </p>        <p><h2>وقت الوصول</h2> <label>الاحداثيات ستصل بين </label> <input type="datetime-local" id="minDate" value="'+dateToIsoFormat(minArrival)+'" onchange="getArrival()"><label> و </label> <input type="datetime-local" id="maxDate" value="'+dateToIsoFormat(maxArrival)+'" onchange="getArrival()" ></input></p> </fieldset> <fieldset><legend>اعدادات القبيلة</legend><p><h2>احداثيات الاهداف</h2><label>رابط الاهداف: </label><input type="text" id="coordsUrl" placeholder="https://dl.dropbox/خذه من أوس" onchange="getCoordsUrl()" disabled="true"></input></p><p><h2>الوصول</h2><label>رابط الوصول: </label><input type="text" id="arrivalUrl" placeholder="https://dl.dropbox/خذه من أوس" onchange="getArrivalUrl()" disabled="true"></input></p></fieldset></form> <fieldset><legend>اعدادات الوحدات</legend><table id="checkboxesTable" border="1"><tr><h2>اختار الوحده للاستعمال</h2></tr><tr>'+images+'</tr><tr>'+checkBoxes+'</tr></table></fieldset><p><input type="button" class="btn evt-confirm-btn btn-confirm-yes" id="save" onclick="saveSettings()" value="حفظ"></input> <input type="button" class="btn evt-confirm-btn btn-confirm-no" id="reset" onclick="reset()" value="اعادة الضبط يانوب"></input> </p></body>';
  401.  
  402.     Dialog.show("Script settings", html);
  403.     if (mode == "manual"){
  404.         document.getElementById("coordsUrl").disabled=true;
  405.         document.getElementById("arrivalUrl").disabled=true;
  406.         document.getElementById("coords").disabled=false;
  407.         document.getElementById("minDate").disabled=false;
  408.         document.getElementById("maxDate").disabled=false;
  409.         document.getElementById("coords").value=coords;
  410.         //document.getElementById("minDate").value=;
  411.         //document.getElementById("maxDate").value=;
  412.     }
  413.     else{
  414.         document.getElementById("coords").disabled=true;
  415.         document.getElementById("minDate").disabled=true;
  416.         document.getElementById("maxDate").disabled=true;
  417.         document.getElementById("coordsUrl").disabled=false;
  418.         document.getElementById("arrivalUrl").disabled=false;
  419.         document.getElementById("coordsUrl").value=coordsUrl;
  420.         document.getElementById("arrivalUrl").value=arrivalUrl;
  421.         document.getElementById("tribe").checked=true;
  422.     }
  423. }
  424.  
  425.  
  426.  
  427. //ACTUAL CODE
  428. if (game_data.screen == 'place') {
  429.     win=window;
  430.     unitConfig=fnCreateUnitConfig();
  431.     var troopCounts = {};
  432.     $('a[id^=units_entry_all_]').each(function (i, el) {
  433.         var id = $(el).attr('id');
  434.         var unit = id.match(/units_entry_all_(\w+)/)[1];
  435.         var count = $(el).text();
  436.         count = count.match(/\((\d+)\)/)[1];
  437.         troopCounts[unit] = parseInt(count);
  438.     });
  439.    
  440.     if(localStorage.smartFakeSettings){
  441.         settings=localStorage.smartFakeSettings;
  442.         mode=settings.split(":::")[0];
  443.         if (mode == "manual"){
  444.             coords=settings.split(":::")[1];
  445.             minArrival=new Date(settings.split(":::")[2]);
  446.             maxArrival=new Date(settings.split(":::")[3]);
  447.             troopPreference=JSON.parse(settings.split(":::")[4]);
  448.         }
  449.         else if(mode == "byUrl"){
  450.             coords=getCoordsByUrl(settings.split(":::")[1]);
  451.             dates=getArrivalDate(settings.split(":::")[2]);
  452.             minArrival=dates[0];
  453.             maxArrival=dates[1];
  454.             troopPreference=JSON.parse(settings.split(":::")[3]);
  455.            
  456.         }
  457.         troops=fillInTroops(troopCounts, troopPreference);
  458.         if(troops!=null){
  459.             target=getGoodCoords(coords,troops, minArrival, maxArrival);
  460.         }
  461.     }
  462.     else{
  463.         var coords=[];
  464.         var coordsUrl="";
  465.         var minArrival=new Date();
  466.         var maxArrival=new Date(minArrival.getTime() + 1000*60*60);
  467.         var arrivalUrl="";
  468.         var unitPreference={};
  469.         var mode="manual";
  470.         var unitNames=[];
  471.         openUI();
  472.     }
  473. }
  474.  
  475. else{
  476.     var coords=[];
  477.     var coordsUrl="";
  478.     var minArrival=new Date();
  479.     var maxArrival=new Date(minArrival.getTime() + 1000*60*60);
  480.     var arrivalUrl="";
  481.     var unitPreference={};
  482.     var mode="manual";
  483.     var unitNames=[];
  484.  
  485.     openUI();
  486. }
Add Comment
Please, Sign In to add comment