File size: 5,614 Bytes
a4a8504 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | """
Core data models for the branching narrative system.
These dataclasses describe the structured content generated by the narrative
composer and manipulated by the engine / UI.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional
@dataclass
class Choice:
"""
A single choice offered to the player within a scene.
"""
id: str
label: str
result_text: str
next_scene_id: Optional[str]
delta_score: int = 0
risk_level: str = "safe" # safe | risky | chaotic
loot: Optional[Dict[str, Any]] = None
stat_changes: Optional[Dict[str, int]] = None
@staticmethod
def from_dict(data: Dict[str, Any]) -> "Choice":
"""Create a `Choice` object from a dictionary payload."""
return Choice(
id=data["id"],
label=data.get("label", ""),
result_text=data.get("result_text", ""),
next_scene_id=data.get("next_scene_id"),
delta_score=int(data.get("delta_score", 0)),
risk_level=data.get("risk_level", "safe"),
loot=data.get("loot"),
stat_changes=data.get("stat_changes"),
)
def to_dict(self) -> Dict[str, Any]:
"""Serialize the choice back into a JSON-friendly dict."""
return {
"id": self.id,
"label": self.label,
"result_text": self.result_text,
"next_scene_id": self.next_scene_id,
"delta_score": self.delta_score,
"risk_level": self.risk_level,
"loot": self.loot,
"stat_changes": self.stat_changes,
}
@dataclass
class Scene:
"""
A single narrative scene containing story text and available choices.
"""
id: str
title: str
body: str
choices: List[Choice]
is_terminal: bool = False
ending_tag: Optional[str] = None # win | lose | weird | etc.
emoji: Optional[str] = None # optional flair for UI
@staticmethod
def from_dict(data: Dict[str, Any]) -> "Scene":
"""Create a `Scene` object from a dictionary payload."""
return Scene(
id=data["id"],
title=data.get("title", ""),
body=data.get("body", ""),
choices=[Choice.from_dict(choice) for choice in data.get("choices", [])],
is_terminal=bool(data.get("is_terminal", False)),
ending_tag=data.get("ending_tag"),
emoji=data.get("emoji"),
)
def to_dict(self) -> Dict[str, Any]:
"""Serialize the scene back into a JSON-friendly dict."""
return {
"id": self.id,
"title": self.title,
"body": self.body,
"choices": [choice.to_dict() for choice in self.choices],
"is_terminal": self.is_terminal,
"ending_tag": self.ending_tag,
"emoji": self.emoji,
}
@dataclass
class GameSpec:
"""
Full branching narrative specification generated by the composer.
"""
id: str
title: str
mode: str
start_scene_id: str
scenes: Dict[str, Scene]
metadata: Dict[str, Any] = field(default_factory=dict)
@staticmethod
def from_dict(data: Dict[str, Any]) -> "GameSpec":
"""Create a `GameSpec` from a dictionary payload."""
scenes_payload = data.get("scenes", {})
return GameSpec(
id=data["id"],
title=data.get("title", "Untitled Adventure"),
mode=data.get("mode", "Unknown"),
start_scene_id=data.get("start_scene_id"),
scenes={scene_id: Scene.from_dict(scene) for scene_id, scene in scenes_payload.items()},
metadata=data.get("metadata", {}),
)
def to_dict(self) -> Dict[str, Any]:
"""Serialize the `GameSpec` into a JSON-friendly dict."""
return {
"id": self.id,
"title": self.title,
"mode": self.mode,
"start_scene_id": self.start_scene_id,
"scenes": {scene_id: scene.to_dict() for scene_id, scene in self.scenes.items()},
"metadata": self.metadata,
}
@dataclass
class GameState:
"""
Mutable state that tracks the player's progress within a `GameSpec`.
"""
game_id: str
current_scene_id: str
score: int = 0
inventory: List[Dict[str, Any]] = field(default_factory=list)
stats: Dict[str, int] = field(default_factory=dict)
history: List[Dict[str, Any]] = field(default_factory=list)
is_complete: bool = False
ending_tag: Optional[str] = None
def to_dict(self) -> Dict[str, Any]:
"""Serialize the current state into a JSON-friendly dict."""
return {
"game_id": self.game_id,
"current_scene_id": self.current_scene_id,
"score": self.score,
"inventory": self.inventory,
"stats": self.stats,
"history": self.history,
"is_complete": self.is_complete,
"ending_tag": self.ending_tag,
}
@staticmethod
def from_dict(data: Dict[str, Any]) -> "GameState":
"""Rehydrate a `GameState` from a dictionary payload."""
return GameState(
game_id=data["game_id"],
current_scene_id=data.get("current_scene_id"),
score=int(data.get("score", 0)),
inventory=data.get("inventory", []),
stats=data.get("stats", {}),
history=data.get("history", []),
is_complete=bool(data.get("is_complete", False)),
ending_tag=data.get("ending_tag"),
)
|