functions

test purposes. code is not mine - only for private use

このスクリプトは単体で利用できません。右のようなメタデータを含むスクリプトから、ライブラリとして読み込まれます: // @require https://update.sleazyfork.org/scripts/14254/122090/functions.js

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください。
  1. Array.prototype.contains = function(val){
  2. return this.indexOf(val) > -1;
  3. };
  4.  
  5. var extender = {
  6. options: {},
  7. command: function (name, args) {
  8. var cmd = { name: name, args: args };
  9. $("textarea#observable").attr("command", JSON.stringify(cmd));
  10. },
  11. lastcommand: function () {
  12. var observable = $("textarea#observable");
  13. var lcmd = observable.attr("command");
  14. observable.val(lcmd);
  15. return lcmd;
  16. },
  17. option: function (option, val) {
  18. if (typeof option != "string") {
  19. error("Please specify name for the option.", "COMMAND");
  20. return;
  21. }
  22.  
  23. val != void 0
  24. ? this.command("option", [option, val])
  25. : this.command("option", [option]);
  26. },
  27. collect: function(){
  28. this.command("collect");
  29. },
  30. import: function(){
  31. this.command("import")
  32. }
  33. };
  34.  
  35. /*
  36. //v100++>>
  37. function addToParty(){
  38. var ssToAdd=[];
  39. $("#ss_Table tr.selected td:first-child").each(function()
  40. {
  41. log("enter success"+ $(this).text());
  42. ssToAdd.push(($(this).text());
  43. }
  44. ssAddToAdventureParty($(this).text(),false);
  45. });
  46. }
  47. //100++<<
  48. */
  49. //v101++>>
  50. function addToParty(){
  51. var ssToAdd=[];
  52. $("#ss_Table tr.selected td:first-child").each(function()
  53. {
  54. log("enter success"+ $(this).text());
  55. ssToAdd.push($(this).text());
  56. });
  57. ssAddToAdventureParty_2(ssToAdd,false);
  58. }
  59.  
  60. function ssAddToAdventureParty_2(ssToAdd,a)
  61. {
  62. if (ssToAdd.length===0) {
  63. return;
  64. }
  65. var b=ssToAdd.pop();
  66. !1==a&&(closeAdventureDialog(),prepareAdvPartyTimeout());
  67. $.ajax({
  68. url:"/play/batch_add_sworn_sword?batch_type=1&ss_id="+b+"&nonce="+userContext.advparty_nonce,dataType:"JSON",
  69. success:function(a){
  70. showAdvPartyResponse(a);
  71. isWeb()&&chooseAvASwornSwordPopup(function(){ssAddToAdventureParty(itemCurrentSelection.id,!1)},"inventory",!0);
  72. ssAddToAdventureParty_2(ssToAdd,a);
  73. },
  74. error:function(a){
  75. ssToAdd.push(b);
  76. 400==a.status?advPartyFail():409==a.status?setTimeout(function(){ssAddToAdventureParty_2(ssToAdd,!0)},5E3):spinTimeout()
  77. }
  78. });
  79. }
  80. //v101++<<
  81. function buildingFinished(b) {
  82.  
  83. // DO NOT call this function for the keep: it has neither build_remaining nor producing_archetype_id
  84.  
  85. // When a building finished either producing or bonus archetype id's should be present along with
  86. // no build time remaining to safely determine that this building has produced the item (luck or not)
  87. // but has not finalized the production yet (that's clicking the finish button)
  88. // Note that upgrades also enlist with a producing_archetype_id here, so they behave similarly
  89. return (b.producing_archetype_id != void 0 || b.bonus_archetype_id != void 0) && b.build_remaining == void 0;
  90. }
  91.  
  92. function buildingProducing(b) {
  93.  
  94. // DO NOT call this function for the keep: it has neither build_remaining nor producing_archetype_id
  95.  
  96. // Luck-based recipes do not assign producing_archetype_id property
  97. // for all others this returns the remaining build
  98. //return b.producing_archetype_id && b.build_remaining;
  99.  
  100. // Simple and short, if this property is present and the value
  101. // is a positive number, then the building is still in production
  102. return b.build_remaining != void 0 && b.build_remaining > 0;
  103. }
  104.  
  105. function refreshPlayerData(callback){
  106. $.ajax({
  107. url: "/play/player_data?client_seqnum=" + userContext.player_data_seqnum + "&platform=web&buildings=true",
  108. success: function(r){
  109. userContext.player_data_seqnum = r.player_data_seqnum;
  110. setupPlayerData(r, void 0);
  111.  
  112. if(typeof callback == "function"){
  113. callback();
  114. }
  115. }
  116. });
  117. }
  118.  
  119. var bruteForceTimeout;
  120. function bruteForce(enabled, times) {
  121.  
  122. if (brutingImmediateTermination) {
  123. bruteForceTimeout = clearTimeout(bruteForceTimeout);
  124.  
  125. msg = "Bruting terminated immediately.";
  126. log(msg, "BRUTING");
  127. inform(msg);
  128. return;
  129. }
  130.  
  131. var msg;
  132. var container = $("#bruteBtn");
  133. if (!container || container.length == 0) {
  134. container = $(".challengerewardbox .rewarditem:last");
  135. }
  136.  
  137. if (!container || container.length == 0) {
  138. container = $(".challengerewards .challengerewarditems:first");
  139. }
  140.  
  141. if (!container || container.length == 0) {
  142. msg = "Cannot find appropriate container for messaging!";
  143. warn(msg);
  144. }
  145.  
  146. if (typeof enabled == "boolean" && !enabled) {
  147. bruteForceTimeout = clearTimeout(bruteForceTimeout);
  148.  
  149. msg = "Bruting terminated.";
  150. log(msg, "BRUTING");
  151.  
  152. // console.debug("Debuging message delivery: is there a container? " + (container) + ", " +
  153. // "what's his length: " + container.length + ", append message? " + (container && container.length > 0) + ", " +
  154. // "and what's the message: " + msg);
  155.  
  156. if (container.hasClass("btnwrap btnmed")) {
  157. container.find("a").text("Done!");
  158. }
  159.  
  160. container.after("<br />" + msg);
  161.  
  162. if (times === "all") {
  163. bruteSendAll();
  164. }
  165.  
  166. return;
  167. }
  168.  
  169. // Do not run if bruting is disabled (but can be viewed)
  170. if (extender.options.bruteWounds && extender.options.bruteWounds == 0) {
  171.  
  172. msg = "Disabled. Please set a positive number of max wounds from options to continue.";
  173. warn(msg, "BRUTING");
  174.  
  175. // console.debug("Debuging message delivery: is there a container? " + (container) + ", " +
  176. // "what's his length: " + container.length + ", append message? " + (container && container.length > 0) + ", " +
  177. // "and what's the message: " + msg);
  178.  
  179. container.after("<br />" + msg);
  180.  
  181. bruteForce(false, times);
  182. return;
  183. }
  184.  
  185. var s = userContext.setSwornSword;
  186.  
  187. if (!s) {
  188. msg = "Failed, no sworn sword set.";
  189. error(msg, "BRUTING");
  190.  
  191. container.after("<br />" + msg);
  192. bruteForce(false, times);
  193. return;
  194. }
  195.  
  196. // console.debug("Debuging sworn sword condition: s.damage: " + s.damage + ", " +
  197. // "extender.options.bruteWounds: " + extender.options.bruteWounds + ", do bruting? " + (s.damage < extender.options.bruteWounds));
  198.  
  199. if (s.damage < extender.options.bruteWounds) {
  200.  
  201. resolveModifier(s);
  202.  
  203. var attack =
  204. (extender.options.sendAllAction === "selected" &&
  205. extender.options.selectedAction &&
  206. extender.options.selectedAction != void 0)
  207. ? extender.options.selectedAction : s.modifier;
  208.  
  209. // console.debug("Attack with: " + attack);
  210. if (times == "once") {
  211. doAdventure("", attack, false, function (failure) {
  212. bruteForce(false, times);
  213. });
  214. } else {
  215. doAdventure("", attack, false, function (failure) {
  216. bruteForce(!failure, times);
  217. });
  218. }
  219.  
  220. return;
  221. }
  222.  
  223. if (times === "all" || extender.options.bruteSwitchOff) {
  224.  
  225. msg = "Sworn sword recieved " + extender.options.bruteWounds + " wounds! Brute timer will self terminate.";
  226. warn(msg, "BRUTING");
  227.  
  228. // console.debug("Debuging message delivery: is there a container? " + (container) + ", " +
  229. // "what's his length: " + container.length + ", append message? " + (container && container.length > 0) + ", " +
  230. // "and what's the message: " + msg);
  231.  
  232. container.after("<br />" + msg);
  233.  
  234. bruteForce(false, times);
  235.  
  236. } else {
  237.  
  238. // Add a minute to the cooldown, and then multiply by wounds
  239. var interval = extender.options.bruteWounds * (s.damage_cooldown + 60);
  240. msg = "Sworn sword recieved " + extender.options.bruteWounds + " wounds! " +
  241. "Brute timer will self adjust. Wait " + extender.options.bruteWounds + " * (" + s.damage_cooldown + " + 60) = " + interval + " seconds.";
  242.  
  243. warn(msg, "BRUTING");
  244.  
  245. bruteForceTimeout = setTimeout(function () {
  246. bruteForce(true, times);
  247. }, interval * 1000);
  248.  
  249. // console.debug("Debuging message delivery: is there a container? " + (container) + ", " +
  250. // "what's his length: " + container.length + ", append message? " + (container && container.length > 0) + ", " +
  251. // "and what's the message: " + msg);
  252.  
  253. container.after("<br />" + msg);
  254. }
  255. }
  256.  
  257. function bruteSendAll() {
  258. var sw = getSwornSwords(extender.options.sendAllAction);
  259. //console.debug("Sworn swords retrieved successfully...");
  260.  
  261. for (var i = 0; i < sw.length; i++) {
  262. var ss = sw[i];
  263.  
  264. if (typeof ss.not_on_adventure == "boolean" && ss.not_on_adventure)
  265. continue;
  266.  
  267. if (ss.cooldown > 0 || ss.damage >= extender.options.bruteWounds)
  268. continue;
  269.  
  270. console.debug("Sending: " + ss.full_name);
  271.  
  272. setSwornSword(ss.id);
  273. bruteForce(true, "all");
  274. return;
  275. }
  276.  
  277. inform("All sworn swords have been bruted to maximum wounds.<br>Action: " + extender.options.sendAllAction);
  278.  
  279. }
  280.  
  281. var brutingImmediateTermination = false;
  282. $(document).on("keyup", function (e) {
  283. if (e.keyCode == 27) {
  284. e.preventDefault();
  285. e.stopPropagation();
  286.  
  287. brutingImmediateTermination = true;
  288. trainImmediateTermination = true;
  289.  
  290. log("Attempting immediate termination.");
  291. }
  292. });
  293.  
  294. function increment(me) {
  295. var opt = $(me);
  296.  
  297. var min = parseInt(opt.attr("min"));
  298. var max = parseInt(opt.attr("max"));
  299. var step = parseInt(opt.attr("step"));
  300. var val = parseInt(opt.text());
  301.  
  302. if (isNaN(min) || isNaN(max) || isNaN(step) || isNaN(val)) {
  303. error("Parsing of attributes failed!", "number option");
  304. return;
  305. }
  306.  
  307. var newVal = val + step > max ? min : val + step;
  308. opt.text(newVal);
  309. };
  310.  
  311. function check(me) {
  312. $(me).toggleClass('checked');
  313. };
  314.  
  315. function bruteSwitchToggle(me) {
  316. var bSwitch = $(me).find("a.btngold");
  317. bSwitch.text() == "switch off"
  318. ? bSwitch.text("adjust")
  319. : bSwitch.text("switch off");
  320. };
  321.  
  322. function inputIncrement(me) {
  323. var input = $(me).parent().find("input[name='quantity']");
  324. if (!input || input.length == 0) {
  325. return;
  326. }
  327.  
  328. var v = parseInt(input.val());
  329. var max = parseInt(input.attr("maxlength"));
  330. if (isNaN(v) || isNaN(max)) {
  331. return;
  332. }
  333.  
  334. if (me.id == "excountup") {
  335. (max == 3 ? max = 999 : max = 99);
  336. (v + 1 > max ? v = max : v = v + 1);
  337. } else if (me.id == "excountdown") {
  338. (v - 1 < 0 ? v = 0 : v = v - 1);
  339. }
  340.  
  341. input.val(v);
  342. }
  343.  
  344. function sort(arr) {
  345. if (typeof arr == "undefined") {
  346. arr = playerInventory;
  347. }
  348.  
  349. if (!(arr instanceof Array)) {
  350. error("Not an array! Sorting failed.");
  351. return arr;
  352. }
  353. // console.debug("Sorting of array initiated, check parameters: boons count: " + boons.length + ", " +
  354. // "sorting property: " + extender.options.boonsSortBy);
  355.  
  356. arr.sort(function (a, b) {
  357. return b[extender.options.boonsSortBy2] - a[extender.options.boonsSortBy2];
  358. });
  359.  
  360. arr.sort(function (a, b) {
  361. return b[extender.options.boonsSortBy] - a[extender.options.boonsSortBy];
  362. });
  363.  
  364. log("Array sorted by " + extender.options.boonsSortBy + " and then by " + extender.options.boonsSortBy2 + " successfully.", "PAGE");
  365. return arr;
  366.  
  367. }
  368.  
  369. function sortShop(arr) {
  370. if (typeof arr == "undefined" || !(arr instanceof Array)) {
  371. error("Not an array. Please pass an array to be sorted.");
  372. return arr;
  373. }
  374.  
  375. //console.debug("Sorting of array initiated, check array count: " + arr.length);
  376.  
  377. arr.sort(function(a, b) {
  378.  
  379. //console.debug("Init first sorting, condition 1, properties exist: ", a.hasOwnProperty(extender.options.shopSortBy2), b.hasOwnProperty(extender.options.shopSortBy2));
  380. if (!a.hasOwnProperty(extender.options.shopSortBy2) || !b.hasOwnProperty(extender.options.shopSortBy2)) {
  381. return false;
  382. }
  383.  
  384. var aVal = a[extender.options.shopSortBy2];
  385. var bVal = b[extender.options.shopSortBy2];
  386.  
  387. //console.debug("Logging property values: ", aVal, bVal);
  388.  
  389. if (typeof aVal != "number" || typeof bVal != "number") {
  390.  
  391. //console.debug("Values need parsing...");
  392.  
  393. aVal = parseInt(aVal);
  394. bVal = parseInt(bVal);
  395.  
  396. //console.debug("Parsed values: ", aVal, bVal);
  397. if (isNaN(aVal) || isNaN(bVal)) {
  398. return false;
  399. }
  400. }
  401.  
  402. //console.debug("Sorting descending...");
  403. return bVal - aVal;
  404. });
  405.  
  406. arr.sort(function (a, b) {
  407.  
  408. //console.debug("Init second sorting, condition 1, properties exist: ", a.hasOwnProperty(extender.options.shopSortBy2), b.hasOwnProperty(extender.options.shopSortBy2));
  409. if (!a.hasOwnProperty(extender.options.shopSortBy) || !b.hasOwnProperty(extender.options.shopSortBy)) {
  410. return false;
  411. }
  412. var aVal = a[extender.options.shopSortBy];
  413. var bVal = b[extender.options.shopSortBy];
  414.  
  415. //console.debug("Logging property values: ", aVal, bVal);
  416.  
  417. if (typeof aVal != "number" || typeof bVal != "number") {
  418.  
  419. //console.debug("Values need parsing...");
  420.  
  421. aVal = parseInt(aVal);
  422. bVal = parseInt(bVal);
  423.  
  424. //console.debug("Parsed values: ", aVal, bVal);
  425. if (isNaN(aVal) || isNaN(bVal)) {
  426. return false;
  427. }
  428. }
  429.  
  430. //console.debug("Sorting descending...");
  431. return bVal - aVal;
  432. });
  433.  
  434. // Array will always be sorted by gold, descending
  435. arr.sort(function (a, b) {
  436. return parseInt(a["price_perk_points"]) - parseInt(b["price_perk_points"]);
  437. });
  438.  
  439. //console.debug("Sorted by gold: ");
  440.  
  441. //arr.forEach(function (element) {
  442. // console.debug(element["price_perk_points"]);
  443. //});
  444.  
  445. log("Array sorted by " + extender.options.shopSortBy + " and then by " + extender.options.shopSortBy2 + " successfully.", "PAGE");
  446. return arr;
  447.  
  448. }
  449.  
  450. function formatStats(battle, trade, intrigue, level) {
  451. return '<div class="exstatbox">' +
  452. '<div id="plevel" class="statitem">' +
  453. '<div><span></span>' +
  454. '</div><var>' + level + '</var></div>' +
  455. '<div id="battle" class="statitem">' +
  456. '<div><span></span>' +
  457. '</div><var class="battle_val" id="battle_val">' + battle + '</var></div>' +
  458. '<div id="trade" class="statitem">' +
  459. '<div><span></span>' +
  460. '</div><var class="trade_val" id="trade_val">' + trade + '</var></div>' +
  461. '<div id="intrigue" class="statitem">' +
  462. '<div><span></span></div>' +
  463. '<var class="intrigue_val" id="intrigue_val">' + intrigue + '</var></div>' +
  464. '</div>';
  465. }
  466.  
  467. function getSwornSwords(filter) {
  468. var ss = [];
  469. var pi = playerInventory;
  470.  
  471. var cfg = Array.prototype.filter.call(arguments, function(c) {
  472. return typeof c == "object";
  473. })[0] || {};
  474.  
  475.  
  476. for (var i = 0; i < pi.length; i++) {
  477. var s = pi[i];
  478.  
  479. if (s.slot !== "Sworn Sword")
  480. continue;
  481.  
  482. if (!s.modifier) {
  483. resolveModifier(s);
  484. }
  485.  
  486. if (typeof filter == "number" && !isNaN(filter) && s.id === filter) {
  487. return s;
  488. }
  489.  
  490. if (typeof filter == "string" && !isNaN(parseInt(filter)) && s.id === parseInt(filter)) {
  491. return s;
  492. }
  493.  
  494. // This check ain't worth shit
  495. //if (s.cooldown > 0 && (cfg.disregardCooldown == void 0 || !cfg.disregardCooldown)) {
  496. // warn("This sworn sword is supposedly busy, continue...");
  497. // continue;
  498. //}
  499.  
  500. if (s.damage == 4 && !cfg.disregardWounds) {
  501. warn("This sworn sword is about to die!");
  502. continue;
  503. }
  504.  
  505. if (!isNaN(cfg.region) && !isNaN(cfg.subRegion) && !checkGarrison(s, cfg.region, cfg.subRegion))
  506. continue;
  507.  
  508. if (typeof filter == "string") {
  509.  
  510. if (filter == "all" || filter == "selected") {
  511. ss.push(s);
  512. continue;
  513.  
  514. } else if (filter == "none"){
  515. continue;
  516. }
  517.  
  518. var friendlyActions = ["aid", "barter", "bribe"];
  519. var hostileActions = ["fight", "harass", "spy", "sabotage", "steal", "hoodwink"];
  520.  
  521. var battleActions = ["fight", "harass", "aid"];
  522. var tradeActions = ["barter", "hoodwink", "bribe"];
  523. var intrigueActions = ["spy", "sabotage", "steal"];
  524.  
  525. //if ((friendlyActions.indexOf(filter) > -1 || hostileActions.indexOf(filter) > -1) && s.modifier != filter) {
  526. // continue;
  527. //}
  528.  
  529. switch (filter) {
  530. case "battle":
  531. {
  532. if (battleActions.indexOf(s.modifier) == -1)
  533. continue;
  534. else
  535. break;
  536. }
  537. case "trade":
  538. {
  539. if (tradeActions.indexOf(s.modifier) == -1)
  540. continue;
  541. else
  542. break;
  543. }
  544. case "intrigue":
  545. {
  546. if (intrigueActions.indexOf(s.modifier) == -1)
  547. continue;
  548. else
  549. break;
  550. }
  551. case "friendly":
  552. {
  553. if (friendlyActions.indexOf(s.modifier) == -1)
  554. continue;
  555. else
  556. break;
  557. }
  558. case "hostile":
  559. {
  560. if (hostileActions.indexOf(s.modifier) == -1)
  561. continue;
  562. else
  563. break;
  564. }
  565. default:
  566. {
  567. if (s.modifier != filter)
  568. continue;
  569. else
  570. break;
  571. }
  572. }
  573. }
  574.  
  575. ss.push(s);
  576. }
  577.  
  578. return ss;
  579. }
  580.  
  581. function checkGarrison(s, region, subregion) {
  582. if (typeof s.garrison_region == "undefined" || typeof s.garrison_subregion == "undefined" ||
  583. isNaN(s.garrison_region) && isNaN(s.garrison_subregion)) {
  584. return false;
  585. }
  586.  
  587. if(subregion == -1 && s.garrison_region == region) {
  588. return true;
  589. }
  590.  
  591. if (s.garrison_subregion == subregion && s.garrison_region == region) {
  592. return true;
  593. }
  594.  
  595. return false;
  596. }
  597.  
  598. function setSwornSword(param) {
  599. if (typeof param == "undefined") {
  600. error("The function requires a sworn sword id as parameter.")
  601. return;
  602. }
  603. if (typeof param == "number") {
  604. var s = extractItemById(playerInventory, param);
  605. s && (userContext.setSwornSword = s);
  606. } else if (typeof param == "string") {
  607. var s = extractItemById(playerInventory, parseInt(param));
  608. s && (userContext.setSwornSword = s);
  609. } else if (typeof param == "object") {
  610. userContext.setSwornSword = param;
  611. }
  612. }
  613.  
  614. function checkCommandPoints() {
  615. if(!userContext || !userContext.playerData || !userContext.playerData.stat)
  616. return false;
  617.  
  618. var commandPoints = userContext.playerData.stat.command - userContext.playerData.stat.current_command;
  619.  
  620. log("Debugging check command points: command points: " + commandPoints + ", " +
  621. "continue then? " + (!isNaN(commandPoints) && commandPoints > 0));
  622.  
  623. return !isNaN(commandPoints) && commandPoints > 0;
  624. }
  625. function getCommandPoints() {
  626. if(!userContext || !userContext.playerData || !userContext.playerData.stat)
  627. return 0;
  628.  
  629. var commandPoints = userContext.playerData.stat.command - userContext.playerData.stat.current_command;
  630.  
  631. log("Debugging get command points: command points: " + commandPoints + ", " +
  632. "continue then? " + (!isNaN(commandPoints) && commandPoints > 0));
  633.  
  634. return commandPoints;
  635. }
  636. function adventureSendAll(adventure, swornswords) {
  637.  
  638. if (extender.options.sendAllAction === "none") {
  639. inform("This function is disabled. Please enable it from options.");
  640. return;
  641. }
  642.  
  643. if(!checkCommandPoints()){
  644. inform("Not enough command points. <br>Action cannot execute.");
  645. if (extender.options.sendAllAction === "selected") {
  646. extender.options.selectedAction = userContext.currentActionLabel;
  647. extender.import();
  648. }
  649. return;
  650. }
  651.  
  652. if (typeof swornswords == "undefined" || !(swornswords instanceof window.Array)) {
  653. error("An array should be passed to the function. Sending failed.");
  654. return;
  655. }
  656.  
  657. if (typeof adventure != "string") {
  658. error("Adventure should be passed as a string. Sending failed.");
  659. return;
  660. }
  661.  
  662. if (swornswords.length == 0) {
  663. inform('All sworn swords send on ' + adventure + '.<br>Action: ' + extender.options.sendAllAction);
  664. if (extender.options.sendAllAction === "selected") {
  665. extender.options.selectedAction = userContext.currentActionLabel;
  666. extender.import();
  667. }
  668. return;
  669. }
  670.  
  671. var s = swornswords.pop();
  672.  
  673. log("Debuging send all: swornswords: " + swornswords.length + ", " +
  674. "current: " + s.full_name + ", " +
  675. "cooldown: " + s.cooldown + ", " +
  676. "damage: " + s.damage + ", " +
  677. "send? " + !(s.cooldown > 0 || s.damage >= extender.options.bruteWounds));
  678.  
  679. if(s.cooldown > 0 || s.damage >= extender.options.bruteWounds) {
  680. adventureSendAll(adventure, swornswords);
  681. return;
  682. }
  683.  
  684. setSwornSword(s.id);
  685.  
  686. if (!s.modifier) {
  687. resolveModifier(s);
  688. }
  689.  
  690. var attack = s.modifier;
  691. if(extender.options.sendAllAction === "selected" && userContext.currentActionLabel != void 0){
  692. attack = userContext.currentActionLabel;
  693. }
  694.  
  695. doAdventure(adventure, attack, false, function (failure) {
  696. adventureSendAll(adventure, swornswords);
  697. });
  698.  
  699. }
  700.  
  701. var pvpFormStore;
  702. function pvpSendBatch(swornswords, loops) {
  703. var loops_new=loops-1;
  704. log("pvpSendBatch pvpSendBatch pvpSendBatch");
  705. if (extender.options.sendAllAction === "none") {
  706. inform("This function is disabled. Please enable it from options.");
  707. return;
  708. }
  709.  
  710.  
  711. if(loops_new<0){
  712. inform("Nunber of loops done. <br>Action finished.");
  713. return;
  714. }
  715.  
  716. if (typeof swornswords == "undefined" || !(swornswords instanceof window.Array)) {
  717. error("An array should be passed to the function. Sending failed.");
  718. return;
  719. }
  720.  
  721. if (swornswords.length == 0) {
  722. inform("All sworn swords are now busy.<br>Action: " + extender.options.sendAllAction);
  723. pvpFormStore = {}; // clear the store for further use
  724. return;
  725. }
  726.  
  727. var s = swornswords.pop();
  728.  
  729. log("Debuging send all: swornswords: " + swornswords.length + ", " +
  730. "current: " + s.full_name + ", " +
  731. "cooldown: " + s.cooldown + ", " +
  732. "damage: " + s.damage + ", " +
  733. "send? " + !(s.cooldown > 0 || s.damage >= extender.options.bruteWounds));
  734.  
  735. if (s.cooldown > 0 || s.damage >= extender.options.bruteWounds) {
  736. pvpSendBatch(swornswords,loops_new);
  737. return;
  738. }
  739.  
  740. // Store the pvp or replace it
  741. // if it is stored already
  742.  
  743. if (!pvpFormStore) {
  744. pvpFormStore = pvpForm;
  745. } else {
  746. pvpForm = pvpFormStore;
  747. }
  748.  
  749. setSwornSword(s.id);
  750.  
  751. if (!s.modifier) {
  752. resolveModifier(s);
  753. }
  754.  
  755. if (extender.options.sendAllAction !== "selected") {
  756. userContext.currentActionLabel = s.modifier;
  757. }
  758.  
  759. pvpLaunch_1(function(success){
  760. setTimeout(function(){
  761. pvpSendBatch(swornswords,loops_new);
  762. }, getRandomInt(500, extender.options.baseDelay * 500));
  763. });
  764.  
  765. if(!checkCommandPoints()){
  766. inform("Not enough command points. <br>Action cannot execute.");
  767. return;
  768. }
  769. }
  770.  
  771. function resolveModifier(s) {
  772. if (s.modifier) {
  773. //log("Modifier already set.");
  774. return;
  775. }
  776. // Calculate what attack shoild we use if there ain't any
  777. var max = Math.max(s.calc_battle, s.calc_trade, s.calc_intrigue);
  778. if (max == s.calc_intrigue) {
  779. s.modifier = extender.options.defaultIntrigue;
  780. } else if (max == s.calc_trade) {
  781. s.modifier = extender.options.defaultTrade;
  782. } else {
  783. s.modifier = extender.options.defaultBattle;
  784. }
  785. }
  786.  
  787. function avaSendBatch(swornswords) {
  788. if (extender.options.sendAllAction === "none") {
  789. inform("This function is disabled. Please enable it from options.");
  790. return;
  791. }
  792.  
  793. // You do not check command points during ava
  794. //if (!checkCommandPoints()) {
  795. // inform("Not enough command points. <br>Action cannot execute.");
  796. // return;
  797. //}
  798.  
  799. if (typeof swornswords == "undefined" || !(swornswords instanceof window.Array)) {
  800. error("An array should be passed to the function. Sending failed.");
  801. return;
  802. }
  803.  
  804. if (swornswords.length == 0) {
  805. inform("All sworn swords are now busy.<br>Action: " + extender.options.sendAllAction);
  806. return;
  807. }
  808.  
  809. var s = swornswords.pop();
  810.  
  811. console.debug("Debuging send all: swornswords: " + swornswords.length + ", " +
  812. "current: " + s.full_name + ", " +
  813. "id: " + s.id + ", " +
  814. "cooldown: " + s.cooldown + ", " +
  815. "damage: " + s.damage + ", " +
  816. "region: " + s.garrison_region + ", " +
  817. "subregion: " + s.garrison_subregion + ", " +
  818. "send? " + !(s.damage >= extender.options.bruteWounds));
  819.  
  820. if (s.damage >= extender.options.bruteWounds) {
  821. avaSendBatch(swornswords);
  822. return;
  823. }
  824.  
  825. itemCurrentSelection = s;
  826.  
  827. if (extender.options.sendAllAction === "selected" && extender.options.selectedAction &&
  828. extender.options.selectedAction != void 0) {
  829. userContext.currentActionLabel = extender.options.selectedAction;
  830. } else {
  831. userContext.currentActionLabel = s.modifier;
  832. }
  833.  
  834. doCampAttack(userContext.campAttackData.id, "skip", function campCallback(r, retry) {
  835. console.debug("Camp attack callback, debugging arguments: ", arguments);
  836.  
  837. if (r.error) {
  838. avaSendBatch(swornswords);
  839. return;
  840. }
  841.  
  842. if (!r.camp_attack) {
  843. error("Camp data is missing, cannot create new attack.");
  844. return;
  845. }
  846.  
  847. var attack = r.camp_attack;
  848.  
  849. $.ajax({
  850. url: "/play/camp_attack_create?alliance_id=" + attack.target_alliance_id + "&region=" + attack.region + "&subregion=" + attack.subregion + "&acnew=" + userContext.playerData.ac,
  851. dataType: "JSON",
  852. success: function(a) {
  853. userContext.camp_battle = a.battle;
  854. userContext.camp_trade = a.trade;
  855. userContext.camp_intrigue = a.intrigue;
  856. userContext.campAttackData = a;
  857. startCampAttack(userContext.campAttackData);
  858.  
  859. setTimeout(function(){
  860. avaSendBatch(swornswords);
  861. }, getRandomInt(500, extender.options.baseDelay * 500));
  862. },
  863. error: function(e) {
  864. warn("Something went wrong: " + e.status + " : " + e.statusText + " (tried: " + (retry || 1) + ") , extender will try again...");
  865.  
  866. setTimeout(function() {
  867. retry != void 0
  868. ? (retry < 21
  869. ? campCallback(r, ++retry)
  870. : error("Extender failed to send attack on this camp after 21 retries.", "CAMP"))
  871. : campCallback(r, 1);
  872. }, getRandomInt(500, extender.options.baseDelay * 500));
  873. }
  874. });
  875. });
  876. }
  877.  
  878. window.addEventListener("message", receiveMessage, false);
  879. function receiveMessage(event) {
  880. // Do we trust the sender of this message?
  881. if (event.origin !== "https://games.disruptorbeam.com") {
  882. error("Message from untrusted source was rejected.");
  883. console.debug(event);
  884. return;
  885. }
  886.  
  887. //console.debug("Message recieved: ", event);
  888. console.log("Evaluated: ", eval(event.data));
  889.  
  890. }
  891.  
  892. function observable_onkeyup(e){
  893. //console.debug(e);
  894.  
  895. if(e.keyCode === 13 && !e.ctrlKey && !e.shiftKey) {
  896. var observable = $("textarea#observable");
  897. if (!observable) {
  898. error("The observable DOM element was not found in the page.");
  899. return false;
  900. }
  901.  
  902. try {
  903. var cmd = observable.val().split("\n")[0];
  904. eval("extender." + cmd);
  905. } catch (err) {
  906. var prefix = "COMMAND FAILED" + " | " + new Date().toLocaleTimeString() + " | ";
  907. observable.val(prefix + err);
  908. }
  909.  
  910. return false;
  911. }
  912.  
  913. if(e.keyCode === 46){
  914. var observable = $("textarea#observable");
  915. if (!observable) {
  916. error("The observable DOM element was not found in the page.");
  917. return false;
  918. }
  919.  
  920. observable.val("");
  921. return false;
  922. }
  923.  
  924. return true;
  925. }
  926.  
  927. function clearLog(){
  928. sessionStorage.set("clientEntries", []);
  929. log("The client log has been cleared successfully.");
  930.  
  931. $("#logTab").trigger("click");
  932. }
  933.  
  934. var bossChallenger = (function(log, error, questClose, questSubmit, localStorage){
  935.  
  936. var _this = {
  937. init: init,
  938. fight: fight,
  939. persist: persist,
  940. config: config,
  941. addQuest: addQuest,
  942. removeQuest: removeQuest,
  943.  
  944. enabled: true,
  945. bossQuests: []
  946. };
  947.  
  948. function init(o){
  949. try {
  950. _this.bossQuests = localStorage.get("bossQuests", []);
  951.  
  952. _this.config(o);
  953.  
  954. if(!_this.enabled) return; // Do not run if disabled
  955.  
  956. // Relaunch any quests pending...
  957. for (var i = 0; i < _this.bossQuests.length; i++) {
  958. var a = _this.bossQuests[i];
  959. questSubmit(a.quest, a.stage, a.attack, a.chosen, null, null, a.questId);
  960. }
  961.  
  962. } catch(e) {
  963. error(e);
  964. }
  965. }
  966.  
  967. function config(o){
  968. //console.debug(o);
  969.  
  970. _this.enabled = o.autoBossChallenge;
  971. if(!_this.enabled){ // If disabled, clear stored quests
  972. log("Boss challenge is disabled. Clearing stored quests...", "EXTENDER");
  973.  
  974. _this.bossQuests = [];
  975. _this.persist();
  976. }
  977. }
  978.  
  979. function persist(){
  980. localStorage.set("bossQuests", _this.bossQuests);
  981. }
  982.  
  983. // Response, attack
  984. function fight(a, c){
  985. if (a.actions_remaining == void 0 || isNaN(a.actions_remaining)){
  986. log("Not on boss challenge (no actions remaining). Exiting...", "BOSS");
  987. return;
  988. }
  989.  
  990. if(!_this.enabled){
  991. log("Boss challenge is not automated. Exiting...", "EXTENDER");
  992. return;
  993. }
  994.  
  995. log("Boss challenge automated. Actions remaining: " + a.actions_remaining + ", " +
  996. "stage: " + a.stage, "BOSS");
  997.  
  998. if(a.stage && a.stage === 1000){
  999. log("Boss challenge complete. Exiting...", "BOSS");
  1000.  
  1001. _this.removeQuest(a.id);
  1002. _this.persist();
  1003.  
  1004. // Close dialog and pop it from whenever necessary
  1005. questClose(a.symbol, a.id, true);
  1006. return;
  1007. }
  1008.  
  1009. if (a.actions_remaining > 0) {
  1010. questSubmit(a.symbol, a.stage, c, a.chosen, null, null, a.id);
  1011. } else {
  1012. log("No actions remaining! Adjusting...", "BOSS");
  1013.  
  1014. var bossQuest = {
  1015. "quest": a.symbol,
  1016. "stage": a.stage,
  1017. "attack": c,
  1018. "chosen": a.chosen,
  1019. "questId": a.id,
  1020. "timeout": setTimeout(function() {
  1021. questSubmit(a.symbol, a.stage, c, a.chosen, null, null, a.id);
  1022. }, 3 * 5 * 60 * 1000)
  1023. };
  1024.  
  1025.  
  1026. _this.addQuest(bossQuest);
  1027. _this.persist();
  1028.  
  1029. log("Timer running. Fire again in 15 minutes.", "BOSS");
  1030. }
  1031. }
  1032.  
  1033. function addQuest(quest){
  1034. if(quest == void 0){
  1035. error("No quest was passed to add function. Exiting...");
  1036. return;
  1037. }
  1038.  
  1039. var quests = _this.bossQuests;
  1040. for(var i = 0; i < quests.length; i++){
  1041. if(quests[i].questId == quest.questId){
  1042. log("Quest is already made persistable.");
  1043. return;
  1044. }
  1045. }
  1046.  
  1047. quests.push(quest);
  1048.  
  1049. }
  1050.  
  1051. function removeQuest(questId){
  1052.  
  1053. // Remove the quest from the array
  1054. _this.bossQuests = _this.bossQuests.filter(function (el) {
  1055. return el.questId !== questId;
  1056. });
  1057. }
  1058.  
  1059. return _this;
  1060.  
  1061. }(log, error, questClose, questSubmit, localStorage));
  1062.  
  1063. function exSendFavors(favor, characterList){
  1064.  
  1065. var json = {
  1066. favor: favor,
  1067. recipients: characterList
  1068. };
  1069.  
  1070. showSpinner();
  1071.  
  1072. $.ajax({
  1073. type: "POST",
  1074. url: "/play/send_mass_favors",
  1075. data: json,
  1076. dataType: "JSON",
  1077. complete: hideSpinner,
  1078. success: function (a) {
  1079. !0 == a.status
  1080. ? void 0 == a.exceptions ? (doAlert("Message Sent", "Your Raven has been sent."), isWeb() || iosSignal("favors_sent", a.count)) : doAlert("Message Sent", "Your Raven has been sent.<br/><br/>" + a.exceptions + " did not receive a favor as you have already sent them the maximum per day.") : !1 == a.status && ("invalid_favor" == a.reason ? doAlert("Message Center",
  1081. "The selected favor is invalid.") : "not_enough_favors" == a.reason ? doAlert("Message Center", "You have reached your daily favor-giving limit. Your raven was not sent.") : "favor_limit_all" == a.reason ? doAlert("Message Center", "You have already sent the favors daily maximum to these recipients.") : "select_min_player" == a.reason && doAlert("Message Center", "Please select at least one recipient."))
  1082. }
  1083. })
  1084. }
  1085.  
  1086. var worldEvent = (function ($, log, warn, error, submitWorldEventAction, getWorldEventAttackResults, userContext) {
  1087. var _this = {
  1088.  
  1089. delay: 4E3,
  1090. enabled: true,
  1091. timeouts: [],
  1092.  
  1093. get attackers() {
  1094. return localStorage.get("weAttackers", []);
  1095. },
  1096.  
  1097. set attackers(val) {
  1098. localStorage.set("weAttackers", val);
  1099. },
  1100.  
  1101. init: function (o) {
  1102. try {
  1103.  
  1104. this.config(o);
  1105. this.analyze();
  1106.  
  1107. log("World event initialized.", "WORLD EVENT");
  1108.  
  1109. } catch(err) {
  1110. error(err);
  1111. }
  1112. },
  1113.  
  1114. config: function (o) {
  1115. //console.debug(o);
  1116.  
  1117. this.delay = o.worldEventDelay * 1E3 || this.delay;
  1118. this.enabled = o.weManagerEnabled != void 0 ? o.weManagerEnabled : this.enabled;
  1119. },
  1120.  
  1121. dispatch: function () {
  1122. if (!this.enabled) {
  1123. log("This feature has been disabled. Exiting...", "WORLD EVENT");
  1124. return;
  1125. }
  1126.  
  1127. var attackers = this.attackers.filter(function(a){
  1128. return a != void 0;
  1129. });
  1130.  
  1131. for (var i = 0; i < attackers.length; i++) {
  1132.  
  1133. var ss = getSwornSwords(attackers[i]);
  1134. if (!ss) {
  1135. error("Failed to find the sworn sword. Request won't be sent.");
  1136. continue;
  1137. }
  1138.  
  1139. var order = $("#slot_" + i + "_orders").length
  1140. ? $("#slot_" + i + "_orders").val()
  1141. : ss.modifier;
  1142.  
  1143. submitWorldEventAction(ss.id, order, false);
  1144. }
  1145. },
  1146.  
  1147. retrieve: function (ss) {
  1148. if (!this.enabled) {
  1149. log("This feature has been disabled. Exiting...", "WORLD EVENT");
  1150. return;
  1151. }
  1152.  
  1153. if (ss != void 0) {
  1154.  
  1155. if (!ss.id || !ss.action || (ss.full_name == void 0 && ss.name == void 0) || (ss.cooldown == void 0 && ss.cooldown_seconds == void 0)) {
  1156. error("Incorrect object passed, lack of parameters.", "WORLD EVENT");
  1157. console.debug("Debug information, passed sworn sword: ", ss);
  1158. return;
  1159. }
  1160.  
  1161. var cooldown =
  1162. ss.cooldown != void 0 // defined as cooldown
  1163. ? (ss.cooldown !== 0
  1164. ? ss.cooldown // there is a cooldown
  1165. : false) // there ain't a cooldown
  1166.  
  1167. : ss.cooldown_seconds != void 0 // defined as cooldown_seconds
  1168. ? (ss.cooldown_seconds !== 0
  1169. ? ss.cooldown_seconds // there is a cooldown
  1170. : false) // there ain't a cooldown
  1171.  
  1172. : 36E2; // default cooldown of 1hour
  1173.  
  1174.  
  1175. if (!cooldown) {
  1176.  
  1177. getWorldEventAttackResults(ss.id, ss.action, true);
  1178.  
  1179. } else {
  1180.  
  1181. var timeout = {
  1182. id: ss.id,
  1183. name: ss.full_name || ss.name,
  1184. timeout: setTimeout(function () {
  1185.  
  1186. getWorldEventAttackResults(ss.id, ss.action, true);
  1187.  
  1188. }, (cooldown * 1E3) + _this.delay)
  1189. }
  1190.  
  1191. this.timeouts.push(timeout);
  1192. }
  1193.  
  1194. return;
  1195. }
  1196. },
  1197.  
  1198. analyze: function () {
  1199. if (!this.enabled) {
  1200. log("This feature has been disabled. Exiting...", "WORLD EVENT");
  1201. return;
  1202. }
  1203.  
  1204. // dummy data
  1205. var data = {
  1206. sworn_sword_id: 0,
  1207. order: "aid"
  1208. };
  1209.  
  1210. $.ajax({
  1211. url: "/play/world_event_attack",
  1212. data: data,
  1213. success: function (a) {
  1214. console.debug("Analyzing response from the server for the world event action: ", a);
  1215.  
  1216. if (a.challenge && a.challenge.active_swornswords) {
  1217. for (var i = 0; i < a.challenge.active_swornswords.length; i++) {
  1218. _this.retrieve(a.challenge.active_swornswords[i]);
  1219. }
  1220. }
  1221.  
  1222. }
  1223. });
  1224. },
  1225.  
  1226. afterSubmit: function (a, ssId) {
  1227. if (!this.enabled) {
  1228. log("This feature has been disabled. Exiting...", "WORLD EVENT");
  1229. return;
  1230. }
  1231.  
  1232. console.debug("Logging response from the server for sending the sworn sword: ", a);
  1233.  
  1234. if (a.swornsword && a.swornsword.cooldown) {
  1235.  
  1236. this.retrieve(a.swornsword);
  1237.  
  1238. } else if (a.challenge && a.challenge.active_swornswords && a.challenge.active_swornswords.length) {
  1239. var swornswords = a.challenge.active_swornswords;
  1240. for (var i = 0; i < swornswords.length; i++) {
  1241. if (swornswords[i].id !== ssId)
  1242. continue;
  1243.  
  1244. this.retrieve(swornswords[i]);
  1245.  
  1246. }
  1247. }
  1248. },
  1249.  
  1250. afterGet: function (b, ssId) {
  1251. if (!this.enabled) {
  1252. log("This feature has been disabled. Exiting...", "WORLD EVENT");
  1253. return;
  1254. }
  1255.  
  1256. console.debug("Logging response from the server from world event attack: ", b);
  1257.  
  1258. if (b.swornsword && b.action) {
  1259. submitWorldEventAction(b.swornsword.id, b.action, false);
  1260. }
  1261. },
  1262.  
  1263. enlistSS: function () {
  1264.  
  1265. try {
  1266. var attackArray = this.attackers;
  1267. var ss = userContext.setSwornSword;
  1268. if (ss == void 0) {
  1269.  
  1270. if (attackArray.length == 5) {
  1271. attackArray.pop();
  1272. }
  1273.  
  1274. attackArray.unshift(null);
  1275.  
  1276. } else {
  1277.  
  1278. if (attackArray.indexOf(ss.id) > -1) {
  1279. warn("Selected sworn sword is already enlisted for the world event. Exiting...", "WORLD EVENT");
  1280. return;
  1281. }
  1282.  
  1283. if (attackArray.length == 5) {
  1284. attackArray.pop();
  1285. attackArray.unshift(ss.id);
  1286. } else {
  1287. attackArray.push(ss.id);
  1288. }
  1289. }
  1290.  
  1291. this.attackers = attackArray;
  1292. $("#weTab").trigger('click');
  1293.  
  1294. } catch (e) {
  1295. error(e);
  1296. }
  1297.  
  1298. }
  1299. };
  1300.  
  1301. return _this;
  1302.  
  1303. }($, log, warn, error, submitWorldEventAction, getWorldEventAttackResults, userContext));
  1304.  
  1305. var trainImmediateTermination;
  1306. function extenderSSTrainAll(){
  1307. trainImmediateTermination = false;
  1308. var swornswords = getSwornSwords("all", {disregardCooldown: true, disregardWounds: true});
  1309.  
  1310. for(var i = 0; i < swornswords.length; i++){
  1311. var ss = swornswords[i];
  1312. if(!ss.upgrade_points)
  1313. continue;
  1314.  
  1315. var attribute = $("#ss_"+ ss.id +"_orders").val();
  1316. if(attribute === "stats") {
  1317. var max = Math.max(ss.calc_battle, ss.calc_trade, ss.calc_intrigue);
  1318. if (max == ss.calc_intrigue) {
  1319. attribute = "intrigue";
  1320. } else if (max == ss.calc_trade) {
  1321. attribute = "trade";
  1322. } else {
  1323. attribute = "battle";
  1324. }
  1325. }
  1326.  
  1327. for(var j = 0; j < ss.upgrade_points; j++)
  1328. ssTrain(ss.id, attribute);
  1329. }
  1330. }
  1331.  
  1332. // TODO: Implement..
  1333. //var PERSISTABLE = {
  1334. //
  1335. // get queue() {
  1336. // return localStorage.get("productionQueue", []);
  1337. // },
  1338. //
  1339. // set queue(val) {
  1340. // localStorage.set("productionQueue", val);
  1341. // }
  1342. //};
  1343.  
  1344. // TODO: CONCEAL IN GREAT SECRECY!!!
  1345. //var jqxr = $.ajax;
  1346. //$.ajax = function () {
  1347. // // DO STUFF WITH THE AJAX
  1348. // // BEFORE IT HAS FIRED
  1349. // console.log(arguments[0]);
  1350. //
  1351. // // Store the success function
  1352. // var onSuccess = arguments[0].success;
  1353. //
  1354. // // Replace success with
  1355. // // your custom function
  1356. // arguments[0].success = function () {
  1357. //
  1358. // // Capture the returned value of the stored
  1359. // // function after execution with corresponding arguments
  1360. // var onsret = onSuccess.apply(this, arguments);
  1361. //
  1362. // // DO STUFF WITH THE RESPONSE
  1363. // // AFTER SUCCESS FUNCTION FIRED
  1364. // console.log(arguments[0]);
  1365. //
  1366. // // Return if any
  1367. // return onsret;
  1368. // };
  1369. //
  1370. // return jqxr.apply(this, arguments);
  1371. //
  1372. //};
  1373. // TODO: TOP SECRET!!!
  1374.  
  1375. function avaSendAll_onclick(){
  1376. if(extender.options.sendAllAction === "selected") {
  1377. extender.options.selectedAction = userContext.currentActionLabel;
  1378. extender.import();
  1379. }
  1380.  
  1381. var cfg = {
  1382. region: userContext.campAttackData.camp_region_index,
  1383. subRegion: (extender.options.avaSendSubcamps ? -1 : userContext.campAttackData.camp_subregion_index)
  1384. };
  1385.  
  1386. var ss = getSwornSwords(extender.options.sendAllAction, cfg);
  1387. avaSendBatch(ss);
  1388. }
  1389.  
  1390. function avaSendAll_Loop(region, subRegion, alliance_id, retry){
  1391.  
  1392. $.ajax({
  1393. url: "/play/camp_attack_create?alliance_id=" + alliance_id + "&region=" + region + "&subregion=" + subRegion + "&acnew=" + userContext.playerData.ac,
  1394. dataType: "JSON",
  1395. success: function(a) {
  1396. if(a.camp_attack == void 0){
  1397. error("No response was received for the given parameters.", "AVA");
  1398. return;
  1399. }
  1400.  
  1401. userContext.camp_battle = a.battle;
  1402. userContext.camp_trade = a.trade;
  1403. userContext.camp_intrigue = a.intrigue;
  1404. userContext.campAttackData = a;
  1405. startCampAttack(userContext.campAttackData);
  1406.  
  1407. var ss = getSwornSwords(extender.options.sendAllAction, {
  1408. region: region,
  1409. subRegion: (extender.options.avaSendSubcamps ? -1 : subRegion)
  1410. });
  1411.  
  1412. avaSendBatch(ss);
  1413. },
  1414. error: function(e) {
  1415. warn("Something went wrong: " + e.status + " : " + e.statusText + ", extender will try again...");
  1416.  
  1417. setTimeout(function() {
  1418. retry
  1419. ? (retry < 7
  1420. ? avaSendAll_Loop(region, subRegion, alliance_id, retry)
  1421. : error("Extender failed to send attack on this camp after 7 retries.", "CAMP"))
  1422. : avaSendAll_Loop(region, subRegion, alliance_id, 1);
  1423. }, 2000);
  1424. }
  1425. });
  1426. }
  1427.  
  1428. var questMan = (function ($) {
  1429. var _this = {
  1430.  
  1431. delay: 4E3,
  1432. enabled: true,
  1433. striveFor: ["old_ways", "truthful", "realm"],
  1434.  
  1435. init: function (o) {
  1436. try {
  1437.  
  1438. this.config(o);
  1439. this.analyze();
  1440.  
  1441. log("Quest manager initialized.", "QUESTMAN");
  1442.  
  1443. } catch (err) {
  1444. error(err);
  1445. }
  1446. },
  1447.  
  1448. config: function (o) {
  1449. //console.debug(o);
  1450.  
  1451. this.delay = o.questManagerDelay * 1E3 || this.delay;
  1452. this.enabled = o.questManagerEnabled != void 0 ? o.questManagerEnabled : this.enabled;
  1453. },
  1454.  
  1455. analyze: function () {
  1456. if (!this.enabled) {
  1457. log("This feature has been disabled. Exiting...", "QUESTMAN");
  1458. return;
  1459. }
  1460.  
  1461. for (var i = 0; i < userContext.actions.length; i++) {
  1462. var action = userContext.actions[i];
  1463.  
  1464. log("Debuging quest analysis: " +
  1465. "quest: " + action.label + ", " +
  1466. "id: " + action.id + ", " +
  1467. "complete: " + (action.complete ? "Yes" : "No") + ", " +
  1468. "remaining: " + action.seconds_remaining + "s");
  1469.  
  1470. // Action is complete => FINISH
  1471. if(action.complete){
  1472. log("Action is complete, attempt to finish up.", "QUESTMAN");
  1473. this.playQuest(action.id); // TODO: Log loot!
  1474.  
  1475. // don't monitor
  1476. // this quest
  1477. continue;
  1478. }
  1479.  
  1480. // Action is in progress => WAIT
  1481. if(!action.complete && action.seconds_remaining){
  1482. log("Action is in progress, QuestMan will wait.", "QUESTMAN");
  1483. setTimeout(function(){
  1484. _this.analyze();
  1485. }, action.seconds_remaining * 1E3 + _this.delay);
  1486.  
  1487. continue;
  1488. }
  1489.  
  1490. // Action expects user input => ACT
  1491. log("Taking action...", "QUESTMAN");
  1492. this.playQuest(action.id, function(r){
  1493. if(r.option == void 0 || !r.option.length){
  1494. error("I am not given options!", "QUESTMAN");
  1495. return;
  1496. }
  1497.  
  1498. var choice = _this.analyzeOptions(r.option);
  1499. // TODO: Verify if this indeed need to be hardcoded
  1500. _this.submitQuest("action", choice, r.chosen, r.id);
  1501.  
  1502. });
  1503. }
  1504. },
  1505.  
  1506. // PLay quest, call callback fn
  1507. playQuest: function(questId, andThen){
  1508.  
  1509. $.ajax({
  1510.  
  1511. url: '/play/quest',
  1512. data: {
  1513. quest_id: questId
  1514. },
  1515.  
  1516. success: function(response){ // if callback, call with response
  1517. typeof andThen == "function" && andThen(response);
  1518. },
  1519.  
  1520. error: function(response){ // if callback, call with response
  1521. typeof andThen == "function" && andThen(response);
  1522. }
  1523. })
  1524. },
  1525.  
  1526. analyzeOptions: function(options){
  1527. var bestOption = 1;
  1528. for(var i = 0; i < options.length; i ++){
  1529. var o = options[i];
  1530.  
  1531. var loot = o.reward_loot || (o.change_reward != void 0 ? o.change_reward.reward_loot : "");
  1532. var silver = o.reward_silver || (o.change_reward != void 0 ? o.change_reward.reward_silver : 0);
  1533. var alignment = o.alignment || (o.change_reward != void 0 ? o.change_reward.alignment : "none");
  1534. console.log("options analysis, loot:", loot, "silver:", silver, "alignment:", alignment);
  1535.  
  1536. if(this.striveFor.contains(alignment)){
  1537. bestOption = i + 1;
  1538. break;
  1539. }
  1540. }
  1541.  
  1542. console.log("Best option resolved: ", options[bestOption]);
  1543. return bestOption;
  1544. },
  1545.  
  1546. submitQuest: function(stage, choice, chosen, questId, andThen){
  1547. $.ajax({
  1548.  
  1549. url: '/play/quest',
  1550. data: {
  1551. quest_id: questId,
  1552. stage: stage,
  1553. choice: choice,
  1554. chosen: chosen,
  1555. },
  1556.  
  1557. success: function(response){ // if callback, call with response
  1558. typeof andThen == "function" && andThen(response);
  1559. },
  1560.  
  1561. error: function(response){ // if callback, call with response
  1562. typeof andThen == "function" && andThen(response);
  1563. }
  1564. })
  1565. }
  1566.  
  1567. };
  1568.  
  1569. return _this;
  1570. }($));
  1571.  
  1572. function getRandomInt(min, max) {
  1573. return Math.floor(Math.random() * (max - min)) + min;
  1574. }
  1575.  
  1576. function findGifts(e) {
  1577. e.preventDefault();
  1578.  
  1579. var loadingBar = $("#loading_bar");
  1580. if(!loadingBar.length) {
  1581. var container = $('div#modals_container_alerts div#modalwrap.alertmodal.modalwrap_input div.alertbox div.alertboxinner div.alertinput');
  1582. container.prepend('<div id="loading_bar" class="loadingbar-inner" style="width: 1%; position: relative; top: -20px; margin-left: 15px;"></div>');
  1583. }
  1584.  
  1585. var limit = parseInt($('#offerIdRangeEnd').val());
  1586. var startId = parseInt($('#offerIdRangeStart').val());
  1587. if(extender.options.lastIdChecked !== limit){
  1588. extender.option("lastIdChecked", limit)
  1589. }
  1590.  
  1591. find_codes(startId, limit)
  1592.  
  1593. }
  1594. function pvpLaunch_1(callback) {
  1595. json = { pvp: {} };
  1596. json.pvp.target_id = pvpForm.target_id;
  1597. json.pvp.sworn_sword_id = userContext.setSwornSword.id;
  1598. json.pvp.pvp_action_symbol = userContext.currentActionLabel;
  1599. json.pvp.region_symbol = fealtySymbol[pvpForm.target_faction_id];
  1600. pvpForm.sub_region_index = Math.floor(3 * Math.random());
  1601. var b = pvpForm.target_faction_id;
  1602. void 0 == b && (b = userContext.playerData.character.faction_id);
  1603. json.pvp.sub_region_symbol = fealtySubRegions[b][pvpForm.sub_region_index].symbol;
  1604. json.pvp.attack_value = userContext.currentQuest.action_type[userContext.currentActionLabel].attacker_strength;
  1605. json.pvp.defense_value = userContext.currentQuest.action_type[userContext.currentActionLabel].defender_strength;
  1606.  
  1607. log( pvpForm.target_id);
  1608. log( userContext.currentActionLabel);
  1609. log( fealtySymbol[pvpForm.target_faction_id]);
  1610. log( userContext.playerData.character.faction_id);
  1611. log( fealtySubRegions[b][pvpForm.sub_region_index].symbol);
  1612. log( userContext.currentQuest.action_type[userContext.currentActionLabel].attacker_strength);
  1613. log( userContext.currentQuest.action_type[userContext.currentActionLabel].defender_strength);
  1614.  
  1615. false || (showSpinner(),
  1616.  
  1617. $.ajax({
  1618. type: "get",
  1619. url: "/pvps/create",
  1620. data: json,
  1621. dataType: "JSON",
  1622. complete: hideSpinner,
  1623. success: function(a) {
  1624. void 0 == a.error ?
  1625. (pvpForm = {},
  1626. userContext.pvp = a,
  1627. pvpRenderProgress(a),
  1628. insertInventoryFromItem(playerInventory, a.attacker.sworn_sword),
  1629. analytics.track("PvP Start", { pvp_context: "attack", pvp_action: json.pvp.pvp_action_symbol }),
  1630. analytics.wizardtrack("PvP Start", {
  1631. pvp_context: "attack",
  1632. pvp_action: json.pvp.pvp_action_symbol
  1633. }))
  1634. : handleSwornSwordError(a.error);
  1635.  
  1636. // EXTENDER :: Modification, call callback
  1637. // parameter indicates successfull sending
  1638. if (typeof callback == "function") {
  1639. if(a.error && a.error === "too_many_attacks") {
  1640. //inform("Too MANY ATTACKS! StAHP STAAAHP!");
  1641. pvpFormStore = null;
  1642. return;
  1643. }
  1644.  
  1645. callback(!a.error);
  1646. }
  1647. }
  1648. }));
  1649. }
  1650. function pvpLaunch_2(callback) {
  1651. json = { pvp: {} };
  1652. log("pvpLaunch2 pvpLaunch2 pvpLaunch2") ;
  1653. json.pvp.target_id = 25334790;
  1654. json.pvp.sworn_sword_id = userContext.setSwornSword.id;
  1655. json.pvp.pvp_action_symbol = "fight";
  1656. json.pvp.region_symbol = undefined;
  1657. var Sub_region_index = Math.floor(3 * Math.random());
  1658. var b = undefined ;
  1659. void 0 == b && (b = userContext.playerData.character.faction_id);
  1660. json.pvp.sub_region_symbol = fealtySubRegions[b][Sub_region_index].symbol;
  1661. json.pvp.attack_value = 11111;
  1662. json.pvp.defense_value = 11;
  1663. false || (showSpinner(),
  1664. $.ajax({
  1665. type: "get",
  1666. url: "/pvps/create",
  1667. data: json,
  1668. dataType: "JSON",
  1669. complete: hideSpinner,
  1670. success: function(a) {
  1671. void 0 == a.error ?
  1672. (pvpForm = {},
  1673. userContext.pvp = a,
  1674. pvpRenderProgress(a),
  1675. insertInventoryFromItem(playerInventory, a.attacker.sworn_sword),
  1676. analytics.track("PvP Start", { pvp_context: "attack", pvp_action: json.pvp.pvp_action_symbol }),
  1677. analytics.wizardtrack("PvP Start", {
  1678. pvp_context: "attack",
  1679. pvp_action: json.pvp.pvp_action_symbol
  1680. }))
  1681. : handleSwornSwordError(a.error);
  1682.  
  1683. // EXTENDER :: Modification, call callback
  1684. // parameter indicates successfull sending
  1685. if (typeof callback == "function") {
  1686. if(a.error && a.error === "too_many_attacks") {
  1687. //inform("Too MANY ATTACKS! StAHP STAAAHP!");
  1688. pvpFormStore = null;
  1689. return;
  1690. }
  1691.  
  1692. callback(!a.error);
  1693. }
  1694. }
  1695. }));
  1696. }
  1697. function find_codes(id, limit, gift_codes) {
  1698. if(gift_codes == void 0) {
  1699. gift_codes = [];
  1700. }
  1701.  
  1702. $.ajax({
  1703. url: '/play/special_offer/' + id,
  1704. success: function (data) {
  1705.  
  1706. var regexp = />[A-Z, 1-9]{4}-[A-Z, 1-9]{4}-[A-Z, 1-9]{4}-[A-Z, 1-9]{4}</
  1707. var code = data.match(regexp);
  1708.  
  1709. if(code && gift_codes.indexOf(code[0]) == -1) {
  1710. gift_codes.push(code + " Offer id: " + id);
  1711. console.log('Found code in page with id: ' + id + ', ' + code);
  1712. }
  1713. },
  1714. statusCode: {
  1715. 404: function () {
  1716. console.log('Page with id ' + id + ' is non existent.');
  1717. },
  1718. 500: function () {
  1719. console.log('Page with id ' + id + ' returned an internal server error.');
  1720. }
  1721. },
  1722. complete: function () {
  1723.  
  1724. var loadingBar = $("#loading_bar");
  1725.  
  1726. // SET LIMIT HERE!
  1727. if (id < limit) {
  1728.  
  1729. var percent = ((id + 1) / limit) * 100;
  1730. loadingBar.css("width", percent + "%");
  1731.  
  1732. setTimeout(function () {
  1733. find_codes(++id, limit, gift_codes);
  1734. }, 100);
  1735.  
  1736. } else {
  1737.  
  1738. if(!gift_codes.length) {
  1739. loadingBar.after("No active codes found in that range.");
  1740. alert("No active codes found in that range.");
  1741. } else {
  1742. for (var j = 0; j < gift_codes.length; j++) {
  1743. var code = gift_codes[j];
  1744. loadingBar.after(code + "<br>");
  1745. }
  1746.  
  1747. var message = "Active gift codes found!\n";
  1748. for (var k = 0; k < gift_codes.length; k++) {
  1749. message += gift_codes[k] + "\n";
  1750. }
  1751.  
  1752. alert(message);
  1753. }
  1754. }
  1755. }
  1756. });
  1757.  
  1758. }