@inglorious/engine 24.9.0 → 24.11.0
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.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/engine",
|
|
3
|
-
"version": "24.
|
|
3
|
+
"version": "24.11.0",
|
|
4
4
|
"description": "A JavaScript game engine written with global state, immutability, and pure functions in mind. Have fun(ctional programming) with it!",
|
|
5
5
|
"author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"sideEffects": false,
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@inglorious/utils": "^5.
|
|
44
|
+
"@inglorious/utils": "^5.10.0"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"@inglorious/store": "^12.9.6",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"@inglorious/eslint-config": "1.2.2",
|
|
52
52
|
"prettier": "^3.10.1",
|
|
53
53
|
"typescript": "~7.0.2",
|
|
54
|
-
"vite": "^8.
|
|
54
|
+
"vite": "^8.4.1",
|
|
55
55
|
"vitest": "^5.1.2"
|
|
56
56
|
},
|
|
57
57
|
"engines": {
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const NO_TIME = 0
|
|
2
|
+
const DONE = 1
|
|
3
|
+
const LAST = -1
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* How long a timeline lasts: the sum of its phases' seconds.
|
|
7
|
+
*
|
|
8
|
+
* @param {Record<string, number>} phases - Each phase's name and seconds, in order.
|
|
9
|
+
* @returns {number} Seconds.
|
|
10
|
+
*/
|
|
11
|
+
export function durationOf(phases) {
|
|
12
|
+
return Object.values(phases).reduce((sum, seconds) => sum + seconds, NO_TIME)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Where a timeline is after `time` seconds: which phase, and how far into it
|
|
17
|
+
* (`progress`, 0 to 1). Phases follow each other in the order they are written,
|
|
18
|
+
* so a cutscene reads as its script: `{ closeIn: 1, look: 0.5, pullBack: 1 }`.
|
|
19
|
+
* Past the end it stays on the last phase, finished, with `isDone` set.
|
|
20
|
+
*
|
|
21
|
+
* @param {Record<string, number>} phases - Each phase's name and seconds, in order.
|
|
22
|
+
* @param {number} time - Seconds since the timeline began.
|
|
23
|
+
* @returns {{ name: string, progress: number, isDone: boolean }}
|
|
24
|
+
*/
|
|
25
|
+
export function phaseAt(phases, time) {
|
|
26
|
+
const entries = Object.entries(phases)
|
|
27
|
+
let start = NO_TIME
|
|
28
|
+
for (const [name, seconds] of entries) {
|
|
29
|
+
if (time < start + seconds) {
|
|
30
|
+
const progress = Math.max(NO_TIME, (time - start) / seconds)
|
|
31
|
+
return { name, progress, isDone: false }
|
|
32
|
+
}
|
|
33
|
+
start += seconds
|
|
34
|
+
}
|
|
35
|
+
const [last] = entries.at(LAST) ?? []
|
|
36
|
+
return { name: last, progress: DONE, isDone: true }
|
|
37
|
+
}
|
|
@@ -5,10 +5,18 @@ import { createRecorder, isSession } from "../core/recorder.js"
|
|
|
5
5
|
const NO_TURNS = 0
|
|
6
6
|
const STAMP_START = 0
|
|
7
7
|
const STAMP_LENGTH = 19 // "2026-12-01T13:42:32", to the second
|
|
8
|
+
const STORE_EVERY_MS = 5000
|
|
9
|
+
// characters, well under the browsers' quota for local storage (5 MB and up)
|
|
10
|
+
const STORE_LIMIT = 1_000_000
|
|
11
|
+
const LAST_RUN = 1
|
|
12
|
+
const NEVER = 0
|
|
13
|
+
const NO_CHARACTERS = 0
|
|
8
14
|
|
|
9
15
|
// One recorder per entity: kept outside the state, which the store copies on every
|
|
10
16
|
// event, while a recording grows all game long.
|
|
11
17
|
const recorders = new Map()
|
|
18
|
+
// when each entity's session was last stored
|
|
19
|
+
const storedAt = new Map()
|
|
12
20
|
|
|
13
21
|
/**
|
|
14
22
|
* Records an entity that holds a deterministic simulation, turned on `tick` (see the
|
|
@@ -39,6 +47,13 @@ const recorders = new Map()
|
|
|
39
47
|
* `{ run, runs, diverged, over }`, else null; once `over`, `tick` holds the simulation
|
|
40
48
|
* still, and the player's input is still replaced by none.
|
|
41
49
|
*
|
|
50
|
+
* With `storageKey`, the session is also kept in the browser's local storage under
|
|
51
|
+
* that key, every few seconds while playing and whenever a run starts, so a page
|
|
52
|
+
* closed by mistake loses little: opened again, the recorder carries on the stored
|
|
53
|
+
* session (see `resume`), and a saved file holds both visits. Past a million
|
|
54
|
+
* characters the oldest runs are left out of the stored copy. A replay is not
|
|
55
|
+
* stored.
|
|
56
|
+
*
|
|
42
57
|
* @param {Object} options
|
|
43
58
|
* @param {(entity: Object) => number | null} options.turn
|
|
44
59
|
* @param {(entity: Object) => *} options.state
|
|
@@ -51,6 +66,7 @@ const recorders = new Map()
|
|
|
51
66
|
* @param {string} [options.format] - What the recordings are; a session of another format is refused.
|
|
52
67
|
* @param {string} [options.build] - Which build of the game made them.
|
|
53
68
|
* @param {string} [options.name] - The start of a saved file's name, followed by the time.
|
|
69
|
+
* @param {string} [options.storageKey] - Where to keep the session in local storage; not kept without one.
|
|
54
70
|
* @returns {Function} The behavior.
|
|
55
71
|
*/
|
|
56
72
|
export function recorded({
|
|
@@ -65,6 +81,7 @@ export function recorded({
|
|
|
65
81
|
format = "recording",
|
|
66
82
|
build,
|
|
67
83
|
name = format,
|
|
84
|
+
storageKey,
|
|
68
85
|
}) {
|
|
69
86
|
const recorderOf = (entity) => recorders.get(entity.id)
|
|
70
87
|
|
|
@@ -78,6 +95,35 @@ export function recorded({
|
|
|
78
95
|
if (turn(entity) != null) recorderOf(entity).open(about(entity))
|
|
79
96
|
}
|
|
80
97
|
|
|
98
|
+
/** Keeps the session so far in local storage, if asked to, and not while replaying. */
|
|
99
|
+
function store(entity) {
|
|
100
|
+
if (!storageKey || entity.replay) return
|
|
101
|
+
storedAt.set(entity.id, Date.now())
|
|
102
|
+
const session = recorderOf(entity).session(
|
|
103
|
+
turn(entity) ?? NO_TURNS,
|
|
104
|
+
state(entity),
|
|
105
|
+
)
|
|
106
|
+
try {
|
|
107
|
+
globalThis.localStorage?.setItem(storageKey, stringify(session))
|
|
108
|
+
} catch (error) {
|
|
109
|
+
console.warn(`The recording could not be stored: ${error.message}`)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Carries on the session left in local storage, if it is one the game can play. */
|
|
114
|
+
function restore(entity) {
|
|
115
|
+
if (!storageKey) return
|
|
116
|
+
try {
|
|
117
|
+
const stored = globalThis.localStorage?.getItem(storageKey)
|
|
118
|
+
const session = stored && JSON.parse(stored)
|
|
119
|
+
if (isSession(session, format) && isPlayable(session)) {
|
|
120
|
+
recorderOf(entity).resume(session)
|
|
121
|
+
}
|
|
122
|
+
} catch (error) {
|
|
123
|
+
console.warn(`The stored recording could not be read: ${error.message}`)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
81
127
|
function play(entity, run) {
|
|
82
128
|
replayRun(entity, run.about)
|
|
83
129
|
entity.replay = recorderOf(entity).progress()
|
|
@@ -92,6 +138,7 @@ export function recorded({
|
|
|
92
138
|
close(entity)
|
|
93
139
|
type[event]?.(entity, payload, api)
|
|
94
140
|
open(entity)
|
|
141
|
+
store(entity)
|
|
95
142
|
},
|
|
96
143
|
]),
|
|
97
144
|
)
|
|
@@ -101,7 +148,9 @@ export function recorded({
|
|
|
101
148
|
|
|
102
149
|
create(entity, payload, api) {
|
|
103
150
|
recorders.set(entity.id, createRecorder({ format, build }))
|
|
151
|
+
storedAt.delete(entity.id)
|
|
104
152
|
entity.replay = null
|
|
153
|
+
restore(entity)
|
|
105
154
|
type.create?.(entity, payload, api)
|
|
106
155
|
open(entity)
|
|
107
156
|
},
|
|
@@ -128,6 +177,8 @@ export function recorded({
|
|
|
128
177
|
if (after === before) return
|
|
129
178
|
if (!entity.replay) {
|
|
130
179
|
recorder.record(before, done)
|
|
180
|
+
const since = Date.now() - (storedAt.get(entity.id) ?? NEVER)
|
|
181
|
+
if (since >= STORE_EVERY_MS) store(entity)
|
|
131
182
|
return
|
|
132
183
|
}
|
|
133
184
|
|
|
@@ -168,3 +219,15 @@ export function recorded({
|
|
|
168
219
|
}
|
|
169
220
|
}
|
|
170
221
|
}
|
|
222
|
+
|
|
223
|
+
/** A session as JSON, leaving out its oldest runs (but the last) while it is too long. */
|
|
224
|
+
function stringify(session) {
|
|
225
|
+
const runs = [...session.runs]
|
|
226
|
+
const sizes = runs.map((run) => JSON.stringify(run).length)
|
|
227
|
+
let total = sizes.reduce((sum, size) => sum + size, NO_CHARACTERS)
|
|
228
|
+
while (total > STORE_LIMIT && runs.length > LAST_RUN) {
|
|
229
|
+
runs.shift()
|
|
230
|
+
total -= sizes.shift()
|
|
231
|
+
}
|
|
232
|
+
return JSON.stringify({ ...session, runs })
|
|
233
|
+
}
|
package/src/core/recorder.js
CHANGED
|
@@ -43,6 +43,14 @@ export function createRecorder({ format = "recording", build } = {}) {
|
|
|
43
43
|
replay = null
|
|
44
44
|
},
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Carries on `session`, saved earlier (by a page closed and opened again): its
|
|
48
|
+
* runs come before the ones recorded from now on.
|
|
49
|
+
*/
|
|
50
|
+
resume(session) {
|
|
51
|
+
runs = [...session.runs, ...runs]
|
|
52
|
+
},
|
|
53
|
+
|
|
46
54
|
/** Starts recording a run, `about` being what it takes to start it again; not while replaying. */
|
|
47
55
|
open(about) {
|
|
48
56
|
if (replay) return
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Where a timeline is: the phase, how far into it (0 to 1), and whether the timeline is over. */
|
|
2
|
+
export interface Phase {
|
|
3
|
+
name: string
|
|
4
|
+
progress: number
|
|
5
|
+
isDone: boolean
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** The seconds a timeline lasts: its phases' seconds added up. */
|
|
9
|
+
export function durationOf(phases: Record<string, number>): number
|
|
10
|
+
|
|
11
|
+
/** The phase a timeline of named phases, in order, is in after `time` seconds. */
|
|
12
|
+
export function phaseAt(phases: Record<string, number>, time: number): Phase
|
package/types/core/recorder.d.ts
CHANGED
|
@@ -24,6 +24,7 @@ export interface ReplayProgress {
|
|
|
24
24
|
|
|
25
25
|
export interface Recorder<About = unknown, Input = unknown> {
|
|
26
26
|
reset(): void
|
|
27
|
+
resume(session: RecordedSession<About, Input>): void
|
|
27
28
|
open(about: About): void
|
|
28
29
|
record(tick: number, input: Input | null | undefined): void
|
|
29
30
|
close(tick: number, state: unknown): void
|