Greasy Fork is available in English.

Donnabots_GOTA_Extender_Production

GOTA_Extender_Production to go with extender

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