-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathsave-load.js
More file actions
284 lines (248 loc) · 6.63 KB
/
Copy pathsave-load.js
File metadata and controls
284 lines (248 loc) · 6.63 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import superjson from 'superjson'
/**
* Helpers to save and load the entire game state.
* We use `superjson` instead of JSON.stringify/parse,
* because the state contains Set()s and Map()s.
*/
/** @typedef {import('../game/dungeon.js').Dungeon} Dungeon */
/** @typedef {import('../game/dungeon.js').Graph} Graph */
/** @typedef {import('../game/dungeon.js').MapNode} MapNode */
/** @typedef {import('../game/actions.js').State} GameState */
// Current version of the game state format
const CURRENT_VERSION = 1
/**
* Extended game state with version information
* @typedef {GameState & {_version?: number}} VersionedGameState
*/
/**
* Compresses a dungeon structure for more efficient serialization
* @param {Dungeon} dungeon - The dungeon to compress
* @returns {object} A compressed representation of the dungeon
*/
function compressDungeon(dungeon) {
// Create a compressed representation
const compressed = {
id: dungeon.id,
x: dungeon.x,
y: dungeon.y,
// Only store active rooms and their positions
rooms: [],
// Store edges as arrays of indices rather than Sets
edges: [],
// Current position and path
pathTaken: dungeon.pathTaken
}
// Extract and store only rooms that exist (not empty nodes)
dungeon.graph.forEach((floor, y) => {
floor.forEach((node, x) => {
if (node.type) {
// Store minimal information about each room
const room = {
id: node.id,
pos: [y, x],
type: node.type,
didVisit: node.didVisit
}
// Only include room data if it has a room
if (node.room) {
room.room = node.room
}
compressed.rooms.push(room)
// Convert Set of edges to array of room IDs
if (node.edges && node.edges.size > 0) {
compressed.edges.push({
from: node.id,
to: Array.from(node.edges)
})
}
}
})
})
// Also preserve the paths for backwards compatibility
compressed.paths = dungeon.paths
return compressed
}
/**
* Expands a compressed dungeon back to its full structure
* @param {object} compressed - The compressed dungeon
* @returns {Dungeon} The expanded dungeon
*/
function expandDungeon(compressed) {
// If it's already an uncompressed dungeon (has graph property), return as is
if (compressed.graph) {
return compressed
}
// Reconstruct the graph from the compressed format
/** @type {Graph} */
const graph = []
let maxY = 0
let maxX = 0
// First find the dimensions needed
compressed.rooms.forEach(room => {
const [y, x] = room.pos
maxY = Math.max(maxY, y)
maxX = Math.max(maxX, x)
})
// Initialize the graph with empty nodes
for (let y = 0; y <= maxY; y++) {
const floor = []
for (let x = 0; x <= maxX; x++) {
// Create a basic empty node with required properties
floor.push({
id: `empty_${y}_${x}`,
type: '', // Empty string for no type
edges: new Set(),
didVisit: false
})
}
graph.push(floor)
}
// Place the rooms in the graph
compressed.rooms.forEach(room => {
const [y, x] = room.pos
graph[y][x] = {
id: room.id,
type: room.type,
room: room.room,
edges: new Set(),
didVisit: room.didVisit
}
})
// Reconstruct the edges
compressed.edges.forEach(edge => {
// Find the node with this ID
let fromNode = null
findNode: for (let y = 0; y < graph.length; y++) {
for (let x = 0; x < graph[y].length; x++) {
if (graph[y][x].id === edge.from) {
fromNode = graph[y][x]
break findNode
}
}
}
if (fromNode) {
// Add all edges
edge.to.forEach(toId => {
fromNode.edges.add(toId)
})
}
})
return {
id: compressed.id,
graph,
paths: compressed.paths,
x: compressed.x,
y: compressed.y,
pathTaken: compressed.pathTaken
}
}
/**
* Migrates a game state from one version to another
* @param {VersionedGameState} state - Game state to migrate
* @param {number} fromVersion - Current version
* @param {number} toVersion - Target version
* @returns {VersionedGameState} The migrated state
*/
function migrateState(state, fromVersion, toVersion) {
// If coming from version 0 (no version), add version property
if (fromVersion === 0) {
state._version = toVersion
return state
}
// Add more migration logic here when you introduce breaking changes
// For example:
// if (fromVersion === 1 && toVersion >= 2) {
// // Convert version 1 format to version 2
// }
return state
}
/**
* Encodes just a dungeon structure into a string using compression
* @param {Dungeon} dungeon
* @returns {string}
*/
export function encodeDungeon(dungeon) {
const compressed = compressDungeon(dungeon)
return superjson.stringify(compressed)
}
/**
* Decodes a dungeon from an encoded string
* @param {string} encodedDungeon
* @returns {Dungeon}
*/
export function decodeDungeon(encodedDungeon) {
const compressed = superjson.parse(encodedDungeon)
return expandDungeon(compressed)
}
/**
* Encodes any object using superjson for testing/analysis
* @param {any} object
* @returns {string}
*/
export function encodeObject(object) {
return superjson.stringify(object)
}
/**
* Decodes any object from a superjson string for testing/analysis
* @param {string} encodedObject
* @returns {any}
*/
export function decodeObject(encodedObject) {
return superjson.parse(encodedObject)
}
/**
* Encodes a game state into a string.
* @param {GameState} state
* @returns {string}
*/
export function encode(state) {
// Make a copy so we don't modify the original
/** @type {VersionedGameState} */
const stateCopy = {...state}
// Add version information
stateCopy._version = CURRENT_VERSION
// Compress the dungeon if it exists
if (stateCopy.dungeon) {
stateCopy.dungeon = compressDungeon(stateCopy.dungeon)
}
return superjson.stringify(stateCopy)
}
/**
* Decodes a serialized game state string back into an object.
* @param {string} state
* @returns {GameState}
*/
export function decode(state) {
// Parse the state
/** @type {VersionedGameState} */
const parsed = superjson.parse(state)
// Check version and migrate if necessary
const version = parsed._version || 0
if (version !== CURRENT_VERSION) {
migrateState(parsed, version, CURRENT_VERSION)
}
// If the state has a dungeon, expand it
if (parsed.dungeon) {
parsed.dungeon = expandDungeon(parsed.dungeon)
}
return parsed
}
/**
* Encodes a game state and stores it in the URL as a hash parameter.
* @param {GameState} state
*/
export function saveToUrl(state) {
try {
location.hash = encodeURIComponent(encode(state))
} catch (err) {
console.log(err)
}
}
/**
* Reads a game state from the URL and decodes it.
* @returns {GameState}
*/
export function loadFromUrl() {
const state = decodeURIComponent(window.location.hash.split('#')[1])
return decode(state)
}