Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Resources helper
- // @namespace http://tampermonkey.net/
- // @version 0.2
- // @description Zaktualizowana wersja dodatku Czogiego na potrzeby Portu Verdent
- // @author Iradap
- // @match https://alkatria.pl/game
- // @grant none
- // ==/UserScript==
- //Timery surowców
- //Nazwa odnowa/wyczerpanie
- //Palma 30s/75s
- //Dąb 60s/150s
- //Sosna 90s/180s
- //Miedź 30s/75s
- //Żelazo 60s/150s
- //Srebro 90s/180s
- //Łowisko N/A/120s
- const resources = [];
- var awaitingForResponse = undefined;
- var timersController = undefined;
- const toolNames = {
- Siek: "cutting",
- Wędk: "fishing",
- Noży: "gathering",
- Kilo: "mining"
- };
- const respawnTimers = {
- Pal: 1200,
- Dęb: 1200,
- Sos: 1200,
- Klo: 1200,
- Mie: 1200,
- Żel: 1200,
- Sre: 1200,
- Zło: 1200
- };
- class TimersController {
- constructor() {
- this.lsName = "resourcesTimers";
- this.timers = {};
- }
- removeTimer(x, y, type) {
- this.removeLs(`${map.current_map},${x},${y},${type}`, 0);
- delete this.timers[`${map.current_map},${x},${y},${type}`];
- }
- setTimer(timeout, x, y, type) {
- this.timers[`${map.current_map},${x},${y},${type}`] = new Timer(
- timeout,
- x,
- y,
- type
- );
- }
- getLs(name, val) {
- const mainObj = JSON.parse(localStorage[this.lsName]);
- if (!mainObj[name]) {
- return val;
- }
- return mainObj[name];
- }
- setLs(name, val) {
- const mainObj = JSON.parse(localStorage[this.lsName]);
- mainObj[name] = val;
- localStorage.setItem(this.lsName, JSON.stringify(mainObj));
- }
- removeLs(name) {
- const mainObj = JSON.parse(localStorage[this.lsName]);
- delete mainObj[name];
- localStorage.setItem(this.lsName, JSON.stringify(mainObj));
- }
- checkLs() {
- if (!localStorage[this.lsName]) {
- localStorage.setItem(this.lsName, "{}");
- return;
- }
- const timers = JSON.parse(localStorage[this.lsName]);
- for (const [id, timer] of Object.entries(timers)) {
- const cords = id.split(",");
- cords.shift();
- if (!timer) {
- this.removeLs(...cords);
- continue;
- }
- const unix = Date.now();
- if (unix > timer) {
- this.removeTimer(...cords);
- continue;
- }
- this.setTimer(timer - unix, ...cords);
- }
- }
- }
- class Timer extends TimersController {
- constructor(timer, x, y, type) {
- super();
- this.time = Date.now() + timer;
- if (this.startTimer(x, y, type)) {
- this.setLs(`${map.current_map},${x},${y},${type}`, this.time);
- }
- }
- async startTimer(x, y, type) {
- const tile = document.querySelector(`#tile_${x}-${y}`);
- if (!tile) {
- return false;
- }
- this.timer = document.createElement("p");
- this.timer.style.textAlign = "center";
- this.timer.style.top = tile.childElementCount === 0 ? "45%" : "55%";
- this.timer.style.left = "25%";
- this.timer.style.position = "absolute";
- this.timer.style.textShadow = "2px 2px 5px Aqua";
- tile.appendChild(this.timer);
- while (this.time > Date.now()) {
- this.timer.innerText = type + ":" + getTimer(this.time - Date.now());
- await sleep(1000);
- }
- this.timer.remove();
- if (tile.childElementCount === 1) {
- tile.childNodes[0].style.top = "50%";
- }
- this.removeTimer(x, y, type);
- }
- }
- const getTimer = (unix) => {
- let seconds = (~~(unix / 1000) % 60).toString();
- let minutes = (~~(~~(unix / 1000) / 60) % 60).toString();
- seconds = seconds.length === 2 ? seconds : "0" + seconds;
- minutes = minutes.length === 2 ? minutes : "0" + minutes;
- return `${minutes}:${seconds}`;
- };
- const oldParse = game.parseServerPacket;
- const oldLoad = map.loadMap;
- const oldSend = game.sendPacket;
- const sleep = (ms) => {
- return new Promise((resolve) => setTimeout(resolve, ms));
- };
- const getDistance = (a, b) => {
- return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
- };
- const equipTool = async (name) => {
- if (document.querySelector(".player-equipment")) {
- document.querySelector("#icon-character").click();
- await sleep(100);
- document.querySelector("#icon-character").click();
- } else {
- document.querySelector("#icon-character").click();
- }
- await sleep(300);
- const item = Array.from(
- document.querySelector(".player-backpack").children
- ).find((el) => {
- if (el.dataset?.tipItem) {
- const tip = el.dataset.tipItem;
- return (
- tip.includes("Doświadczonego") &&
- tip.includes(name) &&
- !tip.includes("Złam")
- );
- }
- });
- if (item) {
- game.sendPacket("equip_item", {
- from: item.dataset.slot,
- type: "backpack"
- });
- }
- document.querySelector("#icon-character").click();
- return item !== undefined;
- };
- const sortReesources = () => {
- return resources.sort((a, b) => {
- if (getDistance(window.player, a) < getDistance(window.player, b)) {
- return -1;
- } else if (getDistance(window.player, a) > getDistance(window.player, b)) {
- return 1;
- }
- return 0;
- });
- };
- const isCloseEnough = (a, b) => {
- const dist = getDistance(a, b);
- return dist == 1
- ? true
- : dist === 2
- ? Math.abs(a.x - b.x) <= 1 && Math.abs(a.y - b.y) <= 1
- : false;
- };
- window.document.addEventListener("keydown", (eventData) => {
- if (eventData.code !== "Space") {
- return;
- }
- var closestResource = sortReesources();
- const action = $(
- "#tile_" + closestResource[0].x + "-" + closestResource[0].y + "_tip"
- ).data("action");
- if (action && action === player.current_element) {
- player.current_element = "";
- $("#my-character").html("");
- return;
- }
- closestResource = closestResource.filter((resource) => {
- return (
- !timersController.getLs(
- `${map.current_map},${resource.x},${resource.y},E`,
- 0
- ) &&
- !timersController.getLs(
- `${map.current_map},${resource.x},${resource.y},R`,
- 0
- )
- );
- });
- if (!closestResource.length) {
- return;
- }
- closestResource = closestResource[0];
- if (
- !closestResource ||
- (!isCloseEnough(window.player, closestResource) &&
- closestResource.action !== "fishing")
- ) {
- return;
- }
- if (getDistance(player, closestResource) > 3) {
- return;
- }
- window.map.useElement(closestResource.x, closestResource.y);
- });
- const secondsToMili = (sec) => {
- return sec * 1000;
- };
- game.parseServerPacket = async function (data) {
- oldParse.apply(this, arguments);
- if (data.code === 1016) {
- if (data.message.includes("Musisz założyć")) {
- const toolName = data.message.split(" ")[2].slice(0, 4);
- const didEquip = await equipTool(toolName);
- const closestTileOfTool = sortReesources().find((tile) => {
- return (
- tile.action === toolNames[toolName] ||
- (window.player.current_element &&
- tile.action === window.player.current_element)
- );
- });
- if (didEquip) {
- game.sendPacket(879, {
- x: closestTileOfTool.x,
- y: closestTileOfTool.y
- });
- }
- }
- if (
- data.message === "Przedmiot został złamany" ||
- data.message === "Niepowodzenie!"
- ) {
- if (awaitingForResponse) {
- timersController.setTimer(
- awaitingForResponse.timeout,
- awaitingForResponse.x,
- awaitingForResponse.y,
- "E"
- );
- awaitingForResponse = undefined;
- }
- }
- }
- if (data.code === "exhaust_tile" && awaitingForResponse) {
- if (data.x === awaitingForResponse.x && data.y === awaitingForResponse.y) {
- timersController.setTimer(
- awaitingForResponse.timeout,
- awaitingForResponse.x,
- awaitingForResponse.y,
- "E"
- );
- const exhausted = resources.find((resource) => {
- return resource.x === data.x && resource.y === data.y;
- });
- const respawn = respawnTimers[exhausted.name.split(" ")[1].slice(0, 3)];
- if (respawn) {
- timersController.setTimer(
- secondsToMili(respawn),
- exhausted.x,
- exhausted.y,
- "R"
- );
- }
- awaitingForResponse = undefined;
- }
- }
- };
- map.loadMap = function (data) {
- oldLoad.apply(this, arguments);
- for (const tile of data.tiles) {
- if (!tile.action || tile.action === "healing") {
- continue;
- }
- resources.push(tile);
- }
- timersController = new TimersController();
- timersController.checkLs();
- };
- game.sendPacket = function (code, data) {
- oldSend.apply(this, arguments);
- switch (code) {
- case 879:
- const tile = sortReesources().find((tile) => {
- return data.x === tile.x && data.y === tile.y;
- });
- if (
- tile.action === "fishing" &&
- !timersController.getLs(`${map.current_map},${tile.x},${tile.y},E`, 0)
- ) {
- timersController.setTimer(tile.timeout, tile.x, tile.y, "E");
- return;
- }
- if (tile) {
- awaitingForResponse = tile;
- setTimeout(() => {
- awaitingForResponse = undefined;
- }, 3000);
- }
- break;
- }
- };
Add Comment
Please, Sign In to add comment