The West - Skills Calc

Calculates and displays sum of skills obtained from items (including bonuses from sets and character's per-level attributes and skills)

当前为 2015-03-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @namespace https://greasyfork.org/users/8637
  3. // @name The West - Skills Calc
  4. // @author neversleep1911
  5. // @description Calculates and displays sum of skills obtained from items (including bonuses from sets and character's per-level attributes and skills)
  6. // @include http://*.the-west.*/game.php*
  7. // @grant none
  8. // @version 2.0.1
  9. // @copyright Copyright (c) 2015 neversleep1911 (full list of contributors/translators see here: https://greasyfork.org/scripts/7829)
  10. // @license MIT (http://opensource.org/licenses/MIT)
  11. // ==/UserScript==
  12.  
  13. (function(func) {
  14. var script;
  15. script = document.createElement("script");
  16. script.setAttribute("type", "text/javascript");
  17. script.textContent = "(" + func.toString() + ")();";
  18. document.body.appendChild(script);
  19. document.body.removeChild(script);
  20. return true;
  21. })(function() {
  22. var I18n, TWSkillsCalc;
  23. I18n = function() {
  24. var DEFAULT_LANGUAGE, STRINGS, language;
  25. function I18n() {}
  26. DEFAULT_LANGUAGE = "en_US";
  27. STRINGS = {
  28. en_US: {
  29. dialogs: {
  30. add_item: "Add item",
  31. add_character_items: "Character's items"
  32. },
  33. groups: {
  34. skills: "Skills",
  35. items: "Items"
  36. },
  37. buttons: {
  38. character: "Character",
  39. add: "Add",
  40. reset: "Reset"
  41. },
  42. checkboxes: {
  43. show_bonus: "Show bonus",
  44. show_skills: "Show my skills"
  45. },
  46. labels: {
  47. level: "Level",
  48. item_id: "Item ID",
  49. character_name: "Character's name",
  50. health: "Health"
  51. },
  52. tooltip: {
  53. health: "Normal / Solder / Solder with bonus"
  54. },
  55. errors: {
  56. player_not_found: "Player not found!"
  57. }
  58. },
  59. pl_PL: {
  60. dialogs: {
  61. add_item: "Dodaj przedmiot",
  62. add_character_items: "Przedmioty gracza"
  63. },
  64. groups: {
  65. skills: "Umiejętności",
  66. items: "Przedmioty"
  67. },
  68. buttons: {
  69. character: "Gracz",
  70. add: "Dodaj",
  71. reset: "Reset"
  72. },
  73. checkboxes: {
  74. show_bonus: "Pokaż bonus",
  75. show_skills: "Pokaż moje umiejętności"
  76. },
  77. labels: {
  78. level: "Level",
  79. item_id: "ID przedmiotu",
  80. character_name: "Nazwa gracza",
  81. health: "Życie"
  82. },
  83. tooltip: {
  84. health: "Normalny / Żołnierz / Żołnierz z bonusem"
  85. },
  86. errors: {
  87. player_not_found: "Gracz nie znaleziony!"
  88. }
  89. },
  90. ru_RU: {
  91. dialogs: {
  92. add_item: "Добавить предмет",
  93. add_character_items: "Предметы персонажа"
  94. },
  95. groups: {
  96. skills: "Навыки",
  97. items: "Предметы"
  98. },
  99. buttons: {
  100. character: "Персонаж",
  101. add: "Добавить",
  102. reset: "Сбросить"
  103. },
  104. checkboxes: {
  105. show_bonus: "Показывать бонусы",
  106. show_skills: "Показывать мои навыки"
  107. },
  108. labels: {
  109. level: "Уровень",
  110. item_id: "ID предмета",
  111. character_name: "Имя персонажа",
  112. health: "Здоровье"
  113. },
  114. tooltip: {
  115. health: "Минимум / Солдат / Солдат с бонусом"
  116. },
  117. errors: {
  118. player_not_found: "Игрок не найден!"
  119. }
  120. }
  121. };
  122. language = DEFAULT_LANGUAGE;
  123. I18n.language = function() {
  124. return language;
  125. };
  126. I18n.setLanguage = function(lang) {
  127. return language = STRINGS[lang] ? lang : DEFAULT_LANGUAGE;
  128. };
  129. I18n.tr = function(id) {
  130. var string;
  131. string = STRINGS[language];
  132. $(id.split(".")).each(function(k, v) {
  133. return (string = string[v]) !== void 0;
  134. });
  135. return string || id;
  136. };
  137. return I18n;
  138. }();
  139. TWSkillsCalc = function() {
  140. var ItemCalculator, Window;
  141. TWSkillsCalc.prototype.NAME = "Skills Calc";
  142. TWSkillsCalc.prototype.MENU_BUTTON_IMAGE = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAZCAIAAAD8NuoTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3QQICBgbVC0rcwAACI5JREFUSMel10tvXF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iOJ3WCAAEw2xakmJhO7TS867v2Tdo42qlHiUCSuhXP7gwM1nAkqrlM7erdGNdefyIsji/lCe9vCkmdVwqDUQtnzhmGAhVIzWVMYZKWgSgwxIfQpdyR6COqo4eyZ4rWzMZ/eyIdeiF375y8ve/eHKIRf1aP05gulwMws6DrV25sLR3sK9ocqUjkRQekNrYvtXo7JIPPt7YbLYvXixtbUMci5iyWEDgs0yK1+sxZ0JDKEDwlxePTYxl9lf3s6M5WVc5hI1PbxwvFeauvPLFtfe3q3sCNLGob9r+5YI8CHXzrt30BXnh4kSvc6iloCYRjzFM0KyhDClS7NE2FRAIxrkI6epWK+qEw4WMbhGc0/lBzTzxjDR9KuSqXhrLl5TJscKl+VOxWB2QmpuckVUB//76+UOReevtf9u+NDWUcnicVlXap1wDQQB1whAGcSQ4QBzLUhxxCLR0yqUBTyK7z9OUT0zpjz991v16ZfmRea4u/fHP7+xGIiOritZXuc7jSFItGrbqkRg1VcejwsgYMQNy3Le5NGRmM1Jlx85IYqMVLS2P/mSpWK3uwN+9/EgaoY1Go+6y0VKx7UT1jud6Hog9azivQZ4GwJCArOB0SklZWJWBFwotV/Tttt13uGg9dWFaTrpr2xZ1SYaA7S7dPTwYGRn1YlFttnuURf12TtUky8i5sJghHg7KukZ0nM7+/zsAkbGuvSM1e5OPphNjaGd3/38JylRK90rXAwAAAABJRU5ErkJggg==";
  143. TWSkillsCalc.prototype.windows = {};
  144. function TWSkillsCalc() {
  145. var menuButton, self;
  146. self = this;
  147. I18n.setLanguage(Game.locale);
  148. TheWestApi.register("tw_skills_calc", this.NAME, "2.17", Game.version.toString(), "neversleep1911", "").setGui("Copyrights, changelog and other details see <a href='https://greasyfork.org/scripts/7829'>here</a>.");
  149. menuButton = $("<div class='menulink' title='" + this.NAME + "' />");
  150. menuButton.css("background-image", "url(" + this.MENU_BUTTON_IMAGE + ")");
  151. menuButton.hover(function() {
  152. $(this).css("background-position", "-25px 0px");
  153. return true;
  154. }, function() {
  155. $(this).css("background-position", "0px 0px");
  156. return true;
  157. }).on("click", function() {
  158. self.createWindow();
  159. return false;
  160. });
  161. $("div#ui_menubar").append($('<div class="ui_menucontainer" />').append(menuButton).append('<div class="menucontainer_bottom" />'));
  162. }
  163. TWSkillsCalc.prototype.createWindow = function() {
  164. var self, wndId;
  165. self = this;
  166. while (true) {
  167. wndId = Math.ceil(Math.random() * 1024);
  168. if (!this.windows[wndId]) {
  169. break;
  170. }
  171. }
  172. this.windows[wndId] = new Window({
  173. id: wndId,
  174. title: this.NAME,
  175. miniTitle: this.NAME
  176. });
  177. this.windows[wndId].onDestroy = function(wnd) {
  178. delete self.windows[wnd.id];
  179. return true;
  180. };
  181. return true;
  182. };
  183. Window = function() {
  184. function Window(options) {
  185. var self;
  186. self = this;
  187. this.images = {};
  188. this.items = {};
  189. this.calculator = new ItemCalculator();
  190. this.calculator.level = Character.level;
  191. this.id = options.id;
  192. this.wnd = wman.open("skills-calc-window-" + options.id, null, "noreload");
  193. this.wnd.setTitle(options.title);
  194. this.wnd.setMiniTitle(options.miniTitle);
  195. this.wnd.addEventListener("WINDOW_DESTROY", function() {
  196. if (self.onDestroy) {
  197. return self.onDestroy(self);
  198. } else {
  199. return true;
  200. }
  201. });
  202. this.wnd.appendToContentPane((this.groupSkills = this.createGroup({
  203. id: "skills",
  204. title: I18n.tr("groups.skills"),
  205. css: {
  206. width: 402,
  207. position: "absolute"
  208. },
  209. scrollPane: {
  210. css: {
  211. height: 264
  212. }
  213. }
  214. })).getMainDiv());
  215. this.wnd.appendToContentPane((this.groupItems = this.createGroup({
  216. id: "items",
  217. title: I18n.tr("groups.items"),
  218. css: {
  219. left: 400,
  220. width: 294
  221. },
  222. scrollPane: {
  223. css: {
  224. height: 264
  225. }
  226. }
  227. })).getMainDiv());
  228. this.wnd.appendToContentPane(this.createButton({
  229. text: I18n.tr("buttons.reset"),
  230. css: {
  231. left: 592,
  232. top: 342,
  233. position: "absolute"
  234. },
  235. onclick: function(button, data) {
  236. return self.onButtonResetClick(button, data);
  237. }
  238. }).getMainDiv());
  239. this.wnd.appendToContentPane(this.createButton({
  240. text: I18n.tr("buttons.add"),
  241. css: {
  242. left: 490,
  243. top: 342,
  244. position: "absolute"
  245. },
  246. onclick: function(button, data) {
  247. return self.onButtonAddClick(button, data);
  248. }
  249. }).getMainDiv());
  250. this.wnd.appendToContentPane(this.createButton({
  251. text: I18n.tr("buttons.character"),
  252. css: {
  253. left: 388,
  254. top: 342,
  255. position: "absolute"
  256. },
  257. onclick: function(button, data) {
  258. return self.onButtonCharacterClick(button, data);
  259. }
  260. }).getMainDiv());
  261. this.wnd.appendToContentPane((this.checkboxBonus = this.createCheckbox({
  262. text: I18n.tr("checkboxes.show_bonus"),
  263. css: {
  264. left: 2,
  265. top: 324,
  266. position: "absolute"
  267. },
  268. onclick: function(state) {
  269. return self.onCheckboxBonusClick(state);
  270. }
  271. })).getMainDiv());
  272. this.wnd.appendToContentPane((this.checkboxSkills = this.createCheckbox({
  273. text: I18n.tr("checkboxes.show_skills"),
  274. css: {
  275. left: 2,
  276. top: 350,
  277. position: "absolute"
  278. },
  279. onclick: function(state) {
  280. return self.onCheckboxSkillsClick(state);
  281. }
  282. })).getMainDiv());
  283. this.wnd.appendToContentPane((this.textfieldLevel = this.createTextfield({
  284. size: 6,
  285. label: I18n.tr("labels.level"),
  286. value: this.calculator.level,
  287. css: {
  288. left: 180,
  289. top: 320,
  290. position: "absolute"
  291. }
  292. })).getMainDiv());
  293. this.wnd.appendToContentPane(this.labelHealth = $("<span />").css({
  294. position: "absolute",
  295. left: 180,
  296. top: 352
  297. }).attr("title", I18n.tr("tooltip.health")));
  298. this.textfieldLevel.onlyNumeric().getField().keyup(function(e) {
  299. var level;
  300. level = parseInt(self.textfieldLevel.getValue());
  301. if (!isNaN(level) && level > 0) {
  302. self.calculator.level = level;
  303. self.recalc();
  304. self.repaint();
  305. }
  306. return true;
  307. });
  308. this.initGroupSkills();
  309. this.initGroupItems();
  310. }
  311. Window.prototype.createGroup = function(options) {
  312. var content, group, scrollPane;
  313. group = new west.gui.Groupframe("", "<div class='skills-calc-" + options.id + "'></div>");
  314. $(group.getMainDiv()).css(options.css);
  315. scrollPane = new west.gui.Scrollpane();
  316. $(scrollPane.getMainDiv()).css(options.scrollPane.css);
  317. content = $(".skills-calc-" + options.id, group.getMainDiv());
  318. if (options.title) {
  319. content.append($("<h2>" + options.title + "</h2><hr style='margin-top: 2px; margin-bottom: 4px;' />"));
  320. }
  321. content.append(scrollPane.getMainDiv());
  322. group.scrollPane = scrollPane;
  323. group.appendToScrollContentPane = function(content) {
  324. scrollPane.appendContent(content);
  325. return group;
  326. };
  327. return group;
  328. };
  329. Window.prototype.createButton = function(options) {
  330. var button;
  331. button = new west.gui.Button(options.text);
  332. button.click(options.onclick);
  333. $(button.getMainDiv()).css(options.css);
  334. return button;
  335. };
  336. Window.prototype.createCheckbox = function(options) {
  337. var checkbox;
  338. checkbox = new west.gui.Checkbox(options.text, "", options.onclick);
  339. $(checkbox.getMainDiv()).css(options.css);
  340. return checkbox;
  341. };
  342. Window.prototype.createTextfield = function(options) {
  343. var field;
  344. field = new west.gui.Textfield();
  345. field.setSize(options.size);
  346. field.setLabel($("<span>" + options.label + "</span>"));
  347. if (options.value) {
  348. field.setValue(options.value);
  349. }
  350. if (options.css) {
  351. $(field.getMainDiv()).css(options.css);
  352. }
  353. return field;
  354. };
  355. Window.prototype.initGroupSkills = function() {
  356. var attr, attrs, div, img, skill, skillType, skills, _i, _j, _len, _len1;
  357. attrs = CharacterSkills.allAttrKeys;
  358. for (_i = 0, _len = attrs.length; _i < _len; _i++) {
  359. attr = attrs[_i];
  360. div = $('<div style="height: 41px;" />');
  361. skills = CharacterSkills.skillKeys4Attr[attr];
  362. for (_j = 0, _len1 = skills.length; _j < _len1; _j++) {
  363. skillType = skills[_j];
  364. skill = new Skill(skillType);
  365. img = skill.getSkillImage();
  366. img.removeAttr("class").css({
  367. width: 72,
  368. display: "inline-block",
  369. "text-align": "center",
  370. "font-weight": "bold",
  371. "margin-left": 2
  372. });
  373. $("img.skillicon", img).removeAttr("class").css({
  374. width: "100%"
  375. });
  376. $("span.skillpoints_label", img).attr("class", "skills-calc-skillpoints_label").css({
  377. display: "inline-block",
  378. position: "relative",
  379. top: -16,
  380. width: "100%",
  381. height: 12,
  382. color: "#ffffff",
  383. "text-align": "center",
  384. "font-size": "9pt",
  385. "text-shadow": "1px 1px 1px rgb(0, 0, 0)",
  386. "background-image": "url('http://westzz.innogamescdn.com/images/tw2gui/plusminus/plusminus_display_bg2.png')"
  387. });
  388. div.append(img);
  389. this.images[skillType] = img.get(0);
  390. }
  391. this.groupSkills.appendToScrollContentPane(div);
  392. this.groupSkills.appendToScrollContentPane($('<hr style="margin: 12px 0;" />'));
  393. }
  394. return true;
  395. };
  396. Window.prototype.initGroupItems = function() {
  397. var items, slot, _i, _len, _ref;
  398. items = [];
  399. _ref = Wear.slots;
  400. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  401. slot = _ref[_i];
  402. if (Wear.wear[slot]) {
  403. items.push(Wear.wear[slot].obj);
  404. }
  405. }
  406. this.addItems(items);
  407. return this.repaint();
  408. };
  409. Window.prototype.onButtonCharacterClick = function() {
  410. var content, dlg, ok, self, textfieldName;
  411. self = this;
  412. dlg = new west.gui.Dialog(I18n.tr("dialogs.add_character_items"));
  413. content = $("<div style='margin-top: 10px; text-align: center;'></div>");
  414. content.append((textfieldName = this.createTextfield({
  415. size: 25,
  416. label: I18n.tr("labels.character_name")
  417. })).getMainDiv());
  418. ok = function() {
  419. var name;
  420. if (!(name = $.trim(textfieldName.getValue()))) {
  421. return false;
  422. }
  423. self.wnd.showLoader();
  424. return Ajax.remoteCallMode("ranking", "get_data", {
  425. rank: NaN,
  426. search: name = name.toLowerCase(),
  427. tab: "duels"
  428. }, function(json) {
  429. var found, player, _i, _len, _ref;
  430. if (json.error) {
  431. self.wnd.hideLoader();
  432. return new UserMessage(json.msg, UserMessage.TYPE_ERROR).show();
  433. }
  434. found = false;
  435. _ref = json.ranking;
  436. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  437. player = _ref[_i];
  438. found = player.name.toLowerCase() === name;
  439. if (found) {
  440. Ajax.remoteCallMode("profile", "init", {
  441. name: player.name,
  442. playerId: player.id
  443. }, function(resp) {
  444. var items, slot, _j, _len1, _ref1;
  445. if (resp.error) {
  446. self.wnd.hideLoader();
  447. return new UserMessage(resp.message, UserMessage.TYPE_ERROR).show();
  448. } else {
  449. items = [];
  450. _ref1 = Wear.slots;
  451. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  452. slot = _ref1[_j];
  453. if (resp.wear[slot]) {
  454. items.push(ItemManager.get(resp.wear[slot]));
  455. }
  456. }
  457. self.reset();
  458. self.calculator.level = resp.level;
  459. self.textfieldLevel.setValue(resp.level);
  460. self.addItems(items);
  461. self.repaint();
  462. self.wnd.hideLoader();
  463. }
  464. return true;
  465. });
  466. break;
  467. }
  468. }
  469. if (!found) {
  470. self.wnd.hideLoader();
  471. new UserMessage(I18n.tr("errors.player_not_found"), UserMessage.TYPE_ERROR).show();
  472. }
  473. return true;
  474. });
  475. };
  476. textfieldName.getField().keypress(function(e) {
  477. if (e.which === 13 && ok()) {
  478. dlg.hide();
  479. }
  480. return true;
  481. });
  482. dlg.setText(content).addButton("ok", ok).addButton("cancel").show();
  483. textfieldName.getField().focus();
  484. return true;
  485. };
  486. Window.prototype.onButtonAddClick = function() {
  487. var content, dlg, ok, self, textfieldId;
  488. self = this;
  489. dlg = new west.gui.Dialog(I18n.tr("dialogs.add_item"));
  490. content = $('<div style="margin-top: 10px; text-align: center;"><div id="skills-calc-preview-item" style="height: 60px; width: 60px; float: right; border: 1px solid black; border-radius: 4px;" /></div>');
  491. content.append((textfieldId = this.createTextfield({
  492. size: 25,
  493. label: I18n.tr("labels.item_id")
  494. })).maxlength(8).getMainDiv());
  495. textfieldId.getItem = function() {
  496. var id;
  497. id = parseInt(this.getValue());
  498. if (isNaN(id)) {
  499. return void 0;
  500. } else {
  501. return ItemManager.get(id);
  502. }
  503. };
  504. ok = function() {
  505. var item;
  506. if (item = textfieldId.getItem()) {
  507. self.addItems([ item ]);
  508. self.repaint();
  509. return true;
  510. } else {
  511. return false;
  512. }
  513. };
  514. dlg.setText(content).addButton("ok", ok).addButton("cancel").show();
  515. textfieldId.getField().keypress(function(e) {
  516. if (e.which === 13 && ok()) {
  517. dlg.hide();
  518. }
  519. return true;
  520. }).keyup(function(e) {
  521. var item, preview;
  522. preview = $("#skills-calc-preview-item", content).empty();
  523. if (item = textfieldId.getItem()) {
  524. item = new tw2widget.InventoryItem(item);
  525. preview.append($(item.getMainDiv()).css({
  526. "float": "none"
  527. }));
  528. }
  529. return true;
  530. }).focus();
  531. return true;
  532. };
  533. Window.prototype.onButtonResetClick = function() {
  534. return this.reset();
  535. };
  536. Window.prototype.onCheckboxBonusClick = function(state) {
  537. return this.repaint();
  538. };
  539. Window.prototype.onCheckboxSkillsClick = function(state) {
  540. this.recalc();
  541. return this.repaint();
  542. };
  543. Window.prototype.addItems = function(items) {
  544. var item, self, _i, _len;
  545. self = this;
  546. for (_i = 0, _len = items.length; _i < _len; _i++) {
  547. item = items[_i];
  548. this.removeItem(item);
  549. this.items[item.type] = new tw2widget.InventoryItem(item);
  550. $(this.items[item.type].getMainDiv()).css({
  551. "float": "none",
  552. display: "inline-block"
  553. }).on("click", function(e) {
  554. if (!e.shiftKey) {
  555. item = ItemManager.get($(e.target).data("itemId"));
  556. if (item && self.removeItem(item)) {
  557. self.repaint();
  558. }
  559. }
  560. return true;
  561. });
  562. this.calculator.sumItem(item);
  563. this.groupItems.appendToScrollContentPane($(this.items[item.type].getMainDiv()));
  564. }
  565. return true;
  566. };
  567. Window.prototype.removeItem = function(item) {
  568. if (this.items[item.type]) {
  569. this.calculator.subItem(this.items[item.type].obj);
  570. $(this.items[item.type].getMainDiv()).remove();
  571. delete this.items[item.type];
  572. return true;
  573. } else {
  574. return false;
  575. }
  576. };
  577. Window.prototype.recalc = function() {
  578. var item, skill, type, value, _ref, _ref1;
  579. this.calculator.reset();
  580. _ref = this.items;
  581. for (type in _ref) {
  582. item = _ref[type];
  583. this.calculator.sumItem(item.obj);
  584. }
  585. if (this.checkboxSkills.isSelected()) {
  586. _ref1 = CharacterSkills.skills;
  587. for (skill in _ref1) {
  588. value = _ref1[skill];
  589. this.calculator.calcSkill(skill, value.points, ItemCalculator.SUM_OP);
  590. }
  591. }
  592. return true;
  593. };
  594. Window.prototype.repaint = function() {
  595. var health, min_health, result, skill, value;
  596. result = this.checkboxBonus.isSelected() ? this.calculator.resultWithBonus() : this.calculator.result;
  597. for (skill in result) {
  598. value = result[skill];
  599. if (this.images[skill]) {
  600. $("span.skills-calc-skillpoints_label", this.images[skill]).text(value);
  601. }
  602. }
  603. min_health = this.calculator.level * 10 + 90;
  604. health = {
  605. normal: format_number(min_health + result.health * 10),
  606. solder: format_number(min_health + result.health * 15),
  607. solderBonus: format_number(min_health + result.health * 20),
  608. toString: function() {
  609. return "" + this.normal + " / " + this.solder + " / " + this.solderBonus;
  610. }
  611. };
  612. this.labelHealth.text("" + I18n.tr("labels.health") + ": " + health.toString());
  613. return true;
  614. };
  615. Window.prototype.reset = function() {
  616. var item, type, _ref;
  617. _ref = this.items;
  618. for (type in _ref) {
  619. item = _ref[type];
  620. $(item.getMainDiv()).remove();
  621. }
  622. $("span.skills-calc-skillpoints_label", this.groupSkills.getMainDiv()).text("0");
  623. this.items = {};
  624. this.calculator.reset();
  625. this.calculator.level = Character.level;
  626. this.textfieldLevel.setValue(Character.level);
  627. this.labelHealth.text("" + I18n.tr("labels.health") + ": 0 / 0 / 0");
  628. this.checkboxBonus.setSelected(false, true);
  629. this.checkboxSkills.setSelected(false, true);
  630. return true;
  631. };
  632. return Window;
  633. }();
  634. ItemCalculator = function() {
  635. ItemCalculator.SUM_OP = "sum";
  636. ItemCalculator.SUB_OP = "sub";
  637. function ItemCalculator() {
  638. this.level = 1;
  639. this._bonusExtractor = new west.item.BonusExtractor({
  640. level: this.level
  641. });
  642. this._resultStack = [];
  643. this.reset();
  644. }
  645. ItemCalculator.prototype.sum = function(skill, value) {
  646. return this.result[skill] += value;
  647. };
  648. ItemCalculator.prototype.sub = function(skill, value) {
  649. return this.result[skill] -= value;
  650. };
  651. ItemCalculator.prototype.sumItem = function(item) {
  652. return this.calcItem(item, ItemCalculator.SUM_OP);
  653. };
  654. ItemCalculator.prototype.subItem = function(item) {
  655. return this.calcItem(item, ItemCalculator.SUB_OP);
  656. };
  657. ItemCalculator.prototype.calcItem = function(item, operator) {
  658. if (item.bonus.attributes) {
  659. this.calcAttributes(item.bonus.attributes, operator);
  660. }
  661. if (item.bonus.skills) {
  662. this.calcSkills(item.bonus.skills, operator);
  663. }
  664. if (item.bonus.item) {
  665. this.calcBonuses(item.bonus.item, operator);
  666. }
  667. if (item.set) {
  668. this.calcSet(item.set, operator);
  669. }
  670. return this.result;
  671. };
  672. ItemCalculator.prototype.calcAttribute = function(attr, value, operator) {
  673. var skill, skills, _i, _len;
  674. skills = CharacterSkills.skillKeys4Attr[attr];
  675. for (_i = 0, _len = skills.length; _i < _len; _i++) {
  676. skill = skills[_i];
  677. this[operator](skill, value);
  678. }
  679. return this.result;
  680. };
  681. ItemCalculator.prototype.calcAttributes = function(attrs, operator) {
  682. var attr, value;
  683. for (attr in attrs) {
  684. value = attrs[attr];
  685. if (this.isAttribute(attr)) {
  686. this.calcAttribute(attr, value, operator);
  687. }
  688. }
  689. return this.result;
  690. };
  691. ItemCalculator.prototype.calcSkill = function(skill, value, operator) {
  692. return this[operator](skill, value);
  693. };
  694. ItemCalculator.prototype.calcSkills = function(skills, operator) {
  695. var skill, value;
  696. for (skill in skills) {
  697. value = skills[skill];
  698. if (this.isSkill(skill)) {
  699. this.calcSkill(skill, value, operator);
  700. }
  701. }
  702. return this.result;
  703. };
  704. ItemCalculator.prototype.calcBonuses = function(bonuses, operator) {
  705. var bonus, _i, _len;
  706. this._bonusExtractor.character.level = this.level;
  707. for (_i = 0, _len = bonuses.length; _i < _len; _i++) {
  708. bonus = bonuses[_i];
  709. this.calcSkills(this._bonusExtractor.getAffectedSkills(bonus), operator);
  710. }
  711. return this.result;
  712. };
  713. ItemCalculator.prototype.calcSet = function(set, operator) {
  714. switch (operator) {
  715. case ItemCalculator.SUM_OP:
  716. this.itemSets[set] = (this.itemSets[set] || 0) + 1;
  717. break;
  718.  
  719. case ItemCalculator.SUB_OP:
  720. if (this.itemSets[set] && (this.itemSets[set] -= 1) <= 0) {
  721. delete this.itemSets[set];
  722. }
  723. }
  724. return this.result;
  725. };
  726. ItemCalculator.prototype.isAttribute = function(attr) {
  727. return CharacterSkills.allAttrKeys.indexOf(attr) !== -1;
  728. };
  729. ItemCalculator.prototype.isSkill = function(skill) {
  730. return CharacterSkills.allSkillKeys.indexOf(skill) !== -1;
  731. };
  732. ItemCalculator.prototype.reset = function() {
  733. var skill, _i, _len, _ref;
  734. this.itemSets = {};
  735. this.result = {};
  736. _ref = CharacterSkills.allSkillKeys;
  737. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  738. skill = _ref[_i];
  739. this.result[skill] = 0;
  740. }
  741. return this.result;
  742. };
  743. ItemCalculator.prototype.resultWithBonus = function() {
  744. var b, bonus, cb, found, i, itemCount, itemSet, result, setId, stage, value, _i, _j, _k, _l, _len, _len1, _len2, _name, _name1, _ref, _ref1, _ref2;
  745. this.pushResult();
  746. this._bonusExtractor.level = this.level;
  747. bonus = {};
  748. _ref = this.itemSets;
  749. for (setId in _ref) {
  750. itemCount = _ref[setId];
  751. itemSet = west.storage.ItemSetManager.get(setId);
  752. for (i = _i = 1; 1 <= itemCount ? _i <= itemCount : _i >= itemCount; i = 1 <= itemCount ? ++_i : --_i) {
  753. stage = itemSet.bonus[i];
  754. if (stage) {
  755. for (_j = 0, _len = stage.length; _j < _len; _j++) {
  756. b = stage[_j];
  757. switch (b.type) {
  758. case "attribute":
  759. case "skill":
  760. bonus[_name = b.type] || (bonus[_name] = {});
  761. bonus[b.type][b.name] = (bonus[b.type][b.name] || 0) + b.value;
  762. break;
  763.  
  764. case "character":
  765. bonus[_name1 = b.type] || (bonus[_name1] = []);
  766. found = false;
  767. _ref1 = bonus[b.type];
  768. for (i = _k = 0, _len1 = _ref1.length; _k < _len1; i = ++_k) {
  769. cb = _ref1[i];
  770. found = cb.type === b.bonus.type && cb.name === b.bonus.name && cb.key === b.key && cb.roundingMethod === b.roundingMethod;
  771. if (found) {
  772. bonus[b.type][i].value += b.bonus.value;
  773. break;
  774. }
  775. }
  776. if (!found) {
  777. bonus[b.type].push({
  778. type: b.bonus.type,
  779. name: b.bonus.name,
  780. value: b.bonus.value,
  781. key: b.key,
  782. roundingMethod: b.roundingMethod
  783. });
  784. }
  785. }
  786. }
  787. }
  788. }
  789. }
  790. if (bonus.attribute) {
  791. this.calcAttributes(bonus.attribute, ItemCalculator.SUM_OP);
  792. }
  793. if (bonus.skill) {
  794. this.calcSkills(bonus.skill, ItemCalculator.SUM_OP);
  795. }
  796. if (bonus.character) {
  797. _ref2 = bonus.character;
  798. for (_l = 0, _len2 = _ref2.length; _l < _len2; _l++) {
  799. b = _ref2[_l];
  800. if (b.key === "level") {
  801. value = this._bonusExtractor.getRoundedValue(b.value * this.level, b.roundingMethod);
  802. switch (b.type) {
  803. case "attribute":
  804. this.calcAttribute(b.name, value, ItemCalculator.SUM_OP);
  805. break;
  806.  
  807. case "skill":
  808. this.calcSkill(b.name, value, ItemCalculator.SUM_OP);
  809. }
  810. }
  811. }
  812. }
  813. result = this.result;
  814. this.popResult();
  815. return result;
  816. };
  817. ItemCalculator.prototype.pushResult = function() {
  818. return this._resultStack.push($.extend(true, {}, this.result));
  819. };
  820. ItemCalculator.prototype.popResult = function() {
  821. return this.result = this._resultStack.pop();
  822. };
  823. return ItemCalculator;
  824. }();
  825. return TWSkillsCalc;
  826. }();
  827. $(document).ready(function() {
  828. new TWSkillsCalc();
  829. return true;
  830. });
  831. return true;
  832. });