BlueGit

timfa questward

Files

questward / shared / skills / levelUpUnlocks.js

levelUpUnlocks.js

8c314ec0 History
levelUpUnlocks.js 8.1 KiB Download Raw
1import { SkillId, SKILL_DEFINITIONS } from "../definitions/skills.js";
2import
3 {
4 MATERIAL_TIER_ORDER,
5 WOOD_TIER_ORDER,
6 CLOTH_TIER_ORDER,
7 LEATHER_TIER_ORDER,
8 GEM_TIER_ORDER,
9 ORE_BEARING_MATERIALS,
10 MATERIAL_DISPLAY_NAME,
11 CLOTH_DISPLAY_NAME,
12 LEATHER_DISPLAY_NAME,
13 woodTierForMetal,
14 tierMinSkillLevel,
15 tierMineSkillLevel,
16 tierSmeltSkillLevel,
17 isGemstoneMaterial,
18 GatherMaterialId
19 } from "../definitions/materialTiers.js";
20import { firestartingDifficulty } from "../definitions/craftingStations.js";
21import { HERB_DEFINITIONS, HERB_GATHER_SKILL_LEVEL } from "../definitions/herbs.js";
22import { SPELL_DEFINITIONS, spellMinMagicLevel } from "../definitions/spells.js";
23import { ALCHEMY_POTION_DEFINITIONS } from "../definitions/alchemy.js";
24
25/**
26 * Unlock lines for skill thresholds crossed when leveling from `fromLevel` (exclusive)
27 * to `toLevel` (inclusive). Pure / shared — used by client toasts.
28 * @param {string} skillId
29 * @param {number} fromLevel
30 * @param {number} toLevel
31 * @returns {string[]}
32 */
33export function skillUnlockMessages (skillId, fromLevel, toLevel)
34{
35 const from = Math.max(0, Math.floor(Number(fromLevel) || 0));
36 const to = Math.max(0, Math.floor(Number(toLevel) || 0));
37 if (to <= from)
38 { return []; }
39 const entries = unlockCatalog().filter((entry) => entry.skillId === skillId
40 && entry.level > from
41 && entry.level <= to);
42 entries.sort((a, b) => a.level - b.level || a.message.localeCompare(b.message));
43 return entries.map((entry) => entry.message);
44}
45
46/** Level-up toast title line. */
47export function skillLevelUpToastTitle (skillId, level)
48{
49 const name = SKILL_DEFINITIONS[skillId]?.displayName ?? skillId;
50 return `Your ${name} leveled up to ${level | 0}!`;
51}
52
53/**
54 * Full toast body: title + unlock lines.
55 * @returns {string}
56 */
57export function skillLevelUpToastText (skillId, level, previousLevel = null)
58{
59 const prev = previousLevel == null ? Math.max(0, (level | 0) - 1) : previousLevel | 0;
60 const lines = [
61 skillLevelUpToastTitle(skillId, level),
62 ...skillUnlockMessages(skillId, prev, level)
63 ];
64 return lines.join("\n");
65}
66
67/** @type {null | Array<{ skillId: string, level: number, message: string }>} */
68let _catalog = null;
69
70function unlockCatalog ()
71{
72 if (!_catalog)
73 { _catalog = buildUnlockCatalog(); }
74 return _catalog;
75}
76
77/** Reset cache (tests). */
78export function clearSkillUnlockCatalogCache ()
79{
80 _catalog = null;
81}
82
83function buildUnlockCatalog ()
84{
85 /** @type {Array<{ skillId: string, level: number, message: string }>} */
86 const out = [];
87 const add = (skillId, level, message) =>
88 {
89 if ((level | 0) <= 1)
90 { return; }
91 out.push({ skillId, level: level | 0, message });
92 };
93
94 for (const metal of MATERIAL_TIER_ORDER)
95 {
96 const name = MATERIAL_DISPLAY_NAME[metal] ?? metal;
97 const wood = woodTierForMetal(metal);
98 const woodName = MATERIAL_DISPLAY_NAME[wood] ?? wood;
99 const equip = tierMinSkillLevel(metal);
100 const craft = tierSmeltSkillLevel(metal);
101
102 add(SkillId.defence, equip, `You can now wear ${name} armour.`);
103 add(SkillId.swordfighting, equip, `You can now equip ${name} swords.`);
104 add(SkillId.axefighting, equip, `You can now equip ${name} war axes.`);
105 add(SkillId.archery, equip, `You can now equip ${woodName} bows and ${name} arrows.`);
106 add(SkillId.woodcutting, equip, `You can now equip ${name} axes.`);
107 add(SkillId.mining, equip, `You can now equip ${name} pickaxes.`);
108 add(SkillId.herbalism, equip, `You can now equip ${name} plant clippers.`);
109 add(SkillId.smithing, craft, `You can now smith ${name} items.`);
110 add(SkillId.crafting, craft, `You can now craft ${name} tools.`);
111 add(SkillId.crafting, equip, `You can now craft ${name} arrows.`);
112 }
113
114 for (const wood of WOOD_TIER_ORDER)
115 {
116 const name = MATERIAL_DISPLAY_NAME[wood] ?? wood;
117 const equip = tierMinSkillLevel(wood);
118 const craft = tierSmeltSkillLevel(wood);
119 const fell = tierMineSkillLevel(wood);
120 const light = firestartingDifficulty(wood);
121
122 add(SkillId.clubfighting, equip, `You can now equip ${name} quarterstaves.`);
123 add(SkillId.magic, equip, `You can now equip ${name} staves.`);
124 add(SkillId.fishing, equip, `You can now equip ${name} fishing rods.`);
125 add(SkillId.woodworking, craft, `You can now carve ${name} wood items.`);
126 add(SkillId.woodworking, equip, `You can now carve ${name} bows.`);
127 add(SkillId.jewelering, equip, `You can now craft ${name} staves.`);
128 add(SkillId.woodcutting, fell, `You can now chop ${name} trees.`);
129 add(SkillId.firestarting, light, `You can now light ${name} logs.`);
130 }
131
132 for (const leather of LEATHER_TIER_ORDER)
133 {
134 const name = LEATHER_DISPLAY_NAME[leather] ?? leather;
135 const craft = tierSmeltSkillLevel(leather);
136 add(SkillId.leatherworking, craft, `You can now craft ${name}.`);
137 }
138
139 for (const cloth of CLOTH_TIER_ORDER)
140 {
141 const name = CLOTH_DISPLAY_NAME[cloth] ?? cloth;
142 const equip = tierMinSkillLevel(cloth);
143 const craft = tierSmeltSkillLevel(cloth);
144 add(SkillId.defence, equip, `You can now wear ${name} clothing.`);
145 add(SkillId.tailoring, craft, `You can now tailor ${name} goods.`);
146 }
147
148 for (const gem of GEM_TIER_ORDER)
149 {
150 const name = MATERIAL_DISPLAY_NAME[gem] ?? gem;
151 const cut = tierSmeltSkillLevel(gem);
152 const equip = tierMinSkillLevel(gem);
153 add(SkillId.jewelering, cut, `You can now cut ${name}.`);
154 add(SkillId.jewelering, equip, `You can now craft ${name} jewellery (wearable at combat level ${equip}).`);
155 }
156
157 /** @type {Map<number, string[]>} */
158 const mineByLevel = new Map();
159 for (const material of ORE_BEARING_MATERIALS)
160 {
161 if (material === GatherMaterialId.bronze || material === GatherMaterialId.steel)
162 { continue; }
163 const level = tierMineSkillLevel(material);
164 const name = MATERIAL_DISPLAY_NAME[material] ?? material;
165 let label = name;
166 if (material === GatherMaterialId.coal)
167 { label = "Coal"; }
168 else if (!isGemstoneMaterial(material))
169 { label = `${name} ore`; }
170 const list = mineByLevel.get(level) ?? [];
171 list.push(label);
172 mineByLevel.set(level, list);
173 }
174 for (const [level, labels] of mineByLevel)
175 {
176 add(SkillId.mining, level, `You can now mine ${joinNames(labels)}.`);
177 }
178
179 /** @type {Map<number, string[]>} */
180 const herbsByLevel = new Map();
181 for (const herb of Object.values(HERB_DEFINITIONS))
182 {
183 const level = HERB_GATHER_SKILL_LEVEL[herb.tier] ?? 1;
184 const list = herbsByLevel.get(level) ?? [];
185 list.push(herb.displayName);
186 herbsByLevel.set(level, list);
187 }
188 for (const [level, names] of herbsByLevel)
189 {
190 add(SkillId.herbalism, level, `You can now gather ${joinNames(names)}.`);
191 }
192
193 /** @type {Map<number, { names: string[], tier: number }>} */
194 const spellsByLevel = new Map();
195 for (const spell of Object.values(SPELL_DEFINITIONS))
196 {
197 if (spell.mobOnly || spell.starter)
198 { continue; }
199 const level = spellMinMagicLevel(spell);
200 const entry = spellsByLevel.get(level) ?? { names: [], tier: spell.tier | 0 || 1 };
201 entry.names.push(spell.displayName);
202 entry.tier = Math.min(entry.tier, spell.tier | 0 || 1);
203 spellsByLevel.set(level, entry);
204 }
205 for (const [level, { names, tier }] of spellsByLevel)
206 {
207 if (names.length <= 4)
208 { add(SkillId.magic, level, `You can now cast ${joinNames(names)}.`); }
209 else
210 { add(SkillId.magic, level, `You can now cast Magic tier ${tier} spells.`); }
211 }
212
213 /** @type {Map<number, string[]>} */
214 const potionsByLevel = new Map();
215 for (const potion of Object.values(ALCHEMY_POTION_DEFINITIONS))
216 {
217 const level = Math.max(1, potion.minAlchemyLevel | 0 || 1);
218 const list = potionsByLevel.get(level) ?? [];
219 list.push(potion.displayName);
220 potionsByLevel.set(level, list);
221 }
222 for (const [level, names] of potionsByLevel)
223 {
224 add(SkillId.alchemy, level, `You can now brew ${joinNames(names)}.`);
225 }
226
227 for (const material of [GatherMaterialId.silver, GatherMaterialId.gold, GatherMaterialId.electrum])
228 {
229 const name = MATERIAL_DISPLAY_NAME[material] ?? material;
230 add(SkillId.smithing, tierSmeltSkillLevel(material), `You can now smith ${name} bars.`);
231 }
232
233 return Object.freeze(out.map((entry) => Object.freeze(entry)));
234}
235
236function joinNames (names)
237{
238 const list = [...names];
239 if (list.length === 0)
240 { return "new materials"; }
241 if (list.length === 1)
242 { return list[0]; }
243 if (list.length === 2)
244 { return `${list[0]} and ${list[1]}`; }
245 return `${list.slice(0, -1).join(", ")}, and ${list[list.length - 1]}`;
246}