Slupik98

[PLEMIONA] SimpleVillageBuilder

Jan 7th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.64 KB | None | 0 0
  1. // ------------------------------------------------------------------------------------------------------------------------
  2. // Plemiona.pl 8.X Skrypt użytkowy
  3. //
  4. // Nazwa: EasierVillagesBuild(Prostsze budowanie wiosek)
  5. // Wersja: Beta 1.0.1
  6. // Autor: Slupio98
  7. //
  8. // Tagi specjalne:
  9. // License: https://creativecommons.org/licenses/by-nc-nd/4.0/
  10. // Support: Napisz do mnie w grze :)
  11. //
  12. // ------------------------------------------------------------------------------------------------------------------------
  13. // ==UserScript==
  14. // @name EasierVillagesBuild
  15. // @version Beta 1.0.1
  16. // @description Wyręcza użytkownika z myślenia co ma wybrać do budowy
  17. // @author Slupio98
  18. // @downloadURL http://pastebin.com/raw/vfNHQtvJ
  19. // @updateURL http://pastebin.com/raw/vfNHQtvJ
  20. // @match https://*.plemiona.pl/game.php?*&screen=main
  21. // @match http://*.plemiona.pl/game.php?*&screen=main
  22. // ==/UserScript==
  23. // ------------------------------------------------------------------------------------------------------------------------
  24.  
  25. if (!('contains' in String.prototype)) {
  26. String.prototype.contains = function (str, startIndex) {
  27. return -1 !== String.prototype.indexOf.call(this, str, startIndex);
  28. };
  29. }
  30. if (!('getHexEncode' in String.prototype)) {
  31. String.prototype.getHexEncode = function () {
  32. var hex, i;
  33.  
  34. var result = "";
  35. for (i = 0; i < this.length; i++) {
  36. hex = this.charCodeAt(i).toString(16);
  37. result += ("000" + hex).slice(-4);
  38. }
  39.  
  40. return result;
  41. };
  42. }
  43. if (!('getHexDecode' in String.prototype)) {
  44. String.prototype.getHexDecode = function () {
  45. var j;
  46. var hexes = this.match(/.{1,4}/g) || [];
  47. var back = "";
  48. for (j = 0; j < hexes.length; j++) {
  49. back += String.fromCharCode(parseInt(hexes[j], 16));
  50. }
  51.  
  52. return back;
  53. };
  54. }
  55. if (!('insertChildAtIndex' in String.prototype)) {
  56. Element.prototype.insertChildAtIndex = function (child, index) {
  57. if (!index) index = 0;
  58. if (index >= this.children.length) {
  59. this.appendChild(child);
  60. } else {
  61. this.insertBefore(child, this.children[index]);
  62. }
  63. };
  64. }
  65. if (!('replaceAll' in String.prototype)) {
  66. String.prototype.replaceAll = function (token, newToken, ignoreCase) {
  67. var _token;
  68. var str = this + "";
  69. var i = -1;
  70.  
  71. if (typeof token === "string") {
  72. if (ignoreCase) {
  73. _token = token.toLowerCase();
  74.  
  75. while ((
  76. i = str.toLowerCase().indexOf(
  77. _token, i >= 0 ? i + newToken.length : 0
  78. )
  79. ) !== -1
  80. ) {
  81. str = str.substring(0, i) +
  82. newToken +
  83. str.substring(i + token.length);
  84. }
  85. } else {
  86. return this.split(token).join(newToken);
  87. }
  88. }
  89. return str;
  90. };
  91. }
  92. function getRandomInt(min, max) {
  93. var value = Math.floor(Math.random() * (max - min + 1)) + min;
  94. return value;
  95. }
  96.  
  97. var EasierVillagesBuild = {
  98. Settings: {
  99. TimeBetweenBuildingUpgrade: {
  100. min: 2100,
  101. max: 3200
  102. }
  103. },
  104. run: function () {
  105. if (game_data.screen === 'main') {
  106. this.Graphic.Loader.load();
  107. }
  108. },
  109. Graphic: {
  110. Loader: {
  111. load: function () {
  112. this.ButtonAutoBuild.insert();
  113. },
  114. ButtonAutoBuild: {
  115. BUTTON_ID: 'autoBuild',
  116. CELL_ID: 'content_value',
  117. BUTTON_CONTAINER_INDEX: 3,
  118. insert: function () {
  119. var containerOfBtnAutoBuild = this.getButtonAutoBuild();
  120. var cell = this.getAutoBuildCell();
  121. cell.insertChildAtIndex(containerOfBtnAutoBuild, this.BUTTON_CONTAINER_INDEX);
  122. this.addSpaceBottom();
  123.  
  124. this.addButtonEventListener();
  125. },
  126. addButtonEventListener: function () {
  127. var btn = this.getButton();
  128. btn.onclick = function () {
  129. EasierVillagesBuild.Handlers.AutoBuild.onClick();
  130. };
  131. },
  132. getButton: function () {
  133. var btn = document.getElementById(this.BUTTON_ID);
  134. return btn;
  135. },
  136. addSpaceBottom: function () {
  137. var NEW_LINE = document.createElement('BR');
  138. var newLineIndex = this.BUTTON_CONTAINER_INDEX;
  139. newLineIndex++;
  140.  
  141. var cell = this.getAutoBuildCell();
  142. cell.insertChildAtIndex(NEW_LINE, newLineIndex);
  143. },
  144. getAutoBuildCell: function () {
  145. var btn = document.getElementById(this.CELL_ID);
  146. return btn;
  147. },
  148. getButtonAutoBuild: function () {
  149. var code = this.getCode();
  150. var container = document.createElement('DIV');
  151. container.innerHTML = code;
  152. return container;
  153. },
  154. getCode() {
  155. var code =
  156. '<input id="' + this.BUTTON_ID + '" type="submit" class="btn" value="Buduj automatycznie">';
  157. return code;
  158. }
  159. }
  160. }
  161. },
  162. Handlers: {
  163. AutoBuild: {
  164. onClick: function () {
  165. EasierVillagesBuild.Graphic.Loader.ButtonAutoBuild.getButton().disabled = true;
  166. //important disabled=false for this button is in EasierVillagesBuild.InformationsProcess.runProcess;
  167. UI.SuccessMessage("Rozpoczynam dodawanie zleceń");
  168. EasierVillagesBuild.Logic.fillQueue(function () {
  169. UI.SuccessMessage("Pomyślnie dodano zlecenia do kolejki budowy");
  170. EasierVillagesBuild.Graphic.Loader.ButtonAutoBuild.getButton().disabled = false;
  171. });
  172. }
  173. },
  174. },
  175. Logic: {
  176. fillQueue: function (callbackOnEnd) {
  177. var freeSlots = EasierVillagesBuild.Informations.Queue.getFreeSlots();
  178. if (freeSlots > 0) {
  179. var process = EasierVillagesBuild.Informations.Process.getProcess();
  180. EasierVillagesBuild.Informations.Process.clickProcess(
  181. process,
  182. function () {
  183. EasierVillagesBuild.Logic.fillQueue(callbackOnEnd);
  184. }
  185. );
  186. } else {
  187. callbackOnEnd();
  188. }
  189. }
  190. },
  191. Informations: {
  192. Queue: {
  193. /** @type {Array.<QueueElement>} */
  194. list: [],
  195. /** @type {number} */
  196. getFreeSlots: function () {
  197. var freeSlots = 2;
  198. if (game_data.player.premium) {
  199. freeSlots = 5;
  200. }
  201. freeSlots = freeSlots - this.getList().length;
  202. return freeSlots;
  203. },
  204. /** @return {Array.<QueueElement>} */
  205. getList: function () {
  206. this.refreshList();
  207. return this.fastGetList();
  208. },
  209. /** @return {Array.<QueueElement>} */
  210. fastGetList: function () {
  211. return this.list;
  212. },
  213. refreshList: function () {
  214. this.list = this.getQueue();
  215. },
  216. /** @return {Array.<QueueElement>} */
  217. getQueue: function () {
  218. var unparseQueueElements = $(document).find('[class*="buildorder_"]');
  219.  
  220. var finishedQueue = [];
  221. for (var i = 0; i < unparseQueueElements.length; i++) {
  222. var queueElement = unparseQueueElements[i];
  223.  
  224. building = this.getBuildingType(queueElement);
  225. lvl = this.getBuildingLvl(queueElement);
  226. const finishedElement = new QueueElement(building, lvl);
  227.  
  228. finishedQueue[i] = finishedElement;
  229. }
  230. return finishedQueue;
  231. },
  232. /** @return {string} */
  233. getBuildingType: function (queueElement) {
  234. var firstCell = queueElement.cells[0];
  235. var image = firstCell.children[0];
  236. var imageURL = image.getAttribute('src');
  237. var unParseName = imageURL.split('/')[imageURL.split('/').length - 1];
  238. unParseName = unParseName.replace(/[0-9]/g, '');
  239. var buildingName = unParseName.replaceAll('.png', '');
  240.  
  241. return buildingName;
  242. },
  243. /** @return {number} */
  244. getBuildingLvl: function (queueElement) {
  245. var firstCell = queueElement.cells[0];
  246. var unparseLvl = firstCell.childNodes[4].data;
  247. unparseLvl = unparseLvl.replace(/\D/g, '');
  248. var lvl = parseInt(unparseLvl);
  249.  
  250. return lvl;
  251. },
  252. },
  253. Buildings: {
  254. /** @type {Object.<String, Buildig>} */
  255. list: [],
  256. /** @return {Object.<String, Buildig>} */
  257. getList: function () {
  258. this.refreshList();
  259. return this.fastGetList();
  260. },
  261. /** @return {Object.<String, Buildig>} */
  262. fastGetList: function () {
  263. return this.list;
  264. },
  265. refreshList: function () {
  266. this.list = this.getBuildings();
  267. },
  268. /** @return {Object.<String, Buildig>} */
  269. getBuildings: function () {
  270. var unparseBuildings = $(document).find('[id*="main_buildrow_"]');
  271.  
  272. var finishedBuildings = {};
  273. for (var i = 0; i < unparseBuildings.length; i++) {
  274. var buildingElement = unparseBuildings[i];
  275. const finishedElement = new Buildig(
  276. this.getBuildingType(buildingElement),
  277. this.getBuildingLvl(buildingElement),
  278. this.getBuildNextLvlLink(buildingElement),
  279. this.getUpragdeButton(buildingElement)
  280. );
  281.  
  282. finishedBuildings[finishedElement.getBuildingID] = finishedElement;
  283. }
  284. return finishedBuildings;
  285. },
  286. /** @param {Element} buildingElement
  287. * @return {object} */
  288. getUpragdeButton: function (buildingElement) {
  289. var cells = buildingElement.cells;
  290. var targetCell = cells[cells.length - 1];
  291. var children = targetCell.children;
  292. var button = children[children.length - 1];
  293. return button;
  294. },
  295. /** @param {Element} buildingElement
  296. * @return {string} */
  297. getBuildingType: function (buildingElement) {
  298. var building = buildingElement.getAttribute('id').replaceAll('main_buildrow_', '');
  299.  
  300. return building;
  301. },
  302. /** @param {Element} buildingElement
  303. * @return {number} */
  304. getBuildingLvl: function (buildingElement) {
  305. var firstCell = buildingElement.cells[0];
  306. var spanElement = firstCell.children[firstCell.children.length - 1];
  307. var unparseLvl = spanElement.textContent;
  308. unparseLvl = unparseLvl.replace(/\D/g, '');
  309. var lvl = 0;
  310. if (unparseLvl.length !== 0) {
  311. lvl = parseInt(unparseLvl);
  312. }
  313.  
  314. return lvl;
  315. },
  316. /** @param {Element} buildingElement
  317. * @return {string} */
  318. getBuildNextLvlLink: function (buildingElement) {
  319. var building = this.getBuildingType(buildingElement);
  320. var lvl = this.getBuildingLvl(buildingElement);
  321. lvl++;
  322. var btn = document.getElementById('main_buildlink_' + building + '_' + lvl);
  323. var href = '';
  324. if (btn !== undefined && btn !== null) {
  325. href = btn.getAttribute('href');
  326. }
  327.  
  328. return href;
  329. },
  330. },
  331. Process: {
  332. /** @type {number} */
  333. processesInBuild: 0,
  334. /** @return {Array.<Process>} */
  335. getProcesses: function () {
  336. return AB_SETTINGS_MANAGER.getProcesses();
  337. },
  338. /** @return {Array.<Process>} */
  339. getProcess: function () {
  340. var processList = this.getProcesses();
  341. var villageBuildings = EasierVillagesBuild.Informations.Buildings.getList();
  342. for (var i = 0; i < processList.length; i++) {
  343. var process = processList[i];
  344. if (process !== null) {
  345. if (villageBuildings[process.getBuildingID] !== undefined &&
  346. villageBuildings[process.getBuildingID].getBuildingNextLvl <= process.getBuildingLvl) {
  347. process.setUpgradeLink = villageBuildings[process.getBuildingID].getBuildLink;
  348. process.setUpgradeButton = villageBuildings[process.getBuildingID].getUpgradeButton;
  349. return process;
  350. }
  351. }
  352. }
  353. return null;
  354. },
  355. /** @param {Process} process
  356. * @param {function} callback
  357. */
  358. clickProcess: function (process, callback) {
  359. process.getUpgradeButton.click();
  360. var waitTime = getRandomInt(
  361. EasierVillagesBuild.Settings.TimeBetweenBuildingUpgrade.min,
  362. EasierVillagesBuild.Settings.TimeBetweenBuildingUpgrade.max
  363. );
  364. var freeSlots = EasierVillagesBuild.Informations.Queue.getFreeSlots();
  365. //must be 1 because free slots are count before packet with success build command arrive to the computer
  366. if (freeSlots <= 1) {
  367. EasierVillagesBuild.Graphic.Loader.ButtonAutoBuild.getButton().disabled = false;
  368. }
  369. setTimeout(function () {
  370. callback();
  371. }, waitTime);
  372. },
  373. /** @param {Process} process
  374. * @param {function} callback
  375. */
  376. runProcess: function (process, callback) {
  377. var link = process.getUpgradeLink;
  378. var waitTime = getRandomInt(
  379. EasierVillagesBuild.Settings.TimeBetweenBuildingUpgrade.min,
  380. EasierVillagesBuild.Settings.TimeBetweenBuildingUpgrade.max
  381. );
  382. var freeSlots = EasierVillagesBuild.Informations.Queue.getFreeSlots();
  383. setTimeout(function () {
  384. SCRIPT_DEBUGGER.get(link, function (data) {
  385. callback();
  386. });
  387. }, waitTime);
  388. }
  389. }
  390. }
  391. };
  392.  
  393. var AB_SETTINGS_MANAGER = {
  394. PROCESSES_STORAGE_NAME: 'AB_Processes',
  395. SCRIPT_NAME: 'AutoBuilder',
  396. getScriptStorage: function () {
  397. var localStorage = window.localStorage;
  398. var scriptStorage = localStorage[AB_SETTINGS_MANAGER.SCRIPT_NAME];
  399. if (scriptStorage === undefined) {
  400. scriptStorage = '{}';
  401. }
  402. var parsedStorage = JSON.parse(scriptStorage);
  403. return parsedStorage;
  404. },
  405. saveScriptStorage: function (storage) {
  406. var finishedStorage = JSON.stringify(storage);
  407. window.localStorage[AB_SETTINGS_MANAGER.SCRIPT_NAME] = finishedStorage;
  408. },
  409. saveProcesses: function (processesObject) {
  410. var scriptStorage = AB_SETTINGS_MANAGER.getScriptStorage();
  411. scriptStorage[AB_SETTINGS_MANAGER.PROCESSES_STORAGE_NAME] = processesObject;
  412.  
  413. AB_SETTINGS_MANAGER.saveScriptStorage(scriptStorage);
  414. },
  415. /** @return {Array.<ProcessObject>} */
  416. getProcesses: function () {
  417. var scriptStorage = AB_SETTINGS_MANAGER.getScriptStorage();
  418. var processesObject = scriptStorage[AB_SETTINGS_MANAGER.PROCESSES_STORAGE_NAME];
  419. var formatedList = [];
  420. for (var i = 0; i < processesObject.length; i++) {
  421. var processObj = processesObject[i];
  422. if (processObj !== null) {
  423. var process = new ProcessObject(
  424. processObj.building,
  425. processObj.lvl
  426. );
  427. formatedList.push(process);
  428. }
  429. }
  430. return formatedList;
  431. }
  432. };
  433.  
  434. var SCRIPT_DEBUGGER = {
  435. lastGet: new Date().getTime(),
  436. minWaitTime: 700,
  437. get: function (link, callback) {
  438. var now = new Date().getTime();
  439. var diff = now - this.lastGet;
  440. var wait = 10;
  441.  
  442. if (diff < this.minWaitTime) {
  443. wait = getRandomInt(this.minWaitTime - diff, 1300 - diff);
  444. }
  445. console.log("wait: " + wait);
  446. setTimeout(function () {
  447. now = new Date().getTime();
  448. diff = now - SCRIPT_DEBUGGER.lastGet;
  449. console.log("time between last get and now: " + diff);
  450. SCRIPT_DEBUGGER.lastGet = now;
  451. $.get(link, callback);
  452. }, wait);
  453. }
  454. };
  455.  
  456. class QueueElement {
  457. /**
  458. * @param {string} buildingID
  459. * @param {number} buildingLvl
  460. */
  461. constructor(buildingID, buildingLvl) {
  462. this.buildingID = buildingID;
  463. this.buildingLvl = buildingLvl;
  464. }
  465.  
  466. /** @return {string} */
  467. get getBuildingID() {
  468. return this.buildingID;
  469. }
  470. /** @param {string} buildingID */
  471. set setBuildingID(buildingID) {
  472. this.buildingID = buildingID;
  473. }
  474.  
  475. /** @return {number} */
  476. get getBuildingLvl() {
  477. return this.buildingLvl;
  478. }
  479. /** @param {number} buildingLvl */
  480. set setBuildingLvl(buildingLvl) {
  481. this.buildingLvl = buildingLvl;
  482. }
  483. };
  484.  
  485. class Buildig {
  486. /**
  487. * @param {string} buildingID
  488. * @param {number} buildingLvl
  489. * @param {string} nextLvlLink
  490. */
  491. constructor(buildingID, buildingLvl, nextLvlLink, upgradeButton) {
  492. this.buildingID = buildingID;
  493. this.buildingLvl = buildingLvl;
  494. this.nextBuildingLvl = buildingLvl + 1;
  495. this.nextLvlLink = nextLvlLink;
  496. this.upgradeButton = upgradeButton;
  497. }
  498.  
  499. /** @return {string} */
  500. get getBuildingID() {
  501. return this.buildingID;
  502. }
  503. /** @param {string} buildingID */
  504. set setBuildingID(buildingID) {
  505. this.buildingID = buildingID;
  506. }
  507.  
  508. /** @return {number} */
  509. get getBuildingLvl() {
  510. return this.buildingLvl;
  511. }
  512. /** @param {number} buildingLvl */
  513. set setBuildingLvl(buildingLvl) {
  514. this.buildingLvl = buildingLvl;
  515. }
  516.  
  517. /** @return {number} */
  518. get getBuildingNextLvl() {
  519. return this.nextBuildingLvl;
  520. }
  521.  
  522. /** @return {string} */
  523. get getBuildLink() {
  524. return this.nextLvlLink;
  525. }
  526. /** @param {string} nextLvlLink */
  527. set setBuildLink(nextLvlLink) {
  528. this.nextLvlLink = nextLvlLink;
  529. }
  530.  
  531. /** @return {object} */
  532. get getUpgradeButton() {
  533. return this.upgradeButton;
  534. }
  535. }
  536.  
  537. class ProcessObject {
  538. /**
  539. * @param {string} buildingID
  540. * @param {number} buildingLvl
  541. */
  542. constructor(buildingID, buildingLvl) {
  543. this.buildingID = buildingID;
  544. this.buildingLvl = buildingLvl;
  545. this.upgradeButton = undefined;
  546. this.upgradeLink = "";
  547. }
  548.  
  549. /** @return {string} */
  550. get getBuildingID() {
  551. return this.buildingID;
  552. }
  553. /** @param {string} buildingID */
  554. set setBuildingID(buildingID) {
  555. this.buildingID = buildingID;
  556. }
  557.  
  558. /** @return {number} */
  559. get getBuildingLvl() {
  560. return this.buildingLvl;
  561. }
  562. /** @param {number} buildingLvl */
  563. set setBuildingLvl(buildingLvl) {
  564. this.buildingLvl = buildingLvl;
  565. }
  566.  
  567. /** @return {string} */
  568. get getUpgradeLink() {
  569. return this.upgradeLink;
  570. }
  571. /** @param {string} upgradeLink */
  572. set setUpgradeLink(upgradeLink) {
  573. this.upgradeLink = upgradeLink;
  574. }
  575.  
  576. /** @return {object} */
  577. get getUpgradeButton() {
  578. return this.upgradeButton;
  579. }
  580. /** @param {object} upgradeButton */
  581. set setUpgradeButton(upgradeButton) {
  582. this.upgradeButton = upgradeButton;
  583. }
  584. }
  585. EasierVillagesBuild.run();
Add Comment
Please, Sign In to add comment