BlueGit

timfa questward

Show live max hit in bag panel and spell list.

Compute neutral max hit from worn weapon stat, skill levels, and gear
bonuses; staff users see a mind spell range from known attack spells.

timfa Tim Falken · pushed by timfa

Authored 2026-09-01 · 2 months ago

Hash
bf5e0a0dc0891b2c566963e499a6859aedfd8927
Parent
73513f22
8 files changed +410 −4
client/index.html +4 −1
@@ -105,7 +105,10 @@
105 105 </div>
106 106
107 107 <aside id="bag-panel" hidden>
108 <h2>Bag</h2>
108 <div class="bag-header">
109 <h2>Bag</h2>
110 <p id="bag-max-hit" class="bag-max-hit hint" hidden></p>
111 </div>
109 112 <p id="bag-coins" hidden>0 coins</p>
110 113 <div id="equip-grid" class="item-grid equip"></div>
111 114 <div id="bag-grid" class="item-grid"></div>
@@ -105,7 +105,10 @@
105 </div>
105 </div>
106
106
107 <aside id="bag-panel" hidden>
107 <aside id="bag-panel" hidden>
108 <h2>Bag</h2>
108 <div class="bag-header">
109 <h2>Bag</h2>
110 <p id="bag-max-hit" class="bag-max-hit hint" hidden></p>
111 </div>
109 <p id="bag-coins" hidden>0 coins</p>
112 <p id="bag-coins" hidden>0 coins</p>
110 <div id="equip-grid" class="item-grid equip"></div>
113 <div id="equip-grid" class="item-grid equip"></div>
111 <div id="bag-grid" class="item-grid"></div>
114 <div id="bag-grid" class="item-grid"></div>
client/src/play/inventory.js +26 −1
@@ -13,6 +13,7 @@ import
13 13 import { formatLetterSignature } from "/shared/courier/systemLetters.js";
14 14 import { spellIdFromSpellbookStack } from "/shared/definitions/spellItems.js";
15 15 import { spellDefinition } from "/shared/definitions/spells.js";
16 import { bagMaxHitDisplay } from "/shared/combat/maxHit.js";
16 17 import { applyItemIcon } from "../render/potionIcon.js";
17 18 import { mountLayerPreview } from "./characterPreview.js";
18 19
@@ -35,12 +36,17 @@ export function initInventory ({
35 36 getLocalName,
36 37 tipArrows = null,
37 38 getSoftClaims = null,
38 knowsSpell = null
39 knowsSpell = null,
40 getSkillLevels = null,
41 getSettings = null,
42 getKnownSpellIds = null,
43 onBagChanged = null
39 44 })
40 45 {
41 46 const bagPanel = document.getElementById("bag-panel");
42 47 const bagGrid = document.getElementById("bag-grid");
43 48 const equipGrid = document.getElementById("equip-grid");
49 const bagMaxHitEl = document.getElementById("bag-max-hit");
44 50 const hint = document.getElementById("bag-hint");
45 51 const bankPanel = document.getElementById("bank-panel");
46 52 const vaultGrid = document.getElementById("vault-grid");
@@ -191,6 +197,22 @@ export function initInventory ({
191 197 return slots;
192 198 }
193 199
200 function refreshMaxHit ()
201 {
202 if (!bagMaxHitEl)
203 { return; }
204 const display = bagMaxHitDisplay({
205 skillLevels: getSkillLevels?.() ?? {},
206 equipmentSlots: bag.equipment?.slots ?? {},
207 settings: getSettings?.() ?? null,
208 knownSpellIds: getKnownSpellIds?.() ?? []
209 });
210 const statSuffix = display.statLabel ? ` (${display.statLabel})` : "";
211 bagMaxHitEl.textContent = `Max hit: ${display.text}${statSuffix}`;
212 bagMaxHitEl.title = display.title ?? "";
213 bagMaxHitEl.hidden = false;
214 }
215
194 216 function render ()
195 217 {
196 218 bagGrid.innerHTML = "";
@@ -327,6 +349,7 @@ export function initInventory ({
327 349 });
328 350 equipGrid.append(button);
329 351 }
352 refreshMaxHit();
330 353 }
331 354
332 355 function showItemMenu (x, y, slotIndex, slot)
@@ -1008,6 +1031,7 @@ export function initInventory ({
1008 1031 {
1009 1032 render();
1010 1033 },
1034 refreshMaxHit,
1011 1035 isBlocking ()
1012 1036 {
1013 1037 if (!sendPanel.hidden || !letterPanel.hidden || !sendTargetPanel.hidden
@@ -1060,6 +1084,7 @@ export function initInventory ({
1060 1084 {
1061 1085 bag = { ...bag, ...message };
1062 1086 render();
1087 onBagChanged?.();
1063 1088 if (bankOpen)
1064 1089 {
1065 1090 renderBank();
@@ -13,6 +13,7 @@ import
13import { formatLetterSignature } from "/shared/courier/systemLetters.js";
13import { formatLetterSignature } from "/shared/courier/systemLetters.js";
14import { spellIdFromSpellbookStack } from "/shared/definitions/spellItems.js";
14import { spellIdFromSpellbookStack } from "/shared/definitions/spellItems.js";
15import { spellDefinition } from "/shared/definitions/spells.js";
15import { spellDefinition } from "/shared/definitions/spells.js";
16import { bagMaxHitDisplay } from "/shared/combat/maxHit.js";
16import { applyItemIcon } from "../render/potionIcon.js";
17import { applyItemIcon } from "../render/potionIcon.js";
17import { mountLayerPreview } from "./characterPreview.js";
18import { mountLayerPreview } from "./characterPreview.js";
18
19
@@ -35,12 +36,17 @@ export function initInventory ({
35 getLocalName,
36 getLocalName,
36 tipArrows = null,
37 tipArrows = null,
37 getSoftClaims = null,
38 getSoftClaims = null,
38 knowsSpell = null
39 knowsSpell = null,
40 getSkillLevels = null,
41 getSettings = null,
42 getKnownSpellIds = null,
43 onBagChanged = null
39})
44})
40{
45{
41 const bagPanel = document.getElementById("bag-panel");
46 const bagPanel = document.getElementById("bag-panel");
42 const bagGrid = document.getElementById("bag-grid");
47 const bagGrid = document.getElementById("bag-grid");
43 const equipGrid = document.getElementById("equip-grid");
48 const equipGrid = document.getElementById("equip-grid");
49 const bagMaxHitEl = document.getElementById("bag-max-hit");
44 const hint = document.getElementById("bag-hint");
50 const hint = document.getElementById("bag-hint");
45 const bankPanel = document.getElementById("bank-panel");
51 const bankPanel = document.getElementById("bank-panel");
46 const vaultGrid = document.getElementById("vault-grid");
52 const vaultGrid = document.getElementById("vault-grid");
@@ -191,6 +197,22 @@ export function initInventory ({
191 return slots;
197 return slots;
192 }
198 }
193
199
200 function refreshMaxHit ()
201 {
202 if (!bagMaxHitEl)
203 { return; }
204 const display = bagMaxHitDisplay({
205 skillLevels: getSkillLevels?.() ?? {},
206 equipmentSlots: bag.equipment?.slots ?? {},
207 settings: getSettings?.() ?? null,
208 knownSpellIds: getKnownSpellIds?.() ?? []
209 });
210 const statSuffix = display.statLabel ? ` (${display.statLabel})` : "";
211 bagMaxHitEl.textContent = `Max hit: ${display.text}${statSuffix}`;
212 bagMaxHitEl.title = display.title ?? "";
213 bagMaxHitEl.hidden = false;
214 }
215
194 function render ()
216 function render ()
195 {
217 {
196 bagGrid.innerHTML = "";
218 bagGrid.innerHTML = "";
@@ -327,6 +349,7 @@ export function initInventory ({
327 });
349 });
328 equipGrid.append(button);
350 equipGrid.append(button);
329 }
351 }
352 refreshMaxHit();
330 }
353 }
331
354
332 function showItemMenu (x, y, slotIndex, slot)
355 function showItemMenu (x, y, slotIndex, slot)
@@ -1008,6 +1031,7 @@ export function initInventory ({
1008 {
1031 {
1009 render();
1032 render();
1010 },
1033 },
1034 refreshMaxHit,
1011 isBlocking ()
1035 isBlocking ()
1012 {
1036 {
1013 if (!sendPanel.hidden || !letterPanel.hidden || !sendTargetPanel.hidden
1037 if (!sendPanel.hidden || !letterPanel.hidden || !sendTargetPanel.hidden
@@ -1060,6 +1084,7 @@ export function initInventory ({
1060 {
1084 {
1061 bag = { ...bag, ...message };
1085 bag = { ...bag, ...message };
1062 render();
1086 render();
1087 onBagChanged?.();
1063 if (bankOpen)
1088 if (bankOpen)
1064 {
1089 {
1065 renderBank();
1090 renderBank();
client/src/play/main.js +31 −0
@@ -1668,6 +1668,22 @@ async function main ()
1668 1668 knowsSpell (spellId)
1669 1669 {
1670 1670 return !!spells?.knowsSpell?.(spellId);
1671 },
1672 getSkillLevels ()
1673 {
1674 return skills?.getSkillLevels?.() ?? {};
1675 },
1676 getSettings ()
1677 {
1678 return view?.settings ?? null;
1679 },
1680 getKnownSpellIds ()
1681 {
1682 return spells?.getKnownSpellIds?.() ?? [];
1683 },
1684 onBagChanged ()
1685 {
1686 spells?.refreshMaxHitDisplay?.();
1671 1687 }
1672 1688 });
1673 1689 skills = initSkills({
@@ -1686,6 +1702,18 @@ async function main ()
1686 1702 getSkillLevel (skillId)
1687 1703 {
1688 1704 return skills?.skillLevel?.(skillId) ?? 1;
1705 },
1706 getSkillLevels ()
1707 {
1708 return skills?.getSkillLevels?.() ?? {};
1709 },
1710 getEquipmentSlots ()
1711 {
1712 return inventory?.getEquipment?.() ?? {};
1713 },
1714 getSettings ()
1715 {
1716 return view?.settings ?? null;
1689 1717 }
1690 1718 });
1691 1719 crafting = initCrafting({
@@ -1906,12 +1934,15 @@ async function main ()
1906 1934 break;
1907 1935 case MessageType.skills:
1908 1936 skills?.render(message.skills, message.combatLevel);
1937 inventory?.refreshMaxHit?.();
1938 spells?.refreshMaxHitDisplay?.();
1909 1939 crafting?.refresh();
1910 1940 spells?.refreshCastGates?.();
1911 1941 break;
1912 1942 case MessageType.spells:
1913 1943 case MessageType.spellsUpdated:
1914 1944 spells?.render(message);
1945 inventory?.refreshMaxHit?.();
1915 1946 break;
1916 1947 case MessageType.spellArmed:
1917 1948 spells?.handleSpellArmed(message);
@@ -1668,6 +1668,22 @@ async function main ()
1668 knowsSpell (spellId)
1668 knowsSpell (spellId)
1669 {
1669 {
1670 return !!spells?.knowsSpell?.(spellId);
1670 return !!spells?.knowsSpell?.(spellId);
1671 },
1672 getSkillLevels ()
1673 {
1674 return skills?.getSkillLevels?.() ?? {};
1675 },
1676 getSettings ()
1677 {
1678 return view?.settings ?? null;
1679 },
1680 getKnownSpellIds ()
1681 {
1682 return spells?.getKnownSpellIds?.() ?? [];
1683 },
1684 onBagChanged ()
1685 {
1686 spells?.refreshMaxHitDisplay?.();
1671 }
1687 }
1672 });
1688 });
1673 skills = initSkills({
1689 skills = initSkills({
@@ -1686,6 +1702,18 @@ async function main ()
1686 getSkillLevel (skillId)
1702 getSkillLevel (skillId)
1687 {
1703 {
1688 return skills?.skillLevel?.(skillId) ?? 1;
1704 return skills?.skillLevel?.(skillId) ?? 1;
1705 },
1706 getSkillLevels ()
1707 {
1708 return skills?.getSkillLevels?.() ?? {};
1709 },
1710 getEquipmentSlots ()
1711 {
1712 return inventory?.getEquipment?.() ?? {};
1713 },
1714 getSettings ()
1715 {
1716 return view?.settings ?? null;
1689 }
1717 }
1690 });
1718 });
1691 crafting = initCrafting({
1719 crafting = initCrafting({
@@ -1906,12 +1934,15 @@ async function main ()
1906 break;
1934 break;
1907 case MessageType.skills:
1935 case MessageType.skills:
1908 skills?.render(message.skills, message.combatLevel);
1936 skills?.render(message.skills, message.combatLevel);
1937 inventory?.refreshMaxHit?.();
1938 spells?.refreshMaxHitDisplay?.();
1909 crafting?.refresh();
1939 crafting?.refresh();
1910 spells?.refreshCastGates?.();
1940 spells?.refreshCastGates?.();
1911 break;
1941 break;
1912 case MessageType.spells:
1942 case MessageType.spells:
1913 case MessageType.spellsUpdated:
1943 case MessageType.spellsUpdated:
1914 spells?.render(message);
1944 spells?.render(message);
1945 inventory?.refreshMaxHit?.();
1915 break;
1946 break;
1916 case MessageType.spellArmed:
1947 case MessageType.spellArmed:
1917 spells?.handleSpellArmed(message);
1948 spells?.handleSpellArmed(message);
client/src/play/skills.js +7 −0
@@ -174,6 +174,13 @@ export function initSkills ({ getEquipmentSlots, getSettings } = {})
174 174 {
175 175 return current.find((entry) => entry.skillId === skillId)?.level ?? 1;
176 176 },
177 getSkillLevels ()
178 {
179 const out = {};
180 for (const skill of current)
181 { out[skill.skillId] = skill.level | 0 || 1; }
182 return out;
183 },
177 184 hide ()
178 185 {
179 186 panel.hidden = true;
@@ -174,6 +174,13 @@ export function initSkills ({ getEquipmentSlots, getSettings } = {})
174 {
174 {
175 return current.find((entry) => entry.skillId === skillId)?.level ?? 1;
175 return current.find((entry) => entry.skillId === skillId)?.level ?? 1;
176 },
176 },
177 getSkillLevels ()
178 {
179 const out = {};
180 for (const skill of current)
181 { out[skill.skillId] = skill.level | 0 || 1; }
182 return out;
183 },
177 hide ()
184 hide ()
178 {
185 {
179 panel.hidden = true;
186 panel.hidden = true;
client/src/play/spells.js +40 −2
@@ -3,8 +3,16 @@
3 3 */
4 4 import { spellDefinition, spellMindPowerFromTier, spellMinMagicLevel } from "/shared/definitions/spells.js";
5 5 import { SkillId } from "/shared/definitions/skills.js";
6 import { spellMaxHitDisplay } from "/shared/combat/maxHit.js";
6 7
7 export function initSpells ({ socket, MessageType, getSkillLevel } = {})
8 export function initSpells ({
9 socket,
10 MessageType,
11 getSkillLevel,
12 getSkillLevels = null,
13 getEquipmentSlots = null,
14 getSettings = null
15 } = {})
8 16 {
9 17 const panel = document.getElementById("spells-panel");
10 18 const list = document.getElementById("spells-list");
@@ -53,6 +61,12 @@ export function initSpells ({ socket, MessageType, getSkillLevel } = {})
53 61 renderBar();
54 62 }
55 63
64 function refreshMaxHitDisplay ()
65 {
66 if (!panel.hidden)
67 { renderList(); }
68 }
69
56 70 function disarmLocal ()
57 71 {
58 72 if (!armedSpellId)
@@ -116,6 +130,22 @@ export function initSpells ({ socket, MessageType, getSkillLevel } = {})
116 130 desc.className = "spell-desc hint";
117 131 desc.textContent = spell.description ?? "";
118 132
133 const maxHit = spellMaxHitDisplay({
134 spellId: spell.spellId,
135 skillLevels: getSkillLevels?.() ?? {},
136 equipmentSlots: getEquipmentSlots?.() ?? {},
137 settings: getSettings?.() ?? null
138 });
139 const maxHitEl = maxHit != null
140 ? (() =>
141 {
142 const line = document.createElement("div");
143 line.className = "spell-max-hit hint";
144 line.textContent = `Max hit: ${maxHit}`;
145 return line;
146 })()
147 : null;
148
119 149 const actions = document.createElement("div");
120 150 actions.className = "spell-actions";
121 151
@@ -146,7 +176,10 @@ export function initSpells ({ socket, MessageType, getSkillLevel } = {})
146 176 });
147 177
148 178 actions.append(favorite, arm);
149 body.append(title, desc, actions);
179 body.append(title, desc);
180 if (maxHitEl)
181 { body.append(maxHitEl); }
182 body.append(actions);
150 183 row.append(icon, body);
151 184 return row;
152 185 }
@@ -253,6 +286,11 @@ export function initSpells ({ socket, MessageType, getSkillLevel } = {})
253 286 {
254 287 return spells.some((spell) => spell.spellId === spellId);
255 288 },
289 getKnownSpellIds ()
290 {
291 return spells.map((spell) => spell.spellId);
292 },
293 refreshMaxHitDisplay,
256 294 handleSpellDisarmed ()
257 295 {
258 296 disarmLocal();
@@ -3,8 +3,16 @@
3 */
3 */
4import { spellDefinition, spellMindPowerFromTier, spellMinMagicLevel } from "/shared/definitions/spells.js";
4import { spellDefinition, spellMindPowerFromTier, spellMinMagicLevel } from "/shared/definitions/spells.js";
5import { SkillId } from "/shared/definitions/skills.js";
5import { SkillId } from "/shared/definitions/skills.js";
6import { spellMaxHitDisplay } from "/shared/combat/maxHit.js";
6
7
7export function initSpells ({ socket, MessageType, getSkillLevel } = {})
8export function initSpells ({
9 socket,
10 MessageType,
11 getSkillLevel,
12 getSkillLevels = null,
13 getEquipmentSlots = null,
14 getSettings = null
15} = {})
8{
16{
9 const panel = document.getElementById("spells-panel");
17 const panel = document.getElementById("spells-panel");
10 const list = document.getElementById("spells-list");
18 const list = document.getElementById("spells-list");
@@ -53,6 +61,12 @@ export function initSpells ({ socket, MessageType, getSkillLevel } = {})
53 renderBar();
61 renderBar();
54 }
62 }
55
63
64 function refreshMaxHitDisplay ()
65 {
66 if (!panel.hidden)
67 { renderList(); }
68 }
69
56 function disarmLocal ()
70 function disarmLocal ()
57 {
71 {
58 if (!armedSpellId)
72 if (!armedSpellId)
@@ -116,6 +130,22 @@ export function initSpells ({ socket, MessageType, getSkillLevel } = {})
116 desc.className = "spell-desc hint";
130 desc.className = "spell-desc hint";
117 desc.textContent = spell.description ?? "";
131 desc.textContent = spell.description ?? "";
118
132
133 const maxHit = spellMaxHitDisplay({
134 spellId: spell.spellId,
135 skillLevels: getSkillLevels?.() ?? {},
136 equipmentSlots: getEquipmentSlots?.() ?? {},
137 settings: getSettings?.() ?? null
138 });
139 const maxHitEl = maxHit != null
140 ? (() =>
141 {
142 const line = document.createElement("div");
143 line.className = "spell-max-hit hint";
144 line.textContent = `Max hit: ${maxHit}`;
145 return line;
146 })()
147 : null;
148
119 const actions = document.createElement("div");
149 const actions = document.createElement("div");
120 actions.className = "spell-actions";
150 actions.className = "spell-actions";
121
151
@@ -146,7 +176,10 @@ export function initSpells ({ socket, MessageType, getSkillLevel } = {})
146 });
176 });
147
177
148 actions.append(favorite, arm);
178 actions.append(favorite, arm);
149 body.append(title, desc, actions);
179 body.append(title, desc);
180 if (maxHitEl)
181 { body.append(maxHitEl); }
182 body.append(actions);
150 row.append(icon, body);
183 row.append(icon, body);
151 return row;
184 return row;
152 }
185 }
@@ -253,6 +286,11 @@ export function initSpells ({ socket, MessageType, getSkillLevel } = {})
253 {
286 {
254 return spells.some((spell) => spell.spellId === spellId);
287 return spells.some((spell) => spell.spellId === spellId);
255 },
288 },
289 getKnownSpellIds ()
290 {
291 return spells.map((spell) => spell.spellId);
292 },
293 refreshMaxHitDisplay,
256 handleSpellDisarmed ()
294 handleSpellDisarmed ()
257 {
295 {
258 disarmLocal();
296 disarmLocal();
client/ui/play.css +24 −0
@@ -861,6 +861,11 @@ button.friend-account-name:hover {
861 861 line-height: 1.35;
862 862 }
863 863
864 .spell-max-hit {
865 margin: 0.15rem 0 0.35rem;
866 font-size: 0.82rem;
867 }
868
864 869 .spell-actions {
865 870 display: flex;
866 871 gap: 0.4rem;
@@ -945,6 +950,25 @@ button.friend-account-name:hover {
945 950 padding: 0.75rem;
946 951 }
947 952
953 .bag-header {
954 display: flex;
955 align-items: baseline;
956 justify-content: space-between;
957 gap: 0.75rem;
958 flex-wrap: wrap;
959 margin-bottom: 0.35rem;
960 }
961
962 .bag-header h2 {
963 margin: 0;
964 }
965
966 .bag-max-hit {
967 margin: 0;
968 font-size: 0.85rem;
969 white-space: nowrap;
970 }
971
948 972 #bank-panel {
949 973 left: auto;
950 974 right: 0.75rem;
@@ -861,6 +861,11 @@ button.friend-account-name:hover {
861 line-height: 1.35;
861 line-height: 1.35;
862}
862}
863
863
864.spell-max-hit {
865 margin: 0.15rem 0 0.35rem;
866 font-size: 0.82rem;
867}
868
864.spell-actions {
869.spell-actions {
865 display: flex;
870 display: flex;
866 gap: 0.4rem;
871 gap: 0.4rem;
@@ -945,6 +950,25 @@ button.friend-account-name:hover {
945 padding: 0.75rem;
950 padding: 0.75rem;
946}
951}
947
952
953.bag-header {
954 display: flex;
955 align-items: baseline;
956 justify-content: space-between;
957 gap: 0.75rem;
958 flex-wrap: wrap;
959 margin-bottom: 0.35rem;
960}
961
962.bag-header h2 {
963 margin: 0;
964}
965
966.bag-max-hit {
967 margin: 0;
968 font-size: 0.85rem;
969 white-space: nowrap;
970}
971
948#bank-panel {
972#bank-panel {
949 left: auto;
973 left: auto;
950 right: 0.75rem;
974 right: 0.75rem;
server/tests/maxHit.test.js added +71 −0
@@ -0,0 +1,71 @@
1 import test from "node:test";
2 import assert from "node:assert/strict";
3 import { SkillId } from "../../shared/definitions/skills.js";
4 import { SwordItemId } from "../../shared/definitions/swordItems.js";
5 import { GemItemId } from "../../shared/definitions/gemItems.js";
6 import { SpellId } from "../../shared/definitions/spells.js";
7 import
8 {
9 bagMaxHitDisplay,
10 effectivePhysicalDamageLevel,
11 isPlayerAttackDamageSpell,
12 maxNeutralPhysicalHit,
13 spellMaxHitDisplay
14 } from "../../shared/combat/maxHit.js";
15
16 test("physical max hit uses quarter-tier damage roll cap", () =>
17 {
18 assert.equal(maxNeutralPhysicalHit(99), 25);
19 assert.equal(maxNeutralPhysicalHit(123), 31);
20 });
21
22 test("bag max hit uses strength for a sword", () =>
23 {
24 const display = bagMaxHitDisplay({
25 skillLevels: { [SkillId.strength]: 10 },
26 equipmentSlots: {
27 weapon: { itemId: SwordItemId.bronzeSword, quantity: 1 }
28 }
29 });
30 assert.equal(display.statLabel, "Strength");
31 assert.equal(display.text, "3");
32 });
33
34 test("bag max hit with staff shows mind spell range from known attack spells", () =>
35 {
36 const display = bagMaxHitDisplay({
37 skillLevels: { [SkillId.mind]: 20, [SkillId.strength]: 20 },
38 equipmentSlots: {
39 weapon: { itemId: GemItemId.pineStaff, quantity: 1 }
40 },
41 knownSpellIds: [SpellId.manaBullet, SpellId.firebolt, SpellId.hellfire]
42 });
43 assert.equal(display.statLabel, "Mind");
44 assert.equal(display.text, "1 – 5");
45 });
46
47 test("spell max hit is shown only for damage spells", () =>
48 {
49 assert.equal(isPlayerAttackDamageSpell(SpellId.firebolt), true);
50 assert.equal(isPlayerAttackDamageSpell(SpellId.light), false);
51 assert.equal(spellMaxHitDisplay({
52 spellId: SpellId.light,
53 skillLevels: { [SkillId.mind]: 50 }
54 }), null);
55 assert.equal(spellMaxHitDisplay({
56 spellId: SpellId.firebolt,
57 skillLevels: { [SkillId.mind]: 50 }
58 }), 13);
59 });
60
61 test("effective physical damage level includes gear strength bonus", () =>
62 {
63 const level = effectivePhysicalDamageLevel({
64 skillLevels: { [SkillId.strength]: 10 },
65 equipmentSlots: {
66 weapon: { itemId: SwordItemId.bronzeSword, quantity: 1 }
67 }
68 });
69 assert.equal(level, 11);
70 assert.equal(maxNeutralPhysicalHit(level), 3);
71 });
@@ -0,0 +1,71 @@
1import test from "node:test";
2import assert from "node:assert/strict";
3import { SkillId } from "../../shared/definitions/skills.js";
4import { SwordItemId } from "../../shared/definitions/swordItems.js";
5import { GemItemId } from "../../shared/definitions/gemItems.js";
6import { SpellId } from "../../shared/definitions/spells.js";
7import
8{
9 bagMaxHitDisplay,
10 effectivePhysicalDamageLevel,
11 isPlayerAttackDamageSpell,
12 maxNeutralPhysicalHit,
13 spellMaxHitDisplay
14} from "../../shared/combat/maxHit.js";
15
16test("physical max hit uses quarter-tier damage roll cap", () =>
17{
18 assert.equal(maxNeutralPhysicalHit(99), 25);
19 assert.equal(maxNeutralPhysicalHit(123), 31);
20});
21
22test("bag max hit uses strength for a sword", () =>
23{
24 const display = bagMaxHitDisplay({
25 skillLevels: { [SkillId.strength]: 10 },
26 equipmentSlots: {
27 weapon: { itemId: SwordItemId.bronzeSword, quantity: 1 }
28 }
29 });
30 assert.equal(display.statLabel, "Strength");
31 assert.equal(display.text, "3");
32});
33
34test("bag max hit with staff shows mind spell range from known attack spells", () =>
35{
36 const display = bagMaxHitDisplay({
37 skillLevels: { [SkillId.mind]: 20, [SkillId.strength]: 20 },
38 equipmentSlots: {
39 weapon: { itemId: GemItemId.pineStaff, quantity: 1 }
40 },
41 knownSpellIds: [SpellId.manaBullet, SpellId.firebolt, SpellId.hellfire]
42 });
43 assert.equal(display.statLabel, "Mind");
44 assert.equal(display.text, "1 – 5");
45});
46
47test("spell max hit is shown only for damage spells", () =>
48{
49 assert.equal(isPlayerAttackDamageSpell(SpellId.firebolt), true);
50 assert.equal(isPlayerAttackDamageSpell(SpellId.light), false);
51 assert.equal(spellMaxHitDisplay({
52 spellId: SpellId.light,
53 skillLevels: { [SkillId.mind]: 50 }
54 }), null);
55 assert.equal(spellMaxHitDisplay({
56 spellId: SpellId.firebolt,
57 skillLevels: { [SkillId.mind]: 50 }
58 }), 13);
59});
60
61test("effective physical damage level includes gear strength bonus", () =>
62{
63 const level = effectivePhysicalDamageLevel({
64 skillLevels: { [SkillId.strength]: 10 },
65 equipmentSlots: {
66 weapon: { itemId: SwordItemId.bronzeSword, quantity: 1 }
67 }
68 });
69 assert.equal(level, 11);
70 assert.equal(maxNeutralPhysicalHit(level), 3);
71});
shared/combat/maxHit.js added +207 −0
@@ -0,0 +1,207 @@
1 import { itemDefinition } from "../definitions/items.js";
2 import { EquipmentKind } from "../definitions/equipmentKinds.js";
3 import { SkillId, UNARMED_ATTACK } from "../definitions/skills.js";
4 import { spellDefinition } from "../definitions/spells.js";
5 import { equipmentBonusesFromSlots, effectiveSkillLevel } from "../equipment/stats.js";
6 import { combatDamageRollLevel } from "./rolls.js";
7
8 /** Attack profile from the equipped weapon (players). */
9 export function attackProfileFromEquipment (equipmentSlots = {})
10 {
11 const weapon = equipmentSlots?.weapon;
12 if (weapon?.itemId != null)
13 {
14 const attack = itemDefinition(weapon.itemId)?.equipment?.attack;
15 if (attack)
16 { return { ...attack }; }
17 }
18 return { ...UNARMED_ATTACK };
19 }
20
21 export function weaponEquipmentKindFromSlots (equipmentSlots = {})
22 {
23 const weapon = equipmentSlots?.weapon;
24 if (!weapon?.itemId)
25 { return null; }
26 return itemDefinition(weapon.itemId)?.equipment?.kind ?? null;
27 }
28
29 export function isMagicStaffEquipped (equipmentSlots = {})
30 {
31 return weaponEquipmentKindFromSlots(equipmentSlots) === EquipmentKind.staff;
32 }
33
34 export function physicalDamageSkillId (profile = UNARMED_ATTACK)
35 {
36 return profile.damageSkillId ?? SkillId.strength;
37 }
38
39 export function physicalDamageSkillLabel (skillId)
40 {
41 switch (skillId)
42 {
43 case SkillId.dexterity:
44 return "Dexterity";
45 case SkillId.strength:
46 return "Strength";
47 case SkillId.mind:
48 return "Mind";
49 default:
50 return skillId;
51 }
52 }
53
54 /** Build `{ skillId: level }` from client skill rows or a plain level map. */
55 export function skillLevelsMap (skills)
56 {
57 if (Array.isArray(skills))
58 {
59 const out = {};
60 for (const row of skills)
61 { out[row.skillId] = row.level | 0 || 1; }
62 return out;
63 }
64 if (skills?.levels)
65 { return { ...skills.levels }; }
66 if (skills && typeof skills === "object")
67 { return { ...skills }; }
68 return {};
69 }
70
71 function mergeEquipmentBonuses (equipmentSlots, settings, statusBonuses = null)
72 {
73 const bonuses = equipmentBonusesFromSlots(equipmentSlots ?? {}, settings);
74 if (statusBonuses)
75 {
76 bonuses[SkillId.strength] = (bonuses[SkillId.strength] ?? 0) + (statusBonuses.strength ?? 0);
77 bonuses[SkillId.mind] = (bonuses[SkillId.mind] ?? 0) + (statusBonuses.mind ?? 0);
78 }
79 return bonuses;
80 }
81
82 export function effectivePhysicalDamageLevel ({
83 skillLevels,
84 equipmentSlots = {},
85 settings = null,
86 profile = null,
87 statusBonuses = null
88 })
89 {
90 const levels = skillLevelsMap(skillLevels);
91 const bonuses = mergeEquipmentBonuses(equipmentSlots, settings, statusBonuses);
92 const attack = profile ?? attackProfileFromEquipment(equipmentSlots);
93 const damageSkillId = physicalDamageSkillId(attack);
94 return effectiveSkillLevel({ levels }, damageSkillId, bonuses);
95 }
96
97 export function effectiveMindDamageLevel ({
98 skillLevels,
99 equipmentSlots = {},
100 settings = null,
101 statusBonuses = null
102 })
103 {
104 const levels = skillLevelsMap(skillLevels);
105 const bonuses = mergeEquipmentBonuses(equipmentSlots, settings, statusBonuses);
106 return effectiveSkillLevel({ levels }, SkillId.mind, bonuses);
107 }
108
109 /** Max melee/archery hit on a neutral target (can still roll 0). */
110 export function maxNeutralPhysicalHit (effectiveDamageLevel)
111 {
112 return combatDamageRollLevel(effectiveDamageLevel);
113 }
114
115 /** Max magic damage hit on a neutral target after a successful cast. */
116 export function maxNeutralMagicHit (effectiveMindLevel)
117 {
118 return Math.max(1, combatDamageRollLevel(effectiveMindLevel));
119 }
120
121 /** Min magic damage on a successful neutral hit (mind roll can be 0). */
122 export function minNeutralMagicHit ()
123 {
124 return 1;
125 }
126
127 export function isPlayerAttackDamageSpell (spellId)
128 {
129 const def = spellDefinition(spellId);
130 return !!def && def.effect === "damage" && !def.mobOnly;
131 }
132
133 /**
134 * UI label for bag panel max hit.
135 * @returns {{ text: string, title: string, statLabel: string|null }}
136 */
137 export function bagMaxHitDisplay ({
138 skillLevels,
139 equipmentSlots = {},
140 settings = null,
141 knownSpellIds = [],
142 statusBonuses = null
143 })
144 {
145 if (isMagicStaffEquipped(equipmentSlots))
146 {
147 const mindLevel = effectiveMindDamageLevel({
148 skillLevels,
149 equipmentSlots,
150 settings,
151 statusBonuses
152 });
153 const attackSpells = (knownSpellIds ?? []).filter(isPlayerAttackDamageSpell);
154 if (!attackSpells.length)
155 {
156 return {
157 text: "—",
158 title: "No attack spells known",
159 statLabel: "Mind"
160 };
161 }
162 const max = maxNeutralMagicHit(mindLevel);
163 const min = minNeutralMagicHit();
164 return {
165 text: min === max ? String(max) : `${min} – ${max}`,
166 title: `Max spell hit on neutral targets (Mind ${mindLevel})`,
167 statLabel: "Mind"
168 };
169 }
170
171 const profile = attackProfileFromEquipment(equipmentSlots);
172 const damageSkillId = physicalDamageSkillId(profile);
173 const statLabel = physicalDamageSkillLabel(damageSkillId);
174 const damageLevel = effectivePhysicalDamageLevel({
175 skillLevels,
176 equipmentSlots,
177 settings,
178 profile,
179 statusBonuses
180 });
181 const max = maxNeutralPhysicalHit(damageLevel);
182 return {
183 text: String(max),
184 title: `Max hit on neutral targets (${statLabel} ${damageLevel})`,
185 statLabel
186 };
187 }
188
189 /** Max neutral hit for one damage spell (same for all spells today). */
190 export function spellMaxHitDisplay ({
191 spellId,
192 skillLevels,
193 equipmentSlots = {},
194 settings = null,
195 statusBonuses = null
196 })
197 {
198 if (!isPlayerAttackDamageSpell(spellId))
199 { return null; }
200 const mindLevel = effectiveMindDamageLevel({
201 skillLevels,
202 equipmentSlots,
203 settings,
204 statusBonuses
205 });
206 return maxNeutralMagicHit(mindLevel);
207 }
@@ -0,0 +1,207 @@
1import { itemDefinition } from "../definitions/items.js";
2import { EquipmentKind } from "../definitions/equipmentKinds.js";
3import { SkillId, UNARMED_ATTACK } from "../definitions/skills.js";
4import { spellDefinition } from "../definitions/spells.js";
5import { equipmentBonusesFromSlots, effectiveSkillLevel } from "../equipment/stats.js";
6import { combatDamageRollLevel } from "./rolls.js";
7
8/** Attack profile from the equipped weapon (players). */
9export function attackProfileFromEquipment (equipmentSlots = {})
10{
11 const weapon = equipmentSlots?.weapon;
12 if (weapon?.itemId != null)
13 {
14 const attack = itemDefinition(weapon.itemId)?.equipment?.attack;
15 if (attack)
16 { return { ...attack }; }
17 }
18 return { ...UNARMED_ATTACK };
19}
20
21export function weaponEquipmentKindFromSlots (equipmentSlots = {})
22{
23 const weapon = equipmentSlots?.weapon;
24 if (!weapon?.itemId)
25 { return null; }
26 return itemDefinition(weapon.itemId)?.equipment?.kind ?? null;
27}
28
29export function isMagicStaffEquipped (equipmentSlots = {})
30{
31 return weaponEquipmentKindFromSlots(equipmentSlots) === EquipmentKind.staff;
32}
33
34export function physicalDamageSkillId (profile = UNARMED_ATTACK)
35{
36 return profile.damageSkillId ?? SkillId.strength;
37}
38
39export function physicalDamageSkillLabel (skillId)
40{
41 switch (skillId)
42 {
43 case SkillId.dexterity:
44 return "Dexterity";
45 case SkillId.strength:
46 return "Strength";
47 case SkillId.mind:
48 return "Mind";
49 default:
50 return skillId;
51 }
52}
53
54/** Build `{ skillId: level }` from client skill rows or a plain level map. */
55export function skillLevelsMap (skills)
56{
57 if (Array.isArray(skills))
58 {
59 const out = {};
60 for (const row of skills)
61 { out[row.skillId] = row.level | 0 || 1; }
62 return out;
63 }
64 if (skills?.levels)
65 { return { ...skills.levels }; }
66 if (skills && typeof skills === "object")
67 { return { ...skills }; }
68 return {};
69}
70
71function mergeEquipmentBonuses (equipmentSlots, settings, statusBonuses = null)
72{
73 const bonuses = equipmentBonusesFromSlots(equipmentSlots ?? {}, settings);
74 if (statusBonuses)
75 {
76 bonuses[SkillId.strength] = (bonuses[SkillId.strength] ?? 0) + (statusBonuses.strength ?? 0);
77 bonuses[SkillId.mind] = (bonuses[SkillId.mind] ?? 0) + (statusBonuses.mind ?? 0);
78 }
79 return bonuses;
80}
81
82export function effectivePhysicalDamageLevel ({
83 skillLevels,
84 equipmentSlots = {},
85 settings = null,
86 profile = null,
87 statusBonuses = null
88})
89{
90 const levels = skillLevelsMap(skillLevels);
91 const bonuses = mergeEquipmentBonuses(equipmentSlots, settings, statusBonuses);
92 const attack = profile ?? attackProfileFromEquipment(equipmentSlots);
93 const damageSkillId = physicalDamageSkillId(attack);
94 return effectiveSkillLevel({ levels }, damageSkillId, bonuses);
95}
96
97export function effectiveMindDamageLevel ({
98 skillLevels,
99 equipmentSlots = {},
100 settings = null,
101 statusBonuses = null
102})
103{
104 const levels = skillLevelsMap(skillLevels);
105 const bonuses = mergeEquipmentBonuses(equipmentSlots, settings, statusBonuses);
106 return effectiveSkillLevel({ levels }, SkillId.mind, bonuses);
107}
108
109/** Max melee/archery hit on a neutral target (can still roll 0). */
110export function maxNeutralPhysicalHit (effectiveDamageLevel)
111{
112 return combatDamageRollLevel(effectiveDamageLevel);
113}
114
115/** Max magic damage hit on a neutral target after a successful cast. */
116export function maxNeutralMagicHit (effectiveMindLevel)
117{
118 return Math.max(1, combatDamageRollLevel(effectiveMindLevel));
119}
120
121/** Min magic damage on a successful neutral hit (mind roll can be 0). */
122export function minNeutralMagicHit ()
123{
124 return 1;
125}
126
127export function isPlayerAttackDamageSpell (spellId)
128{
129 const def = spellDefinition(spellId);
130 return !!def && def.effect === "damage" && !def.mobOnly;
131}
132
133/**
134 * UI label for bag panel max hit.
135 * @returns {{ text: string, title: string, statLabel: string|null }}
136 */
137export function bagMaxHitDisplay ({
138 skillLevels,
139 equipmentSlots = {},
140 settings = null,
141 knownSpellIds = [],
142 statusBonuses = null
143})
144{
145 if (isMagicStaffEquipped(equipmentSlots))
146 {
147 const mindLevel = effectiveMindDamageLevel({
148 skillLevels,
149 equipmentSlots,
150 settings,
151 statusBonuses
152 });
153 const attackSpells = (knownSpellIds ?? []).filter(isPlayerAttackDamageSpell);
154 if (!attackSpells.length)
155 {
156 return {
157 text: "—",
158 title: "No attack spells known",
159 statLabel: "Mind"
160 };
161 }
162 const max = maxNeutralMagicHit(mindLevel);
163 const min = minNeutralMagicHit();
164 return {
165 text: min === max ? String(max) : `${min} – ${max}`,
166 title: `Max spell hit on neutral targets (Mind ${mindLevel})`,
167 statLabel: "Mind"
168 };
169 }
170
171 const profile = attackProfileFromEquipment(equipmentSlots);
172 const damageSkillId = physicalDamageSkillId(profile);
173 const statLabel = physicalDamageSkillLabel(damageSkillId);
174 const damageLevel = effectivePhysicalDamageLevel({
175 skillLevels,
176 equipmentSlots,
177 settings,
178 profile,
179 statusBonuses
180 });
181 const max = maxNeutralPhysicalHit(damageLevel);
182 return {
183 text: String(max),
184 title: `Max hit on neutral targets (${statLabel} ${damageLevel})`,
185 statLabel
186 };
187}
188
189/** Max neutral hit for one damage spell (same for all spells today). */
190export function spellMaxHitDisplay ({
191 spellId,
192 skillLevels,
193 equipmentSlots = {},
194 settings = null,
195 statusBonuses = null
196})
197{
198 if (!isPlayerAttackDamageSpell(spellId))
199 { return null; }
200 const mindLevel = effectiveMindDamageLevel({
201 skillLevels,
202 equipmentSlots,
203 settings,
204 statusBonuses
205 });
206 return maxNeutralMagicHit(mindLevel);
207}