main.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. (() => {
  2. const level_cap = 99;
  3. // @todo: verify that initial stats are indeed 1 across the board
  4. const initial_stats = {
  5. "Level": 1,
  6. "Pw": 5,
  7. "Sk": 5,
  8. "Df": 5,
  9. "Mg": 5,
  10. "Hp": 5,
  11. "Sp": 5,
  12. "Ch": 5,
  13. };
  14. const weapon_tables = {
  15. "Swords": {"Pw": 1, "Sk": 1, "Df": 1, "Mg": .75, "Hp": .75, "Sp": .75, "Ch": .75},
  16. "2H Swords": {"Pw": 1.25, "Sk": 1, "Df": .5, "Mg": .75, "Hp": .75, "Sp": .5, "Ch": .75},
  17. "Knives": {"Pw": .75, "Sk": 1.25, "Df": .75, "Mg": .75, "Hp": .5, "Sp": 1, "Ch": 1},
  18. "Axes": {"Pw": 1, "Sk": .75, "Df": 1, "Mg": .75, "Hp": .75, "Sp": 1, "Ch": .5},
  19. "2H Axes": {"Pw": 1.25, "Sk": 1.25, "Df": .75, "Mg": .75, "Hp": .5, "Sp": .75, "Ch": .5},
  20. "Staves": {"Pw": .5, "Sk": .75, "Df": .75, "Mg": 1.25, "Hp": .5, "Sp": .75, "Ch": 1.25},
  21. "Bows": {"Pw": .5, "Sk": 1.75, "Df": .75, "Mg": .75, "Hp": .5, "Sp": .75, "Ch": 1},
  22. "Spears": {"Pw": .75, "Sk": 1.25, "Df": 1, "Mg": .75, "Hp": .5, "Sp": .75, "Ch": 1},
  23. "Flails": {"Pw": .75, "Sk": 1.25, "Df": .5, "Mg": .75, "Hp": .5, "Sp": .75, "Ch": 1.25},
  24. "Hammers": {"Pw": 1.75, "Sk": .75, "Df": .5, "Mg": .75, "Hp": .75, "Sp": .75, "Ch": .75},
  25. "Gloves": {"Pw": 1.25, "Sk": .75, "Df": .75, "Mg": .5, "Hp": 1, "Sp": .75, "Ch": .75},
  26. };
  27. const decimals = document.forms[0]["Decimals"];
  28. const outputs = {
  29. "Level": document.forms[0]["Level"],
  30. "Pw": document.forms[0]["Pw"],
  31. "Sk": document.forms[0]["Sk"],
  32. "Df": document.forms[0]["Df"],
  33. "Mg": document.forms[0]["Mg"],
  34. "Hp": document.forms[0]["Hp"],
  35. "Sp": document.forms[0]["Sp"],
  36. "Ch": document.forms[0]["Ch"],
  37. };
  38. const inputs = {
  39. "Swords": document.forms[1]["Swords"],
  40. "2H Swords": document.forms[1]["2H Swords"],
  41. "Knives": document.forms[1]["Knives"],
  42. "Axes": document.forms[1]["Axes"],
  43. "2H Axes": document.forms[1]["2H Axes"],
  44. "Staves": document.forms[1]["Staves"],
  45. "Bows": document.forms[1]["Bows"],
  46. "Spears": document.forms[1]["Spears"],
  47. "Flails": document.forms[1]["Flails"],
  48. "Hammers": document.forms[1]["Hammers"],
  49. "Gloves": document.forms[1]["Gloves"],
  50. };
  51. const preflight_check = function() {
  52. let level = 0;
  53. for (const key in inputs) {
  54. if (inputs[key].value == "") {
  55. inputs[key].value = 0;
  56. }
  57. if (!Number.isInteger(inputs[key].value)) {
  58. inputs[key].value = Math.trunc(inputs[key].value);
  59. }
  60. level += Number.parseInt(inputs[key].value);
  61. }
  62. if (level > (level_cap - 1)) {
  63. document.activeElement.value = Number.parseInt(document.activeElement.value) + ((level_cap - 1) - level);
  64. }
  65. };
  66. const recalculating = function() {
  67. preflight_check();
  68. for (const key in outputs) {
  69. outputs[key].value = initial_stats[key];
  70. }
  71. for (const key in inputs) {
  72. const tally = Number.parseInt(inputs[key].value);
  73. if (tally > 0) {
  74. outputs["Level"].value = Number(outputs["Level"].value) + tally;
  75. for (const attribute in weapon_tables[key]) {
  76. outputs[attribute].value = Number(outputs[attribute].value) + (tally * weapon_tables[key][attribute]);
  77. }
  78. }
  79. }
  80. if (decimals.checked) {
  81. for (const key in outputs) {
  82. outputs[key].value = Math.trunc(outputs[key].value);
  83. }
  84. }
  85. };
  86. for (const key in inputs) {
  87. inputs[key].oninput = recalculating;
  88. }
  89. decimals.onchange = recalculating;
  90. recalculating();
  91. })();