Advertisement
iradap

Resources helper

Jun 28th, 2022 (edited)
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.05 KB | None | 0 0
  1. // ==UserScript==
  2. // @name         Resources helper
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  try to take over the world!
  6. // @author       Czogi
  7. // @match        https://alkatria.pl/game
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. //Timery surowców
  12. //Nazwa odnowa/wyczerpanie
  13.  
  14. //Palma 30s/75s
  15. //Dąb 60s/150s
  16. //Sosna 90s/180s
  17.  
  18. //Miedź 30s/75s
  19. //Żelazo 60s/150s
  20. //Srebro 90s/180s
  21.  
  22. //Łowisko N/A/120s
  23.  
  24. const resources = [];
  25. var awaitingForResponse = undefined;
  26. var timersController = undefined;
  27. const toolNames = {
  28.   Siek: "cutting",
  29.   Wędk: "fishing",
  30.   Noży: "gathering",
  31.   Kilo: "mining"
  32. };
  33. const respawnTimers = {
  34.   Pal: 30,
  35.   Dęb: 60,
  36.   Sos: 90,
  37.   Mie: 30,
  38.   Żel: 60,
  39.   Sre: 90
  40. };
  41. class TimersController {
  42.   constructor() {
  43.     this.lsName = "resourcesTimers";
  44.     this.timers = {};
  45.   }
  46.   removeTimer(x, y, type) {
  47.     this.removeLs(`${map.current_map},${x},${y},${type}`, 0);
  48.     delete this.timers[`${map.current_map},${x},${y},${type}`];
  49.   }
  50.   setTimer(timeout, x, y, type) {
  51.     this.timers[`${map.current_map},${x},${y},${type}`] = new Timer(
  52.       timeout,
  53.       x,
  54.       y,
  55.       type
  56.     );
  57.   }
  58.   getLs(name, val) {
  59.     const mainObj = JSON.parse(localStorage[this.lsName]);
  60.     if (!mainObj[name]) {
  61.       return val;
  62.     }
  63.     return mainObj[name];
  64.   }
  65.   setLs(name, val) {
  66.     const mainObj = JSON.parse(localStorage[this.lsName]);
  67.     mainObj[name] = val;
  68.     localStorage.setItem(this.lsName, JSON.stringify(mainObj));
  69.   }
  70.   removeLs(name) {
  71.     const mainObj = JSON.parse(localStorage[this.lsName]);
  72.     delete mainObj[name];
  73.     localStorage.setItem(this.lsName, JSON.stringify(mainObj));
  74.   }
  75.   checkLs() {
  76.     if (!localStorage[this.lsName]) {
  77.       localStorage.setItem(this.lsName, "{}");
  78.       return;
  79.     }
  80.     const timers = JSON.parse(localStorage[this.lsName]);
  81.     for (const [id, timer] of Object.entries(timers)) {
  82.       const cords = id.split(",");
  83.       cords.shift();
  84.       if (!timer) {
  85.         this.removeLs(...cords);
  86.         continue;
  87.       }
  88.  
  89.       const unix = Date.now();
  90.  
  91.       if (unix > timer) {
  92.         this.removeTimer(...cords);
  93.         continue;
  94.       }
  95.  
  96.       this.setTimer(timer - unix, ...cords);
  97.     }
  98.   }
  99. }
  100.  
  101. class Timer extends TimersController {
  102.   constructor(timer, x, y, type) {
  103.     super();
  104.     this.time = Date.now() + timer;
  105.     if (this.startTimer(x, y, type)) {
  106.       this.setLs(`${map.current_map},${x},${y},${type}`, this.time);
  107.     }
  108.   }
  109.   async startTimer(x, y, type) {
  110.     const tile = document.querySelector(`#tile_${x}-${y}`);
  111.     if (!tile) {
  112.       return false;
  113.     }
  114.     this.timer = document.createElement("p");
  115.     this.timer.style.textAlign = "center";
  116.     this.timer.style.top = tile.childElementCount === 0 ? "45%" : "55%";
  117.     this.timer.style.left = "25%";
  118.     this.timer.style.position = "absolute";
  119.     this.timer.style.textShadow = "2px 2px 5px Aqua";
  120.     tile.appendChild(this.timer);
  121.     while (this.time > Date.now()) {
  122.       this.timer.innerText = type + ":" + getTimer(this.time - Date.now());
  123.       await sleep(1000);
  124.     }
  125.     this.timer.remove();
  126.     if (tile.childElementCount === 1) {
  127.       tile.childNodes[0].style.top = "50%";
  128.     }
  129.     this.removeTimer(x, y, type);
  130.   }
  131. }
  132.  
  133. const getTimer = (unix) => {
  134.   let seconds = (~~(unix / 1000) % 60).toString();
  135.   let minutes = (~~(~~(unix / 1000) / 60) % 60).toString();
  136.   seconds = seconds.length === 2 ? seconds : "0" + seconds;
  137.   minutes = minutes.length === 2 ? minutes : "0" + minutes;
  138.  
  139.   return `${minutes}:${seconds}`;
  140. };
  141.  
  142. const oldParse = game.parseServerPacket;
  143. const oldLoad = map.loadMap;
  144. const oldSend = game.sendPacket;
  145.  
  146. const sleep = (ms) => {
  147.   return new Promise((resolve) => setTimeout(resolve, ms));
  148. };
  149.  
  150. const getDistance = (a, b) => {
  151.   return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
  152. };
  153.  
  154. const equipTool = async (name) => {
  155.   if (document.querySelector(".player-equipment")) {
  156.     document.querySelector("#icon-character").click();
  157.     await sleep(100);
  158.     document.querySelector("#icon-character").click();
  159.   } else {
  160.     document.querySelector("#icon-character").click();
  161.   }
  162.   await sleep(300);
  163.  
  164.   const item = Array.from(
  165.     document.querySelector(".player-backpack").children
  166.   ).find((el) => {
  167.     if (el.dataset?.tipItem) {
  168.       const tip = el.dataset.tipItem;
  169.       return (
  170.         tip.includes("Poszukiwacza Przygód") &&
  171.         tip.includes(name) &&
  172.         !tip.includes("Złam")
  173.       );
  174.     }
  175.   });
  176.   if (item) {
  177.     game.sendPacket("equip_item", {
  178.       from: item.dataset.slot,
  179.       type: "backpack"
  180.     });
  181.   }
  182.   document.querySelector("#icon-character").click();
  183.   return item !== undefined;
  184. };
  185.  
  186. const sortReesources = () => {
  187.   return resources.sort((a, b) => {
  188.     if (getDistance(window.player, a) < getDistance(window.player, b)) {
  189.       return -1;
  190.     } else if (getDistance(window.player, a) > getDistance(window.player, b)) {
  191.       return 1;
  192.     }
  193.     return 0;
  194.   });
  195. };
  196.  
  197. const isCloseEnough = (a, b) => {
  198.   const dist = getDistance(a, b);
  199.   return dist == 1
  200.     ? true
  201.     : dist === 2
  202.     ? Math.abs(a.x - b.x) <= 1 && Math.abs(a.y - b.y) <= 1
  203.     : false;
  204. };
  205.  
  206. window.document.addEventListener("keydown", (eventData) => {
  207.   if (eventData.code !== "Space") {
  208.     return;
  209.   }
  210.   var closestResource = sortReesources();
  211.  
  212.   const action = $(
  213.     "#tile_" + closestResource[0].x + "-" + closestResource[0].y + "_tip"
  214.   ).data("action");
  215.   if (action && action === player.current_element) {
  216.     player.current_element = "";
  217.     $("#my-character").html("");
  218.     return;
  219.   }
  220.  
  221.   closestResource = closestResource.filter((resource) => {
  222.     return (
  223.       !timersController.getLs(
  224.         `${map.current_map},${resource.x},${resource.y},E`,
  225.         0
  226.       ) &&
  227.       !timersController.getLs(
  228.         `${map.current_map},${resource.x},${resource.y},R`,
  229.         0
  230.       )
  231.     );
  232.   });
  233.   if (!closestResource.length) {
  234.     return;
  235.   }
  236.   closestResource = closestResource[0];
  237.  
  238.   if (
  239.     !closestResource ||
  240.     (!isCloseEnough(window.player, closestResource) &&
  241.       closestResource.action !== "fishing")
  242.   ) {
  243.     return;
  244.   }
  245.   if (getDistance(player, closestResource) > 3) {
  246.     return;
  247.   }
  248.   window.map.useElement(closestResource.x, closestResource.y);
  249. });
  250. const secondsToMili = (sec) => {
  251.   return sec * 1000;
  252. };
  253. game.parseServerPacket = async function (data) {
  254.   oldParse.apply(this, arguments);
  255.   if (data.code === 1016) {
  256.     if (data.message.includes("Musisz założyć")) {
  257.       const toolName = data.message.split(" ")[2].slice(0, 4);
  258.       const didEquip = await equipTool(toolName);
  259.       const closestTileOfTool = sortReesources().find((tile) => {
  260.         return (
  261.           tile.action === toolNames[toolName] ||
  262.           (window.player.current_element &&
  263.             tile.action === window.player.current_element)
  264.         );
  265.       });
  266.       if (didEquip) {
  267.         game.sendPacket(879, {
  268.           x: closestTileOfTool.x,
  269.           y: closestTileOfTool.y
  270.         });
  271.       }
  272.     }
  273.     if (
  274.       data.message === "Przedmiot został złamany" ||
  275.       data.message === "Niepowodzenie!"
  276.     ) {
  277.       if (awaitingForResponse) {
  278.         timersController.setTimer(
  279.           awaitingForResponse.timeout,
  280.           awaitingForResponse.x,
  281.           awaitingForResponse.y,
  282.           "E"
  283.         );
  284.         awaitingForResponse = undefined;
  285.       }
  286.     }
  287.   }
  288.   if (data.code === "exhaust_tile" && awaitingForResponse) {
  289.     if (data.x === awaitingForResponse.x && data.y === awaitingForResponse.y) {
  290.       timersController.setTimer(
  291.         awaitingForResponse.timeout,
  292.         awaitingForResponse.x,
  293.         awaitingForResponse.y,
  294.         "E"
  295.       );
  296.       const exhausted = resources.find((resource) => {
  297.         return resource.x === data.x && resource.y === data.y;
  298.       });
  299.       const respawn = respawnTimers[exhausted.name.split(" ")[1].slice(0, 3)];
  300.       if (respawn) {
  301.         timersController.setTimer(
  302.           secondsToMili(respawn),
  303.           exhausted.x,
  304.           exhausted.y,
  305.           "R"
  306.         );
  307.       }
  308.       awaitingForResponse = undefined;
  309.     }
  310.   }
  311. };
  312.  
  313. map.loadMap = function (data) {
  314.   oldLoad.apply(this, arguments);
  315.   for (const tile of data.tiles) {
  316.     if (!tile.action || tile.action === "healing") {
  317.       continue;
  318.     }
  319.     resources.push(tile);
  320.   }
  321.   timersController = new TimersController();
  322.   timersController.checkLs();
  323. };
  324.  
  325. game.sendPacket = function (code, data) {
  326.   oldSend.apply(this, arguments);
  327.   switch (code) {
  328.     case 879:
  329.       const tile = sortReesources().find((tile) => {
  330.         return data.x === tile.x && data.y === tile.y;
  331.       });
  332.       if (
  333.         tile.action === "fishing" &&
  334.         !timersController.getLs(`${map.current_map},${tile.x},${tile.y},E`, 0)
  335.       ) {
  336.         timersController.setTimer(tile.timeout, tile.x, tile.y, "E");
  337.         return;
  338.       }
  339.       if (tile) {
  340.         awaitingForResponse = tile;
  341.         setTimeout(() => {
  342.           awaitingForResponse = undefined;
  343.         }, 3000);
  344.       }
  345.       break;
  346.   }
  347. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement