Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ------------------------------------------------------------------------------------------------------------------------
- // Plemiona.pl 8.X Skrypt użytkowy
- //
- // Nazwa: EasierVillagesBuild(Prostsze budowanie wiosek)
- // Wersja: Beta 1.0.1
- // Autor: Slupio98
- //
- // Tagi specjalne:
- // License: https://creativecommons.org/licenses/by-nc-nd/4.0/
- // Support: Napisz do mnie w grze :)
- //
- // ------------------------------------------------------------------------------------------------------------------------
- // ==UserScript==
- // @name EasierVillagesBuild
- // @version Beta 1.0.1
- // @description Wyręcza użytkownika z myślenia co ma wybrać do budowy
- // @author Slupio98
- // @downloadURL http://pastebin.com/raw/vfNHQtvJ
- // @updateURL http://pastebin.com/raw/vfNHQtvJ
- // @match https://*.plemiona.pl/game.php?*&screen=main
- // @match http://*.plemiona.pl/game.php?*&screen=main
- // ==/UserScript==
- // ------------------------------------------------------------------------------------------------------------------------
- if (!('contains' in String.prototype)) {
- String.prototype.contains = function (str, startIndex) {
- return -1 !== String.prototype.indexOf.call(this, str, startIndex);
- };
- }
- if (!('getHexEncode' in String.prototype)) {
- String.prototype.getHexEncode = function () {
- var hex, i;
- var result = "";
- for (i = 0; i < this.length; i++) {
- hex = this.charCodeAt(i).toString(16);
- result += ("000" + hex).slice(-4);
- }
- return result;
- };
- }
- if (!('getHexDecode' in String.prototype)) {
- String.prototype.getHexDecode = function () {
- var j;
- var hexes = this.match(/.{1,4}/g) || [];
- var back = "";
- for (j = 0; j < hexes.length; j++) {
- back += String.fromCharCode(parseInt(hexes[j], 16));
- }
- return back;
- };
- }
- if (!('insertChildAtIndex' in String.prototype)) {
- Element.prototype.insertChildAtIndex = function (child, index) {
- if (!index) index = 0;
- if (index >= this.children.length) {
- this.appendChild(child);
- } else {
- this.insertBefore(child, this.children[index]);
- }
- };
- }
- if (!('replaceAll' in String.prototype)) {
- String.prototype.replaceAll = function (token, newToken, ignoreCase) {
- var _token;
- var str = this + "";
- var i = -1;
- if (typeof token === "string") {
- if (ignoreCase) {
- _token = token.toLowerCase();
- while ((
- i = str.toLowerCase().indexOf(
- _token, i >= 0 ? i + newToken.length : 0
- )
- ) !== -1
- ) {
- str = str.substring(0, i) +
- newToken +
- str.substring(i + token.length);
- }
- } else {
- return this.split(token).join(newToken);
- }
- }
- return str;
- };
- }
- function getRandomInt(min, max) {
- var value = Math.floor(Math.random() * (max - min + 1)) + min;
- return value;
- }
- var EasierVillagesBuild = {
- Settings: {
- TimeBetweenBuildingUpgrade: {
- min: 2100,
- max: 3200
- }
- },
- run: function () {
- if (game_data.screen === 'main') {
- this.Graphic.Loader.load();
- }
- },
- Graphic: {
- Loader: {
- load: function () {
- this.ButtonAutoBuild.insert();
- },
- ButtonAutoBuild: {
- BUTTON_ID: 'autoBuild',
- CELL_ID: 'content_value',
- BUTTON_CONTAINER_INDEX: 3,
- insert: function () {
- var containerOfBtnAutoBuild = this.getButtonAutoBuild();
- var cell = this.getAutoBuildCell();
- cell.insertChildAtIndex(containerOfBtnAutoBuild, this.BUTTON_CONTAINER_INDEX);
- this.addSpaceBottom();
- this.addButtonEventListener();
- },
- addButtonEventListener: function () {
- var btn = this.getButton();
- btn.onclick = function () {
- EasierVillagesBuild.Handlers.AutoBuild.onClick();
- };
- },
- getButton: function () {
- var btn = document.getElementById(this.BUTTON_ID);
- return btn;
- },
- addSpaceBottom: function () {
- var NEW_LINE = document.createElement('BR');
- var newLineIndex = this.BUTTON_CONTAINER_INDEX;
- newLineIndex++;
- var cell = this.getAutoBuildCell();
- cell.insertChildAtIndex(NEW_LINE, newLineIndex);
- },
- getAutoBuildCell: function () {
- var btn = document.getElementById(this.CELL_ID);
- return btn;
- },
- getButtonAutoBuild: function () {
- var code = this.getCode();
- var container = document.createElement('DIV');
- container.innerHTML = code;
- return container;
- },
- getCode() {
- var code =
- '<input id="' + this.BUTTON_ID + '" type="submit" class="btn" value="Buduj automatycznie">';
- return code;
- }
- }
- }
- },
- Handlers: {
- AutoBuild: {
- onClick: function () {
- EasierVillagesBuild.Graphic.Loader.ButtonAutoBuild.getButton().disabled = true;
- //important disabled=false for this button is in EasierVillagesBuild.InformationsProcess.runProcess;
- UI.SuccessMessage("Rozpoczynam dodawanie zleceń");
- EasierVillagesBuild.Logic.fillQueue(function () {
- UI.SuccessMessage("Pomyślnie dodano zlecenia do kolejki budowy");
- EasierVillagesBuild.Graphic.Loader.ButtonAutoBuild.getButton().disabled = false;
- });
- }
- },
- },
- Logic: {
- fillQueue: function (callbackOnEnd) {
- var freeSlots = EasierVillagesBuild.Informations.Queue.getFreeSlots();
- if (freeSlots > 0) {
- var process = EasierVillagesBuild.Informations.Process.getProcess();
- EasierVillagesBuild.Informations.Process.clickProcess(
- process,
- function () {
- EasierVillagesBuild.Logic.fillQueue(callbackOnEnd);
- }
- );
- } else {
- callbackOnEnd();
- }
- }
- },
- Informations: {
- Queue: {
- /** @type {Array.<QueueElement>} */
- list: [],
- /** @type {number} */
- getFreeSlots: function () {
- var freeSlots = 2;
- if (game_data.player.premium) {
- freeSlots = 5;
- }
- freeSlots = freeSlots - this.getList().length;
- return freeSlots;
- },
- /** @return {Array.<QueueElement>} */
- getList: function () {
- this.refreshList();
- return this.fastGetList();
- },
- /** @return {Array.<QueueElement>} */
- fastGetList: function () {
- return this.list;
- },
- refreshList: function () {
- this.list = this.getQueue();
- },
- /** @return {Array.<QueueElement>} */
- getQueue: function () {
- var unparseQueueElements = $(document).find('[class*="buildorder_"]');
- var finishedQueue = [];
- for (var i = 0; i < unparseQueueElements.length; i++) {
- var queueElement = unparseQueueElements[i];
- building = this.getBuildingType(queueElement);
- lvl = this.getBuildingLvl(queueElement);
- const finishedElement = new QueueElement(building, lvl);
- finishedQueue[i] = finishedElement;
- }
- return finishedQueue;
- },
- /** @return {string} */
- getBuildingType: function (queueElement) {
- var firstCell = queueElement.cells[0];
- var image = firstCell.children[0];
- var imageURL = image.getAttribute('src');
- var unParseName = imageURL.split('/')[imageURL.split('/').length - 1];
- unParseName = unParseName.replace(/[0-9]/g, '');
- var buildingName = unParseName.replaceAll('.png', '');
- return buildingName;
- },
- /** @return {number} */
- getBuildingLvl: function (queueElement) {
- var firstCell = queueElement.cells[0];
- var unparseLvl = firstCell.childNodes[4].data;
- unparseLvl = unparseLvl.replace(/\D/g, '');
- var lvl = parseInt(unparseLvl);
- return lvl;
- },
- },
- Buildings: {
- /** @type {Object.<String, Buildig>} */
- list: [],
- /** @return {Object.<String, Buildig>} */
- getList: function () {
- this.refreshList();
- return this.fastGetList();
- },
- /** @return {Object.<String, Buildig>} */
- fastGetList: function () {
- return this.list;
- },
- refreshList: function () {
- this.list = this.getBuildings();
- },
- /** @return {Object.<String, Buildig>} */
- getBuildings: function () {
- var unparseBuildings = $(document).find('[id*="main_buildrow_"]');
- var finishedBuildings = {};
- for (var i = 0; i < unparseBuildings.length; i++) {
- var buildingElement = unparseBuildings[i];
- const finishedElement = new Buildig(
- this.getBuildingType(buildingElement),
- this.getBuildingLvl(buildingElement),
- this.getBuildNextLvlLink(buildingElement),
- this.getUpragdeButton(buildingElement)
- );
- finishedBuildings[finishedElement.getBuildingID] = finishedElement;
- }
- return finishedBuildings;
- },
- /** @param {Element} buildingElement
- * @return {object} */
- getUpragdeButton: function (buildingElement) {
- var cells = buildingElement.cells;
- var targetCell = cells[cells.length - 1];
- var children = targetCell.children;
- var button = children[children.length - 1];
- return button;
- },
- /** @param {Element} buildingElement
- * @return {string} */
- getBuildingType: function (buildingElement) {
- var building = buildingElement.getAttribute('id').replaceAll('main_buildrow_', '');
- return building;
- },
- /** @param {Element} buildingElement
- * @return {number} */
- getBuildingLvl: function (buildingElement) {
- var firstCell = buildingElement.cells[0];
- var spanElement = firstCell.children[firstCell.children.length - 1];
- var unparseLvl = spanElement.textContent;
- unparseLvl = unparseLvl.replace(/\D/g, '');
- var lvl = 0;
- if (unparseLvl.length !== 0) {
- lvl = parseInt(unparseLvl);
- }
- return lvl;
- },
- /** @param {Element} buildingElement
- * @return {string} */
- getBuildNextLvlLink: function (buildingElement) {
- var building = this.getBuildingType(buildingElement);
- var lvl = this.getBuildingLvl(buildingElement);
- lvl++;
- var btn = document.getElementById('main_buildlink_' + building + '_' + lvl);
- var href = '';
- if (btn !== undefined && btn !== null) {
- href = btn.getAttribute('href');
- }
- return href;
- },
- },
- Process: {
- /** @type {number} */
- processesInBuild: 0,
- /** @return {Array.<Process>} */
- getProcesses: function () {
- return AB_SETTINGS_MANAGER.getProcesses();
- },
- /** @return {Array.<Process>} */
- getProcess: function () {
- var processList = this.getProcesses();
- var villageBuildings = EasierVillagesBuild.Informations.Buildings.getList();
- for (var i = 0; i < processList.length; i++) {
- var process = processList[i];
- if (process !== null) {
- if (villageBuildings[process.getBuildingID] !== undefined &&
- villageBuildings[process.getBuildingID].getBuildingNextLvl <= process.getBuildingLvl) {
- process.setUpgradeLink = villageBuildings[process.getBuildingID].getBuildLink;
- process.setUpgradeButton = villageBuildings[process.getBuildingID].getUpgradeButton;
- return process;
- }
- }
- }
- return null;
- },
- /** @param {Process} process
- * @param {function} callback
- */
- clickProcess: function (process, callback) {
- process.getUpgradeButton.click();
- var waitTime = getRandomInt(
- EasierVillagesBuild.Settings.TimeBetweenBuildingUpgrade.min,
- EasierVillagesBuild.Settings.TimeBetweenBuildingUpgrade.max
- );
- var freeSlots = EasierVillagesBuild.Informations.Queue.getFreeSlots();
- //must be 1 because free slots are count before packet with success build command arrive to the computer
- if (freeSlots <= 1) {
- EasierVillagesBuild.Graphic.Loader.ButtonAutoBuild.getButton().disabled = false;
- }
- setTimeout(function () {
- callback();
- }, waitTime);
- },
- /** @param {Process} process
- * @param {function} callback
- */
- runProcess: function (process, callback) {
- var link = process.getUpgradeLink;
- var waitTime = getRandomInt(
- EasierVillagesBuild.Settings.TimeBetweenBuildingUpgrade.min,
- EasierVillagesBuild.Settings.TimeBetweenBuildingUpgrade.max
- );
- var freeSlots = EasierVillagesBuild.Informations.Queue.getFreeSlots();
- setTimeout(function () {
- SCRIPT_DEBUGGER.get(link, function (data) {
- callback();
- });
- }, waitTime);
- }
- }
- }
- };
- var AB_SETTINGS_MANAGER = {
- PROCESSES_STORAGE_NAME: 'AB_Processes',
- SCRIPT_NAME: 'AutoBuilder',
- getScriptStorage: function () {
- var localStorage = window.localStorage;
- var scriptStorage = localStorage[AB_SETTINGS_MANAGER.SCRIPT_NAME];
- if (scriptStorage === undefined) {
- scriptStorage = '{}';
- }
- var parsedStorage = JSON.parse(scriptStorage);
- return parsedStorage;
- },
- saveScriptStorage: function (storage) {
- var finishedStorage = JSON.stringify(storage);
- window.localStorage[AB_SETTINGS_MANAGER.SCRIPT_NAME] = finishedStorage;
- },
- saveProcesses: function (processesObject) {
- var scriptStorage = AB_SETTINGS_MANAGER.getScriptStorage();
- scriptStorage[AB_SETTINGS_MANAGER.PROCESSES_STORAGE_NAME] = processesObject;
- AB_SETTINGS_MANAGER.saveScriptStorage(scriptStorage);
- },
- /** @return {Array.<ProcessObject>} */
- getProcesses: function () {
- var scriptStorage = AB_SETTINGS_MANAGER.getScriptStorage();
- var processesObject = scriptStorage[AB_SETTINGS_MANAGER.PROCESSES_STORAGE_NAME];
- var formatedList = [];
- for (var i = 0; i < processesObject.length; i++) {
- var processObj = processesObject[i];
- if (processObj !== null) {
- var process = new ProcessObject(
- processObj.building,
- processObj.lvl
- );
- formatedList.push(process);
- }
- }
- return formatedList;
- }
- };
- var SCRIPT_DEBUGGER = {
- lastGet: new Date().getTime(),
- minWaitTime: 700,
- get: function (link, callback) {
- var now = new Date().getTime();
- var diff = now - this.lastGet;
- var wait = 10;
- if (diff < this.minWaitTime) {
- wait = getRandomInt(this.minWaitTime - diff, 1300 - diff);
- }
- console.log("wait: " + wait);
- setTimeout(function () {
- now = new Date().getTime();
- diff = now - SCRIPT_DEBUGGER.lastGet;
- console.log("time between last get and now: " + diff);
- SCRIPT_DEBUGGER.lastGet = now;
- $.get(link, callback);
- }, wait);
- }
- };
- class QueueElement {
- /**
- * @param {string} buildingID
- * @param {number} buildingLvl
- */
- constructor(buildingID, buildingLvl) {
- this.buildingID = buildingID;
- this.buildingLvl = buildingLvl;
- }
- /** @return {string} */
- get getBuildingID() {
- return this.buildingID;
- }
- /** @param {string} buildingID */
- set setBuildingID(buildingID) {
- this.buildingID = buildingID;
- }
- /** @return {number} */
- get getBuildingLvl() {
- return this.buildingLvl;
- }
- /** @param {number} buildingLvl */
- set setBuildingLvl(buildingLvl) {
- this.buildingLvl = buildingLvl;
- }
- };
- class Buildig {
- /**
- * @param {string} buildingID
- * @param {number} buildingLvl
- * @param {string} nextLvlLink
- */
- constructor(buildingID, buildingLvl, nextLvlLink, upgradeButton) {
- this.buildingID = buildingID;
- this.buildingLvl = buildingLvl;
- this.nextBuildingLvl = buildingLvl + 1;
- this.nextLvlLink = nextLvlLink;
- this.upgradeButton = upgradeButton;
- }
- /** @return {string} */
- get getBuildingID() {
- return this.buildingID;
- }
- /** @param {string} buildingID */
- set setBuildingID(buildingID) {
- this.buildingID = buildingID;
- }
- /** @return {number} */
- get getBuildingLvl() {
- return this.buildingLvl;
- }
- /** @param {number} buildingLvl */
- set setBuildingLvl(buildingLvl) {
- this.buildingLvl = buildingLvl;
- }
- /** @return {number} */
- get getBuildingNextLvl() {
- return this.nextBuildingLvl;
- }
- /** @return {string} */
- get getBuildLink() {
- return this.nextLvlLink;
- }
- /** @param {string} nextLvlLink */
- set setBuildLink(nextLvlLink) {
- this.nextLvlLink = nextLvlLink;
- }
- /** @return {object} */
- get getUpgradeButton() {
- return this.upgradeButton;
- }
- }
- class ProcessObject {
- /**
- * @param {string} buildingID
- * @param {number} buildingLvl
- */
- constructor(buildingID, buildingLvl) {
- this.buildingID = buildingID;
- this.buildingLvl = buildingLvl;
- this.upgradeButton = undefined;
- this.upgradeLink = "";
- }
- /** @return {string} */
- get getBuildingID() {
- return this.buildingID;
- }
- /** @param {string} buildingID */
- set setBuildingID(buildingID) {
- this.buildingID = buildingID;
- }
- /** @return {number} */
- get getBuildingLvl() {
- return this.buildingLvl;
- }
- /** @param {number} buildingLvl */
- set setBuildingLvl(buildingLvl) {
- this.buildingLvl = buildingLvl;
- }
- /** @return {string} */
- get getUpgradeLink() {
- return this.upgradeLink;
- }
- /** @param {string} upgradeLink */
- set setUpgradeLink(upgradeLink) {
- this.upgradeLink = upgradeLink;
- }
- /** @return {object} */
- get getUpgradeButton() {
- return this.upgradeButton;
- }
- /** @param {object} upgradeButton */
- set setUpgradeButton(upgradeButton) {
- this.upgradeButton = upgradeButton;
- }
- }
- EasierVillagesBuild.run();
Add Comment
Please, Sign In to add comment