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-02-01 提交的版本,查看 最新版本

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