/** * Player quest position: step id, with a numeric index fallback for saves * written before branching quests. `completedStepIds` in the progress blob * records every step the player has ever finished (merge/loop safe). */ /** 1-based step index stored by older builds, or null when `state` is a step id. */ export function legacyQuestStepNumber (state) { if (typeof state === "number" && Number.isFinite(state)) { const n = state | 0; return n >= 1 ? n : null; } if (typeof state === "string" && /^\d+$/.test(state)) { const n = Number(state) | 0; return n >= 1 ? n : null; } return null; } /** * DB `state_value` → runtime value. * Pure integer strings stay numeric (legacy index). Anything else is a step id. * A step id that is itself all digits is ambiguous with a legacy index; callers * that know the quest def must prefer an exact stepId match (see resolveQuestStepId). */ export function parseStoredQuestState (stateValue) { if (stateValue === "not-started" || stateValue === "completed") { return stateValue; } const index = legacyQuestStepNumber(stateValue); if (index != null) { return index; } const text = String(stateValue ?? "").trim(); return text || "not-started"; } export function serializeQuestState (state) { if (state === "not-started" || state === "completed") { return state; } if (typeof state === "number" && Number.isFinite(state)) { return String(state | 0); } return String(state ?? ""); } export function isActiveQuestState (state) { return state != null && state !== "not-started" && state !== "completed"; } /** * Current step id for an active quest, or the terminal token. * Exact stepId match wins over a legacy index so a step literally named "2" * is not read as "the second step". */ export function resolveQuestStepId (def, state) { if (state === "completed") { return "completed"; } if (!isActiveQuestState(state)) { return "not-started"; } const steps = def?.steps ?? []; const text = typeof state === "number" ? String(state | 0) : String(state); const byId = steps.find((step) => step?.stepId === text); if (byId) { return byId.stepId; } const index = legacyQuestStepNumber(state); if (index != null) { return steps[index - 1]?.stepId ?? null; } return text; } /** * Steps the player has finished. * A stored `completedStepIds` array is authoritative (including an empty one). * Older rows derive it: every step before the numeric index, or every step * when the quest is already completed. */ export function deriveCompletedStepIds (def, state, progress) { if (Array.isArray(progress?.completedStepIds)) { const out = []; const seen = new Set(); for (const id of progress.completedStepIds) { const stepId = String(id ?? "").trim(); if (!stepId || seen.has(stepId)) { continue; } seen.add(stepId); out.push(stepId); } return out; } const steps = def?.steps ?? []; if (state === "completed") { return steps.map((step) => step.stepId).filter(Boolean); } const index = legacyQuestStepNumber(state); if (index == null) { return []; } return steps.slice(0, Math.max(0, index - 1)).map((step) => step.stepId).filter(Boolean); } export function rememberCompletedStep (ids, stepId) { const next = Array.isArray(ids) ? [...ids] : []; const id = String(stepId ?? "").trim(); if (id && !next.includes(id)) { next.push(id); } return next; }