@sharpee/world-model 0.9.100 → 0.9.102

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,61 @@
1
+ /**
2
+ * WorldSerializer — persistence logic for WorldModel.
3
+ *
4
+ * Public interface: serialize, deserialize.
5
+ *
6
+ * Owner context: packages/world-model (issue #70). Extracted from WorldModel
7
+ * to isolate serialization concerns and make the JSON shape independently
8
+ * testable and versionable.
9
+ */
10
+ import { IFEntity } from '../entities/if-entity';
11
+ import { SpatialIndex } from './SpatialIndex';
12
+ import { ICapabilityStore } from './capabilities';
13
+ import type { ScoreLedger } from './ScoreLedger';
14
+ /**
15
+ * References to WorldModel's internal state needed for serialization.
16
+ * These are the same references returned by WorldModel.getDataStore().
17
+ */
18
+ export interface SerializableState {
19
+ entities: Map<string, IFEntity>;
20
+ spatialIndex: SpatialIndex;
21
+ state: Record<string, unknown>;
22
+ playerId?: string;
23
+ relationships: Map<string, Map<string, Set<string>>>;
24
+ idCounters: Map<string, number>;
25
+ capabilities: ICapabilityStore;
26
+ }
27
+ /**
28
+ * Handles serialization and deserialization of WorldModel state.
29
+ * Composes with ScoreLedger and WorldEventSystem for their
30
+ * respective serialization.
31
+ */
32
+ export declare class WorldSerializer {
33
+ /**
34
+ * Serialize the world state to a JSON string.
35
+ *
36
+ * @param state - References to WorldModel's internal data structures
37
+ * @param scoreLedger - The score ledger instance
38
+ * @returns JSON string of the complete world state
39
+ */
40
+ serialize(state: SerializableState, scoreLedger: ScoreLedger): string;
41
+ /**
42
+ * Deserialize a JSON string into world state.
43
+ *
44
+ * Expects that the caller has already preserved code registrations
45
+ * (event chains, capabilities) and called clear() before invoking this.
46
+ *
47
+ * @param json - JSON string from a previous serialize() call
48
+ * @param state - References to WorldModel's internal data structures
49
+ * @param scoreLedger - The score ledger instance
50
+ */
51
+ deserialize(json: string, state: SerializableState, scoreLedger: ScoreLedger): void;
52
+ /**
53
+ * Rebuild ID counters by scanning existing entities.
54
+ * Used for backward compatibility when loading saves without idCounters.
55
+ *
56
+ * @param entities - The entity map to scan
57
+ * @param idCounters - The counter map to populate
58
+ */
59
+ static rebuildIdCounters(entities: Map<string, IFEntity>, idCounters: Map<string, number>): void;
60
+ }
61
+ //# sourceMappingURL=WorldSerializer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WorldSerializer.d.ts","sourceRoot":"","sources":["../../../../../../repos/sharpee/packages/world-model/src/world/WorldSerializer.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAGjD;;;GAGG;AACH,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAChC,YAAY,EAAE,YAAY,CAAC;IAC3B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IACrD,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,YAAY,EAAE,gBAAgB,CAAC;CAChC;AAED;;;;GAIG;AACH,qBAAa,eAAe;IAE1B;;;;;;OAMG;IACH,SAAS,CAAC,KAAK,EAAE,iBAAiB,EAAE,WAAW,EAAE,WAAW,GAAG,MAAM;IA2BrE;;;;;;;;;OASG;IACH,WAAW,CACT,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,iBAAiB,EACxB,WAAW,EAAE,WAAW,GACvB,IAAI;IAsDP;;;;;;OAMG;IACH,MAAM,CAAC,iBAAiB,CACtB,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,EAC/B,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAC9B,IAAI;CAmBR"}
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ /**
3
+ * WorldSerializer — persistence logic for WorldModel.
4
+ *
5
+ * Public interface: serialize, deserialize.
6
+ *
7
+ * Owner context: packages/world-model (issue #70). Extracted from WorldModel
8
+ * to isolate serialization concerns and make the JSON shape independently
9
+ * testable and versionable.
10
+ */
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.WorldSerializer = void 0;
13
+ const if_entity_1 = require("../entities/if-entity");
14
+ /**
15
+ * Handles serialization and deserialization of WorldModel state.
16
+ * Composes with ScoreLedger and WorldEventSystem for their
17
+ * respective serialization.
18
+ */
19
+ class WorldSerializer {
20
+ /**
21
+ * Serialize the world state to a JSON string.
22
+ *
23
+ * @param state - References to WorldModel's internal data structures
24
+ * @param scoreLedger - The score ledger instance
25
+ * @returns JSON string of the complete world state
26
+ */
27
+ serialize(state, scoreLedger) {
28
+ const data = {
29
+ entities: Array.from(state.entities.entries()).map(([id, entity]) => ({
30
+ id,
31
+ entity: entity.toJSON()
32
+ })),
33
+ state: state.state,
34
+ playerId: state.playerId,
35
+ spatialIndex: state.spatialIndex.toJSON(),
36
+ relationships: Array.from(state.relationships.entries()).map(([entityId, rels]) => ({
37
+ entityId,
38
+ relationships: Array.from(rels.entries()).map(([type, related]) => ({
39
+ type,
40
+ related: Array.from(related)
41
+ }))
42
+ })),
43
+ idCounters: Array.from(state.idCounters.entries()),
44
+ ...scoreLedger.toJSON(),
45
+ capabilities: Object.entries(state.capabilities).map(([name, cap]) => ({
46
+ name,
47
+ data: cap.data,
48
+ schema: cap.schema
49
+ }))
50
+ };
51
+ return JSON.stringify(data, null, 2);
52
+ }
53
+ /**
54
+ * Deserialize a JSON string into world state.
55
+ *
56
+ * Expects that the caller has already preserved code registrations
57
+ * (event chains, capabilities) and called clear() before invoking this.
58
+ *
59
+ * @param json - JSON string from a previous serialize() call
60
+ * @param state - References to WorldModel's internal data structures
61
+ * @param scoreLedger - The score ledger instance
62
+ */
63
+ deserialize(json, state, scoreLedger) {
64
+ const data = JSON.parse(json);
65
+ // Restore entities
66
+ for (const { id, entity } of data.entities) {
67
+ const newEntity = if_entity_1.IFEntity.fromJSON(entity);
68
+ state.entities.set(id, newEntity);
69
+ }
70
+ // Restore state
71
+ Object.assign(state.state, data.state || {});
72
+ state.playerId = data.playerId;
73
+ // Restore spatial index
74
+ if (data.spatialIndex) {
75
+ state.spatialIndex.loadJSON(data.spatialIndex);
76
+ }
77
+ // Restore relationships
78
+ if (data.relationships) {
79
+ for (const { entityId, relationships } of data.relationships) {
80
+ const entityRels = new Map();
81
+ for (const { type, related } of relationships) {
82
+ entityRels.set(type, new Set(related));
83
+ }
84
+ state.relationships.set(entityId, entityRels);
85
+ }
86
+ }
87
+ // Restore ID counters
88
+ if (data.idCounters) {
89
+ state.idCounters.clear();
90
+ for (const [key, value] of data.idCounters) {
91
+ state.idCounters.set(key, value);
92
+ }
93
+ }
94
+ else {
95
+ // Rebuild from entities for backward compatibility
96
+ WorldSerializer.rebuildIdCounters(state.entities, state.idCounters);
97
+ }
98
+ // Restore capabilities
99
+ if (data.capabilities) {
100
+ for (const { name, data: capData, schema } of data.capabilities) {
101
+ state.capabilities[name] = {
102
+ data: capData,
103
+ schema
104
+ };
105
+ }
106
+ }
107
+ // Restore score ledger
108
+ scoreLedger.fromJSON(data);
109
+ }
110
+ /**
111
+ * Rebuild ID counters by scanning existing entities.
112
+ * Used for backward compatibility when loading saves without idCounters.
113
+ *
114
+ * @param entities - The entity map to scan
115
+ * @param idCounters - The counter map to populate
116
+ */
117
+ static rebuildIdCounters(entities, idCounters) {
118
+ idCounters.clear();
119
+ for (const entity of entities.values()) {
120
+ const id = entity.id;
121
+ if (id.length >= 3) {
122
+ const prefix = id[0];
123
+ const numPart = id.substring(1);
124
+ const num = parseInt(numPart, 36);
125
+ if (!isNaN(num)) {
126
+ const currentMax = idCounters.get(prefix) || 0;
127
+ if (num > currentMax) {
128
+ idCounters.set(prefix, num);
129
+ }
130
+ }
131
+ }
132
+ }
133
+ }
134
+ }
135
+ exports.WorldSerializer = WorldSerializer;
136
+ //# sourceMappingURL=WorldSerializer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"WorldSerializer.js","sourceRoot":"","sources":["../../../../../../../repos/sharpee/packages/world-model/src/world/WorldSerializer.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;AAEH,qDAAiD;AAoBjD;;;;GAIG;AACH,MAAa,eAAe;IAE1B;;;;;;OAMG;IACH,SAAS,CAAC,KAAwB,EAAE,WAAwB;QAC1D,MAAM,IAAI,GAAG;YACX,QAAQ,EAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC;gBACpE,EAAE;gBACF,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE;aACxB,CAAC,CAAC;YACH,KAAK,EAAE,KAAK,CAAC,KAAK;YAClB,QAAQ,EAAE,KAAK,CAAC,QAAQ;YACxB,YAAY,EAAE,KAAK,CAAC,YAAY,CAAC,MAAM,EAAE;YACzC,aAAa,EAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC;gBAClF,QAAQ;gBACR,aAAa,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC;oB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