dopaminejs 2.5.0 → 3.1.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 +55 -50
  2. package/dist/DataService-D1uL6E8R.cjs +1 -0
  3. package/dist/DataService-DN6xwvXU.js +745 -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-7FzZ-yO7.cjs +37 -0
  11. package/dist/host-CdjhXPy-.js +815 -0
  12. package/dist/style.css +395 -2
  13. package/dist/testing.cjs +1 -0
  14. package/dist/testing.js +475 -0
  15. package/package.json +24 -9
  16. package/spec/README.md +228 -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 +957 -0
  22. package/spec/fixtures/scores.json +755 -0
  23. package/spec/fixtures/streaks.json +967 -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 +359 -374
  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 +360 -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 +490 -0
  42. package/types/core.d.ts +9 -1
  43. package/types/games.d.ts +46 -0
  44. package/types/index.d.ts +311 -385
  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
@@ -0,0 +1,475 @@
1
+ import { n as e, s as t } from "./DataService-DN6xwvXU.js";
2
+ import { n } from "./host-CdjhXPy-.js";
3
+ import { describe as r, it as i } from "vitest";
4
+ import { existsSync as a, readFileSync as o, readdirSync as s } from "node:fs";
5
+ import { dirname as c, join as l, resolve as u } from "node:path";
6
+ import { fileURLToPath as d } from "node:url";
7
+ //#region src/testing.js
8
+ var f = {
9
+ xp_gained: {
10
+ amount: 10,
11
+ reason: "test",
12
+ totalXP: 10,
13
+ leveledUp: !1
14
+ },
15
+ level_up: {
16
+ oldLevel: 1,
17
+ newLevel: 2,
18
+ totalXP: 50
19
+ },
20
+ achievement_unlocked: {
21
+ id: "test",
22
+ name: "Test",
23
+ description: "Test",
24
+ icon: "",
25
+ xp: 5
26
+ },
27
+ personal_best: {
28
+ activity: "test",
29
+ score: 10
30
+ },
31
+ streak_updated: {
32
+ current: 1,
33
+ longest: 1
34
+ },
35
+ player_reset: {},
36
+ save_failed: { error: /* @__PURE__ */ Error("save failed") },
37
+ load_failed: { reason: "corrupt" }
38
+ }, p = [
39
+ "confetti",
40
+ "sparkle",
41
+ "fire",
42
+ "stars",
43
+ "coins"
44
+ ], m = [
45
+ "xp",
46
+ "level_up",
47
+ "achievement",
48
+ "personal_best",
49
+ "click",
50
+ "error"
51
+ ], h = [
52
+ "",
53
+ "nope",
54
+ "__proto__",
55
+ "constructor",
56
+ "toString",
57
+ "hasOwnProperty",
58
+ void 0,
59
+ null,
60
+ 42,
61
+ {}
62
+ ], g = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/, _ = (e) => {
63
+ throw Error(e);
64
+ }, v = (e, t) => {
65
+ e || _(t);
66
+ };
67
+ function y(e) {
68
+ if (typeof e == "object" && e) return {
69
+ clock: e,
70
+ move: () => _("this clock is not a test clock, it cannot advance")
71
+ };
72
+ let n = e === void 0 ? "2026-01-15T09:00:00" : e;
73
+ (typeof n != "string" || !g.test(n)) && _("clock must be YYYY-MM-DDTHH:MM:SS local time");
74
+ let r = new Date(n).getTime();
75
+ return {
76
+ clock: {
77
+ now: () => r,
78
+ localDate: (e = r) => t.localDate(e)
79
+ },
80
+ move: (e) => {
81
+ r = e(r);
82
+ }
83
+ };
84
+ }
85
+ function b(t = {}) {
86
+ let { clock: r, storage: i, parts: a = {}, ...o } = t || {}, { clock: s, move: c } = y(r), l = [], u = [], d = [], f = [], p = [], m = /* @__PURE__ */ new Set(), h = [], v = {
87
+ ui: () => ({
88
+ root: null,
89
+ hud: (e) => {
90
+ d.push(e);
91
+ },
92
+ popup: (e) => {
93
+ l.push(e);
94
+ },
95
+ text: (e) => {
96
+ u.push(e);
97
+ },
98
+ destroy() {}
99
+ }),
100
+ particles: () => ({
101
+ burst: (e, t, n) => {
102
+ f.push({
103
+ kind: e,
104
+ at: t,
105
+ opts: n
106
+ });
107
+ },
108
+ destroy() {}
109
+ }),
110
+ sound: () => ({
111
+ play: (e) => {
112
+ p.push(e);
113
+ },
114
+ register(e) {
115
+ return m.add(e), () => {
116
+ m.delete(e);
117
+ };
118
+ },
119
+ destroy() {}
120
+ }),
121
+ haptics: () => ({
122
+ tap: () => {
123
+ h.push("tap");
124
+ },
125
+ double: () => {
126
+ h.push("double");
127
+ },
128
+ setEnabled() {},
129
+ destroy() {}
130
+ })
131
+ }, b = {};
132
+ for (let e of Object.keys(v)) b[e] = Object.prototype.hasOwnProperty.call(a, e) ? a[e] : v[e]();
133
+ let x = n({
134
+ ...o,
135
+ ...b,
136
+ storage: i || e(),
137
+ clock: s,
138
+ log: {
139
+ warn: () => {},
140
+ error: () => {},
141
+ ...o.log || {}
142
+ }
143
+ }), S = [], C = x.progress.emit;
144
+ return x.progress.emit = function(e, t) {
145
+ return S.push({
146
+ event: e,
147
+ data: t
148
+ }), C.call(this, e, t);
149
+ }, {
150
+ host: x,
151
+ progress: x.progress,
152
+ parts: x.parts,
153
+ ready: x.ready,
154
+ use: x.use,
155
+ dispose: () => {
156
+ x.dispose(), x.progress.emit = C;
157
+ },
158
+ clock: s,
159
+ popups: l,
160
+ texts: u,
161
+ huds: d,
162
+ bursts: f,
163
+ sounds: p,
164
+ soundKeys: m,
165
+ haptics: h,
166
+ events: {
167
+ log: S,
168
+ on: (e, t) => x.progress.on(e, t),
169
+ off: (e, t) => x.progress.off(e, t),
170
+ emit: (e, t) => x.progress.emit(e, t)
171
+ },
172
+ advance: (e) => c((t) => t + e),
173
+ advanceDays(e) {
174
+ c((t) => {
175
+ let n = new Date(t);
176
+ return n.setDate(n.getDate() + e), n.getTime();
177
+ });
178
+ },
179
+ setClock(e) {
180
+ g.test(e) || _("clock must be YYYY-MM-DDTHH:MM:SS local time"), c(() => new Date(e).getTime());
181
+ }
182
+ };
183
+ }
184
+ var x = async (e) => {
185
+ await e;
186
+ }, S = () => typeof document > "u" ? null : document.body.innerHTML, C = () => b().parts, w = (e) => [
187
+ ["draws every burst kind without throwing", async () => {
188
+ let t = e();
189
+ try {
190
+ for (let e of p) t.burst(e), t.burst(e, {
191
+ x: 10,
192
+ y: 20
193
+ }), t.burst(e, {
194
+ x: 10,
195
+ y: 20
196
+ }, {
197
+ count: 3,
198
+ color: "#fff"
199
+ });
200
+ } finally {
201
+ t.destroy();
202
+ }
203
+ }],
204
+ ["treats an unknown kind as a no-op", async () => {
205
+ let t = e();
206
+ try {
207
+ for (let e of h) t.burst(e, {
208
+ x: 1,
209
+ y: 1
210
+ });
211
+ } finally {
212
+ t.destroy();
213
+ }
214
+ }],
215
+ ["has destroy", async () => {
216
+ let t = e();
217
+ v(typeof t.destroy == "function", "a particle part needs destroy()"), t.destroy();
218
+ }]
219
+ ], T = (e) => [
220
+ ["plays every preset key without throwing", async () => {
221
+ let t = e();
222
+ try {
223
+ for (let e of m) await x(t.play(e));
224
+ } finally {
225
+ t.destroy();
226
+ }
227
+ }],
228
+ ["treats an unknown key as a no-op", async () => {
229
+ let t = e();
230
+ try {
231
+ for (let e of h) await x(t.play(e));
232
+ } finally {
233
+ t.destroy();
234
+ }
235
+ }],
236
+ ["stops a registered key after its remover runs", async () => {
237
+ let t = e();
238
+ try {
239
+ if (typeof t.register != "function") return;
240
+ let e = 0, n = t.register("conformance_key", () => {
241
+ e += 1;
242
+ });
243
+ v(typeof n == "function", "register() must return a remover"), n(), await x(t.play("conformance_key")), v(e === 0, "a removed key still played");
244
+ } finally {
245
+ t.destroy();
246
+ }
247
+ }]
248
+ ], E = (e) => [
249
+ ["draws the hud", async () => {
250
+ let t = e(C());
251
+ try {
252
+ t.hud({
253
+ xp: 10,
254
+ level: 1,
255
+ progress: .2,
256
+ streak: 0,
257
+ next: 50
258
+ }), t.hud({
259
+ xp: 0,
260
+ level: 1,
261
+ streak: 3
262
+ });
263
+ } finally {
264
+ t.destroy();
265
+ }
266
+ }],
267
+ ["shows every popup kind and tier", async () => {
268
+ let t = e(C());
269
+ try {
270
+ for (let e of [
271
+ "achievement",
272
+ "level",
273
+ "notice",
274
+ "custom"
275
+ ]) for (let n of [
276
+ void 0,
277
+ "common",
278
+ "rare",
279
+ "legendary"
280
+ ]) t.popup({
281
+ kind: e,
282
+ title: "T",
283
+ body: "B",
284
+ icon: "I",
285
+ meta: "M",
286
+ tier: n
287
+ });
288
+ } finally {
289
+ t.destroy();
290
+ }
291
+ }],
292
+ ["ignores a popup it cannot read", async () => {
293
+ let t = e(C());
294
+ try {
295
+ for (let e of [
296
+ void 0,
297
+ null,
298
+ 42,
299
+ "text",
300
+ {},
301
+ { kind: "__proto__" }
302
+ ]) t.popup(e);
303
+ } finally {
304
+ t.destroy();
305
+ }
306
+ }],
307
+ ["floats text", async () => {
308
+ let t = e(C());
309
+ try {
310
+ t.text({
311
+ text: "hello",
312
+ x: 5,
313
+ y: 5,
314
+ color: "#fff"
315
+ }), t.text({ text: "hello" });
316
+ } finally {
317
+ t.destroy();
318
+ }
319
+ }],
320
+ ["has a root that is an element or null", async () => {
321
+ let t = e(C());
322
+ try {
323
+ let e = t.root;
324
+ v(e === null || typeof e == "object" && typeof e.nodeType == "number", "root must be an element or null");
325
+ } finally {
326
+ t.destroy();
327
+ }
328
+ }],
329
+ ["leaves no DOM behind after destroy", async () => {
330
+ let t = S(), n = e(C());
331
+ n.hud({
332
+ xp: 1,
333
+ level: 1,
334
+ streak: 1
335
+ }), n.popup({
336
+ kind: "achievement",
337
+ title: "T"
338
+ }), n.text({ text: "x" }), n.destroy(), v(S() === t, "the page changed after destroy()");
339
+ }]
340
+ ], D = "conformance_key", O = (e) => [
341
+ ["returns null for a missing key", async () => {
342
+ let t = e();
343
+ for (let e of [
344
+ D,
345
+ "__proto__",
346
+ "constructor",
347
+ "toString"
348
+ ]) v(await t.getItem(e) === null, `getItem(${e}) should be null`);
349
+ }],
350
+ ["keeps what setItem stores and overwrites it", async () => {
351
+ let t = e();
352
+ await t.setItem(D, "one"), v(await t.getItem(D) === "one", "getItem should return the stored value"), await t.setItem(D, "two"), v(await t.getItem(D) === "two", "setItem should overwrite");
353
+ }],
354
+ ["keeps keys apart", async () => {
355
+ let t = e();
356
+ await t.setItem("a", "1"), await t.setItem("b", "2"), v(await t.getItem("a") === "1" && await t.getItem("b") === "2", "keys must not share a value");
357
+ }],
358
+ ["forgets a key after removeItem", async () => {
359
+ let t = e();
360
+ await t.setItem(D, "one"), await t.removeItem(D), v(await t.getItem(D) === null, "removeItem should clear the key"), await t.removeItem(D);
361
+ }]
362
+ ];
363
+ function k() {
364
+ let e = /* @__PURE__ */ new Set(), t = {}, n = [
365
+ "setTimeout",
366
+ "setInterval",
367
+ "requestAnimationFrame"
368
+ ], r = {
369
+ setTimeout: "clearTimeout",
370
+ setInterval: "clearInterval",
371
+ requestAnimationFrame: "cancelAnimationFrame"
372
+ };
373
+ for (let i of n) t[i] = globalThis[i], t[r[i]] = globalThis[r[i]], typeof t[i] == "function" && (globalThis[i] = (n, ...r) => {
374
+ let a = i !== "setInterval", o;
375
+ return o = t[i]((...t) => (a && e.delete(o), n(...t)), ...r), e.add(o), o;
376
+ }, typeof t[r[i]] == "function" && (globalThis[r[i]] = (n) => (e.delete(n), t[r[i]](n))));
377
+ return {
378
+ live: () => e.size,
379
+ stop() {
380
+ for (let n of e) t.clearTimeout?.(n), t.clearInterval?.(n), t.cancelAnimationFrame?.(n);
381
+ for (let e of Object.keys(t)) typeof t[e] == "function" && (globalThis[e] = t[e]);
382
+ }
383
+ };
384
+ }
385
+ var A = (e) => {
386
+ let t = 0;
387
+ for (let n of e.listeners.values()) t += n.size;
388
+ return t;
389
+ };
390
+ async function j(e, t) {
391
+ let n = k(), r = [], i = console.error;
392
+ console.error = (...e) => {
393
+ r.push(e);
394
+ };
395
+ let a = b();
396
+ try {
397
+ await a.ready, await t(a, {
398
+ watch: n,
399
+ errors: r
400
+ });
401
+ } finally {
402
+ console.error = i, a.dispose(), n.stop();
403
+ }
404
+ }
405
+ var M = {
406
+ particles: w,
407
+ sound: T,
408
+ ui: E,
409
+ storage: O,
410
+ plugin: (e) => [
411
+ ["has a name, a version and the host api", async () => {
412
+ let t = e();
413
+ v(typeof t.name == "string" && t.name !== "", "a plugin needs a name"), v(typeof t.version == "string" && t.version !== "", "a plugin needs a version"), v(t.api === 3, "a plugin needs api 3"), v(typeof t.install == "function", "a plugin needs install()");
414
+ }],
415
+ ["installs on a host and survives every core event", async () => {
416
+ await j(e, async (t, { errors: n }) => {
417
+ t.use(e()), await t.ready;
418
+ for (let e of Object.keys(f)) t.progress.emit(e, f[e]);
419
+ await t.progress.addXP(5, "conformance"), v(n.length === 0, `a listener threw: ${String(n[0]?.[1] ?? n[0]?.[0])}`);
420
+ });
421
+ }],
422
+ ["leaves no listeners, timers or DOM behind once disposed", async () => {
423
+ await j(e, async (t, { watch: n }) => {
424
+ await t.ready;
425
+ let r = A(t.progress), i = S(), a = n.live(), o = t.use(e());
426
+ await t.progress.addXP(1, "conformance");
427
+ for (let e of Object.keys(f)) t.progress.emit(e, f[e]);
428
+ await t.ready, o(), v(A(t.progress) === r, "the plugin left listeners on the bus"), v(n.live() <= a, "the plugin left timers running"), v(S() === i, "the plugin left DOM in the page");
429
+ });
430
+ }]
431
+ ]
432
+ }, N = (e) => (t) => {
433
+ r(`conformance.${e}`, () => {
434
+ for (let [n, r] of M[e](t)) i(n, r);
435
+ });
436
+ }, P = {
437
+ particles: N("particles"),
438
+ sound: N("sound"),
439
+ ui: N("ui"),
440
+ storage: N("storage"),
441
+ plugin: N("plugin")
442
+ };
443
+ async function F(e, t) {
444
+ Object.prototype.hasOwnProperty.call(M, e) || _(`unknown suite ${String(e)}`);
445
+ let n = [];
446
+ for (let [r, i] of M[e](t)) try {
447
+ await i();
448
+ } catch {
449
+ n.push(r);
450
+ }
451
+ return n;
452
+ }
453
+ function I() {
454
+ let e = typeof __dirname == "string" ? __dirname : c(d(import.meta.url));
455
+ for (let t of ["../spec", "../../../spec"]) {
456
+ let n = u(e, t);
457
+ if (a(l(n, "VERSION"))) return n;
458
+ }
459
+ return _("spec/ was not found next to this package");
460
+ }
461
+ function L(e) {
462
+ let t = e || I(), n = Number(/contract:\s*(\d+)/.exec(o(l(t, "VERSION"), "utf8"))?.[1]), r = JSON.parse(o(l(t, "schema.json"), "utf8")), i = {};
463
+ for (let e of s(l(t, "fixtures")).sort()) {
464
+ if (!e.endsWith(".json")) continue;
465
+ let n = JSON.parse(o(l(t, "fixtures", e), "utf8"));
466
+ i[n.suite] = n;
467
+ }
468
+ return {
469
+ contract: n,
470
+ schema: r,
471
+ suites: i
472
+ };
473
+ }
474
+ //#endregion
475
+ export { P as conformance, F as conformanceFailures, b as createTestHost, L as loadFixtures };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dopaminejs",
3
- "version": "2.5.0",
3
+ "version": "3.1.0",
4
4
  "description": "Progression mechanics for web apps: XP, levels, achievements and timezone-correct daily streaks, with the particles and sound to make them land. Zero dependencies.",
