| 1 | import { ConditionKind, skillRequirementThreshold } from "./conditions.js"; |
| 2 | |
| 3 | /** |
| 4 | * Progress for countable objective conditions (hasItem, killCount, skillLevelAtLeast). |
| 5 | * Uses the first non-negated countable clause in the ConditionSet. |
| 6 | * @returns {{ current: number, required: number } | null} |
| 7 | */ |
| 8 | export function countableObjectiveProgress (when, ctx) |
| 9 | { |
| 10 | for (const clause of when?.all ?? []) |
| 11 | { |
| 12 | if (clause?.not) |
| 13 | { continue; } |
| 14 | const condition = clause?.condition; |
| 15 | if (!condition || typeof condition !== "object") |
| 16 | { continue; } |
| 17 | const progress = countableConditionProgress(condition, ctx); |
| 18 | if (progress) |
| 19 | { return progress; } |
| 20 | } |
| 21 | return null; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @returns {{ current: number, required: number } | null} |
| 26 | */ |
| 27 | export function countableConditionProgress (condition, ctx) |
| 28 | { |
| 29 | const kind = condition?.kind; |
| 30 | if (kind === ConditionKind.hasItem) |
| 31 | { |
| 32 | const required = Math.max(1, Number(condition.quantity) || 1); |
| 33 | const current = Math.max(0, ctx.countItem?.(condition.itemId) ?? 0); |
| 34 | return { current: Math.min(current, required), required }; |
| 35 | } |
| 36 | if (kind === ConditionKind.killCount) |
| 37 | { |
| 38 | const required = Math.max(1, Number(condition.count) || 1); |
| 39 | const current = Math.max(0, ctx.killCount?.(condition) ?? 0); |
| 40 | return { current: Math.min(current, required), required }; |
| 41 | } |
| 42 | if (kind === ConditionKind.skillLevelAtLeast) |
| 43 | { |
| 44 | const required = skillRequirementThreshold(condition); |
| 45 | const current = Math.max(0, ctx.skillLevel?.(condition.skillId) ?? 0); |
| 46 | return { current: Math.min(current, required), required }; |
| 47 | } |
| 48 | return null; |
| 49 | } |
| 50 | |
| 51 | /** Player-facing label: "Collect Logs (5 / 30)" when countable. */ |
| 52 | export function formatObjectiveDisplayText (text, progress) |
| 53 | { |
| 54 | const base = String(text ?? "").trim() || "Objective"; |
| 55 | if (!progress || !(progress.required > 0)) |
| 56 | { return base; } |
| 57 | const current = Math.max(0, progress.current | 0); |
| 58 | const required = Math.max(1, progress.required | 0); |
| 59 | return `${base} (${current} / ${required})`; |
| 60 | } |
| 61 | |
| 62 | /** Fingerprint for detecting progress-only changes without new completions. */ |
| 63 | export function objectiveProgressFingerprint (objectives, ctx) |
| 64 | { |
| 65 | const parts = []; |
| 66 | for (const obj of objectives ?? []) |
| 67 | { |
| 68 | const progress = countableObjectiveProgress(obj.when, ctx); |
| 69 | if (!progress) |
| 70 | { continue; } |
| 71 | parts.push(`${obj.objectiveId}:${progress.current}/${progress.required}`); |
| 72 | } |
| 73 | return parts.join("|"); |
| 74 | } |