dopaminejs 3.2.0 → 3.4.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 +30 -0
- package/dist/{DataService-Bd1S-C1N.js → DataService-CydwGT_D.js} +151 -154
- package/dist/DataService-EJeOPnbo.cjs +1 -0
- package/dist/core.cjs +1 -1
- package/dist/core.js +1 -2
- package/dist/dopamine.cjs +1 -1
- package/dist/dopamine.global.js +37 -0
- package/dist/dopamine.js +3 -3
- package/dist/games.js +1 -2
- package/dist/{host-CkBQnCdp.js → host-DOvdGHZL.js} +39 -20
- package/dist/host-MPzunSej.cjs +37 -0
- package/dist/style.css +13 -12
- package/dist/testing.cjs +1 -1
- package/dist/testing.js +11 -12
- package/package.json +4 -2
- package/spec/README.md +3 -2
- package/spec/fixtures/merge.json +39 -0
- package/spec/fixtures/migrate.json +43 -0
- package/spec/schema.json +1 -1
- package/src/dopamine/core/RewardSystem.js +37 -13
- package/src/dopamine/core/store.js +2 -2
- package/src/dopamine/ui/RewardUI.js +43 -2
- package/src/dopamine/ui/dopamine.css +13 -12
- package/dist/DataService-GRTvq4BS.cjs +0 -1
- package/dist/host-rQMUJTOU.cjs +0 -37
package/README.md
CHANGED
|
@@ -94,6 +94,36 @@ import { myPlugin } from './my-plugin.js';
|
|
|
94
94
|
const dopamine = createDopamine({ plugins: [myPlugin] });
|
|
95
95
|
```
|
|
96
96
|
|
|
97
|
+
## Style the achievement card
|
|
98
|
+
|
|
99
|
+
The achievement card reads CSS variables. Each has a fallback, so the default look stays as it is.
|
|
100
|
+
|
|
101
|
+
| Variable | Sets | Default |
|
|
102
|
+
|---|---|---|
|
|
103
|
+
| `--dopamine-toast-bg` | background | purple gradient |
|
|
104
|
+
| `--dopamine-toast-text` | name colour | `white` |
|
|
105
|
+
| `--dopamine-toast-muted` | title and description colour | translucent white |
|
|
106
|
+
| `--dopamine-toast-accent` | XP chip text | `#ffd700` |
|
|
107
|
+
| `--dopamine-toast-radius` | corner radius | `16px` |
|
|
108
|
+
| `--dopamine-toast-shadow` | box shadow | `0 8px 32px rgba(0, 0, 0, 0.4)` |
|
|
109
|
+
| `--dopamine-toast-top` | distance from the top, plus the safe area inset | `80px` |
|
|
110
|
+
| `--dopamine-toast-right` | distance from the right | `20px` (`12px` on small screens) |
|
|
111
|
+
| `--dopamine-toast-left` | distance from the left | `auto` |
|
|
112
|
+
| `--dopamine-toast-max-width` | max width, never wider than the screen less 24px on small screens | `320px` |
|
|
113
|
+
|
|
114
|
+
```css
|
|
115
|
+
:root {
|
|
116
|
+
--dopamine-toast-bg: #1b1f3a;
|
|
117
|
+
--dopamine-toast-accent: #7cf5c8;
|
|
118
|
+
--dopamine-toast-top: 16px;
|
|
119
|
+
--dopamine-toast-radius: 8px;
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
To put the card on the left, set `--dopamine-toast-right: auto` as well. With both sides set, the card stretches between them up to its max width.
|
|
124
|
+
|
|
125
|
+
Cards show one at a time. A second unlock waits until the first has left, so its announcement and confetti come later too.
|
|
126
|
+
|
|
97
127
|
## Cleanup
|
|
98
128
|
|
|
99
129
|
Everything that touches the DOM can be torn down:
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/dopamine/core/EventEmitter.js
|
|
2
2
|
var e = class {
|
|
3
3
|
constructor() {
|
|
4
4
|
this.listeners = /* @__PURE__ */ new Map();
|
|
@@ -100,11 +100,11 @@ function _(e) {
|
|
|
100
100
|
}
|
|
101
101
|
return t;
|
|
102
102
|
}
|
|
103
|
-
var
|
|
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
104
|
"__proto__",
|
|
105
105
|
"constructor",
|
|
106
106
|
"prototype"
|
|
107
|
-
]),
|
|
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), S = (e) => Number.isInteger(e) && e >= 0, C = (e) => Number.isFinite(e) && e >= 0, w = (e) => Number.isFinite(e), T = (e) => Math.min(e, 2 ** 53 - 1), E = (e, t, n = 0) => Number.isInteger(e) && e >= n ? T(e) : t, D = (e, t = (e) => e) => Object.keys(e).sort().reduce((n, r) => T(n + t(e[r])), 0), O = (e = 0) => ({
|
|
108
108
|
v: 3,
|
|
109
109
|
name: "Player",
|
|
110
110
|
createdAt: e,
|
|
@@ -120,90 +120,90 @@ var v = (e, t) => e.reduce((e, n) => n <= t && (!e || n > e) ? n : e, void 0), y
|
|
|
120
120
|
sumsBy: {},
|
|
121
121
|
achievements: {}
|
|
122
122
|
});
|
|
123
|
-
function
|
|
123
|
+
function k(e, t) {
|
|
124
124
|
let n = {};
|
|
125
|
-
if (!
|
|
126
|
-
for (let r of Object.keys(e))
|
|
125
|
+
if (!x(e)) return n;
|
|
126
|
+
for (let r of Object.keys(e)) y(r) && t(e[r]) && (n[r] = T(e[r]));
|
|
127
127
|
return n;
|
|
128
128
|
}
|
|
129
|
-
function
|
|
129
|
+
function A(e, t) {
|
|
130
130
|
let n = {};
|
|
131
|
-
if (!
|
|
132
|
-
for (let r of Object.keys(e))
|
|
131
|
+
if (!x(e)) return n;
|
|
132
|
+
for (let r of Object.keys(e)) y(r) && (n[r] = k(e[r], t));
|
|
133
133
|
return n;
|
|
134
134
|
}
|
|
135
|
-
function
|
|
135
|
+
function j(e, t) {
|
|
136
136
|
let n = {};
|
|
137
|
-
if (!
|
|
137
|
+
if (!x(e)) return n;
|
|
138
138
|
for (let r of Object.keys(e)) {
|
|
139
139
|
let i = e[r];
|
|
140
|
-
|
|
141
|
-
unlockedAt:
|
|
140
|
+
y(r) && x(i) && (n[r] = {
|
|
141
|
+
unlockedAt: w(i.unlockedAt) ? i.unlockedAt : t,
|
|
142
142
|
seen: i.seen === !0,
|
|
143
|
-
reward:
|
|
143
|
+
reward: C(i.reward) ? T(i.reward) : 0
|
|
144
144
|
});
|
|
145
145
|
}
|
|
146
146
|
return n;
|
|
147
147
|
}
|
|
148
|
-
function
|
|
148
|
+
function M(e) {
|
|
149
149
|
let t = e.sumsBy, n = /* @__PURE__ */ new Set([
|
|
150
150
|
...Object.keys(e.playsBy),
|
|
151
151
|
...Object.keys(e.best),
|
|
152
152
|
...Object.keys(t)
|
|
153
153
|
]);
|
|
154
|
-
for (let r of n)
|
|
154
|
+
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
155
|
return e;
|
|
156
156
|
}
|
|
157
|
-
function
|
|
158
|
-
return
|
|
157
|
+
function N(e, t, n) {
|
|
158
|
+
return M({
|
|
159
159
|
v: 3,
|
|
160
160
|
name: typeof e.name == "string" ? e.name : "Player",
|
|
161
|
-
createdAt:
|
|
162
|
-
lastPlayedAt:
|
|
163
|
-
level:
|
|
164
|
-
longest:
|
|
161
|
+
createdAt: w(e.createdAt) ? e.createdAt : t,
|
|
162
|
+
lastPlayedAt: w(e.lastPlayedAt) ? e.lastPlayedAt : t,
|
|
163
|
+
level: E(e.level, 1, 1),
|
|
164
|
+
longest: E(e.longest, 0),
|
|
165
165
|
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:
|
|
166
|
+
xpBy: k(e.xpBy, C),
|
|
167
|
+
xpSpentBy: k(e.xpSpentBy, C),
|
|
168
|
+
gamesBy: k(e.gamesBy, S),
|
|
169
|
+
playsBy: A(e.playsBy, S),
|
|
170
|
+
best: k(e.best, C),
|
|
171
|
+
sumsBy: P(e.sumsBy),
|
|
172
|
+
achievements: j(e.achievements, t)
|
|
173
173
|
});
|
|
174
174
|
}
|
|
175
|
-
function
|
|
175
|
+
function P(e) {
|
|
176
176
|
let t = {};
|
|
177
|
-
if (!
|
|
178
|
-
for (let n of Object.keys(e))
|
|
177
|
+
if (!x(e)) return t;
|
|
178
|
+
for (let n of Object.keys(e)) y(n) && (t[n] = A(e[n], C));
|
|
179
179
|
return t;
|
|
180
180
|
}
|
|
181
|
-
function
|
|
182
|
-
let a =
|
|
183
|
-
a.name = typeof e.name == "string" ? e.name : "Player", a.createdAt =
|
|
181
|
+
function F(e, t, n, r) {
|
|
182
|
+
let a = O(t);
|
|
183
|
+
a.name = typeof e.name == "string" ? e.name : "Player", a.createdAt = w(e.createdAt) ? e.createdAt : t, a.lastPlayedAt = w(e.lastPlayedAt) ? e.lastPlayedAt : t, a.achievements = j(e.achievements, t);
|
|
184
184
|
let o = 0;
|
|
185
185
|
for (let e of Object.keys(a.achievements)) {
|
|
186
|
-
let t =
|
|
186
|
+
let t = b(n, e) && C(n[e]) ? T(n[e]) : 0;
|
|
187
187
|
a.achievements[e].reward = t, o += t;
|
|
188
188
|
}
|
|
189
|
-
let s =
|
|
190
|
-
c > 0 && (a.xpBy[
|
|
191
|
-
let l =
|
|
192
|
-
if (l > 0 && (a.gamesBy[
|
|
189
|
+
let s = C(e.xp) ? T(e.xp) : 0, c = Math.max(0, s - o);
|
|
190
|
+
c > 0 && (a.xpBy[v] = c), a.level = Math.max(i(s), E(e.level, 1, 1));
|
|
191
|
+
let l = E(e.totalGamesPlayed, 0);
|
|
192
|
+
if (l > 0 && (a.gamesBy[v] = l), x(e.stats)) for (let t of Object.keys(e.stats)) {
|
|
193
193
|
let n = e.stats[t];
|
|
194
|
-
if (!
|
|
195
|
-
let r =
|
|
196
|
-
a.playsBy[t] = r > 0 ? { [
|
|
197
|
-
for (let e of Object.keys(n))
|
|
194
|
+
if (!y(t) || !x(n)) continue;
|
|
195
|
+
let r = E(n.totalPlays, 0);
|
|
196
|
+
a.playsBy[t] = r > 0 ? { [v]: r } : {}, a.best[t] = C(n.highScore) ? T(n.highScore) : 0, a.sumsBy[t] = {};
|
|
197
|
+
for (let e of Object.keys(n)) y(e) && e !== "totalPlays" && e !== "highScore" && C(n[e]) && (a.sumsBy[t][e] = { [v]: T(n[e]) });
|
|
198
198
|
}
|
|
199
|
-
let u =
|
|
200
|
-
if (a.longest = Math.max(d,
|
|
199
|
+
let u = x(e.streak) ? e.streak : {}, d = E(u.current, 1, 1);
|
|
200
|
+
if (a.longest = Math.max(d, E(u.longest, 0)), p(u.lastPlayDate)) {
|
|
201
201
|
for (let e = 0; e < Math.min(d, 400); e++) a.playDates.push(m(u.lastPlayDate, -e));
|
|
202
202
|
a.playDates = h(a.playDates, r);
|
|
203
|
-
} else a.longest =
|
|
204
|
-
return
|
|
203
|
+
} else a.longest = E(u.longest, 0);
|
|
204
|
+
return M(a);
|
|
205
205
|
}
|
|
206
|
-
function
|
|
206
|
+
function I(e) {
|
|
207
207
|
let t = /* @__PURE__ */ new Set([
|
|
208
208
|
...Object.keys(e.xpBy),
|
|
209
209
|
...Object.keys(e.xpSpentBy),
|
|
@@ -213,24 +213,24 @@ function R(e) {
|
|
|
213
213
|
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
214
|
return t;
|
|
215
215
|
}
|
|
216
|
-
function
|
|
217
|
-
if (
|
|
216
|
+
function L(e) {
|
|
217
|
+
if (I(e).size > 20) throw new t("too_many_devices", "[DopamineJS] a save holds at most 20 devices");
|
|
218
218
|
}
|
|
219
|
-
function
|
|
220
|
-
if (!
|
|
219
|
+
function R(e, n = {}) {
|
|
220
|
+
if (!x(e)) throw new t("invalid_document", "[DopamineJS] a save must be an object");
|
|
221
221
|
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
|
|
222
|
+
let r = w(n.now) ? n.now : 0, i = p(n.today) ? n.today : void 0, a = e.v === 3 ? N(e, r, i) : F(e, r, x(n.rewards) ? n.rewards : s, i);
|
|
223
|
+
return L(a), a;
|
|
224
224
|
}
|
|
225
|
-
var
|
|
226
|
-
function
|
|
225
|
+
var z = (e) => T(Math.max(0, D(e.xpBy) + D(e.achievements, (e) => e.reward) - D(e.xpSpentBy))), B = (e) => Math.max(e.level, i(z(e)));
|
|
226
|
+
function V(e, t) {
|
|
227
227
|
let n = {};
|
|
228
228
|
for (let t of Object.keys(e.playsBy)) {
|
|
229
229
|
let r = {
|
|
230
|
-
plays:
|
|
230
|
+
plays: D(e.playsBy[t]),
|
|
231
231
|
best: e.best[t]
|
|
232
232
|
};
|
|
233
|
-
for (let n of Object.keys(e.sumsBy[t])) n !== "plays" && n !== "best" && (r[n] =
|
|
233
|
+
for (let n of Object.keys(e.sumsBy[t])) n !== "plays" && n !== "best" && (r[n] = D(e.sumsBy[t][n]));
|
|
234
234
|
n[t] = r;
|
|
235
235
|
}
|
|
236
236
|
let r = {};
|
|
@@ -238,12 +238,12 @@ function U(e, t) {
|
|
|
238
238
|
let i = {
|
|
239
239
|
current: g(e.playDates, t),
|
|
240
240
|
longest: Math.max(e.longest, _(e.playDates))
|
|
241
|
-
}, a =
|
|
241
|
+
}, a = ee(e.playDates, t);
|
|
242
242
|
return a && (i.lastPlayDate = a), {
|
|
243
243
|
name: e.name,
|
|
244
|
-
xp:
|
|
245
|
-
level:
|
|
246
|
-
totalGamesPlayed:
|
|
244
|
+
xp: z(e),
|
|
245
|
+
level: B(e),
|
|
246
|
+
totalGamesPlayed: D(e.gamesBy),
|
|
247
247
|
createdAt: e.createdAt,
|
|
248
248
|
lastPlayedAt: e.lastPlayedAt,
|
|
249
249
|
streak: i,
|
|
@@ -251,29 +251,28 @@ function U(e, t) {
|
|
|
251
251
|
stats: n
|
|
252
252
|
};
|
|
253
253
|
}
|
|
254
|
-
var
|
|
255
|
-
function
|
|
254
|
+
var H = (e) => JSON.parse(JSON.stringify(e));
|
|
255
|
+
function U(e) {
|
|
256
256
|
if (typeof e == "object" && e && !Object.isFrozen(e)) {
|
|
257
257
|
Object.freeze(e);
|
|
258
|
-
for (let t of Object.keys(e))
|
|
258
|
+
for (let t of Object.keys(e)) U(e[t]);
|
|
259
259
|
}
|
|
260
260
|
return e;
|
|
261
261
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
var K = [
|
|
262
|
+
// src/dopamine/core/middleware.js
|
|
263
|
+
var W = [
|
|
265
264
|
"beforeXP",
|
|
266
265
|
"afterXP",
|
|
267
266
|
"beforeRecord",
|
|
268
267
|
"afterRecord",
|
|
269
268
|
"beforeUnlock",
|
|
270
269
|
"afterUnlock"
|
|
271
|
-
],
|
|
270
|
+
], G = class {
|
|
272
271
|
constructor() {
|
|
273
272
|
this._list = [];
|
|
274
273
|
}
|
|
275
274
|
use(e) {
|
|
276
|
-
if (!
|
|
275
|
+
if (!x(e) || !W.some((t) => typeof e[t] == "function")) throw TypeError(`[DopamineJS] use expects an object with one of: ${W.join(", ")}`);
|
|
277
276
|
return this._list.push(e), () => {
|
|
278
277
|
let t = this._list.indexOf(e);
|
|
279
278
|
t >= 0 && this._list.splice(t, 1);
|
|
@@ -290,7 +289,7 @@ var K = [
|
|
|
290
289
|
continue;
|
|
291
290
|
}
|
|
292
291
|
if (i === !1) return !1;
|
|
293
|
-
if (!
|
|
292
|
+
if (!x(i)) continue;
|
|
294
293
|
let a = { ...t };
|
|
295
294
|
for (let e of Object.keys(t)) Object.prototype.hasOwnProperty.call(i, e) && (a[e] = i[e]);
|
|
296
295
|
n(a) && (t = a);
|
|
@@ -305,20 +304,19 @@ var K = [
|
|
|
305
304
|
}
|
|
306
305
|
}
|
|
307
306
|
};
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
function J(e, t, n) {
|
|
307
|
+
// src/dopamine/core/merge.js
|
|
308
|
+
function K(e, t, n) {
|
|
311
309
|
let r = {};
|
|
312
310
|
for (let i of /* @__PURE__ */ new Set([...Object.keys(e), ...Object.keys(t)])) {
|
|
313
|
-
let a =
|
|
314
|
-
r[i] = n > 0 ?
|
|
311
|
+
let a = b(e, i) ? e[i] : void 0, o = b(t, i) ? t[i] : void 0;
|
|
312
|
+
r[i] = n > 0 ? K(a || {}, o || {}, n - 1) : a === void 0 ? o : o === void 0 ? a : Math.max(a, o);
|
|
315
313
|
}
|
|
316
314
|
return r;
|
|
317
315
|
}
|
|
318
|
-
function
|
|
316
|
+
function q(e, t) {
|
|
319
317
|
let n = {};
|
|
320
318
|
for (let r of /* @__PURE__ */ new Set([...Object.keys(e), ...Object.keys(t)])) {
|
|
321
|
-
let i =
|
|
319
|
+
let i = b(e, r) ? e[r] : void 0, a = b(t, r) ? t[r] : void 0;
|
|
322
320
|
if (!i || !a) {
|
|
323
321
|
n[r] = { ...i || a };
|
|
324
322
|
continue;
|
|
@@ -332,39 +330,38 @@ function ee(e, t) {
|
|
|
332
330
|
}
|
|
333
331
|
return n;
|
|
334
332
|
}
|
|
335
|
-
var
|
|
333
|
+
var J = (e, t) => e > 0 && t > 0 ? Math.min(e, t) : Math.max(e, t);
|
|
336
334
|
function Y(e, t, n) {
|
|
337
|
-
let r =
|
|
335
|
+
let r = R(e, n), i = R(t, n), a = {
|
|
338
336
|
v: 3,
|
|
339
337
|
name: (r.lastPlayedAt === i.lastPlayedAt ? r.name >= i.name ? r : i : r.lastPlayedAt > i.lastPlayedAt ? r : i).name,
|
|
340
|
-
createdAt:
|
|
338
|
+
createdAt: J(r.createdAt, i.createdAt),
|
|
341
339
|
lastPlayedAt: Math.max(r.lastPlayedAt, i.lastPlayedAt),
|
|
342
340
|
level: Math.max(r.level, i.level),
|
|
343
341
|
longest: Math.max(r.longest, i.longest),
|
|
344
342
|
playDates: h([...r.playDates, ...i.playDates], p(n?.today) ? n.today : void 0),
|
|
345
|
-
xpBy:
|
|
346
|
-
xpSpentBy:
|
|
347
|
-
gamesBy:
|
|
348
|
-
playsBy:
|
|
349
|
-
best:
|
|
350
|
-
sumsBy:
|
|
351
|
-
achievements:
|
|
343
|
+
xpBy: K(r.xpBy, i.xpBy, 0),
|
|
344
|
+
xpSpentBy: K(r.xpSpentBy, i.xpSpentBy, 0),
|
|
345
|
+
gamesBy: K(r.gamesBy, i.gamesBy, 0),
|
|
346
|
+
playsBy: K(r.playsBy, i.playsBy, 1),
|
|
347
|
+
best: K(r.best, i.best, 0),
|
|
348
|
+
sumsBy: K(r.sumsBy, i.sumsBy, 2),
|
|
349
|
+
achievements: q(r.achievements, i.achievements)
|
|
352
350
|
};
|
|
353
|
-
return
|
|
351
|
+
return L(a), a;
|
|
354
352
|
}
|
|
355
|
-
|
|
356
|
-
//#region src/dopamine/core/RewardSystem.js
|
|
353
|
+
// src/dopamine/core/RewardSystem.js
|
|
357
354
|
var X = "device", Z = () => {
|
|
358
355
|
let e = /* @__PURE__ */ new Uint8Array(8), t = globalThis.crypto;
|
|
359
356
|
if (typeof t?.getRandomValues == "function") t.getRandomValues(e);
|
|
360
357
|
else for (let t = 0; t < e.length; t++) e[t] = Math.floor(Math.random() * 256);
|
|
361
358
|
return Array.from(e, (e) => e.toString(16).padStart(2, "0")).join("");
|
|
362
|
-
}, ne = (e) =>
|
|
359
|
+
}, ne = (e) => y(e) && e !== "legacy", re = class extends e {
|
|
363
360
|
constructor(e, t = {}) {
|
|
364
361
|
super(), this.dataService = e, this.clock = t.clock && typeof t.clock.now == "function" && typeof t.clock.localDate == "function" ? t.clock : r, this.achievements = {
|
|
365
362
|
...t.builtinAchievements === !1 ? {} : o(),
|
|
366
363
|
...t.achievements || {}
|
|
367
|
-
}, this.streakDays = t.streakDays === "record" ? "record" : "any", this._doc = null, this._deviceId = null, this._deviceSaved = !1, this._middleware = new
|
|
364
|
+
}, this.streakDays = t.streakDays === "record" ? "record" : "any", this._doc = null, this._deviceId = null, this._deviceSaved = !1, this._middleware = new G(), this._unlocking = /* @__PURE__ */ new Set(), this._batchDepth = 0, this._pendingSave = !1, this._loadFailed = !1, this._readFailed = !1;
|
|
368
365
|
}
|
|
369
366
|
get loadFailed() {
|
|
370
367
|
return this._readFailed;
|
|
@@ -399,34 +396,35 @@ var X = "device", Z = () => {
|
|
|
399
396
|
this._loadFailed = !0;
|
|
400
397
|
try {
|
|
401
398
|
let e = await this.dataService.load("player", null, { strict: !0 });
|
|
402
|
-
return this._loadFailed = !1, this._readFailed = !1, e;
|
|
399
|
+
return e !== null && !x(e) ? await this._keepCorrupt(e) : (this._loadFailed = !1, this._readFailed = !1, e);
|
|
403
400
|
} catch (e) {
|
|
404
|
-
|
|
405
|
-
let t = await this.dataService.save("player_corrupt", e.raw);
|
|
406
|
-
return t && (this._loadFailed = !1), this._readFailed = !t, this._publish("load_failed", {
|
|
407
|
-
key: "player",
|
|
408
|
-
reason: "corrupt",
|
|
409
|
-
backedUp: t
|
|
410
|
-
}), null;
|
|
411
|
-
}
|
|
412
|
-
return this._readFailed = !0, this._publish("load_failed", {
|
|
401
|
+
return e?.code === "DOPAMINE_CORRUPT" ? this._keepCorrupt(e.raw) : (this._readFailed = !0, this._publish("load_failed", {
|
|
413
402
|
key: "player",
|
|
414
403
|
reason: "read",
|
|
415
404
|
error: e
|
|
416
|
-
}), null;
|
|
405
|
+
}), null);
|
|
417
406
|
}
|
|
418
407
|
}
|
|
408
|
+
async _keepCorrupt(e) {
|
|
409
|
+
this._loadFailed = !0;
|
|
410
|
+
let t = await this.dataService.save("player_corrupt", e);
|
|
411
|
+
return t && (this._loadFailed = !1), this._readFailed = !t, this._publish("load_failed", {
|
|
412
|
+
key: "player",
|
|
413
|
+
reason: "corrupt",
|
|
414
|
+
backedUp: t
|
|
415
|
+
}), null;
|
|
416
|
+
}
|
|
419
417
|
_load(e) {
|
|
420
|
-
if (!
|
|
418
|
+
if (!x(e)) return O(this.clock.now());
|
|
421
419
|
try {
|
|
422
|
-
return
|
|
420
|
+
return R(e, this._options());
|
|
423
421
|
} catch (e) {
|
|
424
422
|
if (!(e instanceof t)) throw e;
|
|
425
423
|
return this._loadFailed = !0, this._readFailed = !0, this._publish("load_failed", {
|
|
426
424
|
key: "player",
|
|
427
425
|
reason: e.code,
|
|
428
426
|
error: e
|
|
429
|
-
}),
|
|
427
|
+
}), O(this.clock.now());
|
|
430
428
|
}
|
|
431
429
|
}
|
|
432
430
|
async _readDevice() {
|
|
@@ -441,7 +439,7 @@ var X = "device", Z = () => {
|
|
|
441
439
|
ne(e) ? (this._deviceId = e, this._deviceSaved = !0) : this._deviceId = Z();
|
|
442
440
|
}
|
|
443
441
|
_hasSlot() {
|
|
444
|
-
let e =
|
|
442
|
+
let e = I(this._doc);
|
|
445
443
|
return e.has(this._deviceId) || e.size < 20 ? !0 : (this._publish("load_failed", {
|
|
446
444
|
key: "player",
|
|
447
445
|
reason: "too_many_devices"
|
|
@@ -451,15 +449,15 @@ var X = "device", Z = () => {
|
|
|
451
449
|
if (!this._doc) throw Error("[DopamineJS] RewardSystem.init() must be awaited before use");
|
|
452
450
|
}
|
|
453
451
|
snapshot() {
|
|
454
|
-
return this._requirePlayer(),
|
|
452
|
+
return this._requirePlayer(), U(V(this._doc, this.clock.localDate()));
|
|
455
453
|
}
|
|
456
454
|
export() {
|
|
457
|
-
return this._requirePlayer(),
|
|
455
|
+
return this._requirePlayer(), H(this._doc);
|
|
458
456
|
}
|
|
459
457
|
async import(e, { silent: t = !1 } = {}) {
|
|
460
458
|
this._requirePlayer();
|
|
461
|
-
let n = this.snapshot(), r =
|
|
462
|
-
i.level =
|
|
459
|
+
let n = this.snapshot(), r = R(e, this._options()), i = Y(this._doc, r, this._options());
|
|
460
|
+
i.level = B(i), this._doc = i;
|
|
463
461
|
let a = this.snapshot();
|
|
464
462
|
if (await this.save(), t) return a;
|
|
465
463
|
let o = a.xp - n.xp, s = a.level > n.level;
|
|
@@ -474,7 +472,7 @@ var X = "device", Z = () => {
|
|
|
474
472
|
newLevel: a.level,
|
|
475
473
|
totalXP: a.xp
|
|
476
474
|
});
|
|
477
|
-
for (let e of Object.keys(a.achievements)) !
|
|
475
|
+
for (let e of Object.keys(a.achievements)) !b(n.achievements, e) && b(this.achievements, e) && this._publish("achievement_unlocked", {
|
|
478
476
|
...this.achievements[e],
|
|
479
477
|
id: e
|
|
480
478
|
});
|
|
@@ -483,29 +481,23 @@ var X = "device", Z = () => {
|
|
|
483
481
|
async reset(e) {
|
|
484
482
|
let t = e?.overwrite;
|
|
485
483
|
if (e !== void 0 && (typeof e != "object" || !e || Array.isArray(e)) || t !== void 0 && typeof t != "boolean") throw TypeError("[DopamineJS] reset expects { overwrite?: boolean }");
|
|
486
|
-
return this._requirePlayer(), t === !1 && this._readFailed ? (this._doc =
|
|
487
|
-
this._doc =
|
|
484
|
+
return this._requirePlayer(), t === !1 && this._readFailed ? (this._doc = O(this.clock.now()), this._publish("player_reset", {}), this.snapshot()) : (await this._batch(async () => {
|
|
485
|
+
this._doc = O(this.clock.now()), this._loadFailed = !1, this._readFailed = !1, await this.save();
|
|
488
486
|
}), this._publish("player_reset", {}), this.snapshot());
|
|
489
487
|
}
|
|
490
488
|
use(e) {
|
|
491
489
|
return this._middleware.use(e);
|
|
492
490
|
}
|
|
493
491
|
defineAchievement(e, t) {
|
|
494
|
-
if (!
|
|
495
|
-
if (!
|
|
496
|
-
let n =
|
|
492
|
+
if (!y(e)) throw TypeError(`[DopamineJS] defineAchievement expects an id, received ${String(e)}`);
|
|
493
|
+
if (!x(t) || typeof t.check != "function") throw TypeError("[DopamineJS] defineAchievement expects a definition with a check function");
|
|
494
|
+
let n = b(this.achievements, e) ? this.achievements[e] : void 0;
|
|
497
495
|
return this.achievements[e] = t, () => {
|
|
498
496
|
this.achievements[e] === t && (n ? this.achievements[e] = n : delete this.achievements[e]);
|
|
499
497
|
};
|
|
500
498
|
}
|
|
501
499
|
async save() {
|
|
502
|
-
|
|
503
|
-
if (this._batchDepth > 0) {
|
|
504
|
-
this._pendingSave = !0;
|
|
505
|
-
return;
|
|
506
|
-
}
|
|
507
|
-
await this._write();
|
|
508
|
-
}
|
|
500
|
+
this._doc && (this._batchDepth > 0 ? this._pendingSave = !0 : await this._write());
|
|
509
501
|
}
|
|
510
502
|
async _write() {
|
|
511
503
|
this._loadFailed || (this._doc.lastPlayedAt = this.clock.now(), this._deviceSaved ||= await this.dataService.save(X, this._deviceId) !== !1, await this.dataService.save("player", this._doc) === !1 && this._publish("save_failed", { key: "player" }));
|
|
@@ -514,7 +506,7 @@ var X = "device", Z = () => {
|
|
|
514
506
|
if (!Number.isFinite(e)) throw TypeError(`[DopamineJS] addXP expects a finite number, received ${e}`);
|
|
515
507
|
if (this._requirePlayer(), !this._hasSlot()) return {
|
|
516
508
|
leveledUp: !1,
|
|
517
|
-
newLevel:
|
|
509
|
+
newLevel: B(this._doc),
|
|
518
510
|
xpGained: 0
|
|
519
511
|
};
|
|
520
512
|
let n = await this._middleware.before("beforeXP", {
|
|
@@ -523,26 +515,26 @@ var X = "device", Z = () => {
|
|
|
523
515
|
}, (e) => Number.isFinite(e.amount));
|
|
524
516
|
if (n === !1) return {
|
|
525
517
|
leveledUp: !1,
|
|
526
|
-
newLevel:
|
|
518
|
+
newLevel: B(this._doc),
|
|
527
519
|
xpGained: 0
|
|
528
520
|
};
|
|
529
521
|
this.streakDays === "any" && n.amount > 0 && this._markDay();
|
|
530
522
|
let r = await this._applyXP(n.amount, n.reason, (e, t) => {
|
|
531
|
-
t >= 0 ? e.xpBy[this._deviceId] =
|
|
523
|
+
t >= 0 ? e.xpBy[this._deviceId] = T((b(e.xpBy, this._deviceId) ? e.xpBy[this._deviceId] : 0) + t) : e.xpSpentBy[this._deviceId] = T((b(e.xpSpentBy, this._deviceId) ? e.xpSpentBy[this._deviceId] : 0) - t);
|
|
532
524
|
});
|
|
533
525
|
return await this._middleware.after("afterXP", {
|
|
534
526
|
amount: r.xpGained,
|
|
535
527
|
reason: n.reason,
|
|
536
|
-
total:
|
|
528
|
+
total: z(this._doc),
|
|
537
529
|
level: r.newLevel
|
|
538
530
|
}), r;
|
|
539
531
|
}
|
|
540
532
|
async _applyXP(e, t, n) {
|
|
541
|
-
let r = this._doc, i =
|
|
542
|
-
return e = Math.max(e, -
|
|
533
|
+
let r = this._doc, i = B(r);
|
|
534
|
+
return e = Math.max(e, -z(r)), n(r, e), this._announceXP(i, e, t);
|
|
543
535
|
}
|
|
544
536
|
async _announceXP(e, t, n) {
|
|
545
|
-
let r = this._doc, a =
|
|
537
|
+
let r = this._doc, a = z(r), o = Math.max(e, i(a)), s = o > e;
|
|
546
538
|
return o > r.level && (r.level = o), await this.save(), this._publish("xp_gained", {
|
|
547
539
|
amount: t,
|
|
548
540
|
reason: n,
|
|
@@ -561,7 +553,7 @@ var X = "device", Z = () => {
|
|
|
561
553
|
}
|
|
562
554
|
getXPForNextLevel() {
|
|
563
555
|
this._requirePlayer();
|
|
564
|
-
let e =
|
|
556
|
+
let e = B(this._doc), t = z(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;
|
|
565
557
|
return {
|
|
566
558
|
total: n,
|
|
567
559
|
needed: Math.max(0, n - t),
|
|
@@ -569,8 +561,8 @@ var X = "device", Z = () => {
|
|
|
569
561
|
};
|
|
570
562
|
}
|
|
571
563
|
_checkRecord(e, t) {
|
|
572
|
-
if (!
|
|
573
|
-
if (!
|
|
564
|
+
if (!y(e)) throw TypeError(`[DopamineJS] record expects an activity name, received ${String(e)}`);
|
|
565
|
+
if (!x(t)) throw TypeError("[DopamineJS] record expects a result object");
|
|
574
566
|
if (t.score !== void 0 && !Number.isFinite(t.score)) throw TypeError(`[DopamineJS] record expects a finite score, received ${String(t.score)}`);
|
|
575
567
|
}
|
|
576
568
|
async record(e, t = {}) {
|
|
@@ -604,20 +596,20 @@ var X = "device", Z = () => {
|
|
|
604
596
|
}
|
|
605
597
|
async _record(e, t) {
|
|
606
598
|
let n = this._doc, r = this._deviceId, i = (e, t) => {
|
|
607
|
-
e[r] =
|
|
599
|
+
e[r] = T((b(e, r) ? e[r] : 0) + t);
|
|
608
600
|
};
|
|
609
|
-
this._markDay(),
|
|
601
|
+
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", {
|
|
610
602
|
activity: e,
|
|
611
603
|
score: t.score
|
|
612
604
|
}));
|
|
613
|
-
for (let r of Object.keys(t)) r !== "score" && r !== "plays" && r !== "best" &&
|
|
605
|
+
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));
|
|
614
606
|
let a = 10;
|
|
615
607
|
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;
|
|
616
608
|
}
|
|
617
609
|
async checkAchievements(e, t) {
|
|
618
610
|
let n = [];
|
|
619
611
|
for (let [r, i] of Object.entries(this.achievements)) {
|
|
620
|
-
if (
|
|
612
|
+
if (b(this._doc.achievements, r)) continue;
|
|
621
613
|
let a = !1;
|
|
622
614
|
try {
|
|
623
615
|
a = i.check(this.snapshot(), e, t);
|
|
@@ -630,15 +622,22 @@ var X = "device", Z = () => {
|
|
|
630
622
|
return n;
|
|
631
623
|
}
|
|
632
624
|
async unlockAchievement(e) {
|
|
633
|
-
if (this._requirePlayer(), !
|
|
625
|
+
if (this._requirePlayer(), !y(e) || !b(this.achievements, e) || b(this._doc.achievements, e) || this._unlocking.has(e)) return !1;
|
|
634
626
|
let t = this.achievements[e];
|
|
635
627
|
if (!t) return !1;
|
|
636
|
-
let { [e]: n } = c(this.achievements)
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
628
|
+
let { [e]: n } = c(this.achievements);
|
|
629
|
+
this._unlocking.add(e);
|
|
630
|
+
let r;
|
|
631
|
+
try {
|
|
632
|
+
r = await this._middleware.before("beforeUnlock", {
|
|
633
|
+
id: e,
|
|
634
|
+
reward: n
|
|
635
|
+
}, (e) => Number.isFinite(e.reward) && e.reward >= 0);
|
|
636
|
+
} finally {
|
|
637
|
+
this._unlocking.delete(e);
|
|
638
|
+
}
|
|
639
|
+
if (r === !1 || b(this._doc.achievements, e)) return !1;
|
|
640
|
+
let i = this._doc, a = B(i);
|
|
642
641
|
return i.achievements[e] = {
|
|
643
642
|
unlockedAt: this.clock.now(),
|
|
644
643
|
seen: !1,
|
|
@@ -662,7 +661,8 @@ var X = "device", Z = () => {
|
|
|
662
661
|
return this.getUnlockedAchievements().filter((e) => !e.seen);
|
|
663
662
|
}
|
|
664
663
|
async markAchievementsSeen(e) {
|
|
665
|
-
|
|
664
|
+
this._requirePlayer();
|
|
665
|
+
for (let t of e) b(this._doc.achievements, t) && (this._doc.achievements[t].seen = !0);
|
|
666
666
|
await this.save();
|
|
667
667
|
}
|
|
668
668
|
getStreakMultiplier() {
|
|
@@ -670,8 +670,7 @@ var X = "device", Z = () => {
|
|
|
670
670
|
return e >= 7 ? 2 : e >= 5 ? 1.5 : e >= 3 ? 1.25 : 1;
|
|
671
671
|
}
|
|
672
672
|
};
|
|
673
|
-
|
|
674
|
-
//#region src/dopamine/utils/storage.js
|
|
673
|
+
// src/dopamine/utils/storage.js
|
|
675
674
|
function Q() {
|
|
676
675
|
let e = /* @__PURE__ */ new Map();
|
|
677
676
|
return {
|
|
@@ -696,8 +695,7 @@ function $() {
|
|
|
696
695
|
return Q();
|
|
697
696
|
}
|
|
698
697
|
}
|
|
699
|
-
|
|
700
|
-
//#region src/dopamine/core/DataService.js
|
|
698
|
+
// src/dopamine/core/DataService.js
|
|
701
699
|
var ie = class {
|
|
702
700
|
constructor(e = {}) {
|
|
703
701
|
this.storage = e.storage || $(), this.prefix = e.prefix || "dopamine_", this._tail = null;
|
|
@@ -743,5 +741,4 @@ var ie = class {
|
|
|
743
741
|
await this._write(() => this.storage.removeItem(this.prefix + e));
|
|
744
742
|
}
|
|
745
743
|
};
|
|
746
|
-
|
|
747
|
-
export { Y as a, t as c, re as i, e as l, Q as n, B as o, $ as r, r as s, ie as t };
|
|
744
|
+
export { Y as a, t as c, re as i, e as l, Q as n, R as o, $ as r, r as s, ie as t };
|