Donnabots_GOTA_Extender_Production

GOTA_Extender_Production to go with extender

目前为 2015-01-30 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/7612/34265/Donnabots_GOTA_Extender_Production.js

  1. var production = (function ($, localStorage, log, error, buildingBySymbol,
  2. buildingProducing, buildingFinished, buildingBySymbol,
  3. doFinishProduction, userContext, doProduction,
  4. applySelectedUpgrade, buildingUpgrades, inform) {
  5.  
  6.  
  7. // Retrieves production
  8. // queue from localStorage
  9. function init(o) {
  10. _this.queue = localStorage.get("productionQueue", []);
  11.  
  12. _this.config(o);
  13.  
  14. $("#modal_dialogs_top").on('click', '#upgradeQueue', enqueue);
  15. $("#modal_dialogs_top").on('click', 'span.btnwrap.btnmed.equipbtn.queue', enqueue);
  16. $("#credits_roll").on('click', '.tableRow', deleteTableRow);
  17.  
  18. // Attempt production on initialize
  19. attempt();
  20. }
  21.  
  22. function config(o){
  23. //console.debug(o);
  24.  
  25. try {
  26. _this.queueDelay = o.queueDelay * 1E3;
  27. _this.superiorMaterials = o.superiorMaterials;
  28. } catch(e){
  29. error(e);
  30. }
  31. }
  32.  
  33. // Saves the queue locally
  34. // NOTE: do it after every change of the queue!
  35. function persist() {
  36. localStorage.set("productionQueue", _this.queue);
  37. }
  38.  
  39. // Attempts building
  40. // production
  41. function attempt(bSymbol) {
  42.  
  43. if (!_this.queue || _this.queue.length == 0) {
  44. log('Attempted production, but queue was missing or empty. Exiting...', "PRODUCTION");
  45. return;
  46. }
  47.  
  48. var element;
  49. var building;
  50.  
  51. if (bSymbol != void 0) {
  52.  
  53. // Check _this building for production
  54. building = buildingBySymbol(bSymbol);
  55.  
  56. if (buildingProducing(building)) {
  57. log("Building " + building.symbol + " is busy.", "PRODUCTION");
  58. return;
  59. }
  60.  
  61. if (buildingFinished(building)) {
  62. log("Building " + building.symbol + " finished production.", "PRODUCTION");
  63.  
  64. doFinishProduction(building.item_id, function () {
  65. setTimeout(function () {
  66. attempt(building.symbol);
  67. }, _this.queueDelay);
  68. });
  69.  
  70. return;
  71. }
  72.  
  73. element = getElement(building.symbol);
  74. element && executeElement(element);
  75. return;
  76. }
  77.  
  78. for (var i = 0; i < userContext.buildingsData.length; i++) {
  79. building = userContext.buildingsData[i];
  80.  
  81. if (buildingProducing(building)) {
  82. log("Building " + building.symbol + " is busy.", "PRODUCTION");
  83. continue;
  84. }
  85.  
  86. if (buildingFinished(building)) {
  87. log("Building " + building.symbol + " finished production.", "PRODUCTION");
  88. doFinishProduction(building.item_id, function () {
  89. setTimeout(function () {
  90. attempt(building.symbol);
  91. }, _this.queueDelay);
  92. });
  93.  
  94. return;
  95. }
  96.  
  97. element = getElement(building.symbol);
  98. element && executeElement(element,
  99. function () {
  100. attempt();
  101. });
  102. }
  103. }
  104.  
  105. function getElement(bSymbol) {
  106. //if (!_this.queue || _this.queue.length == 0) {
  107. // log('Attempted to extract item from queue, but the production queue was missing or empty. Exiting...', "PRODUCTION");
  108. // return null;
  109. //}
  110.  
  111. var element;
  112.  
  113. for (var i = 0; i < _this.queue.length; i++) {
  114.  
  115. if (_this.queue[i].activeBuildingPanel == bSymbol) {
  116. element = _this.queue[i];
  117. break;
  118. }
  119. }
  120.  
  121. if (!element) {
  122. log('No elements enqueued for building ' + bSymbol + '. Array size: ' + _this.queue.length, "PRODUCTION");
  123. return null;
  124. }
  125.  
  126. return element;
  127. }
  128.  
  129. function executeElement(element, callback) {
  130.  
  131. var index = _this.queue.indexOf(element);
  132. log('Production of element ' + element.name + ' : ' + element.type + ' with index ' + index + ' initiated. ' +
  133. (callback == void 0 ? 'No callback set.' : 'Callback set after production.'), "PRODUCTION");
  134.  
  135. if (element.type == "item") {
  136. userContext.recipeData = element.recipeData;
  137. userContext.activeBuildingPanel = element.activeBuildingPanel;
  138.  
  139. doProduction(element.outputSymbol, element.recipeCategory, null, null, element.recipeName, callback);
  140. _this.queue.splice(index, 1);
  141. persist();
  142.  
  143. log('Production details: ' + element.name + ' at ' + element.activeBuildingPanel + ', ' + element.outputSymbol + ', ' + element.recipeCategory + ', ' + element.recipeName + ';', "PRODUCTION");
  144. } else {
  145.  
  146. var buildingId = buildingBySymbol(element.activeBuildingPanel).id;
  147.  
  148. applySelectedUpgrade({building_id: buildingId, id: element.upgradeId, gold: 0, silver: 0}, null, callback);
  149. _this.queue.splice(index, 1);
  150. persist();
  151.  
  152. log('Production details: ' + element.name + ' : ' + element.type + ' at ' + element.activeBuildingPanel + ', ' + element.symbol + ';', "PRODUCTION");
  153. }
  154. }
  155.  
  156. function removeElement(index) {
  157. if (_this.queue.length == 1) {
  158. _this.queue.pop();
  159. } else {
  160. _this.queue.splice(index, 1);
  161. persist();
  162. }
  163. }
  164.  
  165. function enqueue(e) {
  166. e.preventDefault();
  167.  
  168. try {
  169. var queueingUpgrade = $(this).hasClass('upgradeQueue');
  170. log("Queing " + (queueingUpgrade ? "upgrade." : "item(s)."));
  171.  
  172. if (queueingUpgrade) {
  173.  
  174. var container = $(this).parents('div#selected_upgrade');
  175. var name = $(container).find('h5:first').text();
  176.  
  177. var infoBtm = $(this).parents('div.buildinginfobtm');
  178. var func = $(infoBtm).find('.upgradeicon.active').attr('onclick');
  179. var upgradeImg = $(infoBtm).find('.upgradeicon.active .upgradeiconart img').attr('src');
  180.  
  181. if (func.indexOf("clickSelectUpgrade") == -1) {
  182. error("Cannot resolve upgrade id.");
  183. return;
  184. }
  185.  
  186. // TODO: Improve...
  187. // "return clickSelectUpgrade('7', 'balcony');"
  188. var symbol = func.split("'")[3];
  189. log("Selected " + symbol + " upgrade. Retrieve successful.");
  190.  
  191. var upgradeId;
  192.  
  193. var bUpgrades = buildingUpgrades[userContext.activeBuildingPanel];
  194. for (var j = 0; j < bUpgrades.length; j++) {
  195. if (bUpgrades[j].symbol == symbol) {
  196. upgradeId = bUpgrades[j].id;
  197. break;
  198. }
  199. }
  200.  
  201. if (!upgradeId) {
  202. error("Fatal error, cannot resolve upgrade id.");
  203. return;
  204. }
  205.  
  206. log("Upgrade id resolved: " + upgradeId);
  207.  
  208. var upgrade = {
  209. "name": name,
  210. "upgradeId": upgradeId,
  211. "type": "upgrade",
  212. "symbol": symbol,
  213. "img": upgradeImg,
  214. "activeBuildingPanel": userContext.activeBuildingPanel
  215. };
  216.  
  217. _this.queue.push(upgrade);
  218. persist();
  219.  
  220. log("Pushed upgrade to queue.");
  221.  
  222. } else {
  223.  
  224. // Extract and construct object
  225. var statview = $(this).parents(".statview");
  226. var imgSrc = $(statview).find("div.statviewimg img").attr('src');
  227.  
  228. if (typeof (imgSrc) == "undefined") {
  229. imgSrc = $(statview).find("span.iconview img").attr('src');
  230. }
  231.  
  232. var statViewName = $(statview).find(".statviewname h3").text();
  233. var quantity = $(this).attr("data-quantity");
  234.  
  235. // Extract variables needed
  236. var recipeName;
  237. var recipeData;
  238.  
  239. var source = userContext.productionItemsClick[userContext.currentProductionItem];
  240.  
  241. if (!source) {
  242. error('Failed to extract source production item.');
  243. return;
  244. }
  245.  
  246. for (var i = 0; i < userContext.recipeData.length; i++) {
  247. var r = userContext.recipeData[i];
  248. if (r.output == source.outputSymbol) {
  249. recipeName = r.symbol;
  250. recipeData = [r];
  251. break;
  252. }
  253.  
  254. if (r.success_loot_table && r.success_loot_table == source.outputSymbol) {
  255. recipeName = r.symbol;
  256. recipeData = [r];
  257. break;
  258. }
  259.  
  260. if (r.success_loot_item && r.success_loot_item == source.outputSymbol) {
  261. recipeName = r.symbol;
  262. recipeData = [r];
  263. break;
  264. }
  265. }
  266.  
  267. // Last attempt, these here are expensive operations
  268. if (!recipeName) {
  269. for (var i = 0; i < userContext.recipeData.length; i++) {
  270. var r = userContext.recipeData[i];
  271. var recipeInputs = JSON.stringify(r.input.split(","));
  272. if (JSON.stringify(source.recipeInputs) === recipeInputs) {
  273. recipeName = r.symbol;
  274. recipeData = [r];
  275. break;
  276. }
  277. }
  278. }
  279.  
  280. if (!recipeName) {
  281. error('Failed to extract recipeName.');
  282. return;
  283. }
  284.  
  285. if (!recipeData) {
  286. error('Failed to extract recipeData.');
  287. return;
  288. }
  289.  
  290. log('All needed variables were extracted.');
  291.  
  292. do {
  293.  
  294. // Construct production element
  295. var element = {
  296. "recipeName": recipeName,
  297. "name": statViewName,
  298. "img": imgSrc,
  299. "type": "item",
  300. "outputSymbol": source.outputSymbol,
  301. "recipeCategory": source.recipeCategory,
  302. "recipeData": recipeData,
  303. "activeBuildingPanel": userContext.activeBuildingPanel
  304. };
  305.  
  306. // Insert the element into the queueArray (cloneInto for Mozilla)
  307. //if (typeof (cloneInto) == "function") {
  308. // var elementClone = cloneInto(element, unsafeWindow);
  309. // _this.queue.push(elementClone);
  310. //} else {
  311. // _this.queue.push(element);
  312. //}
  313.  
  314. _this.queue.push(element);
  315. persist();
  316.  
  317. //options._this.queue = _this.queue;
  318. //options.set("_this.queue");
  319.  
  320. quantity--;
  321.  
  322. log('Pushed element to queue.');
  323.  
  324. } while (quantity > 0);
  325. }
  326.  
  327. log('Attempting immediate production...');
  328. attempt(userContext.activeBuildingPanel);
  329. inform('Enqueued.');
  330.  
  331. } catch (err) {
  332. error(err);
  333. }
  334. }
  335.  
  336. function tableRow(i, el) {
  337. return '<tr class="tableRow" style="cursor: pointer">' +
  338. '<td><span class="ranklist colsort">' + i + '</span></td>' +
  339. '<td><span class="ranklist colsort">' + el.type + '</span></td>' +
  340. '<td><span class="name colsort">' + el.activeBuildingPanel + '</span></td>' +
  341. '<td><span class="name colsort">' + el.name + '</span></td>' +
  342. '<td><span class="avatarimg"><img src="' + el.img + '"></span></td>' +
  343. '</tr>';
  344. }
  345.  
  346. function deleteTableRow(e) {
  347. e.preventDefault();
  348.  
  349. try {
  350. var index = $(this).find("td:first span.ranklist").text();
  351.  
  352. log("Attempting to delete element with index " + index + " from the queue array.");
  353.  
  354. removeElement(index);
  355. render();
  356.  
  357. } catch (err) {
  358. error(err);
  359. }
  360.  
  361. }
  362.  
  363. function render() {
  364.  
  365. log("Rendering production queue table.");
  366.  
  367. var qTable = $("#queueTable");
  368. if (qTable.length == 0) {
  369. error("Can't find queue table! Rendering production items failed.");
  370. return;
  371. }
  372.  
  373. // Clear table from any rows first
  374. $("#queueTable .tableRow").each(function () {
  375. $(this).remove();
  376. });
  377.  
  378. if (!_this.queue || _this.queue.length == 0) {
  379. log("No queue was found to render.");
  380. return;
  381. }
  382.  
  383. // Render items
  384. for (var i = 0; i < _this.queue.length; i++) {
  385. $("#headerRow").after(tableRow(i, _this.queue[i]));
  386. }
  387.  
  388. log("Production queue rendered " + _this.queue.length + " items.");
  389. }
  390.  
  391. var _this = {
  392. init: init,
  393. attempt: attempt,
  394. persist: persist,
  395. enqueue: enqueue,
  396. render: render,
  397. config: config,
  398. getElement: getElement,
  399. removeElement: removeElement,
  400. executeElement: executeElement,
  401.  
  402. queue: [],
  403. queueDelay: 4E3,
  404. superiorMaterials: true
  405. }
  406. return _this;
  407.  
  408. }($, localStorage, log, error, buildingBySymbol,
  409. buildingProducing, buildingFinished, buildingBySymbol,
  410. doFinishProduction, userContext, doProduction,
  411. applySelectedUpgrade, buildingUpgrades, inform));