| 1 | /** |
| 2 | * Authored wooden-door passage gates (ConditionSet + toast). |
| 3 | * Empty requirements = unrestricted. Players who fail may not pass even if the door is held open. |
| 4 | */ |
| 5 | import { normalizeConditionSet, emptyConditionSet, evaluateConditionSet } from "../quests/conditions.js"; |
| 6 | |
| 7 | export const DEFAULT_DOOR_BLOCKED_MESSAGE = "You cannot go through here."; |
| 8 | |
| 9 | /** |
| 10 | * Preserve runtime restingState while normalizing authored gate fields. |
| 11 | * @param {object | null | undefined} raw |
| 12 | */ |
| 13 | export function normalizeDoorConfig (raw) |
| 14 | { |
| 15 | if (!raw || typeof raw !== "object") |
| 16 | { return null; } |
| 17 | const when = normalizeConditionSet(raw.when ?? raw.requirements); |
| 18 | const blockedMessage = String(raw.blockedMessage ?? raw.denyMessage ?? "") |
| 19 | .trim() |
| 20 | .slice(0, 200); |
| 21 | const restingState = typeof raw.restingState === "string" && raw.restingState |
| 22 | ? raw.restingState |
| 23 | : undefined; |
| 24 | const hasWhen = (when.all?.length ?? 0) > 0; |
| 25 | const hasMessage = blockedMessage.length > 0; |
| 26 | if (!hasWhen && !hasMessage && restingState == null) |
| 27 | { return null; } |
| 28 | const out = {}; |
| 29 | if (restingState != null) |
| 30 | { out.restingState = restingState; } |
| 31 | if (hasWhen) |
| 32 | { out.when = when; } |
| 33 | if (hasMessage) |
| 34 | { out.blockedMessage = blockedMessage; } |
| 35 | return Object.keys(out).length ? out : null; |
| 36 | } |
| 37 | |
| 38 | export function doorRequirementSet (block) |
| 39 | { |
| 40 | const when = block?.config?.when ?? block?.config?.requirements; |
| 41 | return normalizeConditionSet(when ?? emptyConditionSet()); |
| 42 | } |
| 43 | |
| 44 | export function doorHasRequirements (block) |
| 45 | { |
| 46 | return (doorRequirementSet(block).all?.length ?? 0) > 0; |
| 47 | } |
| 48 | |
| 49 | export function doorBlockedMessage (block) |
| 50 | { |
| 51 | const text = String(block?.config?.blockedMessage ?? block?.config?.denyMessage ?? "").trim(); |
| 52 | return text || DEFAULT_DOOR_BLOCKED_MESSAGE; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param {object} block |
| 57 | * @param {object | null} ctx — evaluateConditionSet snapshot; null/undefined fails closed when gated |
| 58 | */ |
| 59 | export function doorRequirementsMet (block, ctx) |
| 60 | { |
| 61 | if (!doorHasRequirements(block)) |
| 62 | { return true; } |
| 63 | if (!ctx) |
| 64 | { return false; } |
| 65 | return evaluateConditionSet(doorRequirementSet(block), ctx); |
| 66 | } |