Advertisement
Guest User

Travian Animal Finder Bot

a guest
Jul 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This was made as a firefox extension, this is the script that gets injected into the site
  2. // This alone will probably not work
  3. // I know it's really messy
  4.  
  5. var script_is_running = false;
  6.  
  7. var typeOfAnimal = ["Array index 0", "Rat", "Spider", "Snake", "Bat", "Wild Boar", "Wolf", "Bear", "Crocodile", "Tiger", "Elephant"];
  8. var selectedAnimal = 9;
  9.  
  10. var player_x = -5;
  11. var player_y = 9;
  12. var oasis_x = 0;
  13. var oasis_y = 0;
  14. var distance_to_oasis = 0; // Math.hypot(player_x-oasis_x, player_y-oasis_y)
  15.  
  16. var json_data = {};
  17.  
  18. var oasis_with_selected_animal = []; // To be able to sort by distance
  19. var cells_with_oasis = {};
  20.  
  21. function selectAnimal(animal_number){
  22.   selectedAnimal = animal_number;
  23.   console.log("Selected animal: " + typeOfAnimal[selectedAnimal]);
  24.   fetchJSONAndContinue();
  25. }
  26.  
  27. // Sorting filter/algorithm used to sort by closest distance to oasis
  28. function sortByDistance(a, b) {
  29.     if (a[0] === b[0]) {
  30.         return 0;
  31.     }
  32.     else {
  33.         return (a[0] < b[0]) ? -1 : 1;
  34.     }
  35. }
  36.  
  37. // Get all oasis cells as input, run MapDetails.get() on each cell to load it's data, then the run the function in second input after it's finished loading.
  38. function getMapDetails(oasis_cells, callback){
  39.   for(i=0; i<(oasis_cells.length); i++){
  40.     MapDetails.get(oasis_cells[i].id).data;
  41.   }
  42.   var counter = 0;
  43.   var interval = setInterval(function(){
  44.     counter++;
  45.     console.log("Loading all map tiles... " + counter + "/20 seconds");
  46.   }, 1000);
  47.   setTimeout(function(){
  48.     clearInterval(interval);
  49.     callback();
  50.   }, 21000);
  51.  
  52. }
  53.  
  54. // Find oasis cells containing wanted animal, log out results in console
  55. function findSelectedAnimal(){
  56.   oasis_with_selected_animal = [];
  57.   for(i=0; i<(cells_with_oasis.length); i++){
  58.     var animals = MapDetails.get(cells_with_oasis[i].id).data.troops.units;
  59.     if(Object.prototype.toString.call(animals) == "[object Object]"){
  60.       if(!(isNaN(animals[selectedAnimal]))){
  61.         distance_to_oasis = Math.hypot(player_x-cells_with_oasis[i].x, player_y-cells_with_oasis[i].y);
  62.         oasis_with_selected_animal.push([distance_to_oasis, typeOfAnimal[selectedAnimal], animals[selectedAnimal], "(" + cells_with_oasis[i].x + "|" + cells_with_oasis[i].y + ")"]);
  63.       }
  64.     }
  65.   }
  66.  
  67.   oasis_with_selected_animal.sort(sortByDistance)
  68.   for(i=0; i<(oasis_with_selected_animal.length); i++){
  69.     console.log(oasis_with_selected_animal[i][1] + ", " + oasis_with_selected_animal[i][3] + ", Amount: " + oasis_with_selected_animal[i][2]);
  70.   }
  71.   script_is_running = false;
  72. }
  73.  
  74. // Fetch API JSON data, then return only Oasis cells, then run getMapDetails();
  75. function fetchJSONAndContinue() {
  76.   if(script_is_running){
  77.     console.log("Script is already running, wait for it to finish and try again.");
  78.     alert("Script is already running, wait for it to finish and try again.");
  79.   }
  80.   else{
  81.     script_is_running = true;
  82.     fetch('https://com7.kingdoms.com/api/external.php?action=getMapData&privateApiKey=ENTER_YOUR_OWN_API_KEY_HERE') // https://forum.kingdoms.com/thread/4099-api-for-external-tools/ send a get request to API url first to request your API key, replace "ENTER_YOUR_OWN_API_KEY_HERE" with your API key.
  83.       .then(function(response) {
  84.         return response.json();
  85.       })
  86.       .then(function(jsonData) {
  87.         json_data = jsonData;
  88.         cells_with_oasis = json_data.response.map.cells.filter(function(i) {
  89.           return i.oasis > 0;
  90.         });
  91.         return cells_with_oasis;
  92.       })
  93.       .then(function(oasis_cells) {
  94.         getMapDetails(oasis_cells, findSelectedAnimal);
  95.       });
  96.   }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement