dopaminejs 2.5.0 → 3.0.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.
Files changed (81) hide show
  1. package/README.md +40 -50
  2. package/dist/DataService-E4pGQ1V3.cjs +1 -0
  3. package/dist/DataService-MfmvkqXR.js +737 -0
  4. package/dist/core.cjs +1 -1
  5. package/dist/core.js +2 -2
  6. package/dist/dopamine.cjs +1 -37
  7. package/dist/dopamine.js +3 -635
  8. package/dist/games.cjs +1 -0
  9. package/dist/games.js +82 -0
  10. package/dist/host-CkRL74ck.js +814 -0
  11. package/dist/host-Pzb-QBPU.cjs +37 -0
  12. package/dist/style.css +395 -2
  13. package/dist/testing.cjs +1 -0
  14. package/dist/testing.js +474 -0
  15. package/package.json +24 -9
  16. package/spec/README.md +227 -0
  17. package/spec/VERSION +1 -0
  18. package/spec/fixtures/achievements.json +611 -0
  19. package/spec/fixtures/levels.json +421 -0
  20. package/spec/fixtures/merge.json +1739 -0
  21. package/spec/fixtures/migrate.json +916 -0
  22. package/spec/fixtures/scores.json +755 -0
  23. package/spec/fixtures/streaks.json +924 -0
  24. package/spec/schema.json +61 -0
  25. package/src/dopamine/audio/SoundManager.js +54 -24
  26. package/src/dopamine/core/DopamineError.js +7 -0
  27. package/src/dopamine/core/RewardSystem.js +323 -373
  28. package/src/dopamine/core/achievements.js +49 -0
  29. package/src/dopamine/core/clock.js +11 -0
  30. package/src/dopamine/core/levels.js +4 -0
  31. package/src/dopamine/core/merge.js +68 -0
  32. package/src/dopamine/core/middleware.js +58 -0
  33. package/src/dopamine/core/store.js +261 -0
  34. package/src/dopamine/core/streaks.js +55 -0
  35. package/src/dopamine/effects/ParticleSystem.js +30 -0
  36. package/src/dopamine/host.js +358 -0
  37. package/src/dopamine/ui/{GameUI.js → RewardUI.js} +166 -202
  38. package/src/games.js +138 -0
  39. package/src/index.js +11 -34
  40. package/src/progression.js +3 -0
  41. package/src/testing.js +489 -0
  42. package/types/core.d.ts +9 -1
  43. package/types/games.d.ts +46 -0
  44. package/types/index.d.ts +291 -384
  45. package/types/testing.d.ts +96 -0
  46. package/dist/DataService-BPXCldpo.cjs +0 -1
  47. package/dist/DataService-CH9_gv_8.js +0 -398
  48. package/dist/ScreenShake-9jgNXPS6.cjs +0 -1
  49. package/dist/ScreenShake-CLvO1wx4.js +0 -827
  50. package/dist/engine.cjs +0 -1
  51. package/dist/engine.js +0 -2
  52. package/src/core/Animator.js +0 -62
  53. package/src/core/Collider.js +0 -86
  54. package/src/core/Component.js +0 -38
  55. package/src/core/DopamineKernel.js +0 -170
  56. package/src/core/EventBus.js +0 -206
  57. package/src/core/Game.js +0 -90
  58. package/src/core/GameObject.js +0 -99
  59. package/src/core/PluginRegistry.js +0 -359
  60. package/src/core/Scene.js +0 -107
  61. package/src/core/Sprite.js +0 -51
  62. package/src/core/SystemRegistry.js +0 -263
  63. package/src/core/Vector2.js +0 -50
  64. package/src/dopamine/components/ParticleEmitter.js +0 -50
  65. package/src/dopamine/components/ScreenShake.js +0 -50
  66. package/src/dopamine/components/index.js +0 -2
  67. package/src/dopamine/index.js +0 -93
  68. package/src/dopamine/utils/TextureGenerator.js +0 -90
  69. package/src/engine.js +0 -38
  70. package/src/interfaces/IAudioSystem.js +0 -58
  71. package/src/interfaces/IParticleSystem.js +0 -51
  72. package/src/interfaces/IPhysicsSystem.js +0 -52
  73. package/src/interfaces/ISystem.js +0 -48
  74. package/src/interfaces/index.js +0 -7
  75. package/src/renderer/Renderer.js +0 -45
  76. package/src/systems/Director.js +0 -34
  77. package/src/systems/Input.js +0 -81
  78. package/src/systems/Loader.js +0 -48
  79. package/src/systems/Physics.js +0 -210
  80. package/src/systems/Ticker.js +0 -91
  81. package/src/systems/deprecate.js +0 -38
package/spec/README.md ADDED
@@ -0,0 +1,227 @@
1
+ # Conformance fixtures
2
+
3
+ Language-neutral cases that pin the progression core: saved-state migration,
4
+ merging saves, streaks, score and XP, level thresholds and achievements. They
5
+ describe what the JS core does today. Any port runs the same files and must
6
+ match.
7
+
8
+ `spec/VERSION` holds the contract number (`contract: 3`). It is the same
9
+ number as `v` in a saved document. `spec/schema.json` describes that document.
10
+
11
+ The JS runner is `packages/dopaminejs/tests/conformance.test.js`. The `dopaminejs`
12
+ package ships this folder as `spec/`, pinned to the package version, with a
13
+ `dopaminejs/spec/*` export. The build copies it from here, so the repo root is
14
+ the one place to edit.
15
+
16
+ A rule change means changing a fixture in the same commit. A port that has not
17
+ caught up goes red.
18
+
19
+ ## Files
20
+
21
+ `spec/fixtures/*.json`, each `{ "suite", "description", "cases": [...] }`.
22
+
23
+ ## A case
24
+
25
+ | Key | Meaning |
26
+ |---|---|
27
+ | `name` | Unique within the file. |
28
+ | `note` | Optional. Why the case exists, or a quirk it pins. |
29
+ | `clock` | Required. Start time, local `YYYY-MM-DDTHH:MM:SS`. No offset. |
30
+ | `config` | Optional. See below. |
31
+ | `device` | Optional. A device id stored before `init`, so the document has known keys. Without it a random id is made. |
32
+ | `storage` | Optional. `{ "failWrites": true }` makes every storage write throw, to exercise `save_failed`. Reads still work and `savedJson`/`savedRaw` are seeded first. |
33
+ | `savedJson` | Optional. Any JSON value, stored as `JSON.stringify(value)`. |
34
+ | `savedRaw` | Optional. A string stored as is, for corrupt data. |
35
+ | `afterInit` | Optional. Expectation checked after the first `init`. |
36
+ | `steps` | Optional. Ordered list of steps. |
37
+
38
+ With neither `savedJson` nor `savedRaw`, storage is empty. A case needs
39
+ `afterInit`, or at least one step, or it checks nothing and fails.
40
+
41
+ Storage is an in-memory string store. The player document lives under the key
42
+ `dopamine_player` (prefix `dopamine_` plus `player`). The device id lives under
43
+ `dopamine_device`, outside the document.
44
+
45
+ A document with no `v` is a 2.x save. It is migrated on load and on `import`
46
+ (see `migrate.json`). Any other `v` than 3, including a string, `null` or `NaN`,
47
+ is refused with `unsupported_version`.
48
+
49
+ ### Clock
50
+
51
+ The core takes a clock: `{ now(), localDate(ms?) }`. `now` is the time in
52
+ milliseconds and `localDate` is the local calendar day as `YYYY-MM-DD`, never
53
+ UTC. A runner passes a clock it controls. It does not fake the system time.
54
+
55
+ Streak dates are local calendar dates, so the case clock is a local date and
56
+ time, not an instant. The core runs in a non-UTC zone in CI (`Asia/Manila`). A
57
+ port sets its clock to the local wall time given, and must not convert through
58
+ UTC.
59
+
60
+ ### Config
61
+
62
+ - `builtinAchievements`: `false` removes the five built-ins (`first_game`,
63
+ `level_5`, `level_10`, `streak_3`, `streak_7`). Default `true`. The JS core
64
+ has no such option; the runner emulates it by replacing the achievement table
65
+ after construction. A port must do the same.
66
+ - `streakDays`: `"any"` (default) or `"record"`. With `any`, `record` and `addXP` mark today as a play day. With `record`, only `record` does. XP from an achievement reward never marks a day.
67
+ - `achievements`: extra achievements by id, `{ name, description?, icon?, xp?, when }`.
68
+
69
+ `when` replaces the check function. One of:
70
+
71
+ | Condition | True when |
72
+ |---|---|
73
+ | `{ "always": true }` | Always. |
74
+ | `{ "never": true }` | Never. |
75
+ | `{ "throws": true }` | The check raises an error. Must not stop other achievements. |
76
+ | `{ "player": "totalGamesPlayed", "gte": 2 }` | The dotted field of the snapshot is `>=` the number. |
77
+ | `{ "activity": "arcade" }` | The recorded activity name equals the string. |
78
+ | `{ "score_gte": 100 }` | The result has a score and it is `>=` the number. |
79
+
80
+ Achievements are checked in the order: built-ins in the order listed above,
81
+ then `config.achievements` in file order. Two limits come from JS objects:
82
+ integer-like ids (`"1"`, `"2"`) sort first regardless of file order, and a
83
+ config entry that reuses a built-in id replaces its definition but keeps the
84
+ built-in's position. Fixtures avoid both; do not rely on either.
85
+
86
+ `gte` and `score_gte` must be numbers, and a `player` path must name a numeric
87
+ field on a new player's snapshot, or the runner rejects the case.
88
+
89
+ ## Steps
90
+
91
+ Every step is `{ "op": ..., ...args, "expect": {...} }`. `expect` is required
92
+ except for `setClock`, where it is accepted and checked if present.
93
+
94
+ | op | args | Does |
95
+ |---|---|---|
96
+ | `setClock` | `at` | Moves the clock. |
97
+ | `reload` | `device?` | Builds a new reward system on the same storage and runs `init`, like a page load. `device` stores a new device id first, as a second device sharing the save. |
98
+ | `addXP` | `amount`, `reason?` | `addXP(amount, reason)`. |
99
+ | `record` | `activity`, `result?` | `record(activity, result)`. A missing `result` means the default. `null` is passed as `null`. Returns `true`. |
100
+ | `unlockAchievement` | `id` | `unlockAchievement(id)`. |
101
+ | `markAchievementsSeen` | `ids` | `markAchievementsSeen(ids)`. |
102
+ | `getXPForNextLevel` | none | Reads `{ total, needed, progress }`. |
103
+ | `export` | `as` | `export()`. Keeps the document under the name `as`. |
104
+ | `import` | `doc` or `from`, `silent?` | `import(doc, { silent })`. `from` names a kept document. Returns the snapshot. |
105
+ | `merge` | `a`, `b`, `as?` | `merge(a, b)`. Each side is a document or the name of a kept one. Returns the document. |
106
+ | `validate` | `doc`, `as?` | `validate(doc)`. Returns the cleaned document. |
107
+
108
+ `merge` and `validate` are pure and need no reward system. The cases run them
109
+ as steps only because the runner has one.
110
+
111
+ Events are collected from the start of each step (and from construction for
112
+ `afterInit` and `reload`) and compared after it.
113
+
114
+ ## Expectations
115
+
116
+ `expect` and `afterInit` take these keys. Unknown keys are an error.
117
+
118
+ | Key | Checks |
119
+ |---|---|
120
+ | `player` | Subset match against `snapshot()`. Every listed key must exist as an own key and match. Objects match by listed keys, arrays by length and element, numbers and strings exactly. |
121
+ | `playerExact` | Map of dotted path to value. The value at that path in the snapshot must equal it strictly, no extra keys. |
122
+ | `doc` | Subset match against `export()`, the saved document. |
123
+ | `docExact` | Like `playerExact`, against `export()`. |
124
+ | `absent` | Dotted paths under the snapshot that must not exist as own keys. |
125
+ | `achievements` | Ids in `player.achievements`, as a set. |
126
+ | `events` | The complete ordered list of events since the step began. Each is `{ "event": name, ...payload fields }`, matched as a subset. The list length must match. |
127
+ | `returns` | Subset match against the operation's return value. |
128
+ | `returnsExact` | Like `playerExact`, against the return value. |
129
+ | `same` | List of pairs of kept document names. Each pair must be equal. |
130
+ | `throws` | An error with this name (`TypeError`, `DopamineError`) is raised. State checks still run afterwards. |
131
+ | `code` | With `throws`: the `code` of the `DopamineError`. |
132
+
133
+ An empty `player`, `playerExact`, `doc`, `docExact`, `returns`, `returnsExact` or `absent`, or an empty `expect`,
134
+ is an error because it checks nothing. An empty `events` or `achievements`
135
+ list is a real check (nothing fired, nothing unlocked).
136
+
137
+ Each event is matched as `{ ...payload, "event": name }`. The `event` key is
138
+ the event name, so it wins over a payload field called `event`; the
139
+ `achievement_unlocked` payload has its own `name` (the achievement's name),
140
+ which is why the event name is not under `name`.
141
+
142
+ CI also runs the conformance file once in `America/Los_Angeles`
143
+ (`DOPAMINE_TEST_TZ`), so the daylight saving case can fail.
144
+
145
+ Event names: `xp_gained`, `level_up`, `achievement_unlocked`, `personal_best`
146
+ (payload `{ activity, score }`), `streak_updated`, `save_failed` and
147
+ `load_failed`. `load_failed` is covered for a corrupt value and for a document
148
+ the core refuses (`reason` is `corrupt`, `unsupported_version` or
149
+ `too_many_devices`). A read that fails in storage cannot be made by a fixture.
150
+
151
+ Payloads are frozen copies, and `snapshot()` is a frozen deep copy.
152
+
153
+ Timestamps (`createdAt`, `lastPlayedAt`, `unlockedAt`) are not checked.
154
+ Fixture paths use `.` as the separator, so keys in fixtures contain no dots.
155
+
156
+ ## Running them from a port
157
+
158
+ 1. Load every `spec/fixtures/*.json`.
159
+ 2. For each case, build a clock at the local time given, seed storage,
160
+ construct the system with the config and the clock, subscribe to the seven
161
+ events, run `init`.
162
+ 3. Check `afterInit`, then run each step and check its `expect`.
163
+ 4. Fail on any unknown op, step key, expectation key or condition. A fixture
164
+ must never pass by checking nothing.
165
+
166
+ Some JSON files contain keys such as `__proto__`. Parse them into a plain map,
167
+ not into a structure that treats those names specially.
168
+
169
+ ## The saved document
170
+
171
+ `export()` returns a version 3 document (`schema.json`). Every progress number
172
+ is kept per device, and a device only changes its own entries, so two copies of
173
+ a save join by taking the larger number per key. That is what `merge` does.
174
+
175
+ | Field | Merge rule |
176
+ |---|---|
177
+ | `xpBy`, `xpSpentBy`, `gamesBy`, `playsBy`, `sumsBy` | Max per device. |
178
+ | `best` | Max. |
179
+ | `level` | Max. The shown level is never lower than the curve gives for the XP. |
180
+ | `longest` | Max. |
181
+ | `playDates` | Union. When `today` is known (load, record, import), days older than 400 days before today (inclusive) and days more than 1 day after today are dropped. `merge` called without `today` drops nothing. A future date does not move the window. |
182
+ | `achievements` | Union. The earliest `unlockedAt` decides the entry and its stored `reward`. `seen` is true if either side has it true. |
183
+ | `name` | From the side with the later `lastPlayedAt`, ties take the larger string. |
184
+ | `createdAt`, `lastPlayedAt` | Min of the set values, max. |
185
+
186
+ XP is derived: the sum of `xpBy`, plus the stored `reward` of every unlocked
187
+ achievement, minus the sum of `xpSpentBy`, and not below 0. An achievement's
188
+ reward is never part of `xpBy`, so it counts once however many devices unlock
189
+ it. The current streak is derived from `playDates` as of today, so `merge` reads
190
+ no clock.
191
+
192
+ More than 20 devices across the per-device maps is refused with a
193
+ `DopamineError` (`too_many_devices`). The `legacy` key counts as a device, so
194
+ `legacy` plus 19 real devices is full. A device that would be the 21st does not
195
+ write: `addXP` and `record` leave the stored save alone and publish `load_failed`
196
+ with reason `too_many_devices`. A document with a `v` other than 3 is refused
197
+ with `unsupported_version`. A value that is not an object is `invalid_document`. Counters and sums stop at
198
+ `Number.MAX_SAFE_INTEGER`.
199
+
200
+ ### From 2.x
201
+
202
+ A save with no `v` moves under the fixed device key `legacy`, never a real
203
+ device id. The old `xp` already holds the rewards of the achievements it
204
+ unlocked, so the legacy entry is the old `xp` minus those rewards, not below 0.
205
+ Two devices that read the same 2.x save get the same entries and merge without
206
+ doubling. 2.x progress made separately on two devices before migration merges
207
+ by max, not by sum. `playDates` is rebuilt from `streak.current` and
208
+ `streak.lastPlayDate`. A level above the curve is kept.
209
+
210
+ ## Quirks pinned on purpose
211
+
212
+ Cases with a `note` pin behaviour that may look odd. Changing any of these is a
213
+ rule change and needs a fixture change.
214
+
215
+ - A new player has no play dates: `streak.current` is 0 and there is no
216
+ `lastPlayDate`. The streak starts with the first `record`, which fires no
217
+ `streak_updated` for a player with no earlier play. Loading a save counts no
218
+ day.
219
+ - Only `record` marks a day as played. `addXP` does not.
220
+ - Numbers must be finite numbers. A numeric string is dropped to the default.
221
+ - Dates must be real calendar days. `9999-99-99` and `2026-02-30` are dropped.
222
+ - A play date after today is kept and ignored for the current streak.
223
+ - Fields the core does not know are not carried.
224
+ - `__proto__`, `constructor` and `prototype` are never kept as a key.
225
+ - Achievement ids the core does not define are kept.
226
+ - Extra numbers in a `record` result add up per device. A value that is not a
227
+ positive finite number adds 0.
package/spec/VERSION ADDED
@@ -0,0 +1 @@
1
+ contract: 3