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.
- package/README.md +52 -2
- package/dist/{DataService-QQBNqJtN.js → DataService-B5noGvMr.js} +228 -254
- package/dist/DataService-DKCk0nOu.cjs +1 -0
- package/dist/SoundManager-C6VdZEFo.js +308 -0
- package/dist/SoundManager-DBGDYIkz.cjs +1 -0
- package/dist/celebrate.cjs +1 -0
- package/dist/celebrate.js +103 -0
- package/dist/certificate.cjs +1 -0
- package/dist/certificate.js +234 -0
- package/dist/core.cjs +1 -1
- package/dist/core.js +3 -1
- package/dist/dopamine.cjs +1 -1
- package/dist/dopamine.global.js +4 -4
- package/dist/dopamine.js +5 -3
- package/dist/host-BWKiYtjY.cjs +37 -0
- package/dist/host-Bhf9EqSk.js +457 -0
- package/dist/icon-BakWSiz_.js +89 -0
- package/dist/icon-gkpxMw48.cjs +1 -0
- package/dist/storage-B6z-zaBe.cjs +1 -0
- package/dist/storage-C2s4Da9b.js +26 -0
- package/dist/testing.cjs +1 -1
- package/dist/testing.js +3 -2
- package/package.json +15 -3
- package/src/celebrate.js +213 -0
- package/src/certificate.js +391 -0
- package/src/dopamine/effects/ParticleSystem.js +5 -1
- package/src/dopamine/ui/RewardUI.js +13 -154
- package/src/dopamine/ui/icon.js +157 -0
- package/types/celebrate.d.ts +39 -0
- package/types/certificate.d.ts +30 -0
- package/types/index.d.ts +2 -0
- package/dist/DataService-Xk7egYXe.cjs +0 -1
- package/dist/host-1c7QSAKI.cjs +0 -37
- package/dist/host-_essSMef.js +0 -835
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { n as e } from "./storage-C2s4Da9b.js";
|
|
1
2
|
// src/dopamine/core/EventEmitter.js
|
|
2
|
-
var
|
|
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
|
-
},
|
|
25
|
+
}, n = class extends Error {
|
|
25
26
|
constructor(e, t) {
|
|
26
27
|
super(t || e), this.name = "DopamineError", this.code = e;
|
|
27
28
|
}
|
|
28
|
-
},
|
|
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 `${
|
|
33
|
+
return `${r(t.getFullYear(), 4)}-${r(t.getMonth() + 1)}-${r(t.getDate())}`;
|
|
33
34
|
}
|
|
34
|
-
},
|
|
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
|
-
}),
|
|
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
|
-
},
|
|
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
|
-
},
|
|
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() /
|
|
83
|
-
},
|
|
84
|
-
function
|
|
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 =
|
|
88
|
-
return n.filter((e) =>
|
|
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
|
|
91
|
-
let n = new Set(e), r = n.has(t) ? t :
|
|
92
|
-
for (; n.has(r);) i++, r =
|
|
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
|
|
96
|
+
function v(e) {
|
|
96
97
|
let t = 0, n = 0, r = NaN;
|
|
97
98
|
for (let i of [...e].sort()) {
|
|
98
|
-
let e =
|
|
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),
|
|
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
|
-
]),
|
|
108
|
-
function
|
|
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
|
|
112
|
-
let t =
|
|
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
|
-
},
|
|
115
|
-
let t =
|
|
115
|
+
}, E = (e) => {
|
|
116
|
+
let t = C(e);
|
|
116
117
|
return Number.isInteger(t) && t >= 0 ? t : void 0;
|
|
117
|
-
},
|
|
118
|
-
let r =
|
|
119
|
-
return Number.isInteger(r) && r >= n ?
|
|
120
|
-
},
|
|
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
|
|
137
|
+
function M(e, t) {
|
|
137
138
|
let n = {};
|
|
138
|
-
if (!
|
|
139
|
+
if (!S(e)) return n;
|
|
139
140
|
for (let r of Object.keys(e)) {
|
|
140
|
-
let i =
|
|
141
|
-
i !== void 0 && (n[r] =
|
|
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
|
|
146
|
+
function N(e, t) {
|
|
146
147
|
let n = {};
|
|
147
|
-
if (!
|
|
148
|
-
for (let r of Object.keys(e))
|
|
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
|
|
152
|
+
function P(e, t) {
|
|
152
153
|
let n = {};
|
|
153
|
-
if (!
|
|
154
|
+
if (!S(e)) return n;
|
|
154
155
|
for (let r of Object.keys(e)) {
|
|
155
156
|
let i = e[r];
|
|
156
|
-
|
|
157
|
-
unlockedAt:
|
|
157
|
+
b(r) && S(i) && (n[r] = {
|
|
158
|
+
unlockedAt: D(i.unlockedAt, t),
|
|
158
159
|
seen: i.seen === !0,
|
|
159
|
-
reward:
|
|
160
|
+
reward: w(O(i.reward, 0))
|
|
160
161
|
});
|
|
161
162
|
}
|
|
162
163
|
return n;
|
|
163
164
|
}
|
|
164
|
-
var
|
|
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
|
-
...
|
|
181
|
+
...F,
|
|
181
182
|
"xp",
|
|
182
183
|
"totalGamesPlayed",
|
|
183
184
|
"stats",
|
|
184
185
|
"streak"
|
|
185
186
|
]);
|
|
186
|
-
function
|
|
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) =>
|
|
191
|
-
if (!
|
|
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 =
|
|
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
|
|
200
|
+
function L(e, t) {
|
|
200
201
|
for (let n of Object.keys(e)) {
|
|
201
|
-
if (ie.has(n) || !
|
|
202
|
-
let r =
|
|
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
|
|
208
|
-
function
|
|
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)
|
|
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
|
|
218
|
-
return
|
|
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:
|
|
222
|
-
lastPlayedAt:
|
|
223
|
-
level:
|
|
224
|
-
longest:
|
|
225
|
-
playDates:
|
|
226
|
-
xpBy:
|
|
227
|
-
xpSpentBy:
|
|
228
|
-
gamesBy:
|
|
229
|
-
playsBy:
|
|
230
|
-
best:
|
|
231
|
-
sumsBy:
|
|
232
|
-
achievements:
|
|
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
|
|
236
|
+
function oe(e) {
|
|
236
237
|
let t = {};
|
|
237
|
-
if (!
|
|
238
|
-
for (let n of Object.keys(e))
|
|
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
|
|
242
|
-
let
|
|
243
|
-
|
|
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(
|
|
246
|
-
let t =
|
|
247
|
-
|
|
248
|
-
}
|
|
249
|
-
let s =
|
|
250
|
-
c > 0 && (
|
|
251
|
-
let l =
|
|
252
|
-
if (l > 0 && (
|
|
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 (!
|
|
255
|
-
let r =
|
|
256
|
-
|
|
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 (!
|
|
259
|
-
let r =
|
|
260
|
-
r !== void 0 && (
|
|
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 =
|
|
264
|
-
if (
|
|
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++)
|
|
267
|
-
|
|
268
|
-
} else
|
|
269
|
-
return
|
|
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
|
|
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
|
|
282
|
-
if (
|
|
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
|
|
285
|
-
if (!
|
|
286
|
-
if (e.v !== void 0 && e.v !== 3) throw new
|
|
287
|
-
let r = Number.isFinite(
|
|
288
|
-
return
|
|
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
|
|
291
|
-
function
|
|
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:
|
|
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] =
|
|
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:
|
|
305
|
-
longest: Math.max(e.longest,
|
|
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:
|
|
310
|
-
level:
|
|
311
|
-
totalGamesPlayed:
|
|
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
|
|
320
|
-
function
|
|
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))
|
|
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
|
|
329
|
+
var q = [
|
|
329
330
|
"beforeXP",
|
|
330
331
|
"afterXP",
|
|
331
332
|
"beforeRecord",
|
|
332
333
|
"afterRecord",
|
|
333
334
|
"beforeUnlock",
|
|
334
335
|
"afterUnlock"
|
|
335
|
-
],
|
|
336
|
+
], le = class {
|
|
336
337
|
constructor() {
|
|
337
338
|
this._list = [];
|
|
338
339
|
}
|
|
339
340
|
use(e) {
|
|
340
|
-
if (!
|
|
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 (!
|
|
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
|
|
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 =
|
|
377
|
-
r[i] = n > 0 ?
|
|
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
|
|
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 =
|
|
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
|
|
399
|
-
function
|
|
400
|
-
for (let r of /* @__PURE__ */ new Set([...
|
|
401
|
-
let i =
|
|
402
|
-
n[r] = i === void 0 ? a : a === void 0 ? i :
|
|
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
|
|
406
|
-
function
|
|
407
|
-
let r =
|
|
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:
|
|
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:
|
|
415
|
-
xpBy:
|
|
416
|
-
xpSpentBy:
|
|
417
|
-
gamesBy:
|
|
418
|
-
playsBy:
|
|
419
|
-
best:
|
|
420
|
-
sumsBy:
|
|
421
|
-
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
|
|
424
|
+
return fe(r, i, a), V(a), a;
|
|
424
425
|
}
|
|
425
|
-
function
|
|
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
|
|
440
|
+
function me(e) {
|
|
440
441
|
let t = /* @__PURE__ */ new Set();
|
|
441
|
-
for (let n of
|
|
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
|
|
445
|
+
function Q(e, t) {
|
|
445
446
|
let n = /* @__PURE__ */ new Map();
|
|
446
|
-
for (let [r, i] of
|
|
447
|
+
for (let [r, i] of Z(e)) x(i, t) && n.set(r, i[t]);
|
|
447
448
|
return n;
|
|
448
449
|
}
|
|
449
|
-
function
|
|
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))
|
|
453
|
-
n[t] =
|
|
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
|
|
457
|
-
let a =
|
|
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
|
|
460
|
-
let c =
|
|
461
|
-
for (let [e, t] of
|
|
462
|
-
if (!
|
|
463
|
-
let r = Math.min(i.get(e) ?? 0, t[n]), a = l.has(e) &&
|
|
464
|
-
c.get(e)[n] =
|
|
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
|
|
469
|
-
let r =
|
|
470
|
-
|
|
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
|
|
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
|
-
},
|
|
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 :
|
|
490
|
-
...t.builtinAchievements === !1 ? {} :
|
|
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
|
|
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
|
-
...
|
|
515
|
-
...
|
|
515
|
+
...c,
|
|
516
|
+
...l(this.achievements)
|
|
516
517
|
}
|
|
517
518
|
};
|
|
518
519
|
}
|
|
519
520
|
async init() {
|
|
520
|
-
let e = this._doc && !this._loadFailed &&
|
|
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 && !
|
|
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 && !
|
|
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(
|
|
554
|
-
|
|
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 ||
|
|
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 ?
|
|
563
|
-
r = t === null ?
|
|
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 (!
|
|
583
|
+
if (!S(e)) return j(this.clock.now());
|
|
583
584
|
try {
|
|
584
|
-
return
|
|
585
|
+
return H(e, this._options());
|
|
585
586
|
} catch (e) {
|
|
586
|
-
if (!(e instanceof
|
|
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
|
-
}),
|
|
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(
|
|
599
|
+
e = await this.dataService.load($, null, { strict: !0 });
|
|
599
600
|
} catch {
|
|
600
|
-
this._deviceId =
|
|
601
|
+
this._deviceId = ve();
|
|
601
602
|
return;
|
|
602
603
|
}
|
|
603
|
-
|
|
604
|
+
ye(e) ? (this._deviceId = e, this._deviceSaved = !0) : this._deviceId = ve();
|
|
604
605
|
}
|
|
605
606
|
_hasSlot() {
|
|
606
|
-
let e =
|
|
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(),
|
|
617
|
+
return this._requirePlayer(), K(ce(this._doc, this.clock.localDate()));
|
|
617
618
|
}
|
|
618
619
|
export() {
|
|
619
|
-
return this._requirePlayer(),
|
|
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 =
|
|
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 =
|
|
627
|
-
|
|
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 =
|
|
630
|
+
}), i = G(this._doc), he(i, this._deviceId, e);
|
|
630
631
|
}
|
|
631
|
-
let o =
|
|
632
|
-
o.level =
|
|
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)) !
|
|
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 =
|
|
657
|
-
this._doc =
|
|
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 (!
|
|
665
|
-
if (!
|
|
666
|
-
let n =
|
|
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(
|
|
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:
|
|
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:
|
|
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] =
|
|
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:
|
|
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 =
|
|
706
|
-
return e = Math.max(e, -
|
|
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,
|
|
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 && (
|
|
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:
|
|
720
|
+
total: i
|
|
720
721
|
}), c && this._publish("level_up", {
|
|
721
722
|
oldLevel: e,
|
|
722
723
|
newLevel: o,
|
|
723
|
-
totalXP:
|
|
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 =
|
|
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:
|
|
737
|
+
progress: a
|
|
737
738
|
};
|
|
738
739
|
}
|
|
739
740
|
_checkRecord(e, t) {
|
|
740
|
-
if (!
|
|
741
|
-
if (!
|
|
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 =
|
|
769
|
-
current:
|
|
770
|
-
longest: Math.max(e.longest,
|
|
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] =
|
|
776
|
+
e[r] = w((x(e, r) ? e[r] : 0) + t);
|
|
776
777
|
};
|
|
777
|
-
this._markDay(),
|
|
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" &&
|
|
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 (
|
|
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(), !
|
|
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 } =
|
|
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 ||
|
|
816
|
-
let i = this._doc, a =
|
|
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)
|
|
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
|
-
|
|
850
|
-
|
|
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 {
|
|
894
|
+
export { i as a, H as i, be as n, n as o, X as r, t as s, xe as t };
|