refactor for shared states and multi chapters

This commit is contained in:
Dan 2026-01-22 11:03:05 +00:00
parent 691b809b41
commit b9bea16f78
13 changed files with 2308 additions and 1786 deletions

View file

@ -1,7 +1,8 @@
// State Manager - Manages game state with persistence and conditions
class StateManager {
constructor(gameId) {
constructor(gameId, sharedState = null) {
this.gameId = gameId;
this.sharedState = sharedState; // Optional SharedStateManager for series games
this.state = {};
this.storageKey = `game_${gameId}_state`;
}
@ -13,18 +14,38 @@ class StateManager {
}
// Get value using dot notation path (e.g., "inventory.sword")
// Checks local state first, then falls back to shared state if available
get(path, defaultValue = undefined) {
// First check local state
const localValue = this._getFromState(this.state, path);
if (localValue !== undefined) {
return localValue;
}
// Fall back to shared state if available
if (this.sharedState) {
const sharedValue = this.sharedState.get(path);
if (sharedValue !== undefined) {
return sharedValue;
}
}
return defaultValue;
}
// Internal helper to get value from a specific state object
_getFromState(stateObj, path) {
const parts = path.split(".");
let current = this.state;
let current = stateObj;
for (const part of parts) {
if (current === undefined || current === null) {
return defaultValue;
return undefined;
}
current = current[part];
}
return current !== undefined ? current : defaultValue;
return current;
}
// Set value using dot notation path
@ -80,6 +101,25 @@ class StateManager {
return arr.includes(item);
}
// Set value in shared state (for series games)
setShared(path, value) {
if (!this.sharedState) {
console.warn("No shared state manager - setting locally instead");
return this.set(path, value);
}
this.sharedState.set(path, value);
}
// Check if path exists in shared state
hasShared(path) {
return this.sharedState?.get(path) !== undefined;
}
// Get reference to shared state manager
getSharedState() {
return this.sharedState;
}
// Evaluate a condition against current state
evaluate(condition) {
if (typeof condition === "boolean") {