dopaminejs 3.5.0 → 3.7.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.
@@ -1,5 +1,6 @@
1
+ import { n as e } from "./storage-C2s4Da9b.js";
1
2
  // src/dopamine/core/EventEmitter.js
2
- var e = class {
3
+ var t = class {
3
4
  constructor() {
4
5
  this.listeners = /* @__PURE__ */ new Map();
5
6
  }
@@ -21,17 +22,17 @@ var e = class {
21
22
  clear() {
22
23
  this.listeners.clear();
23
24
  }
24
- }, t = class extends Error {
25
+ }, n = class extends Error {
25
26
  constructor(e, t) {
26
27
  super(t || e), this.name = "DopamineError", this.code = e;
27
28
  }
28
- }, n = (e, t = 2) => String(e).padStart(t, "0"), r = {
29
+ }, r = (e, t = 2) => String(e).padStart(t, "0"), i = {
29
30
  now: () => Date.now(),
30
31
  localDate(e = Date.now()) {
31
32
  let t = new Date(e);
32
- return `${n(t.getFullYear(), 4)}-${n(t.getMonth() + 1)}-${n(t.getDate())}`;
33
+ return `${r(t.getFullYear(), 4)}-${r(t.getMonth() + 1)}-${r(t.getDate())}`;
33
34
  }
34
- }, i = (e) => e > 0 ? Math.max(1, Math.floor((1 + Math.sqrt(1 + 4 * e / 50)) / 2)) : 1, a = (e) => 50 * e * (e - 1), o = () => ({
35
+ }, a = (e) => e > 0 ? Math.max(1, Math.floor((1 + Math.sqrt(1 + 4 * e / 50)) / 2)) : 1, o = (e) => 50 * e * (e - 1), s = () => ({
35
36
  first_game: {
36
37
  name: "First Steps",
37
38
  description: "Play your first game",
@@ -67,57 +68,57 @@ var e = class {
67
68
  xp: 300,
68
69
  check: (e) => e.streak.current >= 7
69
70
  }
70
- }), s = {
71
+ }), c = {
71
72
  first_game: 50,
72
73
  level_5: 100,
73
74
  level_10: 250,
74
75
  streak_3: 100,
75
76
  streak_7: 300
76
- }, c = (e) => {
77
+ }, l = (e) => {
77
78
  let t = {};
78
79
  for (let n of Object.keys(e)) t[n] = Number.isFinite(e[n]?.xp) && e[n].xp > 0 ? e[n].xp : 0;
79
80
  return t;
80
- }, l = 864e5, u = /^\d{4}-\d{2}-\d{2}$/, d = (e) => {
81
+ }, u = 864e5, d = /^\d{4}-\d{2}-\d{2}$/, f = (e) => {
81
82
  let t = /* @__PURE__ */ new Date(0);
82
- return t.setUTCFullYear(+e.slice(0, 4), e.slice(5, 7) - 1, +e.slice(8)), t.getTime() / l;
83
- }, f = (e) => (/* @__PURE__ */ new Date(e * l)).toISOString().slice(0, 10), p = (e) => typeof e == "string" && u.test(e) && f(d(e)) === e, m = (e, t) => f(d(e) + t);
84
- function h(e, t) {
83
+ return t.setUTCFullYear(+e.slice(0, 4), e.slice(5, 7) - 1, +e.slice(8)), t.getTime() / u;
84
+ }, p = (e) => (/* @__PURE__ */ new Date(e * u)).toISOString().slice(0, 10), m = (e) => typeof e == "string" && d.test(e) && p(f(e)) === e, h = (e, t) => p(f(e) + t);
85
+ function g(e, t) {
85
86
  let n = [...new Set(e)].sort();
86
87
  if (!t) return n;
87
- let r = d(t) - 399, i = d(t) + 1;
88
- return n.filter((e) => d(e) >= r && d(e) <= i);
88
+ let r = f(t) - 399, i = f(t) + 1;
89
+ return n.filter((e) => f(e) >= r && f(e) <= i);
89
90
  }
90
- function g(e, t) {
91
- let n = new Set(e), r = n.has(t) ? t : m(t, -1), i = 0;
92
- for (; n.has(r);) i++, r = m(r, -1);
91
+ function _(e, t) {
92
+ let n = new Set(e), r = n.has(t) ? t : h(t, -1), i = 0;
93
+ for (; n.has(r);) i++, r = h(r, -1);
93
94
  return i;
94
95
  }
95
- function _(e) {
96
+ function v(e) {
96
97
  let t = 0, n = 0, r = NaN;
97
98
  for (let i of [...e].sort()) {
98
- let e = d(i);
99
+ let e = f(i);
99
100
  n = e === r + 1 ? n + 1 : 1, r = e, n > t && (t = n);
100
101
  }
101
102
  return t;
102
103
  }
103
- var ee = (e, t) => e.reduce((e, n) => n <= t && (!e || n > e) ? n : e, void 0), v = "legacy", te = /* @__PURE__ */ new Set([
104
+ var ee = (e, t) => e.reduce((e, n) => n <= t && (!e || n > e) ? n : e, void 0), y = "legacy", te = /* @__PURE__ */ new Set([
104
105
  "__proto__",
105
106
  "constructor",
106
107
  "prototype"
107
- ]), y = (e) => typeof e == "string" && e.length > 0 && !te.has(e), b = (e, t) => Object.prototype.hasOwnProperty.call(e, t), x = (e) => typeof e == "object" && !!e && !Array.isArray(e), ne = /^\s*-?\d+(\.\d+)?\s*$/;
108
- function S(e) {
108
+ ]), b = (e) => typeof e == "string" && e.length > 0 && !te.has(e), x = (e, t) => Object.prototype.hasOwnProperty.call(e, t), S = (e) => typeof e == "object" && !!e && !Array.isArray(e), ne = /^\s*-?\d+(\.\d+)?\s*$/;
109
+ function C(e) {
109
110
  return typeof e == "string" && (e = ne.test(e) ? Number(e) : void 0), typeof e == "number" && Number.isFinite(e) ? e : void 0;
110
111
  }
111
- var C = (e) => Math.min(e, 2 ** 53 - 1), re = S, w = (e) => {
112
- let t = S(e);
112
+ var w = (e) => Math.min(e, 2 ** 53 - 1), re = C, T = (e) => {
113
+ let t = C(e);
113
114
  return t !== void 0 && t >= 0 ? t : void 0;
114
- }, T = (e) => {
115
- let t = S(e);
115
+ }, E = (e) => {
116
+ let t = C(e);
116
117
  return Number.isInteger(t) && t >= 0 ? t : void 0;
117
- }, E = (e, t) => re(e) ?? t, D = (e, t) => w(e) ?? t, O = (e, t, n = 0) => {
118
- let r = S(e);
119
- return Number.isInteger(r) && r >= n ? C(r) : t;
120
- }, k = (e, t = (e) => e) => Object.keys(e).sort().reduce((n, r) => C(n + t(e[r])), 0), A = (e = 0) => ({
118
+ }, D = (e, t) => re(e) ?? t, O = (e, t) => T(e) ?? t, k = (e, t, n = 0) => {
119
+ let r = C(e);
120
+ return Number.isInteger(r) && r >= n ? w(r) : t;
121
+ }, A = (e, t = (e) => e) => Object.keys(e).sort().reduce((n, r) => w(n + t(e[r])), 0), j = (e = 0) => ({
121
122
  v: 3,
122
123
  name: "Player",
123
124
  createdAt: e,
@@ -133,35 +134,35 @@ var C = (e) => Math.min(e, 2 ** 53 - 1), re = S, w = (e) => {
133
134
  sumsBy: {},
134
135
  achievements: {}
135
136
  });
136
- function j(e, t) {
137
+ function M(e, t) {
137
138
  let n = {};
138
- if (!x(e)) return n;
139
+ if (!S(e)) return n;
139
140
  for (let r of Object.keys(e)) {
140
- let i = y(r) ? t(e[r]) : void 0;
141
- i !== void 0 && (n[r] = C(i));
141
+ let i = b(r) ? t(e[r]) : void 0;
142
+ i !== void 0 && (n[r] = w(i));
142
143
  }
143
144
  return n;
144
145
  }
145
- function M(e, t) {
146
+ function N(e, t) {
146
147
  let n = {};
147
- if (!x(e)) return n;
148
- for (let r of Object.keys(e)) y(r) && (n[r] = j(e[r], t));
148
+ if (!S(e)) return n;
149
+ for (let r of Object.keys(e)) b(r) && (n[r] = M(e[r], t));
149
150
  return n;
150
151
  }
151
- function N(e, t) {
152
+ function P(e, t) {
152
153
  let n = {};
153
- if (!x(e)) return n;
154
+ if (!S(e)) return n;
154
155
  for (let r of Object.keys(e)) {
155
156
  let i = e[r];
156
- y(r) && x(i) && (n[r] = {
157
- unlockedAt: E(i.unlockedAt, t),
157
+ b(r) && S(i) && (n[r] = {
158
+ unlockedAt: D(i.unlockedAt, t),
158
159
  seen: i.seen === !0,
159
- reward: C(D(i.reward, 0))
160
+ reward: w(O(i.reward, 0))
160
161
  });
161
162
  }
162
163
  return n;
163
164
  }
164
- var P = /* @__PURE__ */ new Set([
165
+ var F = /* @__PURE__ */ new Set([
165
166
  "v",
166
167
  "name",
167
168
  "createdAt",
@@ -177,98 +178,98 @@ var P = /* @__PURE__ */ new Set([
177
178
  "sumsBy",
178
179
  "achievements"
179
180
  ]), ie = /* @__PURE__ */ new Set([
180
- ...P,
181
+ ...F,
181
182
  "xp",
182
183
  "totalGamesPlayed",
183
184
  "stats",
184
185
  "streak"
185
186
  ]);
186
- function F(e, t = 0) {
187
+ function I(e, t = 0) {
187
188
  if (e === null || typeof e == "string" || typeof e == "boolean") return e;
188
189
  if (typeof e == "number") return Number.isFinite(e) ? e : void 0;
189
190
  if (t >= 32) return;
190
- if (Array.isArray(e)) return e.map((e) => F(e, t + 1) ?? null);
191
- if (!x(e)) return;
191
+ if (Array.isArray(e)) return e.map((e) => I(e, t + 1) ?? null);
192
+ if (!S(e)) return;
192
193
  let n = {};
193
194
  for (let r of Object.keys(e)) {
194
- let i = y(r) ? F(e[r], t + 1) : void 0;
195
+ let i = b(r) ? I(e[r], t + 1) : void 0;
195
196
  i !== void 0 && (n[r] = i);
196
197
  }
197
198
  return n;
198
199
  }
199
- function I(e, t) {
200
+ function L(e, t) {
200
201
  for (let n of Object.keys(e)) {
201
- if (ie.has(n) || !y(n)) continue;
202
- let r = F(e[n]);
202
+ if (ie.has(n) || !b(n)) continue;
203
+ let r = I(e[n]);
203
204
  r !== void 0 && (t[n] = r);
204
205
  }
205
206
  return t;
206
207
  }
207
- var L = (e) => Object.keys(e).filter((e) => !P.has(e));
208
- function ae(e) {
208
+ var R = (e) => Object.keys(e).filter((e) => !F.has(e));
209
+ function z(e) {
209
210
  let t = e.sumsBy, n = /* @__PURE__ */ new Set([
210
211
  ...Object.keys(e.playsBy),
211
212
  ...Object.keys(e.best),
212
213
  ...Object.keys(t)
213
214
  ]);
214
- for (let r of n) b(e.playsBy, r) || (e.playsBy[r] = {}), b(e.best, r) || (e.best[r] = 0), b(t, r) || (t[r] = {});
215
+ for (let r of n) x(e.playsBy, r) || (e.playsBy[r] = {}), x(e.best, r) || (e.best[r] = 0), x(t, r) || (t[r] = {});
215
216
  return e;
216
217
  }
217
- function oe(e, t, n) {
218
- return I(e, ae({
218
+ function ae(e, t, n) {
219
+ return L(e, z({
219
220
  v: 3,
220
221
  name: typeof e.name == "string" ? e.name : "Player",
221
- createdAt: E(e.createdAt, t),
222
- lastPlayedAt: E(e.lastPlayedAt, t),
223
- level: O(e.level, 1, 1),
224
- longest: O(e.longest, 0),
225
- playDates: h(Array.isArray(e.playDates) ? e.playDates.filter(p) : [], n),
226
- xpBy: j(e.xpBy, w),
227
- xpSpentBy: j(e.xpSpentBy, w),
228
- gamesBy: j(e.gamesBy, T),
229
- playsBy: M(e.playsBy, T),
230
- best: j(e.best, w),
231
- sumsBy: se(e.sumsBy),
232
- achievements: N(e.achievements, t)
222
+ createdAt: D(e.createdAt, t),
223
+ lastPlayedAt: D(e.lastPlayedAt, t),
224
+ level: k(e.level, 1, 1),
225
+ longest: k(e.longest, 0),
226
+ playDates: g(Array.isArray(e.playDates) ? e.playDates.filter(m) : [], n),
227
+ xpBy: M(e.xpBy, T),
228
+ xpSpentBy: M(e.xpSpentBy, T),
229
+ gamesBy: M(e.gamesBy, E),
230
+ playsBy: N(e.playsBy, E),
231
+ best: M(e.best, T),
232
+ sumsBy: oe(e.sumsBy),
233
+ achievements: P(e.achievements, t)
233
234
  }));
234
235
  }
235
- function se(e) {
236
+ function oe(e) {
236
237
  let t = {};
237
- if (!x(e)) return t;
238
- for (let n of Object.keys(e)) y(n) && (t[n] = M(e[n], w));
238
+ if (!S(e)) return t;
239
+ for (let n of Object.keys(e)) b(n) && (t[n] = N(e[n], T));
239
240
  return t;
240
241
  }
241
- function ce(e, t, n, r) {
242
- let a = A(t);
243
- a.name = typeof e.name == "string" ? e.name : "Player", a.createdAt = E(e.createdAt, t), a.lastPlayedAt = E(e.lastPlayedAt, t), a.achievements = N(e.achievements, t);
242
+ function se(e, t, n, r) {
243
+ let i = j(t);
244
+ i.name = typeof e.name == "string" ? e.name : "Player", i.createdAt = D(e.createdAt, t), i.lastPlayedAt = D(e.lastPlayedAt, t), i.achievements = P(e.achievements, t);
244
245
  let o = 0;
245
- for (let e of Object.keys(a.achievements)) {
246
- let t = b(n, e) ? C(D(n[e], 0)) : 0;
247
- a.achievements[e].reward = t, o += t;
248
- }
249
- let s = C(D(e.xp, 0)), c = Math.max(0, s - o);
250
- c > 0 && (a.xpBy[v] = c), a.level = Math.max(i(s), O(e.level, 1, 1));
251
- let l = O(e.totalGamesPlayed, 0);
252
- if (l > 0 && (a.gamesBy[v] = l), x(e.stats)) for (let t of Object.keys(e.stats)) {
246
+ for (let e of Object.keys(i.achievements)) {
247
+ let t = x(n, e) ? w(O(n[e], 0)) : 0;
248
+ i.achievements[e].reward = t, o += t;
249
+ }
250
+ let s = w(O(e.xp, 0)), c = Math.max(0, s - o);
251
+ c > 0 && (i.xpBy[y] = c), i.level = Math.max(a(s), k(e.level, 1, 1));
252
+ let l = k(e.totalGamesPlayed, 0);
253
+ if (l > 0 && (i.gamesBy[y] = l), S(e.stats)) for (let t of Object.keys(e.stats)) {
253
254
  let n = e.stats[t];
254
- if (!y(t) || !x(n)) continue;
255
- let r = O(n.totalPlays, 0);
256
- a.playsBy[t] = r > 0 ? { [v]: r } : {}, a.best[t] = C(D(n.highScore, 0)), a.sumsBy[t] = {};
255
+ if (!b(t) || !S(n)) continue;
256
+ let r = k(n.totalPlays, 0);
257
+ i.playsBy[t] = r > 0 ? { [y]: r } : {}, i.best[t] = w(O(n.highScore, 0)), i.sumsBy[t] = {};
257
258
  for (let e of Object.keys(n)) {
258
- if (!y(e) || e === "totalPlays" || e === "highScore") continue;
259
- let r = w(n[e]);
260
- r !== void 0 && (a.sumsBy[t][e] = { [v]: C(r) });
259
+ if (!b(e) || e === "totalPlays" || e === "highScore") continue;
260
+ let r = T(n[e]);
261
+ r !== void 0 && (i.sumsBy[t][e] = { [y]: w(r) });
261
262
  }
262
263
  }
263
- let u = x(e.streak) ? e.streak : {}, d = O(u.current, 1, 1);
264
- if (a.longest = Math.max(d, O(u.longest, 0)), p(u.lastPlayDate)) {
264
+ let u = S(e.streak) ? e.streak : {}, d = k(u.current, 1, 1);
265
+ if (i.longest = Math.max(d, k(u.longest, 0)), m(u.lastPlayDate)) {
265
266
  let e = r && u.lastPlayDate > r ? r : u.lastPlayDate;
266
- for (let t = 0; t < Math.min(d, 400); t++) a.playDates.push(m(e, -t));
267
- a.playDates = h(a.playDates, r);
268
- } else a.longest = Math.max(O(u.current, 0), O(u.longest, 0));
269
- return I(e, ae(a));
267
+ for (let t = 0; t < Math.min(d, 400); t++) i.playDates.push(h(e, -t));
268
+ i.playDates = g(i.playDates, r);
269
+ } else i.longest = Math.max(k(u.current, 0), k(u.longest, 0));
270
+ return L(e, z(i));
270
271
  }
271
- function R(e) {
272
+ function B(e) {
272
273
  let t = /* @__PURE__ */ new Set([
273
274
  ...Object.keys(e.xpBy),
274
275
  ...Object.keys(e.xpSpentBy),
@@ -278,37 +279,37 @@ function R(e) {
278
279
  for (let n of Object.keys(e.sumsBy)) for (let r of Object.keys(e.sumsBy[n])) for (let i of Object.keys(e.sumsBy[n][r])) t.add(i);
279
280
  return t;
280
281
  }
281
- function z(e) {
282
- if (R(e).size > 20) throw new t("too_many_devices", "[DopamineJS] a save holds at most 20 devices");
282
+ function V(e) {
283
+ if (B(e).size > 20) throw new n("too_many_devices", "[DopamineJS] a save holds at most 20 devices");
283
284
  }
284
- function B(e, n = {}) {
285
- if (!x(e)) throw new t("invalid_document", "[DopamineJS] a save must be an object");
286
- if (e.v !== void 0 && e.v !== 3) throw new t("unsupported_version", `[DopamineJS] save version ${String(e.v)} is not 3`);
287
- let r = Number.isFinite(n.now) ? n.now : 0, i = p(n.today) ? n.today : void 0, a = e.v === 3 ? oe(e, r, i) : ce(e, r, x(n.rewards) ? n.rewards : s, i);
288
- return z(a), a;
285
+ function H(e, t = {}) {
286
+ if (!S(e)) throw new n("invalid_document", "[DopamineJS] a save must be an object");
287
+ if (e.v !== void 0 && e.v !== 3) throw new n("unsupported_version", `[DopamineJS] save version ${String(e.v)} is not 3`);
288
+ let r = Number.isFinite(t.now) ? t.now : 0, i = m(t.today) ? t.today : void 0, a = e.v === 3 ? ae(e, r, i) : se(e, r, S(t.rewards) ? t.rewards : c, i);
289
+ return V(a), a;
289
290
  }
290
- var V = (e) => C(Math.max(0, k(e.xpBy) + k(e.achievements, (e) => e.reward) - k(e.xpSpentBy))), H = (e) => Math.max(e.level, i(V(e)));
291
- function le(e, t) {
291
+ var U = (e) => w(Math.max(0, A(e.xpBy) + A(e.achievements, (e) => e.reward) - A(e.xpSpentBy))), W = (e) => Math.max(e.level, a(U(e)));
292
+ function ce(e, t) {
292
293
  let n = {};
293
294
  for (let t of Object.keys(e.playsBy)) {
294
295
  let r = {
295
- plays: k(e.playsBy[t]),
296
+ plays: A(e.playsBy[t]),
296
297
  best: e.best[t]
297
298
  };
298
- for (let n of Object.keys(e.sumsBy[t])) n !== "plays" && n !== "best" && (r[n] = k(e.sumsBy[t][n]));
299
+ for (let n of Object.keys(e.sumsBy[t])) n !== "plays" && n !== "best" && (r[n] = A(e.sumsBy[t][n]));
299
300
  n[t] = r;
300
301
  }
301
302
  let r = {};
302
303
  for (let t of Object.keys(e.achievements)) r[t] = { ...e.achievements[t] };
303
304
  let i = {
304
- current: g(e.playDates, t),
305
- longest: Math.max(e.longest, _(e.playDates))
305
+ current: _(e.playDates, t),
306
+ longest: Math.max(e.longest, v(e.playDates))
306
307
  }, a = ee(e.playDates, t);
307
308
  return a && (i.lastPlayDate = a), {
308
309
  name: e.name,
309
- xp: V(e),
310
- level: H(e),
311
- totalGamesPlayed: k(e.gamesBy),
310
+ xp: U(e),
311
+ level: W(e),
312
+ totalGamesPlayed: A(e.gamesBy),
312
313
  createdAt: e.createdAt,
313
314
  lastPlayedAt: e.lastPlayedAt,
314
315
  streak: i,
@@ -316,28 +317,28 @@ function le(e, t) {
316
317
  stats: n
317
318
  };
318
319
  }
319
- var U = (e) => JSON.parse(JSON.stringify(e));
320
- function W(e) {
320
+ var G = (e) => JSON.parse(JSON.stringify(e));
321
+ function K(e) {
321
322
  if (typeof e == "object" && e && !Object.isFrozen(e)) {
322
323
  Object.freeze(e);
323
- for (let t of Object.keys(e)) W(e[t]);
324
+ for (let t of Object.keys(e)) K(e[t]);
324
325
  }
325
326
  return e;
326
327
  }
327
328
  // src/dopamine/core/middleware.js
328
- var G = [
329
+ var q = [
329
330
  "beforeXP",
330
331
  "afterXP",
331
332
  "beforeRecord",
332
333
  "afterRecord",
333
334
  "beforeUnlock",
334
335
  "afterUnlock"
335
- ], ue = class {
336
+ ], le = class {
336
337
  constructor() {
337
338
  this._list = [];
338
339
  }
339
340
  use(e) {
340
- if (!x(e) || !G.some((t) => typeof e[t] == "function")) throw TypeError(`[DopamineJS] use expects an object with one of: ${G.join(", ")}`);
341
+ if (!S(e) || !q.some((t) => typeof e[t] == "function")) throw TypeError(`[DopamineJS] use expects an object with one of: ${q.join(", ")}`);
341
342
  return this._list.push(e), () => {
342
343
  let t = this._list.indexOf(e);
343
344
  t >= 0 && this._list.splice(t, 1);
@@ -354,7 +355,7 @@ var G = [
354
355
  continue;
355
356
  }
356
357
  if (i === !1) return !1;
357
- if (!x(i)) continue;
358
+ if (!S(i)) continue;
358
359
  let a = { ...t };
359
360
  for (let e of Object.keys(t)) Object.prototype.hasOwnProperty.call(i, e) && (a[e] = i[e]);
360
361
  n(a) && (t = a);
@@ -370,18 +371,18 @@ var G = [
370
371
  }
371
372
  };
372
373
  // src/dopamine/core/merge.js
373
- function K(e, t, n) {
374
+ function J(e, t, n) {
374
375
  let r = {};
375
376
  for (let i of /* @__PURE__ */ new Set([...Object.keys(e), ...Object.keys(t)])) {
376
- let a = b(e, i) ? e[i] : void 0, o = b(t, i) ? t[i] : void 0;
377
- r[i] = n > 0 ? K(a || {}, o || {}, n - 1) : a === void 0 ? o : o === void 0 ? a : Math.max(a, o);
377
+ let a = x(e, i) ? e[i] : void 0, o = x(t, i) ? t[i] : void 0;
378
+ r[i] = n > 0 ? J(a || {}, o || {}, n - 1) : a === void 0 ? o : o === void 0 ? a : Math.max(a, o);
378
379
  }
379
380
  return r;
380
381
  }
381
- function de(e, t) {
382
+ function ue(e, t) {
382
383
  let n = {};
383
384
  for (let r of /* @__PURE__ */ new Set([...Object.keys(e), ...Object.keys(t)])) {
384
- let i = b(e, r) ? e[r] : void 0, a = b(t, r) ? t[r] : void 0;
385
+ let i = x(e, r) ? e[r] : void 0, a = x(t, r) ? t[r] : void 0;
385
386
  if (!i || !a) {
386
387
  n[r] = { ...i || a };
387
388
  continue;
@@ -395,34 +396,34 @@ function de(e, t) {
395
396
  }
396
397
  return n;
397
398
  }
398
- var q = (e) => Array.isArray(e) ? `[${e.map(q).join(",")}]` : typeof e == "object" && e ? `{${Object.keys(e).sort().map((t) => `${JSON.stringify(t)}:${q(e[t])}`).join(",")}}` : JSON.stringify(e), fe = (e, t) => (Number.isFinite(e) && Number.isFinite(t) && e !== t ? e > t : q(e) >= q(t)) ? e : t;
399
- function pe(e, t, n) {
400
- for (let r of /* @__PURE__ */ new Set([...L(e), ...L(t)])) {
401
- let i = b(e, r) ? e[r] : void 0, a = b(t, r) ? t[r] : void 0;
402
- n[r] = i === void 0 ? a : a === void 0 ? i : fe(i, a);
399
+ var Y = (e) => Array.isArray(e) ? `[${e.map(Y).join(",")}]` : typeof e == "object" && e ? `{${Object.keys(e).sort().map((t) => `${JSON.stringify(t)}:${Y(e[t])}`).join(",")}}` : JSON.stringify(e), de = (e, t) => (Number.isFinite(e) && Number.isFinite(t) && e !== t ? e > t : Y(e) >= Y(t)) ? e : t;
400
+ function fe(e, t, n) {
401
+ for (let r of /* @__PURE__ */ new Set([...R(e), ...R(t)])) {
402
+ let i = x(e, r) ? e[r] : void 0, a = x(t, r) ? t[r] : void 0;
403
+ n[r] = i === void 0 ? a : a === void 0 ? i : de(i, a);
403
404
  }
404
405
  }
405
- var me = (e, t) => e > 0 && t > 0 ? Math.min(e, t) : Math.max(e, t);
406
- function J(e, t, n) {
407
- let r = B(e, n), i = B(t, n), a = {
406
+ var pe = (e, t) => e > 0 && t > 0 ? Math.min(e, t) : Math.max(e, t);
407
+ function X(e, t, n) {
408
+ let r = H(e, n), i = H(t, n), a = {
408
409
  v: 3,
409
410
  name: (r.lastPlayedAt === i.lastPlayedAt ? r.name >= i.name ? r : i : r.lastPlayedAt > i.lastPlayedAt ? r : i).name,
410
- createdAt: me(r.createdAt, i.createdAt),
411
+ createdAt: pe(r.createdAt, i.createdAt),
411
412
  lastPlayedAt: Math.max(r.lastPlayedAt, i.lastPlayedAt),
412
413
  level: Math.max(r.level, i.level),
413
414
  longest: Math.max(r.longest, i.longest),
414
- playDates: h([...r.playDates, ...i.playDates], p(n?.today) ? n.today : void 0),
415
- xpBy: K(r.xpBy, i.xpBy, 0),
416
- xpSpentBy: K(r.xpSpentBy, i.xpSpentBy, 0),
417
- gamesBy: K(r.gamesBy, i.gamesBy, 0),
418
- playsBy: K(r.playsBy, i.playsBy, 1),
419
- best: K(r.best, i.best, 0),
420
- sumsBy: K(r.sumsBy, i.sumsBy, 2),
421
- achievements: de(r.achievements, i.achievements)
415
+ playDates: g([...r.playDates, ...i.playDates], m(n?.today) ? n.today : void 0),
416
+ xpBy: J(r.xpBy, i.xpBy, 0),
417
+ xpSpentBy: J(r.xpSpentBy, i.xpSpentBy, 0),
418
+ gamesBy: J(r.gamesBy, i.gamesBy, 0),
419
+ playsBy: J(r.playsBy, i.playsBy, 1),
420
+ best: J(r.best, i.best, 0),
421
+ sumsBy: J(r.sumsBy, i.sumsBy, 2),
422
+ achievements: ue(r.achievements, i.achievements)
422
423
  };
423
- return pe(r, i, a), z(a), a;
424
+ return fe(r, i, a), V(a), a;
424
425
  }
425
- function Y(e) {
426
+ function Z(e) {
426
427
  let t = /* @__PURE__ */ new Map([
427
428
  [JSON.stringify(["xpBy"]), e.xpBy],
428
429
  [JSON.stringify(["xpSpentBy"]), e.xpSpentBy],
@@ -436,38 +437,38 @@ function Y(e) {
436
437
  ]), e.sumsBy[n][r]);
437
438
  return t;
438
439
  }
439
- function he(e) {
440
+ function me(e) {
440
441
  let t = /* @__PURE__ */ new Set();
441
- for (let n of Y(e).values()) for (let e in n) n[e] > 0 && t.add(e);
442
+ for (let n of Z(e).values()) for (let e in n) n[e] > 0 && t.add(e);
442
443
  return t;
443
444
  }
444
- function X(e, t) {
445
+ function Q(e, t) {
445
446
  let n = /* @__PURE__ */ new Map();
446
- for (let [r, i] of Y(e)) b(i, t) && n.set(r, i[t]);
447
+ for (let [r, i] of Z(e)) x(i, t) && n.set(r, i[t]);
447
448
  return n;
448
449
  }
449
- function ge(e, t, n) {
450
+ function he(e, t, n) {
450
451
  for (let [r, i] of n) {
451
452
  let n = e;
452
- for (let e of JSON.parse(r)) b(n, e) || (n[e] = {}), n = n[e];
453
- n[t] = C((b(n, t) ? n[t] : 0) + i);
453
+ for (let e of JSON.parse(r)) x(n, e) || (n[e] = {}), n = n[e];
454
+ n[t] = w((x(n, t) ? n[t] : 0) + i);
454
455
  }
455
456
  }
456
- function _e(e, t, n, r, i = /* @__PURE__ */ new Map()) {
457
- let a = B(e, r), o = B(t, r), s = J(a, o, r);
457
+ function ge(e, t, n, r, i = /* @__PURE__ */ new Map()) {
458
+ let a = H(e, r), o = H(t, r), s = X(a, o, r);
458
459
  s.name = a.name;
459
- for (let e of L(a)) s[e] = a[e];
460
- let c = Y(s), l = Y(a);
461
- for (let [e, t] of Y(o)) {
462
- if (!b(t, n)) continue;
463
- let r = Math.min(i.get(e) ?? 0, t[n]), a = l.has(e) && b(l.get(e), n) ? l.get(e)[n] : 0;
464
- c.get(e)[n] = C(Math.max(a, r) + (t[n] - r));
460
+ for (let e of R(a)) s[e] = a[e];
461
+ let c = Z(s), l = Z(a);
462
+ for (let [e, t] of Z(o)) {
463
+ if (!x(t, n)) continue;
464
+ let r = Math.min(i.get(e) ?? 0, t[n]), a = l.has(e) && x(l.get(e), n) ? l.get(e)[n] : 0;
465
+ c.get(e)[n] = w(Math.max(a, r) + (t[n] - r));
465
466
  }
466
467
  return s;
467
468
  }
468
- function ve(e, t, n) {
469
- let r = U(e), i = (e) => {
470
- b(e, t) && (e[n] = b(e, n) ? Math.max(e[n], e[t]) : e[t], delete e[t]);
469
+ function _e(e, t, n) {
470
+ let r = G(e), i = (e) => {
471
+ x(e, t) && (e[n] = x(e, n) ? Math.max(e[n], e[t]) : e[t], delete e[t]);
471
472
  };
472
473
  for (let e of [
473
474
  r.xpBy,
@@ -479,17 +480,17 @@ function ve(e, t, n) {
479
480
  return r;
480
481
  }
481
482
  // src/dopamine/core/RewardSystem.js
482
- var Z = "device", ye = () => {
483
+ var $ = "device", ve = () => {
483
484
  let e = /* @__PURE__ */ new Uint8Array(8), t = globalThis.crypto;
484
485
  if (typeof t?.getRandomValues == "function") t.getRandomValues(e);
485
486
  else for (let t = 0; t < e.length; t++) e[t] = Math.floor(Math.random() * 256);
486
487
  return Array.from(e, (e) => e.toString(16).padStart(2, "0")).join("");
487
- }, be = (e) => y(e) && e !== "legacy", xe = class extends e {
488
+ }, ye = (e) => b(e) && e !== "legacy", be = class extends t {
488
489
  constructor(e, t = {}) {
489
- super(), this.dataService = e, this.clock = t.clock && typeof t.clock.now == "function" && typeof t.clock.localDate == "function" ? t.clock : r, this.achievements = {
490
- ...t.builtinAchievements === !1 ? {} : o(),
490
+ super(), this.dataService = e, this.clock = t.clock && typeof t.clock.now == "function" && typeof t.clock.localDate == "function" ? t.clock : i, this.achievements = {
491
+ ...t.builtinAchievements === !1 ? {} : s(),
491
492
  ...t.achievements || {}
492
- }, this.streakDays = t.streakDays === "record" ? "record" : "any", this._doc = null, this._deviceId = null, this._deviceSaved = !1, this._middleware = new ue(), this._unlocking = /* @__PURE__ */ new Set(), this._batchDepth = 0, this._pendingSave = !1, this._loadFailed = !1, this._readFailed = !1, this.loadRetryMs = Number.isFinite(t.loadRetryMs) && t.loadRetryMs >= 0 ? t.loadRetryMs : 3e4, this._lastRead = 0, this._reading = !1, this._recovering = null, this._epoch = 0, this._forgetImports(), this._recoveries = 0;
493
+ }, this.streakDays = t.streakDays === "record" ? "record" : "any", this._doc = null, this._deviceId = null, this._deviceSaved = !1, this._middleware = new le(), this._unlocking = /* @__PURE__ */ new Set(), this._batchDepth = 0, this._pendingSave = !1, this._loadFailed = !1, this._readFailed = !1, this.loadRetryMs = Number.isFinite(t.loadRetryMs) && t.loadRetryMs >= 0 ? t.loadRetryMs : 3e4, this._lastRead = 0, this._reading = !1, this._recovering = null, this._epoch = 0, this._forgetImports(), this._recoveries = 0;
493
494
  }
494
495
  get loadFailed() {
495
496
  return this._readFailed;
@@ -511,20 +512,20 @@ var Z = "device", ye = () => {
511
512
  now: this.clock.now(),
512
513
  today: this.clock.localDate(),
513
514
  rewards: {
514
- ...s,
515
- ...c(this.achievements)
515
+ ...c,
516
+ ...l(this.achievements)
516
517
  }
517
518
  };
518
519
  }
519
520
  async init() {
520
- let e = this._doc && !this._loadFailed && X(this._doc, this._deviceId), t = await this._readSaved();
521
+ let e = this._doc && !this._loadFailed && Q(this._doc, this._deviceId), t = await this._readSaved();
521
522
  return this._loadFailed && this._doc ? e && e.forEach((e, t) => this._base.set(t, Math.max(this._base.get(t) ?? 0, e))) : (this._doc = this._load(t), this._forgetImports()), await this._readDevice(), await this.save(), this.snapshot();
522
523
  }
523
524
  async _readSaved() {
524
525
  this._loadFailed = !0, this._reading = !0, this._epoch++, this._recovering = null, this._lastRead = this.clock.now();
525
526
  try {
526
527
  let e = await this.dataService.load("player", null, { strict: !0 });
527
- return e !== null && !x(e) ? await this._keepCorrupt(e) : (this._loadFailed = !1, this._readFailed = !1, e);
528
+ return e !== null && !S(e) ? await this._keepCorrupt(e) : (this._loadFailed = !1, this._readFailed = !1, e);
528
529
  } catch (e) {
529
530
  return e?.code === "DOPAMINE_CORRUPT" ? this._keepCorrupt(e.raw) : (this._readFailed = !0, this._publish("load_failed", {
530
531
  key: "player",
@@ -548,19 +549,19 @@ var Z = "device", ye = () => {
548
549
  async _retryRead(e) {
549
550
  let t, n = null;
550
551
  try {
551
- if (t = await this.dataService.load("player", null, { strict: !0 }), t !== null && !x(t)) return !1;
552
+ if (t = await this.dataService.load("player", null, { strict: !0 }), t !== null && !S(t)) return !1;
552
553
  if (!this._deviceSaved) {
553
- let e = await this.dataService.load(Z, null, { strict: !0 });
554
- be(e) && e !== this._deviceId && (n = e);
554
+ let e = await this.dataService.load($, null, { strict: !0 });
555
+ ye(e) && e !== this._deviceId && (n = e);
555
556
  }
556
557
  } catch {
557
558
  return !1;
558
559
  }
559
- if (e !== this._epoch || !this._loadFailed || he(this._doc).has(n)) return !1;
560
+ if (e !== this._epoch || !this._loadFailed || me(this._doc).has(n)) return !1;
560
561
  let r;
561
562
  try {
562
- let e = this._options(), i = n ?? this._deviceId, a = n ? ve(this._doc, this._deviceId, n) : this._doc;
563
- r = t === null ? U(a) : _e(t, a, i, e, this._base), z(r), r.level = H(r);
563
+ let e = this._options(), i = n ?? this._deviceId, a = n ? _e(this._doc, this._deviceId, n) : this._doc;
564
+ r = t === null ? G(a) : ge(t, a, i, e, this._base), V(r), r.level = W(r);
564
565
  } catch {
565
566
  return !1;
566
567
  }
@@ -579,31 +580,31 @@ var Z = "device", ye = () => {
579
580
  }), null;
580
581
  }
581
582
  _load(e) {
582
- if (!x(e)) return A(this.clock.now());
583
+ if (!S(e)) return j(this.clock.now());
583
584
  try {
584
- return B(e, this._options());
585
+ return H(e, this._options());
585
586
  } catch (e) {
586
- if (!(e instanceof t)) throw e;
587
+ if (!(e instanceof n)) throw e;
587
588
  return this._loadFailed = !0, this._readFailed = !0, this._publish("load_failed", {
588
589
  key: "player",
589
590
  reason: e.code,
590
591
  error: e
591
- }), A(this.clock.now());
592
+ }), j(this.clock.now());
592
593
  }
593
594
  }
594
595
  async _readDevice() {
595
596
  if (this._deviceId) return;
596
597
  let e = null;
597
598
  try {
598
- e = await this.dataService.load(Z, null, { strict: !0 });
599
+ e = await this.dataService.load($, null, { strict: !0 });
599
600
  } catch {
600
- this._deviceId = ye();
601
+ this._deviceId = ve();
601
602
  return;
602
603
  }
603
- be(e) ? (this._deviceId = e, this._deviceSaved = !0) : this._deviceId = ye();
604
+ ye(e) ? (this._deviceId = e, this._deviceSaved = !0) : this._deviceId = ve();
604
605
  }
605
606
  _hasSlot() {
606
- let e = R(this._doc);
607
+ let e = B(this._doc);
607
608
  return e.has(this._deviceId) || e.size < 20 ? !0 : (this._publish("load_failed", {
608
609
  key: "player",
609
610
  reason: "too_many_devices"
@@ -613,23 +614,23 @@ var Z = "device", ye = () => {
613
614
  if (!this._doc) throw Error("[DopamineJS] RewardSystem.init() must be awaited before use");
614
615
  }
615
616
  snapshot() {
616
- return this._requirePlayer(), W(le(this._doc, this.clock.localDate()));
617
+ return this._requirePlayer(), K(ce(this._doc, this.clock.localDate()));
617
618
  }
618
619
  export() {
619
- return this._requirePlayer(), U(this._doc);
620
+ return this._requirePlayer(), G(this._doc);
620
621
  }
621
622
  async import(e, { silent: t = !1 } = {}) {
622
623
  this._requirePlayer();
623
- let n = this.snapshot(), r = B(e, this._options()), i = this._doc, a = null;
624
+ let n = this.snapshot(), r = H(e, this._options()), i = this._doc, a = null;
624
625
  if (this._readFailed) {
625
626
  a = new Map(this._base);
626
- let e = /* @__PURE__ */ new Map(), t = X(this._doc, this._deviceId);
627
- X(r, this._deviceId).forEach((n, r) => {
627
+ let e = /* @__PURE__ */ new Map(), t = Q(this._doc, this._deviceId);
628
+ Q(r, this._deviceId).forEach((n, r) => {
628
629
  n > (t.get(r) ?? 0) && (e.set(r, n - (a.get(r) ?? 0)), a.set(r, n));
629
- }), i = U(this._doc), ge(i, this._deviceId, e);
630
+ }), i = G(this._doc), he(i, this._deviceId, e);
630
631
  }
631
- let o = J(i, r, this._options());
632
- o.level = H(o), this._doc = o, a && (this._base = a);
632
+ let o = X(i, r, this._options());
633
+ o.level = W(o), this._doc = o, a && (this._base = a);
633
634
  let s = this.snapshot();
634
635
  if (await this.save(), t) return s;
635
636
  let c = s.xp - n.xp, l = s.level > n.level;
@@ -644,7 +645,7 @@ var Z = "device", ye = () => {
644
645
  newLevel: s.level,
645
646
  totalXP: s.xp
646
647
  });
647
- for (let e of Object.keys(s.achievements)) !b(n.achievements, e) && b(this.achievements, e) && this._publish("achievement_unlocked", {
648
+ for (let e of Object.keys(s.achievements)) !x(n.achievements, e) && x(this.achievements, e) && this._publish("achievement_unlocked", {
648
649
  ...this.achievements[e],
649
650
  id: e
650
651
  });
@@ -653,17 +654,17 @@ var Z = "device", ye = () => {
653
654
  async reset(e) {
654
655
  let t = e?.overwrite;
655
656
  if (e !== void 0 && (typeof e != "object" || !e || Array.isArray(e)) || t !== void 0 && typeof t != "boolean") throw TypeError("[DopamineJS] reset expects { overwrite?: boolean }");
656
- return this._requirePlayer(), t === !1 && this._readFailed ? (this._doc = A(this.clock.now()), this._forgetImports(), this._publish("player_reset", {}), this.snapshot()) : (await this._batch(async () => {
657
- this._doc = A(this.clock.now()), this._loadFailed = !1, this._readFailed = !1, this._forgetImports(), await this.save();
657
+ return this._requirePlayer(), t === !1 && this._readFailed ? (this._doc = j(this.clock.now()), this._forgetImports(), this._publish("player_reset", {}), this.snapshot()) : (await this._batch(async () => {
658
+ this._doc = j(this.clock.now()), this._loadFailed = !1, this._readFailed = !1, this._forgetImports(), await this.save();
658
659
  }), this._publish("player_reset", {}), this.snapshot());
659
660
  }
660
661
  use(e) {
661
662
  return this._middleware.use(e);
662
663
  }
663
664
  defineAchievement(e, t) {
664
- if (!y(e)) throw TypeError(`[DopamineJS] defineAchievement expects an id, received ${String(e)}`);
665
- if (!x(t) || typeof t.check != "function") throw TypeError("[DopamineJS] defineAchievement expects a definition with a check function");
666
- let n = b(this.achievements, e) ? this.achievements[e] : void 0;
665
+ if (!b(e)) throw TypeError(`[DopamineJS] defineAchievement expects an id, received ${String(e)}`);
666
+ if (!S(t) || typeof t.check != "function") throw TypeError("[DopamineJS] defineAchievement expects a definition with a check function");
667
+ let n = x(this.achievements, e) ? this.achievements[e] : void 0;
667
668
  return this.achievements[e] = t, () => {
668
669
  this.achievements[e] === t && (n ? this.achievements[e] = n : delete this.achievements[e]);
669
670
  };
@@ -672,13 +673,13 @@ var Z = "device", ye = () => {
672
673
  this._doc && (this._batchDepth > 0 ? this._pendingSave = !0 : await this._write());
673
674
  }
674
675
  async _write() {
675
- this._loadFailed ? await this._recover() : (this._doc.lastPlayedAt = this.clock.now(), this._deviceSaved ||= await this.dataService.save(Z, this._deviceId) !== !1, await this.dataService.save("player", this._doc) === !1 && this._publish("save_failed", { key: "player" }));
676
+ this._loadFailed ? await this._recover() : (this._doc.lastPlayedAt = this.clock.now(), this._deviceSaved ||= await this.dataService.save($, this._deviceId) !== !1, await this.dataService.save("player", this._doc) === !1 && this._publish("save_failed", { key: "player" }));
676
677
  }
677
678
  async addXP(e, t = "") {
678
679
  if (!Number.isFinite(e)) throw TypeError(`[DopamineJS] addXP expects a finite number, received ${e}`);
679
680
  if (this._requirePlayer(), !this._hasSlot()) return {
680
681
  leveledUp: !1,
681
- newLevel: H(this._doc),
682
+ newLevel: W(this._doc),
682
683
  xpGained: 0
683
684
  };
684
685
  let n = await this._middleware.before("beforeXP", {
@@ -687,40 +688,40 @@ var Z = "device", ye = () => {
687
688
  }, (e) => Number.isFinite(e.amount));
688
689
  if (n === !1) return {
689
690
  leveledUp: !1,
690
- newLevel: H(this._doc),
691
+ newLevel: W(this._doc),
691
692
  xpGained: 0
692
693
  };
693
694
  this.streakDays === "any" && n.amount > 0 && this._markDay();
694
695
  let r = await this._applyXP(n.amount, n.reason, (e, t) => {
695
- t >= 0 ? e.xpBy[this._deviceId] = C((b(e.xpBy, this._deviceId) ? e.xpBy[this._deviceId] : 0) + t) : e.xpSpentBy[this._deviceId] = C((b(e.xpSpentBy, this._deviceId) ? e.xpSpentBy[this._deviceId] : 0) - t);
696
+ t >= 0 ? e.xpBy[this._deviceId] = w((x(e.xpBy, this._deviceId) ? e.xpBy[this._deviceId] : 0) + t) : e.xpSpentBy[this._deviceId] = w((x(e.xpSpentBy, this._deviceId) ? e.xpSpentBy[this._deviceId] : 0) - t);
696
697
  });
697
698
  return await this._middleware.after("afterXP", {
698
699
  amount: r.xpGained,
699
700
  reason: n.reason,
700
- total: V(this._doc),
701
+ total: U(this._doc),
701
702
  level: r.newLevel
702
703
  }), r;
703
704
  }
704
705
  async _applyXP(e, t, n) {
705
- let r = this._doc, i = H(r);
706
- return e = Math.max(e, -V(r)), n(r, e), this._announceXP(i, e, t);
706
+ let r = this._doc, i = W(r);
707
+ return e = Math.max(e, -U(r)), n(r, e), this._announceXP(i, e, t);
707
708
  }
708
709
  async _announceXP(e, t, n) {
709
- let r = this._doc, a = V(r), o = Math.max(e, i(a));
710
+ let r = this._doc, i = U(r), o = Math.max(e, a(i));
710
711
  o > r.level && (r.level = o);
711
712
  let s = this._recoveries;
712
- await this.save(), this._recoveries !== s && (a = V(this._doc), o = Math.max(o, H(this._doc)), e = Math.max(e, this._savedLevel, i(a - t)));
713
+ await this.save(), this._recoveries !== s && (i = U(this._doc), o = Math.max(o, W(this._doc)), e = Math.max(e, this._savedLevel, a(i - t)));
713
714
  let c = o > e;
714
715
  return this._publish("xp_gained", {
715
716
  amount: t,
716
717
  reason: n,
717
718
  leveledUp: c,
718
719
  newLevel: o,
719
- total: a
720
+ total: i
720
721
  }), c && this._publish("level_up", {
721
722
  oldLevel: e,
722
723
  newLevel: o,
723
- totalXP: a
724
+ totalXP: i
724
725
  }), {
725
726
  leveledUp: c,
726
727
  newLevel: o,
@@ -729,16 +730,16 @@ var Z = "device", ye = () => {
729
730
  }
730
731
  getXPForNextLevel() {
731
732
  this._requirePlayer();
732
- let e = H(this._doc), t = V(this._doc), n = a(e + 1), r = a(e), i = n - r, o = i > 0 ? Math.max(0, Math.min(1, (t - r) / i)) : 0;
733
+ let e = W(this._doc), t = U(this._doc), n = o(e + 1), r = o(e), i = n - r, a = i > 0 ? Math.max(0, Math.min(1, (t - r) / i)) : 0;
733
734
  return {
734
735
  total: n,
735
736
  needed: Math.max(0, n - t),
736
- progress: o
737
+ progress: a
737
738
  };
738
739
  }
739
740
  _checkRecord(e, t) {
740
- if (!y(e)) throw TypeError(`[DopamineJS] record expects an activity name, received ${String(e)}`);
741
- if (!x(t)) throw TypeError("[DopamineJS] record expects a result object");
741
+ if (!b(e)) throw TypeError(`[DopamineJS] record expects an activity name, received ${String(e)}`);
742
+ if (!S(t)) throw TypeError("[DopamineJS] record expects a result object");
742
743
  if (t.score !== void 0 && !Number.isFinite(t.score)) throw TypeError(`[DopamineJS] record expects a finite score, received ${String(t.score)}`);
743
744
  }
744
745
  async record(e, t = {}) {
@@ -765,27 +766,27 @@ var Z = "device", ye = () => {
765
766
  let e = this._doc, t = this.clock.localDate();
766
767
  if (e.playDates.includes(t)) return;
767
768
  let n = e.playDates.length === 0;
768
- e.playDates = h([...e.playDates, t], t), e.longest = Math.max(e.longest, _(e.playDates)), n || this._publish("streak_updated", {
769
- current: g(e.playDates, t),
770
- longest: Math.max(e.longest, _(e.playDates))
769
+ e.playDates = g([...e.playDates, t], t), e.longest = Math.max(e.longest, v(e.playDates)), n || this._publish("streak_updated", {
770
+ current: _(e.playDates, t),
771
+ longest: Math.max(e.longest, v(e.playDates))
771
772
  });
772
773
  }
773
774
  async _record(e, t) {
774
775
  let n = this._doc, r = this._deviceId, i = (e, t) => {
775
- e[r] = C((b(e, r) ? e[r] : 0) + t);
776
+ e[r] = w((x(e, r) ? e[r] : 0) + t);
776
777
  };
777
- this._markDay(), b(n.playsBy, e) || (n.playsBy[e] = {}, n.best[e] = 0, n.sumsBy[e] = {}), i(n.playsBy[e], 1), i(n.gamesBy, 1), t.score !== void 0 && t.score > n.best[e] && (n.best[e] = t.score, this._publish("personal_best", {
778
+ this._markDay(), x(n.playsBy, e) || (n.playsBy[e] = {}, n.best[e] = 0, n.sumsBy[e] = {}), i(n.playsBy[e], 1), i(n.gamesBy, 1), t.score !== void 0 && t.score > n.best[e] && (n.best[e] = t.score, this._publish("personal_best", {
778
779
  activity: e,
779
780
  score: t.score
780
781
  }));
781
- for (let r of Object.keys(t)) r !== "score" && r !== "plays" && r !== "best" && y(r) && (b(n.sumsBy[e], r) || (n.sumsBy[e][r] = {}), i(n.sumsBy[e][r], Number.isFinite(t[r]) && t[r] > 0 ? t[r] : 0));
782
+ for (let r of Object.keys(t)) r !== "score" && r !== "plays" && r !== "best" && b(r) && (x(n.sumsBy[e], r) || (n.sumsBy[e][r] = {}), i(n.sumsBy[e][r], Number.isFinite(t[r]) && t[r] > 0 ? t[r] : 0));
782
783
  let a = 10;
783
784
  return t.score && (a += Math.max(0, Math.floor(t.score / 5))), a = Math.floor(a * this.getStreakMultiplier()), await this.addXP(a, `Played ${e}`), await this.checkAchievements(e, t), await this.save(), a;
784
785
  }
785
786
  async checkAchievements(e, t) {
786
787
  let n = [];
787
788
  for (let [r, i] of Object.entries(this.achievements)) {
788
- if (b(this._doc.achievements, r)) continue;
789
+ if (x(this._doc.achievements, r)) continue;
789
790
  let a = !1;
790
791
  try {
791
792
  a = i.check(this.snapshot(), e, t);
@@ -798,10 +799,10 @@ var Z = "device", ye = () => {
798
799
  return n;
799
800
  }
800
801
  async unlockAchievement(e) {
801
- if (this._requirePlayer(), !y(e) || !b(this.achievements, e) || b(this._doc.achievements, e) || this._unlocking.has(e)) return !1;
802
+ if (this._requirePlayer(), !b(e) || !x(this.achievements, e) || x(this._doc.achievements, e) || this._unlocking.has(e)) return !1;
802
803
  let t = this.achievements[e];
803
804
  if (!t) return !1;
804
- let { [e]: n } = c(this.achievements);
805
+ let { [e]: n } = l(this.achievements);
805
806
  this._unlocking.add(e);
806
807
  let r;
807
808
  try {
@@ -812,8 +813,8 @@ var Z = "device", ye = () => {
812
813
  } finally {
813
814
  this._unlocking.delete(e);
814
815
  }
815
- if (r === !1 || b(this._doc.achievements, e)) return !1;
816
- let i = this._doc, a = H(i);
816
+ if (r === !1 || x(this._doc.achievements, e)) return !1;
817
+ let i = this._doc, a = W(i);
817
818
  return i.achievements[e] = {
818
819
  unlockedAt: this.clock.now(),
819
820
  seen: !1,
@@ -838,43 +839,16 @@ var Z = "device", ye = () => {
838
839
  }
839
840
  async markAchievementsSeen(e) {
840
841
  this._requirePlayer();
841
- for (let t of e) b(this._doc.achievements, t) && (this._doc.achievements[t].seen = !0);
842
+ for (let t of e) x(this._doc.achievements, t) && (this._doc.achievements[t].seen = !0);
842
843
  await this.save();
843
844
  }
844
845
  getStreakMultiplier() {
845
846
  let e = this.snapshot().streak.current;
846
847
  return e >= 7 ? 2 : e >= 5 ? 1.5 : e >= 3 ? 1.25 : 1;
847
848
  }
848
- };
849
- // src/dopamine/utils/storage.js
850
- function Q() {
851
- let e = /* @__PURE__ */ new Map();
852
- return {
853
- getItem: (t) => e.has(t) ? e.get(t) : null,
854
- setItem: (t, n) => {
855
- e.set(t, String(n));
856
- },
857
- removeItem: (t) => {
858
- e.delete(t);
859
- },
860
- clear: () => {
861
- e.clear();
862
- }
863
- };
864
- }
865
- function $() {
866
- try {
867
- if (typeof localStorage > "u") return Q();
868
- let e = "__dopamine_probe__";
869
- return localStorage.setItem(e, "1"), localStorage.removeItem(e), localStorage;
870
- } catch {
871
- return Q();
872
- }
873
- }
874
- // src/dopamine/core/DataService.js
875
- var Se = class {
876
- constructor(e = {}) {
877
- this.storage = e.storage || $(), this.prefix = e.prefix || "dopamine_", this._tail = null;
849
+ }, xe = class {
850
+ constructor(t = {}) {
851
+ this.storage = t.storage || e(), this.prefix = t.prefix || "dopamine_", this._tail = null;
878
852
  }
879
853
  _write(e) {
880
854
  let t = this._tail ? this._tail.then(e) : e();
@@ -917,4 +891,4 @@ var Se = class {
917
891
  await this._write(() => this.storage.removeItem(this.prefix + e));
918
892
  }
919
893
  };
920
- export { J as a, t as c, xe as i, e as l, Q as n, B as o, $ as r, r as s, Se as t };
894
+ export { i as a, H as i, be as n, n as o, X as r, t as s, xe as t };