5
5
  "type": "module",
6
6
  "main": "./dist/dopamine.cjs",
@@ -11,21 +11,28 @@
11
11
  "import": "./dist/dopamine.js",
12
12
  "require": "./dist/dopamine.cjs"
13
13
  },
14
- "./engine": {
15
- "types": "./types/index.d.ts",
16
- "import": "./dist/engine.js",
17
- "require": "./dist/engine.cjs"
18
- },
19
14
  "./core": {
20
15
  "types": "./types/core.d.ts",
21
16
  "import": "./dist/core.js",
22
17
  "require": "./dist/core.cjs"
23
18
  },
24
- "./style.css": "./dist/style.css"
19
+ "./games": {
20
+ "types": "./types/games.d.ts",
21
+ "import": "./dist/games.js",
22
+ "require": "./dist/games.cjs"
23
+ },
24
+ "./testing": {
25
+ "types": "./types/testing.d.ts",
26
+ "import": "./dist/testing.js",
27
+ "require": "./dist/testing.cjs"
28
+ },
29
+ "./style.css": "./dist/style.css",
30
+ "./spec/*": "./spec/*"
25
31
  },
26
32
  "sideEffects": false,
27
33
  "files": [
28
34
  "dist",
35
+ "spec",
29
36
  "src",
30
37
  "types"
31
38
  ],
@@ -47,10 +54,10 @@
47
54
  "engagement",
48
55
  "rewards",
49
56
  "user-retention",
50
- "game-feel",
51
57
  "juice",
52
58
  "particles",
53
- "confetti"
59
+ "confetti",
60
+ "feedback"
54
61
  ],
55
62
  "author": "BaryoDev",
56
63
  "license": "MPL-2.0",
@@ -66,5 +73,13 @@
66
73
  "types": "./types/index.d.ts",
67
74
  "engines": {
68
75
  "node": ">=20.19"
76
+ },
77
+ "peerDependencies": {
78
+ "vitest": ">=2.0.0"
79
+ },
80
+ "peerDependenciesMeta": {
81
+ "vitest": {
82
+ "optional": true
83
+ }
69
84
  }
70
85
  }