dopaminejs 3.4.0 → 3.5.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/dist/{DataService-CydwGT_D.js → DataService-QQBNqJtN.js} +291 -115
- package/dist/DataService-Xk7egYXe.cjs +1 -0
- package/dist/core.cjs +1 -1
- package/dist/core.js +1 -1
- package/dist/dopamine.cjs +1 -1
- package/dist/dopamine.global.js +4 -4
- package/dist/dopamine.js +2 -2
- package/dist/{host-MPzunSej.cjs → host-1c7QSAKI.cjs} +2 -2
- package/dist/{host-DOvdGHZL.js → host-_essSMef.js} +4 -3
- package/dist/testing.cjs +1 -1
- package/dist/testing.js +4 -3
- package/package.json +1 -1
- package/spec/README.md +113 -14
- package/spec/fixtures/merge.json +345 -0
- package/spec/fixtures/migrate.json +345 -22
- package/spec/fixtures/recovery.json +1861 -0
- package/spec/schema.json +2 -2
- package/src/dopamine/core/RewardSystem.js +150 -11
- package/src/dopamine/core/merge.js +116 -1
- package/src/dopamine/core/store.js +90 -32
- package/src/dopamine/host.js +3 -1
- package/src/testing.js +2 -1
- package/types/index.d.ts +25 -4
- package/dist/DataService-EJeOPnbo.cjs +0 -1
|
@@ -104,7 +104,20 @@ var ee = (e, t) => e.reduce((e, n) => n <= t && (!e || n > e) ? n : e, void 0),
|
|
|
104
104
|
"__proto__",
|
|
105
105
|
"constructor",
|
|
106
106
|
"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),
|
|
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) {
|
|
109
|
+
return typeof e == "string" && (e = ne.test(e) ? Number(e) : void 0), typeof e == "number" && Number.isFinite(e) ? e : void 0;
|
|
110
|
+
}
|
|
111
|
+
var C = (e) => Math.min(e, 2 ** 53 - 1), re = S, w = (e) => {
|
|
112
|
+
let t = S(e);
|
|
113
|
+
return t !== void 0 && t >= 0 ? t : void 0;
|
|
114
|
+
}, T = (e) => {
|
|
115
|
+
let t = S(e);
|
|
116
|
+
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) => ({
|
|
108
121
|
v: 3,
|
|
109
122
|
name: "Player",
|
|
110
123
|
createdAt: e,
|
|
@@ -120,32 +133,79 @@ var ee = (e, t) => e.reduce((e, n) => n <= t && (!e || n > e) ? n : e, void 0),
|
|
|
120
133
|
sumsBy: {},
|
|
121
134
|
achievements: {}
|
|
122
135
|
});
|
|
123
|
-
function
|
|
136
|
+
function j(e, t) {
|
|
124
137
|
let n = {};
|
|
125
138
|
if (!x(e)) return n;
|
|
126
|
-
for (let r of Object.keys(e))
|
|
139
|
+
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));
|
|
142
|
+
}
|
|
127
143
|
return n;
|
|
128
144
|
}
|
|
129
|
-
function
|
|
145
|
+
function M(e, t) {
|
|
130
146
|
let n = {};
|
|
131
147
|
if (!x(e)) return n;
|
|
132
|
-
for (let r of Object.keys(e)) y(r) && (n[r] =
|
|
148
|
+
for (let r of Object.keys(e)) y(r) && (n[r] = j(e[r], t));
|
|
133
149
|
return n;
|
|
134
150
|
}
|
|
135
|
-
function
|
|
151
|
+
function N(e, t) {
|
|
136
152
|
let n = {};
|
|
137
153
|
if (!x(e)) return n;
|
|
138
154
|
for (let r of Object.keys(e)) {
|
|
139
155
|
let i = e[r];
|
|
140
156
|
y(r) && x(i) && (n[r] = {
|
|
141
|
-
unlockedAt:
|
|
157
|
+
unlockedAt: E(i.unlockedAt, t),
|
|
142
158
|
seen: i.seen === !0,
|
|
143
|
-
reward: C(
|
|
159
|
+
reward: C(D(i.reward, 0))
|
|
144
160
|
});
|
|
145
161
|
}
|
|
146
162
|
return n;
|
|
147
163
|
}
|
|
148
|
-
|
|
164
|
+
var P = /* @__PURE__ */ new Set([
|
|
165
|
+
"v",
|
|
166
|
+
"name",
|
|
167
|
+
"createdAt",
|
|
168
|
+
"lastPlayedAt",
|
|
169
|
+
"level",
|
|
170
|
+
"longest",
|
|
171
|
+
"playDates",
|
|
172
|
+
"xpBy",
|
|
173
|
+
"xpSpentBy",
|
|
174
|
+
"gamesBy",
|
|
175
|
+
"playsBy",
|
|
176
|
+
"best",
|
|
177
|
+
"sumsBy",
|
|
178
|
+
"achievements"
|
|
179
|
+
]), ie = /* @__PURE__ */ new Set([
|
|
180
|
+
...P,
|
|
181
|
+
"xp",
|
|
182
|
+
"totalGamesPlayed",
|
|
183
|
+
"stats",
|
|
184
|
+
"streak"
|
|
185
|
+
]);
|
|
186
|
+
function F(e, t = 0) {
|
|
187
|
+
if (e === null || typeof e == "string" || typeof e == "boolean") return e;
|
|
188
|
+
if (typeof e == "number") return Number.isFinite(e) ? e : void 0;
|
|
189
|
+
if (t >= 32) return;
|
|
190
|
+
if (Array.isArray(e)) return e.map((e) => F(e, t + 1) ?? null);
|
|
191
|
+
if (!x(e)) return;
|
|
192
|
+
let n = {};
|
|
193
|
+
for (let r of Object.keys(e)) {
|
|
194
|
+
let i = y(r) ? F(e[r], t + 1) : void 0;
|
|
195
|
+
i !== void 0 && (n[r] = i);
|
|
196
|
+
}
|
|
197
|
+
return n;
|
|
198
|
+
}
|
|
199
|
+
function I(e, t) {
|
|
200
|
+
for (let n of Object.keys(e)) {
|
|
201
|
+
if (ie.has(n) || !y(n)) continue;
|
|
202
|
+
let r = F(e[n]);
|
|
203
|
+
r !== void 0 && (t[n] = r);
|
|
204
|
+
}
|
|
205
|
+
return t;
|
|
206
|
+
}
|
|
207
|
+
var L = (e) => Object.keys(e).filter((e) => !P.has(e));
|
|
208
|
+
function ae(e) {
|
|
149
209
|
let t = e.sumsBy, n = /* @__PURE__ */ new Set([
|
|
150
210
|
...Object.keys(e.playsBy),
|
|
151
211
|
...Object.keys(e.best),
|
|
@@ -154,56 +214,61 @@ function M(e) {
|
|
|
154
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] = {});
|
|
155
215
|
return e;
|
|
156
216
|
}
|
|
157
|
-
function
|
|
158
|
-
return
|
|
217
|
+
function oe(e, t, n) {
|
|
218
|
+
return I(e, ae({
|
|
159
219
|
v: 3,
|
|
160
220
|
name: typeof e.name == "string" ? e.name : "Player",
|
|
161
|
-
createdAt:
|
|
162
|
-
lastPlayedAt:
|
|
163
|
-
level:
|
|
164
|
-
longest:
|
|
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),
|
|
165
225
|
playDates: h(Array.isArray(e.playDates) ? e.playDates.filter(p) : [], n),
|
|
166
|
-
xpBy:
|
|
167
|
-
xpSpentBy:
|
|
168
|
-
gamesBy:
|
|
169
|
-
playsBy:
|
|
170
|
-
best:
|
|
171
|
-
sumsBy:
|
|
172
|
-
achievements:
|
|
173
|
-
});
|
|
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)
|
|
233
|
+
}));
|
|
174
234
|
}
|
|
175
|
-
function
|
|
235
|
+
function se(e) {
|
|
176
236
|
let t = {};
|
|
177
237
|
if (!x(e)) return t;
|
|
178
|
-
for (let n of Object.keys(e)) y(n) && (t[n] =
|
|
238
|
+
for (let n of Object.keys(e)) y(n) && (t[n] = M(e[n], w));
|
|
179
239
|
return t;
|
|
180
240
|
}
|
|
181
|
-
function
|
|
182
|
-
let a =
|
|
183
|
-
a.name = typeof e.name == "string" ? e.name : "Player", a.createdAt =
|
|
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);
|
|
184
244
|
let o = 0;
|
|
185
245
|
for (let e of Object.keys(a.achievements)) {
|
|
186
|
-
let t = b(n, e)
|
|
246
|
+
let t = b(n, e) ? C(D(n[e], 0)) : 0;
|
|
187
247
|
a.achievements[e].reward = t, o += t;
|
|
188
248
|
}
|
|
189
|
-
let s = C(
|
|
190
|
-
c > 0 && (a.xpBy[v] = c), a.level = Math.max(i(s),
|
|
191
|
-
let l =
|
|
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);
|
|
192
252
|
if (l > 0 && (a.gamesBy[v] = l), x(e.stats)) for (let t of Object.keys(e.stats)) {
|
|
193
253
|
let n = e.stats[t];
|
|
194
254
|
if (!y(t) || !x(n)) continue;
|
|
195
|
-
let r =
|
|
196
|
-
a.playsBy[t] = r > 0 ? { [v]: r } : {}, a.best[t] = C(
|
|
197
|
-
for (let e of Object.keys(n))
|
|
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] = {};
|
|
257
|
+
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) });
|
|
261
|
+
}
|
|
198
262
|
}
|
|
199
|
-
let u = x(e.streak) ? e.streak : {}, d =
|
|
200
|
-
if (a.longest = Math.max(d,
|
|
201
|
-
|
|
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)) {
|
|
265
|
+
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));
|
|
202
267
|
a.playDates = h(a.playDates, r);
|
|
203
|
-
} else a.longest =
|
|
204
|
-
return
|
|
268
|
+
} else a.longest = Math.max(O(u.current, 0), O(u.longest, 0));
|
|
269
|
+
return I(e, ae(a));
|
|
205
270
|
}
|
|
206
|
-
function
|
|
271
|
+
function R(e) {
|
|
207
272
|
let t = /* @__PURE__ */ new Set([
|
|
208
273
|
...Object.keys(e.xpBy),
|
|
209
274
|
...Object.keys(e.xpSpentBy),
|
|
@@ -213,24 +278,24 @@ function I(e) {
|
|
|
213
278
|
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);
|
|
214
279
|
return t;
|
|
215
280
|
}
|
|
216
|
-
function
|
|
217
|
-
if (
|
|
281
|
+
function z(e) {
|
|
282
|
+
if (R(e).size > 20) throw new t("too_many_devices", "[DopamineJS] a save holds at most 20 devices");
|
|
218
283
|
}
|
|
219
|
-
function
|
|
284
|
+
function B(e, n = {}) {
|
|
220
285
|
if (!x(e)) throw new t("invalid_document", "[DopamineJS] a save must be an object");
|
|
221
286
|
if (e.v !== void 0 && e.v !== 3) throw new t("unsupported_version", `[DopamineJS] save version ${String(e.v)} is not 3`);
|
|
222
|
-
let r =
|
|
223
|
-
return
|
|
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;
|
|
224
289
|
}
|
|
225
|
-
var
|
|
226
|
-
function
|
|
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) {
|
|
227
292
|
let n = {};
|
|
228
293
|
for (let t of Object.keys(e.playsBy)) {
|
|
229
294
|
let r = {
|
|
230
|
-
plays:
|
|
295
|
+
plays: k(e.playsBy[t]),
|
|
231
296
|
best: e.best[t]
|
|
232
297
|
};
|
|
233
|
-
for (let n of Object.keys(e.sumsBy[t])) n !== "plays" && n !== "best" && (r[n] =
|
|
298
|
+
for (let n of Object.keys(e.sumsBy[t])) n !== "plays" && n !== "best" && (r[n] = k(e.sumsBy[t][n]));
|
|
234
299
|
n[t] = r;
|
|
235
300
|
}
|
|
236
301
|
let r = {};
|
|
@@ -241,9 +306,9 @@ function V(e, t) {
|
|
|
241
306
|
}, a = ee(e.playDates, t);
|
|
242
307
|
return a && (i.lastPlayDate = a), {
|
|
243
308
|
name: e.name,
|
|
244
|
-
xp:
|
|
245
|
-
level:
|
|
246
|
-
totalGamesPlayed:
|
|
309
|
+
xp: V(e),
|
|
310
|
+
level: H(e),
|
|
311
|
+
totalGamesPlayed: k(e.gamesBy),
|
|
247
312
|
createdAt: e.createdAt,
|
|
248
313
|
lastPlayedAt: e.lastPlayedAt,
|
|
249
314
|
streak: i,
|
|
@@ -251,28 +316,28 @@ function V(e, t) {
|
|
|
251
316
|
stats: n
|
|
252
317
|
};
|
|
253
318
|
}
|
|
254
|
-
var
|
|
255
|
-
function
|
|
319
|
+
var U = (e) => JSON.parse(JSON.stringify(e));
|
|
320
|
+
function W(e) {
|
|
256
321
|
if (typeof e == "object" && e && !Object.isFrozen(e)) {
|
|
257
322
|
Object.freeze(e);
|
|
258
|
-
for (let t of Object.keys(e))
|
|
323
|
+
for (let t of Object.keys(e)) W(e[t]);
|
|
259
324
|
}
|
|
260
325
|
return e;
|
|
261
326
|
}
|
|
262
327
|
// src/dopamine/core/middleware.js
|
|
263
|
-
var
|
|
328
|
+
var G = [
|
|
264
329
|
"beforeXP",
|
|
265
330
|
"afterXP",
|
|
266
331
|
"beforeRecord",
|
|
267
332
|
"afterRecord",
|
|
268
333
|
"beforeUnlock",
|
|
269
334
|
"afterUnlock"
|
|
270
|
-
],
|
|
335
|
+
], ue = class {
|
|
271
336
|
constructor() {
|
|
272
337
|
this._list = [];
|
|
273
338
|
}
|
|
274
339
|
use(e) {
|
|
275
|
-
if (!x(e) || !
|
|
340
|
+
if (!x(e) || !G.some((t) => typeof e[t] == "function")) throw TypeError(`[DopamineJS] use expects an object with one of: ${G.join(", ")}`);
|
|
276
341
|
return this._list.push(e), () => {
|
|
277
342
|
let t = this._list.indexOf(e);
|
|
278
343
|
t >= 0 && this._list.splice(t, 1);
|
|
@@ -313,7 +378,7 @@ function K(e, t, n) {
|
|
|
313
378
|
}
|
|
314
379
|
return r;
|
|
315
380
|
}
|
|
316
|
-
function
|
|
381
|
+
function de(e, t) {
|
|
317
382
|
let n = {};
|
|
318
383
|
for (let r of /* @__PURE__ */ new Set([...Object.keys(e), ...Object.keys(t)])) {
|
|
319
384
|
let i = b(e, r) ? e[r] : void 0, a = b(t, r) ? t[r] : void 0;
|
|
@@ -330,12 +395,19 @@ function q(e, t) {
|
|
|
330
395
|
}
|
|
331
396
|
return n;
|
|
332
397
|
}
|
|
333
|
-
var
|
|
334
|
-
function
|
|
335
|
-
let r
|
|
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);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
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 = {
|
|
336
408
|
v: 3,
|
|
337
409
|
name: (r.lastPlayedAt === i.lastPlayedAt ? r.name >= i.name ? r : i : r.lastPlayedAt > i.lastPlayedAt ? r : i).name,
|
|
338
|
-
createdAt:
|
|
410
|
+
createdAt: me(r.createdAt, i.createdAt),
|
|
339
411
|
lastPlayedAt: Math.max(r.lastPlayedAt, i.lastPlayedAt),
|
|
340
412
|
level: Math.max(r.level, i.level),
|
|
341
413
|
longest: Math.max(r.longest, i.longest),
|
|
@@ -346,22 +418,78 @@ function Y(e, t, n) {
|
|
|
346
418
|
playsBy: K(r.playsBy, i.playsBy, 1),
|
|
347
419
|
best: K(r.best, i.best, 0),
|
|
348
420
|
sumsBy: K(r.sumsBy, i.sumsBy, 2),
|
|
349
|
-
achievements:
|
|
421
|
+
achievements: de(r.achievements, i.achievements)
|
|
350
422
|
};
|
|
351
|
-
return
|
|
423
|
+
return pe(r, i, a), z(a), a;
|
|
424
|
+
}
|
|
425
|
+
function Y(e) {
|
|
426
|
+
let t = /* @__PURE__ */ new Map([
|
|
427
|
+
[JSON.stringify(["xpBy"]), e.xpBy],
|
|
428
|
+
[JSON.stringify(["xpSpentBy"]), e.xpSpentBy],
|
|
429
|
+
[JSON.stringify(["gamesBy"]), e.gamesBy]
|
|
430
|
+
]);
|
|
431
|
+
for (let n of Object.keys(e.playsBy)) t.set(JSON.stringify(["playsBy", n]), e.playsBy[n]);
|
|
432
|
+
for (let n of Object.keys(e.sumsBy)) for (let r of Object.keys(e.sumsBy[n])) t.set(JSON.stringify([
|
|
433
|
+
"sumsBy",
|
|
434
|
+
n,
|
|
435
|
+
r
|
|
436
|
+
]), e.sumsBy[n][r]);
|
|
437
|
+
return t;
|
|
438
|
+
}
|
|
439
|
+
function he(e) {
|
|
440
|
+
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
|
+
return t;
|
|
443
|
+
}
|
|
444
|
+
function X(e, t) {
|
|
445
|
+
let n = /* @__PURE__ */ new Map();
|
|
446
|
+
for (let [r, i] of Y(e)) b(i, t) && n.set(r, i[t]);
|
|
447
|
+
return n;
|
|
448
|
+
}
|
|
449
|
+
function ge(e, t, n) {
|
|
450
|
+
for (let [r, i] of n) {
|
|
451
|
+
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);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
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);
|
|
458
|
+
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));
|
|
465
|
+
}
|
|
466
|
+
return s;
|
|
467
|
+
}
|
|
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]);
|
|
471
|
+
};
|
|
472
|
+
for (let e of [
|
|
473
|
+
r.xpBy,
|
|
474
|
+
r.xpSpentBy,
|
|
475
|
+
r.gamesBy
|
|
476
|
+
]) i(e);
|
|
477
|
+
for (let e of Object.keys(r.playsBy)) i(r.playsBy[e]);
|
|
478
|
+
for (let e of Object.keys(r.sumsBy)) for (let t of Object.keys(r.sumsBy[e])) i(r.sumsBy[e][t]);
|
|
479
|
+
return r;
|
|
352
480
|
}
|
|
353
481
|
// src/dopamine/core/RewardSystem.js
|
|
354
|
-
var
|
|
482
|
+
var Z = "device", ye = () => {
|
|
355
483
|
let e = /* @__PURE__ */ new Uint8Array(8), t = globalThis.crypto;
|
|
356
484
|
if (typeof t?.getRandomValues == "function") t.getRandomValues(e);
|
|
357
485
|
else for (let t = 0; t < e.length; t++) e[t] = Math.floor(Math.random() * 256);
|
|
358
486
|
return Array.from(e, (e) => e.toString(16).padStart(2, "0")).join("");
|
|
359
|
-
},
|
|
487
|
+
}, be = (e) => y(e) && e !== "legacy", xe = class extends e {
|
|
360
488
|
constructor(e, t = {}) {
|
|
361
489
|
super(), this.dataService = e, this.clock = t.clock && typeof t.clock.now == "function" && typeof t.clock.localDate == "function" ? t.clock : r, this.achievements = {
|
|
362
490
|
...t.builtinAchievements === !1 ? {} : o(),
|
|
363
491
|
...t.achievements || {}
|
|
364
|
-
}, this.streakDays = t.streakDays === "record" ? "record" : "any", this._doc = null, this._deviceId = null, this._deviceSaved = !1, this._middleware = new
|
|
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;
|
|
365
493
|
}
|
|
366
494
|
get loadFailed() {
|
|
367
495
|
return this._readFailed;
|
|
@@ -389,11 +517,11 @@ var X = "device", Z = () => {
|
|
|
389
517
|
};
|
|
390
518
|
}
|
|
391
519
|
async init() {
|
|
392
|
-
let e = await this._readSaved();
|
|
393
|
-
return this._loadFailed && this._doc
|
|
520
|
+
let e = this._doc && !this._loadFailed && X(this._doc, this._deviceId), t = await this._readSaved();
|
|
521
|
+
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();
|
|
394
522
|
}
|
|
395
523
|
async _readSaved() {
|
|
396
|
-
this._loadFailed = !0;
|
|
524
|
+
this._loadFailed = !0, this._reading = !0, this._epoch++, this._recovering = null, this._lastRead = this.clock.now();
|
|
397
525
|
try {
|
|
398
526
|
let e = await this.dataService.load("player", null, { strict: !0 });
|
|
399
527
|
return e !== null && !x(e) ? await this._keepCorrupt(e) : (this._loadFailed = !1, this._readFailed = !1, e);
|
|
@@ -403,7 +531,43 @@ var X = "device", Z = () => {
|
|
|
403
531
|
reason: "read",
|
|
404
532
|
error: e
|
|
405
533
|
}), null);
|
|
534
|
+
} finally {
|
|
535
|
+
this._reading = !1;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
_recover() {
|
|
539
|
+
if (this._recovering || !this._readFailed || this._reading) return Promise.resolve(!1);
|
|
540
|
+
let e = this.clock.now() - this._lastRead;
|
|
541
|
+
if (e >= 0 && e < this.loadRetryMs) return Promise.resolve(!1);
|
|
542
|
+
this._lastRead = this.clock.now();
|
|
543
|
+
let t = this._retryRead(this._epoch).finally(() => {
|
|
544
|
+
this._recovering === t && (this._recovering = null);
|
|
545
|
+
});
|
|
546
|
+
return this._recovering = t, t;
|
|
547
|
+
}
|
|
548
|
+
async _retryRead(e) {
|
|
549
|
+
let t, n = null;
|
|
550
|
+
try {
|
|
551
|
+
if (t = await this.dataService.load("player", null, { strict: !0 }), t !== null && !x(t)) return !1;
|
|
552
|
+
if (!this._deviceSaved) {
|
|
553
|
+
let e = await this.dataService.load(Z, null, { strict: !0 });
|
|
554
|
+
be(e) && e !== this._deviceId && (n = e);
|
|
555
|
+
}
|
|
556
|
+
} catch {
|
|
557
|
+
return !1;
|
|
406
558
|
}
|
|
559
|
+
if (e !== this._epoch || !this._loadFailed || he(this._doc).has(n)) return !1;
|
|
560
|
+
let r;
|
|
561
|
+
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);
|
|
564
|
+
} catch {
|
|
565
|
+
return !1;
|
|
566
|
+
}
|
|
567
|
+
return n && (this._deviceId = n, this._deviceSaved = !0), this._doc = r, this._recoveries++, this._savedLevel = Number(t?.level) || 0, this._loadFailed = !1, this._readFailed = !1, this._forgetImports(), await this._write(), this._publish("load_recovered", { key: "player" }), !0;
|
|
568
|
+
}
|
|
569
|
+
_forgetImports() {
|
|
570
|
+
this._base = /* @__PURE__ */ new Map();
|
|
407
571
|
}
|
|
408
572
|
async _keepCorrupt(e) {
|
|
409
573
|
this._loadFailed = !0;
|
|
@@ -415,31 +579,31 @@ var X = "device", Z = () => {
|
|
|
415
579
|
}), null;
|
|
416
580
|
}
|
|
417
581
|
_load(e) {
|
|
418
|
-
if (!x(e)) return
|
|
582
|
+
if (!x(e)) return A(this.clock.now());
|
|
419
583
|
try {
|
|
420
|
-
return
|
|
584
|
+
return B(e, this._options());
|
|
421
585
|
} catch (e) {
|
|
422
586
|
if (!(e instanceof t)) throw e;
|
|
423
587
|
return this._loadFailed = !0, this._readFailed = !0, this._publish("load_failed", {
|
|
424
588
|
key: "player",
|
|
425
589
|
reason: e.code,
|
|
426
590
|
error: e
|
|
427
|
-
}),
|
|
591
|
+
}), A(this.clock.now());
|
|
428
592
|
}
|
|
429
593
|
}
|
|
430
594
|
async _readDevice() {
|
|
431
595
|
if (this._deviceId) return;
|
|
432
596
|
let e = null;
|
|
433
597
|
try {
|
|
434
|
-
e = await this.dataService.load(
|
|
598
|
+
e = await this.dataService.load(Z, null, { strict: !0 });
|
|
435
599
|
} catch {
|
|
436
|
-
this._deviceId =
|
|
600
|
+
this._deviceId = ye();
|
|
437
601
|
return;
|
|
438
602
|
}
|
|
439
|
-
|
|
603
|
+
be(e) ? (this._deviceId = e, this._deviceSaved = !0) : this._deviceId = ye();
|
|
440
604
|
}
|
|
441
605
|
_hasSlot() {
|
|
442
|
-
let e =
|
|
606
|
+
let e = R(this._doc);
|
|
443
607
|
return e.has(this._deviceId) || e.size < 20 ? !0 : (this._publish("load_failed", {
|
|
444
608
|
key: "player",
|
|
445
609
|
reason: "too_many_devices"
|
|
@@ -449,40 +613,48 @@ var X = "device", Z = () => {
|
|
|
449
613
|
if (!this._doc) throw Error("[DopamineJS] RewardSystem.init() must be awaited before use");
|
|
450
614
|
}
|
|
451
615
|
snapshot() {
|
|
452
|
-
return this._requirePlayer(),
|
|
616
|
+
return this._requirePlayer(), W(le(this._doc, this.clock.localDate()));
|
|
453
617
|
}
|
|
454
618
|
export() {
|
|
455
|
-
return this._requirePlayer(),
|
|
619
|
+
return this._requirePlayer(), U(this._doc);
|
|
456
620
|
}
|
|
457
621
|
async import(e, { silent: t = !1 } = {}) {
|
|
458
622
|
this._requirePlayer();
|
|
459
|
-
let n = this.snapshot(), r =
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
623
|
+
let n = this.snapshot(), r = B(e, this._options()), i = this._doc, a = null;
|
|
624
|
+
if (this._readFailed) {
|
|
625
|
+
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) => {
|
|
628
|
+
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
|
+
}
|
|
631
|
+
let o = J(i, r, this._options());
|
|
632
|
+
o.level = H(o), this._doc = o, a && (this._base = a);
|
|
633
|
+
let s = this.snapshot();
|
|
634
|
+
if (await this.save(), t) return s;
|
|
635
|
+
let c = s.xp - n.xp, l = s.level > n.level;
|
|
636
|
+
c > 0 && this._publish("xp_gained", {
|
|
637
|
+
amount: c,
|
|
466
638
|
reason: "import",
|
|
467
|
-
leveledUp:
|
|
468
|
-
newLevel:
|
|
469
|
-
total:
|
|
470
|
-
}),
|
|
639
|
+
leveledUp: l,
|
|
640
|
+
newLevel: s.level,
|
|
641
|
+
total: s.xp
|
|
642
|
+
}), l && this._publish("level_up", {
|
|
471
643
|
oldLevel: n.level,
|
|
472
|
-
newLevel:
|
|
473
|
-
totalXP:
|
|
644
|
+
newLevel: s.level,
|
|
645
|
+
totalXP: s.xp
|
|
474
646
|
});
|
|
475
|
-
for (let e of Object.keys(
|
|
647
|
+
for (let e of Object.keys(s.achievements)) !b(n.achievements, e) && b(this.achievements, e) && this._publish("achievement_unlocked", {
|
|
476
648
|
...this.achievements[e],
|
|
477
649
|
id: e
|
|
478
650
|
});
|
|
479
|
-
return
|
|
651
|
+
return s;
|
|
480
652
|
}
|
|
481
653
|
async reset(e) {
|
|
482
654
|
let t = e?.overwrite;
|
|
483
655
|
if (e !== void 0 && (typeof e != "object" || !e || Array.isArray(e)) || t !== void 0 && typeof t != "boolean") throw TypeError("[DopamineJS] reset expects { overwrite?: boolean }");
|
|
484
|
-
return this._requirePlayer(), t === !1 && this._readFailed ? (this._doc =
|
|
485
|
-
this._doc =
|
|
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();
|
|
486
658
|
}), this._publish("player_reset", {}), this.snapshot());
|
|
487
659
|
}
|
|
488
660
|
use(e) {
|
|
@@ -500,13 +672,13 @@ var X = "device", Z = () => {
|
|
|
500
672
|
this._doc && (this._batchDepth > 0 ? this._pendingSave = !0 : await this._write());
|
|
501
673
|
}
|
|
502
674
|
async _write() {
|
|
503
|
-
this._loadFailed
|
|
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" }));
|
|
504
676
|
}
|
|
505
677
|
async addXP(e, t = "") {
|
|
506
678
|
if (!Number.isFinite(e)) throw TypeError(`[DopamineJS] addXP expects a finite number, received ${e}`);
|
|
507
679
|
if (this._requirePlayer(), !this._hasSlot()) return {
|
|
508
680
|
leveledUp: !1,
|
|
509
|
-
newLevel:
|
|
681
|
+
newLevel: H(this._doc),
|
|
510
682
|
xpGained: 0
|
|
511
683
|
};
|
|
512
684
|
let n = await this._middleware.before("beforeXP", {
|
|
@@ -515,45 +687,49 @@ var X = "device", Z = () => {
|
|
|
515
687
|
}, (e) => Number.isFinite(e.amount));
|
|
516
688
|
if (n === !1) return {
|
|
517
689
|
leveledUp: !1,
|
|
518
|
-
newLevel:
|
|
690
|
+
newLevel: H(this._doc),
|
|
519
691
|
xpGained: 0
|
|
520
692
|
};
|
|
521
693
|
this.streakDays === "any" && n.amount > 0 && this._markDay();
|
|
522
694
|
let r = await this._applyXP(n.amount, n.reason, (e, t) => {
|
|
523
|
-
t >= 0 ? e.xpBy[this._deviceId] =
|
|
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);
|
|
524
696
|
});
|
|
525
697
|
return await this._middleware.after("afterXP", {
|
|
526
698
|
amount: r.xpGained,
|
|
527
699
|
reason: n.reason,
|
|
528
|
-
total:
|
|
700
|
+
total: V(this._doc),
|
|
529
701
|
level: r.newLevel
|
|
530
702
|
}), r;
|
|
531
703
|
}
|
|
532
704
|
async _applyXP(e, t, n) {
|
|
533
|
-
let r = this._doc, i =
|
|
534
|
-
return e = Math.max(e, -
|
|
705
|
+
let r = this._doc, i = H(r);
|
|
706
|
+
return e = Math.max(e, -V(r)), n(r, e), this._announceXP(i, e, t);
|
|
535
707
|
}
|
|
536
708
|
async _announceXP(e, t, n) {
|
|
537
|
-
let r = this._doc, a =
|
|
538
|
-
|
|
709
|
+
let r = this._doc, a = V(r), o = Math.max(e, i(a));
|
|
710
|
+
o > r.level && (r.level = o);
|
|
711
|
+
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
|
+
let c = o > e;
|
|
714
|
+
return this._publish("xp_gained", {
|
|
539
715
|
amount: t,
|
|
540
716
|
reason: n,
|
|
541
|
-
leveledUp:
|
|
717
|
+
leveledUp: c,
|
|
542
718
|
newLevel: o,
|
|
543
719
|
total: a
|
|
544
|
-
}),
|
|
720
|
+
}), c && this._publish("level_up", {
|
|
545
721
|
oldLevel: e,
|
|
546
722
|
newLevel: o,
|
|
547
723
|
totalXP: a
|
|
548
724
|
}), {
|
|
549
|
-
leveledUp:
|
|
725
|
+
leveledUp: c,
|
|
550
726
|
newLevel: o,
|
|
551
727
|
xpGained: t
|
|
552
728
|
};
|
|
553
729
|
}
|
|
554
730
|
getXPForNextLevel() {
|
|
555
731
|
this._requirePlayer();
|
|
556
|
-
let e =
|
|
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;
|
|
557
733
|
return {
|
|
558
734
|
total: n,
|
|
559
735
|
needed: Math.max(0, n - t),
|
|
@@ -596,7 +772,7 @@ var X = "device", Z = () => {
|
|
|
596
772
|
}
|
|
597
773
|
async _record(e, t) {
|
|
598
774
|
let n = this._doc, r = this._deviceId, i = (e, t) => {
|
|
599
|
-
e[r] =
|
|
775
|
+
e[r] = C((b(e, r) ? e[r] : 0) + t);
|
|
600
776
|
};
|
|
601
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", {
|
|
602
778
|
activity: e,
|
|
@@ -637,7 +813,7 @@ var X = "device", Z = () => {
|
|
|
637
813
|
this._unlocking.delete(e);
|
|
638
814
|
}
|
|
639
815
|
if (r === !1 || b(this._doc.achievements, e)) return !1;
|
|
640
|
-
let i = this._doc, a =
|
|
816
|
+
let i = this._doc, a = H(i);
|
|
641
817
|
return i.achievements[e] = {
|
|
642
818
|
unlockedAt: this.clock.now(),
|
|
643
819
|
seen: !1,
|
|
@@ -696,7 +872,7 @@ function $() {
|
|
|
696
872
|
}
|
|
697
873
|
}
|
|
698
874
|
// src/dopamine/core/DataService.js
|
|
699
|
-
var
|
|
875
|
+
var Se = class {
|
|
700
876
|
constructor(e = {}) {
|
|
701
877
|
this.storage = e.storage || $(), this.prefix = e.prefix || "dopamine_", this._tail = null;
|
|
702
878
|
}
|
|
@@ -741,4 +917,4 @@ var ie = class {
|
|
|
741
917
|
await this._write(() => this.storage.removeItem(this.prefix + e));
|
|
742
918
|
}
|
|
743
919
|
};
|
|
744
|
-
export {
|
|
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 };
|