@gamepropel/sdk-crazygames 0.0.0-stage → 1.0.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 +131 -2
- package/SERVER_HANDOFF.md +29 -0
- package/VALIDATION.md +39 -0
- package/dist/commonjs/index.js +5 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/package.json +1 -0
- package/dist/index.js +2224 -0
- package/dist/index.js.map +1 -0
- package/dist/types/crazygames/src/ads.d.ts +26 -0
- package/dist/types/crazygames/src/host.d.ts +37 -0
- package/dist/types/crazygames/src/index.d.ts +68 -0
- package/dist/types/crazygames/src/storage.d.ts +10 -0
- package/dist/types/crazygames/src/transport.d.ts +8 -0
- package/dist/types/rest-api/src/browser-storage.d.ts +42 -0
- package/dist/types/rest-api/src/core-client.d.ts +250 -0
- package/dist/types/rest-api/src/dda.d.ts +22 -0
- package/dist/types/rest-api/src/event-queue-store.d.ts +20 -0
- package/dist/types/rest-api/src/http-url.d.ts +8 -0
- package/dist/types/rest-api/src/sha256.d.ts +1 -0
- package/dist/types/rest-api/src/types.d.ts +244 -0
- package/dist/types/rest-api/src/utf8.d.ts +2 -0
- package/package.json +34 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,2224 @@
|
|
|
1
|
+
//#region ../rest-api/src/utf8.ts
|
|
2
|
+
function e(e) {
|
|
3
|
+
if (typeof TextEncoder < "u") return new TextEncoder().encode(e);
|
|
4
|
+
let t = [];
|
|
5
|
+
for (let n = 0; n < e.length; n++) {
|
|
6
|
+
let r = e.charCodeAt(n);
|
|
7
|
+
if (r >= 55296 && r <= 56319) {
|
|
8
|
+
let t = e.charCodeAt(n + 1);
|
|
9
|
+
t >= 56320 && t <= 57343 ? (r = 65536 + (r - 55296 << 10) + t - 56320, n++) : r = 65533;
|
|
10
|
+
} else r >= 56320 && r <= 57343 && (r = 65533);
|
|
11
|
+
r < 128 ? t.push(r) : r < 2048 ? t.push(192 | r >> 6, 128 | r & 63) : r < 65536 ? t.push(224 | r >> 12, 128 | r >> 6 & 63, 128 | r & 63) : t.push(240 | r >> 18, 128 | r >> 12 & 63, 128 | r >> 6 & 63, 128 | r & 63);
|
|
12
|
+
}
|
|
13
|
+
return new Uint8Array(t);
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region ../rest-api/src/sha256.ts
|
|
17
|
+
function t(t) {
|
|
18
|
+
let i = e(t), a = i.length * 8, o = Math.ceil((i.length + 9) / 64) * 64, s = new Uint8Array(o);
|
|
19
|
+
s.set(i), s[i.length] = 128;
|
|
20
|
+
let c = new DataView(s.buffer);
|
|
21
|
+
c.setUint32(o - 8, Math.floor(a / 4294967296)), c.setUint32(o - 4, a >>> 0);
|
|
22
|
+
let l = new Uint32Array([
|
|
23
|
+
1779033703,
|
|
24
|
+
3144134277,
|
|
25
|
+
1013904242,
|
|
26
|
+
2773480762,
|
|
27
|
+
1359893119,
|
|
28
|
+
2600822924,
|
|
29
|
+
528734635,
|
|
30
|
+
1541459225
|
|
31
|
+
]), u = /* @__PURE__ */ new Uint32Array(64);
|
|
32
|
+
for (let e = 0; e < o; e += 64) {
|
|
33
|
+
for (let t = 0; t < 16; t += 1) u[t] = c.getUint32(e + t * 4);
|
|
34
|
+
for (let e = 16; e < 64; e += 1) {
|
|
35
|
+
let t = u[e - 15], r = u[e - 2], i = n(t, 7) ^ n(t, 18) ^ t >>> 3, a = n(r, 17) ^ n(r, 19) ^ r >>> 10;
|
|
36
|
+
u[e] = u[e - 16] + i + u[e - 7] + a >>> 0;
|
|
37
|
+
}
|
|
38
|
+
let [t, i, a, o, s, d, f, p] = l;
|
|
39
|
+
for (let e = 0; e < 64; e += 1) {
|
|
40
|
+
let c = n(s, 6) ^ n(s, 11) ^ n(s, 25), l = s & d ^ ~s & f, m = p + c + l + r[e] + u[e] >>> 0, h = (n(t, 2) ^ n(t, 13) ^ n(t, 22)) + (t & i ^ t & a ^ i & a) >>> 0;
|
|
41
|
+
p = f, f = d, d = s, s = o + m >>> 0, o = a, a = i, i = t, t = m + h >>> 0;
|
|
42
|
+
}
|
|
43
|
+
l[0] = l[0] + t >>> 0, l[1] = l[1] + i >>> 0, l[2] = l[2] + a >>> 0, l[3] = l[3] + o >>> 0, l[4] = l[4] + s >>> 0, l[5] = l[5] + d >>> 0, l[6] = l[6] + f >>> 0, l[7] = l[7] + p >>> 0;
|
|
44
|
+
}
|
|
45
|
+
return [...l].map((e) => e.toString(16).padStart(8, "0")).join("");
|
|
46
|
+
}
|
|
47
|
+
function n(e, t) {
|
|
48
|
+
return e >>> t | e << 32 - t;
|
|
49
|
+
}
|
|
50
|
+
var r = new Uint32Array([
|
|
51
|
+
1116352408,
|
|
52
|
+
1899447441,
|
|
53
|
+
3049323471,
|
|
54
|
+
3921009573,
|
|
55
|
+
961987163,
|
|
56
|
+
1508970993,
|
|
57
|
+
2453635748,
|
|
58
|
+
2870763221,
|
|
59
|
+
3624381080,
|
|
60
|
+
310598401,
|
|
61
|
+
607225278,
|
|
62
|
+
1426881987,
|
|
63
|
+
1925078388,
|
|
64
|
+
2162078206,
|
|
65
|
+
2614888103,
|
|
66
|
+
3248222580,
|
|
67
|
+
3835390401,
|
|
68
|
+
4022224774,
|
|
69
|
+
264347078,
|
|
70
|
+
604807628,
|
|
71
|
+
770255983,
|
|
72
|
+
1249150122,
|
|
73
|
+
1555081692,
|
|
74
|
+
1996064986,
|
|
75
|
+
2554220882,
|
|
76
|
+
2821834349,
|
|
77
|
+
2952996808,
|
|
78
|
+
3210313671,
|
|
79
|
+
3336571891,
|
|
80
|
+
3584528711,
|
|
81
|
+
113926993,
|
|
82
|
+
338241895,
|
|
83
|
+
666307205,
|
|
84
|
+
773529912,
|
|
85
|
+
1294757372,
|
|
86
|
+
1396182291,
|
|
87
|
+
1695183700,
|
|
88
|
+
1986661051,
|
|
89
|
+
2177026350,
|
|
90
|
+
2456956037,
|
|
91
|
+
2730485921,
|
|
92
|
+
2820302411,
|
|
93
|
+
3259730800,
|
|
94
|
+
3345764771,
|
|
95
|
+
3516065817,
|
|
96
|
+
3600352804,
|
|
97
|
+
4094571909,
|
|
98
|
+
275423344,
|
|
99
|
+
430227734,
|
|
100
|
+
506948616,
|
|
101
|
+
659060556,
|
|
102
|
+
883997877,
|
|
103
|
+
958139571,
|
|
104
|
+
1322822218,
|
|
105
|
+
1537002063,
|
|
106
|
+
1747873779,
|
|
107
|
+
1955562222,
|
|
108
|
+
2024104815,
|
|
109
|
+
2227730452,
|
|
110
|
+
2361852424,
|
|
111
|
+
2428436474,
|
|
112
|
+
2756734187,
|
|
113
|
+
3204031479,
|
|
114
|
+
3329325298
|
|
115
|
+
]);
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region ../rest-api/src/http-url.ts
|
|
118
|
+
function i(e) {
|
|
119
|
+
if (typeof URL < "u") {
|
|
120
|
+
let t = new URL(e);
|
|
121
|
+
if (!/^https?:$/.test(t.protocol) || t.username || t.password) throw Error("Invalid GameAlgo HTTP URL");
|
|
122
|
+
return {
|
|
123
|
+
origin: t.origin,
|
|
124
|
+
pathname: t.pathname,
|
|
125
|
+
href: t.href
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
let t = /^(https?):\/\/([^\s/?#@]+)(\/[^?#\s]*)?(?:\?[^#\s]*)?(?:#[^\s]*)?$/i.exec(e);
|
|
129
|
+
if (!t) throw Error("Invalid GameAlgo HTTP URL");
|
|
130
|
+
let n = t[1].toLowerCase(), r = `${n}://${t[2].toLowerCase().replace(n === "https" ? /:443$/ : /:80$/, "")}`, i = o(t[3] || "/");
|
|
131
|
+
return {
|
|
132
|
+
origin: r,
|
|
133
|
+
pathname: i,
|
|
134
|
+
href: r + i
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function a(e, t) {
|
|
138
|
+
if (typeof URL < "u") return new URL(e, t.href).href;
|
|
139
|
+
if (/^https?:\/\//i.test(e)) return i(e).href;
|
|
140
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(e) || e.startsWith("//")) throw Error("Invalid GameAlgo HTTP URL");
|
|
141
|
+
let n = e.startsWith("/") ? e : t.pathname.replace(/[^/]*$/, "") + e;
|
|
142
|
+
return t.origin + o(n);
|
|
143
|
+
}
|
|
144
|
+
function o(e) {
|
|
145
|
+
let t = [];
|
|
146
|
+
for (let n of e.split("/")) n === ".." ? t.pop() : n !== "." && t.push(n);
|
|
147
|
+
return t.join("/") || "/";
|
|
148
|
+
}
|
|
149
|
+
//#endregion
|
|
150
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/typeof.js
|
|
151
|
+
function s(e) {
|
|
152
|
+
"@babel/helpers - typeof";
|
|
153
|
+
return s = typeof Symbol == "function" && typeof Symbol.iterator == "symbol" ? function(e) {
|
|
154
|
+
return typeof e;
|
|
155
|
+
} : function(e) {
|
|
156
|
+
return e && typeof Symbol == "function" && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
|
|
157
|
+
}, s(e);
|
|
158
|
+
}
|
|
159
|
+
//#endregion
|
|
160
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPrimitive.js
|
|
161
|
+
function c(e, t) {
|
|
162
|
+
if (s(e) != "object" || !e) return e;
|
|
163
|
+
var n = e[Symbol.toPrimitive];
|
|
164
|
+
if (n !== void 0) {
|
|
165
|
+
var r = n.call(e, t || "default");
|
|
166
|
+
if (s(r) != "object") return r;
|
|
167
|
+
throw TypeError("@@toPrimitive must return a primitive value.");
|
|
168
|
+
}
|
|
169
|
+
return (t === "string" ? String : Number)(e);
|
|
170
|
+
}
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/toPropertyKey.js
|
|
173
|
+
function l(e) {
|
|
174
|
+
var t = c(e, "string");
|
|
175
|
+
return s(t) == "symbol" ? t : t + "";
|
|
176
|
+
}
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region \0@oxc-project+runtime@0.149.0/helpers/esm/defineProperty.js
|
|
179
|
+
function u(e, t, n) {
|
|
180
|
+
return (t = l(t)) in e ? Object.defineProperty(e, t, {
|
|
181
|
+
value: n,
|
|
182
|
+
enumerable: !0,
|
|
183
|
+
configurable: !0,
|
|
184
|
+
writable: !0
|
|
185
|
+
}) : e[t] = n, e;
|
|
186
|
+
}
|
|
187
|
+
//#endregion
|
|
188
|
+
//#region ../rest-api/src/dda.ts
|
|
189
|
+
var d = 10, f = class {
|
|
190
|
+
constructor(e, t, n, r = {}, i) {
|
|
191
|
+
var a;
|
|
192
|
+
u(this, "executor", void 0), u(this, "storage", void 0), u(this, "storageKey", void 0), u(this, "recentWindowSize", void 0), u(this, "logger", void 0), u(this, "statePromise", void 0), u(this, "writeTail", Promise.resolve()), this.executor = e, this.storage = t, this.storageKey = (a = r.storageKey) == null ? n : a, this.recentWindowSize = v(r.recentWindowSize), this.logger = i, this.statePromise = this.loadState();
|
|
193
|
+
}
|
|
194
|
+
async recordBehavior(e, t = 1) {
|
|
195
|
+
let n = _(e);
|
|
196
|
+
if (!Number.isFinite(t) || t <= 0) throw Error("DDA behavior amount must be greater than 0");
|
|
197
|
+
await this.mutate((e) => {
|
|
198
|
+
var r, i;
|
|
199
|
+
e.current[n] = ((r = e.current[n]) == null ? 0 : r) + t, e.lifetime[n] = ((i = e.lifetime[n]) == null ? 0 : i) + t;
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
async completeStep(e) {
|
|
203
|
+
await this.mutate((t) => {
|
|
204
|
+
t.recentSteps.push({
|
|
205
|
+
...x(e) ? { stepId: x(e) } : {},
|
|
206
|
+
behaviors: { ...t.current }
|
|
207
|
+
}), t.recentSteps.length > this.recentWindowSize && t.recentSteps.splice(0, t.recentSteps.length - this.recentWindowSize), t.current = {}, t.completedSteps += 1;
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
async reset(e = "all") {
|
|
211
|
+
await this.mutate((t) => {
|
|
212
|
+
if (e === "all") Object.assign(t, p());
|
|
213
|
+
else if (e === "current") t.current = {};
|
|
214
|
+
else if (e === "recent") t.recentSteps = [];
|
|
215
|
+
else throw Error(`Unsupported DDA reset scope: ${e}`);
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
async snapshot(e = {}) {
|
|
219
|
+
await this.writeTail;
|
|
220
|
+
let t = await this.statePromise, n = g(t.recentSteps);
|
|
221
|
+
return {
|
|
222
|
+
context: e,
|
|
223
|
+
behavior: {
|
|
224
|
+
current: { ...t.current },
|
|
225
|
+
recent: n,
|
|
226
|
+
lifetime: { ...t.lifetime },
|
|
227
|
+
recentSteps: t.recentSteps.map((e) => ({
|
|
228
|
+
...e.stepId ? { stepId: e.stepId } : {},
|
|
229
|
+
behaviors: { ...e.behaviors }
|
|
230
|
+
})),
|
|
231
|
+
completedSteps: t.completedSteps,
|
|
232
|
+
windowSize: this.recentWindowSize
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
async decide(e = {}) {
|
|
237
|
+
try {
|
|
238
|
+
let t = await this.executor.execute(await this.snapshot(e));
|
|
239
|
+
if (!t) return b("executor_not_ready");
|
|
240
|
+
let n = y(t.payload);
|
|
241
|
+
return n ? {
|
|
242
|
+
adjustment: n,
|
|
243
|
+
payload: t.payload,
|
|
244
|
+
diagnostics: t.diagnostics,
|
|
245
|
+
assignment: t.assignment,
|
|
246
|
+
isFallback: !1
|
|
247
|
+
} : b("invalid_adjustment", t);
|
|
248
|
+
} catch (e) {
|
|
249
|
+
var t;
|
|
250
|
+
return (t = this.logger) == null || t.call(this, `[GameAlgoSDK] DDA decision failed: ${e instanceof Error ? e.message : String(e)}`), b("execution_failed");
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
async loadState() {
|
|
254
|
+
try {
|
|
255
|
+
var e;
|
|
256
|
+
let t = await ((e = this.storage) == null ? void 0 : e.getItem(this.storageKey));
|
|
257
|
+
if (!t) return p();
|
|
258
|
+
let n = m(JSON.parse(t));
|
|
259
|
+
return n.recentSteps.length > this.recentWindowSize && n.recentSteps.splice(0, n.recentSteps.length - this.recentWindowSize), n;
|
|
260
|
+
} catch (e) {
|
|
261
|
+
var t;
|
|
262
|
+
return (t = this.logger) == null || t.call(this, `[GameAlgoSDK] DDA state load failed; using empty state: ${e instanceof Error ? e.message : String(e)}`), p();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
async mutate(e) {
|
|
266
|
+
let t = this.writeTail.then(async () => {
|
|
267
|
+
var t;
|
|
268
|
+
let n = await this.statePromise;
|
|
269
|
+
e(n), await ((t = this.storage) == null ? void 0 : t.setItem(this.storageKey, JSON.stringify(n)));
|
|
270
|
+
});
|
|
271
|
+
return this.writeTail = t.catch(() => void 0), t;
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
function p() {
|
|
275
|
+
return {
|
|
276
|
+
schemaVersion: 1,
|
|
277
|
+
current: {},
|
|
278
|
+
lifetime: {},
|
|
279
|
+
recentSteps: [],
|
|
280
|
+
completedSteps: 0
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
function m(e) {
|
|
284
|
+
if (!e || typeof e != "object" || Array.isArray(e)) return p();
|
|
285
|
+
let t = e, n = Array.isArray(t.recentSteps) ? t.recentSteps.flatMap((e) => {
|
|
286
|
+
if (!e || typeof e != "object" || Array.isArray(e)) return [];
|
|
287
|
+
let t = e;
|
|
288
|
+
return [{
|
|
289
|
+
...x(t.stepId) ? { stepId: x(t.stepId) } : {},
|
|
290
|
+
behaviors: h(t.behaviors)
|
|
291
|
+
}];
|
|
292
|
+
}) : [];
|
|
293
|
+
return {
|
|
294
|
+
schemaVersion: 1,
|
|
295
|
+
current: h(t.current),
|
|
296
|
+
lifetime: h(t.lifetime),
|
|
297
|
+
recentSteps: n,
|
|
298
|
+
completedSteps: typeof t.completedSteps == "number" && Number.isFinite(t.completedSteps) && t.completedSteps >= 0 ? Math.floor(t.completedSteps) : 0
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
function h(e) {
|
|
302
|
+
if (!e || typeof e != "object" || Array.isArray(e)) return {};
|
|
303
|
+
let t = {};
|
|
304
|
+
for (let [n, r] of Object.entries(e)) n && typeof r == "number" && Number.isFinite(r) && r > 0 && (t[n] = r);
|
|
305
|
+
return t;
|
|
306
|
+
}
|
|
307
|
+
function g(e) {
|
|
308
|
+
let t = {};
|
|
309
|
+
for (let r of e) {
|
|
310
|
+
var n;
|
|
311
|
+
for (let [e, i] of Object.entries(r.behaviors)) t[e] = ((n = t[e]) == null ? 0 : n) + i;
|
|
312
|
+
}
|
|
313
|
+
return t;
|
|
314
|
+
}
|
|
315
|
+
function _(e) {
|
|
316
|
+
let t = x(e);
|
|
317
|
+
if (!t) throw Error("DDA behavior type is required");
|
|
318
|
+
if (t.length > 128) throw Error("DDA behavior type must be at most 128 characters");
|
|
319
|
+
return t;
|
|
320
|
+
}
|
|
321
|
+
function v(e) {
|
|
322
|
+
if (e === void 0) return d;
|
|
323
|
+
if (!Number.isInteger(e) || e < 1 || e > 100) throw Error("DDA recentWindowSize must be an integer between 1 and 100");
|
|
324
|
+
return e;
|
|
325
|
+
}
|
|
326
|
+
function y(e) {
|
|
327
|
+
if (!e || typeof e != "object" || Array.isArray(e)) return;
|
|
328
|
+
let t = e.adjustment;
|
|
329
|
+
return t === "increase" || t === "keep" || t === "decrease" ? t : void 0;
|
|
330
|
+
}
|
|
331
|
+
function b(e, t) {
|
|
332
|
+
return {
|
|
333
|
+
adjustment: "keep",
|
|
334
|
+
payload: { adjustment: "keep" },
|
|
335
|
+
diagnostics: {
|
|
336
|
+
fallback: !0,
|
|
337
|
+
reason: e
|
|
338
|
+
},
|
|
339
|
+
...t ? { assignment: t.assignment } : {},
|
|
340
|
+
isFallback: !0
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
function x(e) {
|
|
344
|
+
return typeof e == "string" && e.trim() ? e.trim() : void 0;
|
|
345
|
+
}
|
|
346
|
+
//#endregion
|
|
347
|
+
//#region ../rest-api/src/event-queue-store.ts
|
|
348
|
+
var S = "gamealgo-event-chunks-v2", C = class {
|
|
349
|
+
constructor(e, n) {
|
|
350
|
+
u(this, "prefix", void 0), u(this, "chunks", []), u(this, "locations", /* @__PURE__ */ new Map()), u(this, "contexts", /* @__PURE__ */ new Map()), u(this, "dirty", /* @__PURE__ */ new Set()), u(this, "revision", 0), u(this, "sequence", 0), u(this, "reloadAfterFailure", !1), u(this, "storage", void 0), u(this, "key", void 0);
|
|
351
|
+
let r = e;
|
|
352
|
+
this.storage = r.setItemDurably ? {
|
|
353
|
+
getItem: (t) => {
|
|
354
|
+
var n;
|
|
355
|
+
return ((n = r.getItemDurably) == null ? e.getItem : n).call(e, t);
|
|
356
|
+
},
|
|
357
|
+
setItem: (t, n) => r.setItemDurably.call(e, t, n),
|
|
358
|
+
removeItem: (t) => {
|
|
359
|
+
var n, i;
|
|
360
|
+
return (n = (i = r.removeItemDurably) == null ? e.removeItem : i) == null ? void 0 : n.call(e, t);
|
|
361
|
+
}
|
|
362
|
+
} : e, this.key = n, this.prefix = `gamealgo:queue-chunk:${t(n)}`;
|
|
363
|
+
}
|
|
364
|
+
reset() {
|
|
365
|
+
this.chunks = [], this.locations.clear(), this.contexts.clear(), this.dirty.clear(), this.revision = 0, this.sequence = 0, this.reloadAfterFailure = !1;
|
|
366
|
+
}
|
|
367
|
+
async load(e) {
|
|
368
|
+
var t, n;
|
|
369
|
+
let r = e == null ? await this.storage.getItem(this.key) : e;
|
|
370
|
+
if (!r) return [];
|
|
371
|
+
let i;
|
|
372
|
+
try {
|
|
373
|
+
i = JSON.parse(r);
|
|
374
|
+
} catch {}
|
|
375
|
+
if ((i == null ? void 0 : i.format) !== S) {
|
|
376
|
+
let e = r.split("\n").filter(Boolean).map((e) => JSON.parse(e)).filter((e) => e.eventId && e.userId && e.sessionId && e.eventType);
|
|
377
|
+
for (let t of e) this.upsert(t);
|
|
378
|
+
return e;
|
|
379
|
+
}
|
|
380
|
+
this.revision = (t = i.revision) == null ? 0 : t;
|
|
381
|
+
let a = [];
|
|
382
|
+
for (let e of (n = i.chunks) == null ? [] : n) {
|
|
383
|
+
let t = this.chunks.length, n = {
|
|
384
|
+
ref: e,
|
|
385
|
+
bank: Number(e.key.slice(-1))
|
|
386
|
+
};
|
|
387
|
+
this.chunks.push(n);
|
|
388
|
+
try {
|
|
389
|
+
var o;
|
|
390
|
+
let r = JSON.parse((o = await this.storage.getItem(e.key)) == null ? "null" : o);
|
|
391
|
+
if ((r == null ? void 0 : r.revision) !== e.revision || !Array.isArray(r.rows)) continue;
|
|
392
|
+
let i = /* @__PURE__ */ new Map(), s = r.rows.map((e) => {
|
|
393
|
+
let t = JSON.parse(e.event);
|
|
394
|
+
if (!t.eventId || !t.sessionId) throw Error("invalid stored event");
|
|
395
|
+
return {
|
|
396
|
+
row: e,
|
|
397
|
+
event: t,
|
|
398
|
+
id: t.eventId
|
|
399
|
+
};
|
|
400
|
+
});
|
|
401
|
+
for (let { row: e, event: n, id: r } of s) i.set(r, e), a.push({
|
|
402
|
+
sequence: e.sequence,
|
|
403
|
+
event: n
|
|
404
|
+
}), this.locations.set(r, t), this.contexts.set(r, n.contextId), this.sequence = Math.max(this.sequence, e.sequence);
|
|
405
|
+
n.rows = i;
|
|
406
|
+
} catch {}
|
|
407
|
+
}
|
|
408
|
+
return a.sort((e, t) => e.sequence - t.sequence).map((e) => e.event);
|
|
409
|
+
}
|
|
410
|
+
upsert(e) {
|
|
411
|
+
var t, n;
|
|
412
|
+
let r = e.eventId, i = this.locations.get(r);
|
|
413
|
+
if (i !== void 0 && this.contexts.get(r) === e.contextId) return;
|
|
414
|
+
let a = i;
|
|
415
|
+
a === void 0 && (a = this.chunks.findIndex((e) => e.rows !== void 0 && e.rows.size < 100), a < 0 && (a = this.chunks.length, this.chunks.push({
|
|
416
|
+
rows: /* @__PURE__ */ new Map(),
|
|
417
|
+
bank: 1
|
|
418
|
+
})));
|
|
419
|
+
let o = this.chunks[a].rows;
|
|
420
|
+
o.set(r, {
|
|
421
|
+
sequence: (t = (n = o.get(r)) == null ? void 0 : n.sequence) == null ? ++this.sequence : t,
|
|
422
|
+
event: JSON.stringify(e)
|
|
423
|
+
}), this.locations.set(r, a), this.contexts.set(r, e.contextId), this.dirty.add(a);
|
|
424
|
+
}
|
|
425
|
+
async save(e) {
|
|
426
|
+
try {
|
|
427
|
+
return this.reloadAfterFailure && (this.reset(), await this.load()), await this.saveCurrent(e);
|
|
428
|
+
} catch (e) {
|
|
429
|
+
throw this.reloadAfterFailure = !0, e;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
async saveCurrent(e) {
|
|
433
|
+
let t = new Set(e.map((e) => e.eventId));
|
|
434
|
+
for (let [e, n] of this.locations) t.has(e) || (this.chunks[n].rows.delete(e), this.locations.delete(e), this.contexts.delete(e), this.dirty.add(n));
|
|
435
|
+
for (let t of e) this.upsert(t);
|
|
436
|
+
if (this.chunks.length === 0) return !1;
|
|
437
|
+
if (this.locations.size === 0 && this.chunks.every((e) => e.rows !== void 0)) {
|
|
438
|
+
this.storage.removeItem ? await this.storage.removeItem(this.key) : await this.storage.setItem(this.key, "");
|
|
439
|
+
for (let e = 0; e < this.chunks.length; e++) for (let t of [0, 1]) {
|
|
440
|
+
var n, r;
|
|
441
|
+
await ((n = (r = this.storage).removeItem) == null ? void 0 : n.call(r, `${this.prefix}:${e}:${t}`));
|
|
442
|
+
}
|
|
443
|
+
return this.chunks = [], this.dirty.clear(), !1;
|
|
444
|
+
}
|
|
445
|
+
if (this.dirty.size === 0) return !0;
|
|
446
|
+
let i = ++this.revision, a = this.chunks.map((e) => e.ref);
|
|
447
|
+
for (let e of this.dirty) {
|
|
448
|
+
let t = this.chunks[e], n = `${this.prefix}:${e}:${1 - t.bank}`;
|
|
449
|
+
await this.storage.setItem(n, JSON.stringify({
|
|
450
|
+
revision: i,
|
|
451
|
+
rows: [...t.rows.values()]
|
|
452
|
+
})), a[e] = {
|
|
453
|
+
key: n,
|
|
454
|
+
revision: i
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
await this.storage.setItem(this.key, JSON.stringify({
|
|
458
|
+
format: S,
|
|
459
|
+
revision: i,
|
|
460
|
+
chunks: a
|
|
461
|
+
}));
|
|
462
|
+
for (let e of this.dirty) this.chunks[e].ref = a[e], this.chunks[e].bank = Number(a[e].key.slice(-1));
|
|
463
|
+
return this.dirty.clear(), !0;
|
|
464
|
+
}
|
|
465
|
+
}, w = /* @__PURE__ */ new Set([
|
|
466
|
+
"session_end",
|
|
467
|
+
"level_start",
|
|
468
|
+
"level_end",
|
|
469
|
+
"ad_view",
|
|
470
|
+
"purchase",
|
|
471
|
+
"milestone"
|
|
472
|
+
]), T = class extends Error {
|
|
473
|
+
constructor(e, t, n) {
|
|
474
|
+
super(t), u(this, "status", void 0), u(this, "code", void 0), this.status = e, this.code = n;
|
|
475
|
+
}
|
|
476
|
+
}, E = class {
|
|
477
|
+
constructor(e) {
|
|
478
|
+
var n, r, a, o, s, c, l, d, f, p, m, h, g, _;
|
|
479
|
+
if (u(this, "baseUrl", void 0), u(this, "gameKey", void 0), u(this, "sdkVersion", void 0), u(this, "appVersion", void 0), u(this, "accountUserId", void 0), u(this, "accountUserCreatedAt", void 0), u(this, "experimentIntegrationVersion", void 0), u(this, "platform", void 0), u(this, "timezone", void 0), u(this, "isDebug", void 0), u(this, "fetchImpl", void 0), u(this, "now", void 0), u(this, "storage", void 0), u(this, "scriptRuntime", void 0), u(this, "logger", void 0), u(this, "snapshotCacheKey", void 0), u(this, "legacySnapshotCacheKey", void 0), u(this, "attributionAckCacheKey", void 0), u(this, "userIdKey", void 0), u(this, "userCreatedAtKey", void 0), u(this, "userCreatedLocalAtKey", void 0), u(this, "cachedConfig", null), u(this, "snapshot", {
|
|
480
|
+
configFiles: /* @__PURE__ */ new Map(),
|
|
481
|
+
updatedAt: 0
|
|
482
|
+
}), u(this, "currentIdentity", null), u(this, "readyPromise", null), u(this, "didLogUserId", !1), u(this, "didMigrateLegacyIdentity", !1), u(this, "ddaControllers", /* @__PURE__ */ new Map()), u(this, "config", void 0), u(this, "tracker", void 0), !e.baseUrl) throw Error("baseUrl is required");
|
|
483
|
+
if (!e.gameKey) throw Error("gameKey is required");
|
|
484
|
+
if (this.baseUrl = i(e.baseUrl), this.gameKey = e.gameKey, this.sdkVersion = (n = e.sdkVersion) == null ? "1.0.0" : n, this.appVersion = e.appVersion, this.accountUserId = P(e.accountUserId), this.accountUserCreatedAt = P(e.accountUserCreatedAt), this.experimentIntegrationVersion = F(e.experimentIntegrationVersion), this.platform = e.platform === "rest" || (r = e.platform) == null ? "maker" : r, this.timezone = (a = P(e.timezone)) == null ? L() : a, this.isDebug = (o = e.isDebug) != null && o, this.fetchImpl = (s = e.fetchImpl) == null ? (c = globalThis.fetch) == null ? void 0 : c.bind(globalThis) : s, !this.fetchImpl) throw Error("An HTTP transport is required");
|
|
485
|
+
this.now = (l = e.now) == null ? Date.now : l, this.storage = e.storage, this.scriptRuntime = (d = e.scriptRuntime) == null ? { execute() {
|
|
486
|
+
throw Error("Remote scripts are unavailable in this host");
|
|
487
|
+
} } : d, this.logger = ie(e.logger);
|
|
488
|
+
let v = N(this.baseUrl), y = t(this.gameKey), b = (f = P(e.userId)) == null ? "anonymous" : f;
|
|
489
|
+
this.legacySnapshotCacheKey = `gamealgo:v1:snapshot:${v}:${this.gameKey.slice(0, 16)}`, this.snapshotCacheKey = (p = e.cacheKey) == null ? `gamealgo:v1:snapshot:${v}:${y}:${b}` : p, this.attributionAckCacheKey = `gamealgo:v1:attribution:${v}:${y}:${b}`, this.userIdKey = `gamealgo:v1:identity:${v}:${y}:user-id`, this.userCreatedAtKey = `gamealgo:v1:identity:${v}:${y}:created-at`, this.userCreatedLocalAtKey = `gamealgo:v1:identity:${v}:${y}:created-local-at`, this.config = new ee(() => this.snapshot), this.tracker = new D({
|
|
490
|
+
uploadEvents: (e) => this.uploadEvents(e),
|
|
491
|
+
reportDiagnostic: (e) => this.reportSdkDiagnostic(e),
|
|
492
|
+
platform: this.platform,
|
|
493
|
+
sdkVersion: this.sdkVersion,
|
|
494
|
+
appVersion: this.appVersion,
|
|
495
|
+
timezone: this.timezone,
|
|
496
|
+
isDebug: this.isDebug,
|
|
497
|
+
flushIntervalMs: (m = e.eventFlushIntervalMs) == null ? 3e4 : m,
|
|
498
|
+
maxBatchSize: (h = e.eventMaxBatchSize) == null ? 100 : h,
|
|
499
|
+
maxBatchBytes: e.eventMaxBatchBytes,
|
|
500
|
+
queueLimit: (g = e.eventQueueLimit) == null ? 1e3 : g,
|
|
501
|
+
persistOnEnqueue: (_ = e.eventPersistOnEnqueue) != null && _,
|
|
502
|
+
now: this.now,
|
|
503
|
+
storage: this.storage,
|
|
504
|
+
persistenceKey: `gamealgo:v1:event-queue:${v}:${y}:${b}`,
|
|
505
|
+
onAdIdentityState: (e, t) => {
|
|
506
|
+
try {
|
|
507
|
+
this.log(`ad_view identity ${e}: count=${t}`);
|
|
508
|
+
} catch {}
|
|
509
|
+
}
|
|
510
|
+
}), e.autoStart !== !1 && (this.readyPromise = this.initialize(e), this.readyPromise.catch(() => void 0));
|
|
511
|
+
}
|
|
512
|
+
async waitForReady(e = 5e3) {
|
|
513
|
+
if (!this.readyPromise) return this.snapshot.config !== void 0;
|
|
514
|
+
let t;
|
|
515
|
+
try {
|
|
516
|
+
return await Promise.race([this.readyPromise, new Promise((n, r) => {
|
|
517
|
+
t = setTimeout(() => r(/* @__PURE__ */ Error("GameAlgo ready timeout")), e);
|
|
518
|
+
})]), !0;
|
|
519
|
+
} catch {
|
|
520
|
+
return !1;
|
|
521
|
+
} finally {
|
|
522
|
+
t && clearTimeout(t);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
executor(e) {
|
|
526
|
+
return new O(e, () => this.snapshot, this.scriptRuntime, this.logger);
|
|
527
|
+
}
|
|
528
|
+
dda(e, n = {}) {
|
|
529
|
+
var r, i;
|
|
530
|
+
let a = P(e);
|
|
531
|
+
if (!a) throw Error("DDA strategy key is required");
|
|
532
|
+
let o = this.ddaControllers.get(a);
|
|
533
|
+
if (o) return o;
|
|
534
|
+
let s = (r = (i = this.currentIdentity) == null ? void 0 : i.userId) == null ? "anonymous" : r, c = `gamealgo:v1:dda:${N(this.baseUrl)}:${t(this.gameKey)}:${s}:${a}`, l = new f(this.executor(a), this.storage, c, n, this.logger);
|
|
535
|
+
return this.ddaControllers.set(a, l), l;
|
|
536
|
+
}
|
|
537
|
+
async userIdentity(e) {
|
|
538
|
+
var t, n, r, i;
|
|
539
|
+
await this.migrateLegacyIdentity();
|
|
540
|
+
let a = P(e);
|
|
541
|
+
if (a) {
|
|
542
|
+
var o, s, c, l, u, d, f;
|
|
543
|
+
if (((o = this.currentIdentity) == null ? void 0 : o.userId) === a) return this.currentIdentity;
|
|
544
|
+
let e = P(await ((s = this.storage) == null ? void 0 : s.getItem(this.userIdKey))), t = P(await ((c = this.storage) == null ? void 0 : c.getItem(this.userCreatedAtKey))), n = P(await ((l = this.storage) == null ? void 0 : l.getItem(this.userCreatedLocalAtKey))), r = e === a && t ? t : new Date(this.now()).toISOString();
|
|
545
|
+
return this.currentIdentity = {
|
|
546
|
+
userId: a,
|
|
547
|
+
userCreatedAt: r,
|
|
548
|
+
userCreatedLocalAt: e === a && n ? n : k(Date.parse(r))
|
|
549
|
+
}, await ((u = this.storage) == null ? void 0 : u.setItem(this.userIdKey, this.currentIdentity.userId)), await ((d = this.storage) == null ? void 0 : d.setItem(this.userCreatedAtKey, this.currentIdentity.userCreatedAt)), await ((f = this.storage) == null ? void 0 : f.setItem(this.userCreatedLocalAtKey, this.currentIdentity.userCreatedLocalAt)), this.currentIdentity;
|
|
550
|
+
}
|
|
551
|
+
if (this.currentIdentity) return this.currentIdentity;
|
|
552
|
+
let p = P(await ((t = this.storage) == null ? void 0 : t.getItem(this.userIdKey)));
|
|
553
|
+
if (p) {
|
|
554
|
+
var m, h, g, _, v, y;
|
|
555
|
+
let e = (m = P(await ((h = this.storage) == null ? void 0 : h.getItem(this.userCreatedAtKey)))) == null ? new Date(this.now()).toISOString() : m, t = (g = P(await ((_ = this.storage) == null ? void 0 : _.getItem(this.userCreatedLocalAtKey)))) == null ? k(Date.parse(e)) : g;
|
|
556
|
+
return this.currentIdentity = {
|
|
557
|
+
userId: p,
|
|
558
|
+
userCreatedAt: e,
|
|
559
|
+
userCreatedLocalAt: t
|
|
560
|
+
}, await ((v = this.storage) == null ? void 0 : v.setItem(this.userCreatedAtKey, e)), await ((y = this.storage) == null ? void 0 : y.setItem(this.userCreatedLocalAtKey, t)), this.currentIdentity;
|
|
561
|
+
}
|
|
562
|
+
return this.currentIdentity = {
|
|
563
|
+
userId: M(),
|
|
564
|
+
userCreatedAt: new Date(this.now()).toISOString(),
|
|
565
|
+
userCreatedLocalAt: k(this.now())
|
|
566
|
+
}, await ((n = this.storage) == null ? void 0 : n.setItem(this.userIdKey, this.currentIdentity.userId)), await ((r = this.storage) == null ? void 0 : r.setItem(this.userCreatedAtKey, this.currentIdentity.userCreatedAt)), await ((i = this.storage) == null ? void 0 : i.setItem(this.userCreatedLocalAtKey, this.currentIdentity.userCreatedLocalAt)), this.currentIdentity;
|
|
567
|
+
}
|
|
568
|
+
async configureAdjustServerCallbackParams(e, t) {
|
|
569
|
+
if (typeof e != "function") throw Error("callback parameter setter is required");
|
|
570
|
+
let n = await this.userIdentity(t);
|
|
571
|
+
await e("gamealgo_user_id", n.userId), await e("gamealgo_user_created_at", n.userCreatedAt);
|
|
572
|
+
}
|
|
573
|
+
defaultDeviceContext() {
|
|
574
|
+
return ae();
|
|
575
|
+
}
|
|
576
|
+
onConfigReady(e, t) {}
|
|
577
|
+
async fetchConfig(e = {}) {
|
|
578
|
+
var t, n, r, i, a, o, s, c, l, u, d, f;
|
|
579
|
+
let p = await this.userIdentity(e.userId), m = (t = P(e.userCreatedAt)) == null ? p.userCreatedAt : t, h = (n = P(e.userCreatedLocalAt)) == null ? P(e.userCreatedAt) ? k(Date.parse(m)) : p.userCreatedLocalAt : n, g = k(this.now()), _ = (r = P(e.accountUserId)) == null ? this.accountUserId : r, v = (i = P(e.accountUserCreatedAt)) == null ? this.accountUserCreatedAt : i;
|
|
580
|
+
this.logUserId(p.userId), this.tracker.identify(p.userId, e.sessionId, m, _);
|
|
581
|
+
let y = (a = e.platform) == null ? this.platform : a, b = (o = e.sdkVersion) == null ? this.sdkVersion : o, x = (s = e.appVersion) == null ? this.appVersion : s, S = F((c = e.experimentIntegrationVersion) == null ? this.experimentIntegrationVersion : c), C = (l = P(e.sessionId)) == null ? this.tracker.currentSessionId() : l, w = (u = e.isDebug) == null ? this.isDebug : u, T = {
|
|
582
|
+
...this.defaultDeviceContext(),
|
|
583
|
+
...(d = e.device) == null ? {} : d,
|
|
584
|
+
...e.deviceId ? { deviceId: e.deviceId } : {}
|
|
585
|
+
}, E = JSON.stringify({
|
|
586
|
+
userId: p.userId,
|
|
587
|
+
userCreatedAt: m,
|
|
588
|
+
userCreatedLocalAt: h,
|
|
589
|
+
accountUserId: _,
|
|
590
|
+
accountUserCreatedAt: v,
|
|
591
|
+
sessionId: C,
|
|
592
|
+
platform: y,
|
|
593
|
+
sdkVersion: b,
|
|
594
|
+
appVersion: x,
|
|
595
|
+
experimentIntegrationVersion: S,
|
|
596
|
+
deviceId: e.deviceId,
|
|
597
|
+
timezone: (f = e.timezone) == null ? this.timezone : f,
|
|
598
|
+
device: T,
|
|
599
|
+
isDebug: w
|
|
600
|
+
});
|
|
601
|
+
if (!e.forceRefresh && this.cachedConfig && this.cachedConfig.cacheKey === E && this.cachedConfig.expiresAt > this.now()) return this.log(`config cache hit: ${this.cachedConfig.value.configVersion}`), this.cachedConfig.value;
|
|
602
|
+
this.log(`fetching config: userId=${p.userId}, platform=${y}`);
|
|
603
|
+
try {
|
|
604
|
+
var D;
|
|
605
|
+
let t = await this.requestJson(this.url("/v1/config"), {
|
|
606
|
+
method: "POST",
|
|
607
|
+
headers: { "content-type": "application/json" },
|
|
608
|
+
body: JSON.stringify({
|
|
609
|
+
userId: p.userId,
|
|
610
|
+
userCreatedAt: m,
|
|
611
|
+
userCreatedLocalAt: h,
|
|
612
|
+
accountUserId: _,
|
|
613
|
+
accountUserCreatedAt: v,
|
|
614
|
+
createdLocalAt: g,
|
|
615
|
+
sessionId: C,
|
|
616
|
+
platform: y,
|
|
617
|
+
sdkVersion: b,
|
|
618
|
+
appVersion: x,
|
|
619
|
+
experimentIntegrationVersion: S,
|
|
620
|
+
timezone: (D = e.timezone) == null ? this.timezone : D,
|
|
621
|
+
device: T,
|
|
622
|
+
isDebug: w
|
|
623
|
+
})
|
|
624
|
+
});
|
|
625
|
+
if (this.tracker.bindSessionContext(C, t.contextId), J(this.platform) && P(t.contextId)) try {
|
|
626
|
+
this.log("context ready");
|
|
627
|
+
} catch {}
|
|
628
|
+
return C === this.tracker.currentSessionId() ? (this.cachedConfig = {
|
|
629
|
+
value: t,
|
|
630
|
+
cacheKey: E,
|
|
631
|
+
expiresAt: this.now() + Math.max(Number(t.ttlSeconds) || 0, 0) * 1e3
|
|
632
|
+
}, this.tracker.setEventQuota(t.eventQuota), this.tracker.setAssignments(t.experiments), this.snapshot = {
|
|
633
|
+
...this.snapshot,
|
|
634
|
+
config: t,
|
|
635
|
+
updatedAt: this.now(),
|
|
636
|
+
userId: p.userId
|
|
637
|
+
}, this.onConfigReady(t, {
|
|
638
|
+
userId: p.userId,
|
|
639
|
+
sessionId: C,
|
|
640
|
+
isDebug: w
|
|
641
|
+
}), await this.persistSnapshot(), this.log(`config fetched: version=${t.configVersion}, experiments=${t.experiments.length}, configFiles=${t.configFiles.length}, ttl=${t.ttlSeconds}s`), this.logAssignments(t.experiments, "config ready"), t) : t;
|
|
642
|
+
} catch (e) {
|
|
643
|
+
var O;
|
|
644
|
+
if (((O = this.cachedConfig) == null ? void 0 : O.cacheKey) === E) return this.log(`config fetch failed, using cached config: ${I(e)}`), this.cachedConfig.value;
|
|
645
|
+
throw this.log(`config fetch failed: ${I(e)}`), e;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
async fetchConfigFile(e) {
|
|
649
|
+
var t, n;
|
|
650
|
+
let r = ne(e), i = await this.request(this.url(`/v1/config-files/${encodeURIComponent(r)}`), { method: "GET" }), a = {
|
|
651
|
+
name: r,
|
|
652
|
+
content: await i.text(),
|
|
653
|
+
contentType: (t = i.headers.get("content-type")) == null ? "application/octet-stream" : t,
|
|
654
|
+
etag: (n = i.headers.get("etag")) == null ? void 0 : n
|
|
655
|
+
}, o = new Map(this.snapshot.configFiles);
|
|
656
|
+
return o.set(r, a), this.snapshot = {
|
|
657
|
+
...this.snapshot,
|
|
658
|
+
configFiles: o,
|
|
659
|
+
updatedAt: this.now()
|
|
660
|
+
}, await this.persistSnapshot(), this.log(`config file loaded: ${a.name} (${a.contentType})`), a;
|
|
661
|
+
}
|
|
662
|
+
async fetchScriptFile(e) {
|
|
663
|
+
var t, n, r, i, o;
|
|
664
|
+
let s = P(e.versionId);
|
|
665
|
+
if (!s) throw Error(`script versionId is required: ${e.name}`);
|
|
666
|
+
if (!P(e.url)) throw Error(`script url is required: ${e.name}@${s}`);
|
|
667
|
+
if (!/^sha256:[a-f0-9]{64}$/i.test(e.hash)) throw Error(`script hash is invalid: ${e.name}@${s}`);
|
|
668
|
+
let c = a(e.url, this.baseUrl), l = await this.request(c, { method: "GET" }), u = await l.text();
|
|
669
|
+
await V(u, e.hash), await ((t = (n = this.scriptRuntime).prepare) == null ? void 0 : t.call(n, u));
|
|
670
|
+
let d = {
|
|
671
|
+
name: j(e),
|
|
672
|
+
content: u,
|
|
673
|
+
contentType: (r = (i = l.headers.get("content-type")) == null ? e.contentType : i) == null ? "application/octet-stream" : r,
|
|
674
|
+
etag: (o = l.headers.get("etag")) == null ? void 0 : o
|
|
675
|
+
}, f = new Map(this.snapshot.configFiles);
|
|
676
|
+
return f.set(d.name, d), this.snapshot = {
|
|
677
|
+
...this.snapshot,
|
|
678
|
+
configFiles: f,
|
|
679
|
+
updatedAt: this.now()
|
|
680
|
+
}, await this.persistSnapshot(), this.log(`script loaded: ${e.name}${e.versionId ? ` (${e.versionId})` : ""}`), d;
|
|
681
|
+
}
|
|
682
|
+
async uploadEvents(e) {
|
|
683
|
+
if (!Array.isArray(e) || e.length === 0) throw Error("events must be a non-empty array");
|
|
684
|
+
if (e.length > 100) throw Error("Maximum 100 events per batch");
|
|
685
|
+
let t = e.map((e) => {
|
|
686
|
+
var t, n, r, i;
|
|
687
|
+
return {
|
|
688
|
+
...e,
|
|
689
|
+
eventId: (t = e.eventId) == null ? M() : t,
|
|
690
|
+
isDebug: !!e.isDebug,
|
|
691
|
+
timestamp: (n = e.timestamp) == null ? new Date(this.now()).toISOString() : n,
|
|
692
|
+
createdLocalAt: (r = e.createdLocalAt) == null ? k(e.timestamp ? Date.parse(e.timestamp) : this.now()) : r,
|
|
693
|
+
payload: z((i = e.payload) == null ? {} : i)
|
|
694
|
+
};
|
|
695
|
+
}), n = await this.requestJson(this.url("/v1/events/batch"), {
|
|
696
|
+
method: "POST",
|
|
697
|
+
headers: { "content-type": "application/json" },
|
|
698
|
+
body: JSON.stringify({ events: t })
|
|
699
|
+
});
|
|
700
|
+
if (t.some((e) => e.eventType === "ad_view")) {
|
|
701
|
+
let e = n.stored;
|
|
702
|
+
try {
|
|
703
|
+
var r, i;
|
|
704
|
+
this.log(`ad_view upload ack: accepted=${n.accepted} stored=${typeof e == "number" ? e : "unknown"} rejected=${(r = (i = n.rejected) == null ? void 0 : i.length) == null ? 0 : r}`);
|
|
705
|
+
} catch {}
|
|
706
|
+
}
|
|
707
|
+
return n;
|
|
708
|
+
}
|
|
709
|
+
async reportSdkDiagnostic(e) {
|
|
710
|
+
await this.requestJson(this.url("/v1/diagnostics/sdk"), {
|
|
711
|
+
method: "POST",
|
|
712
|
+
headers: { "content-type": "application/json" },
|
|
713
|
+
body: JSON.stringify(e)
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
async setAttribution(e) {
|
|
717
|
+
var t, n, r, i, a, o, s;
|
|
718
|
+
let c = P(e.provider);
|
|
719
|
+
if (!c) throw Error("provider is required");
|
|
720
|
+
let l = await this.userIdentity(e.userId), u = (t = e.platform) == null ? this.platform : t, d = ce(e.attribution), f = le(c, P(e.status), d), p = P(e.attributedAt), m = (n = P(e.attributionHash)) == null ? await H(q({
|
|
721
|
+
platform: u,
|
|
722
|
+
provider: c,
|
|
723
|
+
status: f,
|
|
724
|
+
attribution: d,
|
|
725
|
+
attributedAt: p == null ? "" : p
|
|
726
|
+
})) : n, h = `${this.attributionAckCacheKey}:${c}`;
|
|
727
|
+
if (m && await ((r = this.storage) == null ? void 0 : r.getItem(h)) === m) return this.log(`attribution already synced: provider=${c}`), {
|
|
728
|
+
ok: !0,
|
|
729
|
+
accepted: 0,
|
|
730
|
+
attributionHash: m
|
|
731
|
+
};
|
|
732
|
+
let g = await this.requestJson(this.url("/v1/attribution"), {
|
|
733
|
+
method: "POST",
|
|
734
|
+
headers: { "content-type": "application/json" },
|
|
735
|
+
body: JSON.stringify({
|
|
736
|
+
userId: l.userId,
|
|
737
|
+
userCreatedAt: (i = P(e.userCreatedAt)) == null ? l.userCreatedAt : i,
|
|
738
|
+
sessionId: (a = P(e.sessionId)) == null ? this.tracker.currentSessionId() : a,
|
|
739
|
+
contextId: (o = P(e.contextId)) == null ? (s = this.snapshot.config) == null ? void 0 : s.contextId : o,
|
|
740
|
+
platform: u,
|
|
741
|
+
provider: c,
|
|
742
|
+
status: f,
|
|
743
|
+
attribution: d,
|
|
744
|
+
attributedAt: p,
|
|
745
|
+
attributionHash: m
|
|
746
|
+
})
|
|
747
|
+
});
|
|
748
|
+
if (g.attributionHash) {
|
|
749
|
+
var _;
|
|
750
|
+
await ((_ = this.storage) == null ? void 0 : _.setItem(h, g.attributionHash));
|
|
751
|
+
}
|
|
752
|
+
return this.log(`attribution synced: provider=${c}, accepted=${g.accepted}`), g;
|
|
753
|
+
}
|
|
754
|
+
async setAdjustAdid(e, t) {
|
|
755
|
+
return this.setContextIdentifier("adjust_adid", e, t);
|
|
756
|
+
}
|
|
757
|
+
async setFirebaseAppInstanceId(e, t) {
|
|
758
|
+
return this.setContextIdentifier("firebase_app_instance_id", e, t);
|
|
759
|
+
}
|
|
760
|
+
async setGoogleAdvertisingId(e, t) {
|
|
761
|
+
return this.setContextIdentifier("gaid", e, t);
|
|
762
|
+
}
|
|
763
|
+
async setIdfa(e, t) {
|
|
764
|
+
return this.setContextIdentifier("idfa", e, t);
|
|
765
|
+
}
|
|
766
|
+
async setIdfv(e, t) {
|
|
767
|
+
return this.setContextIdentifier("idfv", e, t);
|
|
768
|
+
}
|
|
769
|
+
async setContextIdentifier(e, t, n) {
|
|
770
|
+
var r, i;
|
|
771
|
+
!this.snapshot.config && this.readyPromise && await this.readyPromise;
|
|
772
|
+
let a = P((r = this.snapshot.config) == null ? void 0 : r.contextId);
|
|
773
|
+
if (!a) throw Error("config context is not ready");
|
|
774
|
+
let o = await this.userIdentity(), s = re(e, t), c = await H(JSON.stringify({
|
|
775
|
+
identifierType: e,
|
|
776
|
+
identifierValue: s
|
|
777
|
+
})), l = await this.requestJson(this.url("/v1/context-identifiers"), {
|
|
778
|
+
method: "POST",
|
|
779
|
+
headers: { "content-type": "application/json" },
|
|
780
|
+
body: JSON.stringify({
|
|
781
|
+
userId: o.userId,
|
|
782
|
+
sessionId: this.tracker.currentSessionId(),
|
|
783
|
+
contextId: a,
|
|
784
|
+
platform: this.platform,
|
|
785
|
+
identifierType: e,
|
|
786
|
+
identifierValue: s,
|
|
787
|
+
observedAt: (i = P(n)) == null ? new Date(this.now()).toISOString() : i,
|
|
788
|
+
identifierHash: c
|
|
789
|
+
})
|
|
790
|
+
});
|
|
791
|
+
return this.log(`context identifier synced: type=${e}, accepted=${l.accepted}`), l;
|
|
792
|
+
}
|
|
793
|
+
clearConfigCache() {
|
|
794
|
+
this.cachedConfig = null;
|
|
795
|
+
}
|
|
796
|
+
snapshotValue() {
|
|
797
|
+
return {
|
|
798
|
+
config: this.snapshot.config,
|
|
799
|
+
configFiles: new Map(this.snapshot.configFiles),
|
|
800
|
+
updatedAt: this.snapshot.updatedAt
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
async initialize(e) {
|
|
804
|
+
let t = "identity";
|
|
805
|
+
try {
|
|
806
|
+
var n, r;
|
|
807
|
+
let i = this.now();
|
|
808
|
+
if (J(this.platform)) try {
|
|
809
|
+
this.log("identity restore started");
|
|
810
|
+
} catch {}
|
|
811
|
+
let a = await this.userIdentity(e.userId), o = (n = P(e.userCreatedAt)) == null ? a.userCreatedAt : n;
|
|
812
|
+
if (this.logUserId(a.userId), this.tracker.identify(a.userId, e.sessionId, o, (r = P(e.accountUserId)) == null ? this.accountUserId : r), J(this.platform)) try {
|
|
813
|
+
this.log(`identity ready: elapsedMs=${Math.max(0, this.now() - i)}`);
|
|
814
|
+
} catch {}
|
|
815
|
+
this.tracker.markSessionStarted(), t = "event-queue-restore", await this.tracker.waitForRestore(), t = "snapshot-restore", await this.loadPersistedSnapshot();
|
|
816
|
+
try {
|
|
817
|
+
t = "config", await this.refresh({
|
|
818
|
+
...e,
|
|
819
|
+
userId: a.userId,
|
|
820
|
+
userCreatedAt: o,
|
|
821
|
+
forceRefresh: !0
|
|
822
|
+
});
|
|
823
|
+
} catch (e) {
|
|
824
|
+
if (!this.snapshot.config) throw this.log(`config fetch failed: ${I(e)}`), e;
|
|
825
|
+
this.log(`config fetch failed, using cached snapshot: ${I(e)}`);
|
|
826
|
+
}
|
|
827
|
+
} catch (e) {
|
|
828
|
+
if (J(this.platform)) try {
|
|
829
|
+
this.log(`initialization failed: stage=${t} errorType=${e instanceof Error ? e.name : typeof e}`);
|
|
830
|
+
} catch {}
|
|
831
|
+
throw e;
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
async refresh(e) {
|
|
835
|
+
var t;
|
|
836
|
+
let n = await this.fetchConfig(e), r = (t = e.preloadConfigFiles) == null || t;
|
|
837
|
+
if (!r) {
|
|
838
|
+
this.log("config file preload skipped");
|
|
839
|
+
return;
|
|
840
|
+
}
|
|
841
|
+
let i = Array.isArray(r) ? r : n.configFiles.map((e) => e.name), a = Array.isArray(r) ? [] : [...new Map(n.experiments.flatMap((e) => e.script ? [[j(e.script), e.script]] : [])).values()], o = [.../* @__PURE__ */ new Set([...i, ...a.map((e) => e.versionId ? `${e.name}@${e.versionId}` : e.name)])];
|
|
842
|
+
o.length === 0 ? this.log("no config files to preload") : this.log(`preloading config files: ${o.sort().join(", ")}`), await Promise.all([...[...new Set(i)].map((e) => this.fetchConfigFile(e)), ...a.map((e) => this.fetchScriptFile(e))]);
|
|
843
|
+
for (let e of n.experiments) {
|
|
844
|
+
let t = e.script;
|
|
845
|
+
t && this.snapshot.configFiles.has(j(t)) && this.log(`script ready: ${e.key} -> ${t.name}${t.versionId ? ` (${t.versionId})` : ""}`);
|
|
846
|
+
}
|
|
847
|
+
o.length > 0 && this.log("all config files loaded"), this.tracker.setAssignments(n.experiments), this.logAssignments(n.experiments, "experiment");
|
|
848
|
+
}
|
|
849
|
+
async loadPersistedSnapshot() {
|
|
850
|
+
if (!this.storage) return;
|
|
851
|
+
let e = await this.storage.getItem(this.snapshotCacheKey);
|
|
852
|
+
if (!e && this.legacySnapshotCacheKey !== this.snapshotCacheKey && (e = await this.storage.getItem(this.legacySnapshotCacheKey), e)) {
|
|
853
|
+
var t, n;
|
|
854
|
+
await this.storage.setItem(this.snapshotCacheKey, e), await ((t = (n = this.storage).removeItem) == null ? void 0 : t.call(n, this.legacySnapshotCacheKey));
|
|
855
|
+
}
|
|
856
|
+
if (e) try {
|
|
857
|
+
var r;
|
|
858
|
+
let t = JSON.parse(e);
|
|
859
|
+
this.snapshot = {
|
|
860
|
+
config: t.config,
|
|
861
|
+
configFiles: new Map(((r = t.configFiles) == null ? [] : r).map((e) => [e.name, e])),
|
|
862
|
+
updatedAt: Number(t.updatedAt || 0),
|
|
863
|
+
userId: t.userId
|
|
864
|
+
}, this.log("cached snapshot loaded");
|
|
865
|
+
} catch {
|
|
866
|
+
var i, a;
|
|
867
|
+
await ((i = (a = this.storage).removeItem) == null ? void 0 : i.call(a, this.snapshotCacheKey));
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
async persistSnapshot() {
|
|
871
|
+
this.storage && await this.storage.setItem(this.snapshotCacheKey, JSON.stringify({
|
|
872
|
+
config: this.snapshot.config,
|
|
873
|
+
configFiles: [...this.snapshot.configFiles.values()],
|
|
874
|
+
updatedAt: this.snapshot.updatedAt,
|
|
875
|
+
userId: this.snapshot.userId
|
|
876
|
+
}));
|
|
877
|
+
}
|
|
878
|
+
async migrateLegacyIdentity() {
|
|
879
|
+
var e, t, n, r, i, a;
|
|
880
|
+
if (this.didMigrateLegacyIdentity || !this.storage || (this.didMigrateLegacyIdentity = !0, P(await this.storage.getItem(this.userIdKey)))) return;
|
|
881
|
+
let o = P(await this.storage.getItem("gamealgo_user_id"));
|
|
882
|
+
if (!o) return;
|
|
883
|
+
let s = P(await this.storage.getItem("gamealgo_user_created_at")), c = P(await this.storage.getItem("gamealgo_user_created_local_at"));
|
|
884
|
+
await this.storage.setItem(this.userIdKey, o), s && await this.storage.setItem(this.userCreatedAtKey, s), c && await this.storage.setItem(this.userCreatedLocalAtKey, c), await ((e = (t = this.storage).removeItem) == null ? void 0 : e.call(t, "gamealgo_user_id")), await ((n = (r = this.storage).removeItem) == null ? void 0 : n.call(r, "gamealgo_user_created_at")), await ((i = (a = this.storage).removeItem) == null ? void 0 : i.call(a, "gamealgo_user_created_local_at"));
|
|
885
|
+
}
|
|
886
|
+
url(e) {
|
|
887
|
+
return `${N(this.baseUrl)}/${e.replace(/^\/+/, "")}`;
|
|
888
|
+
}
|
|
889
|
+
async requestJson(e, t) {
|
|
890
|
+
return (await this.request(e, t)).json();
|
|
891
|
+
}
|
|
892
|
+
async request(e, t) {
|
|
893
|
+
let n = await this.fetchImpl(e, {
|
|
894
|
+
...t,
|
|
895
|
+
headers: {
|
|
896
|
+
...t.headers,
|
|
897
|
+
"X-GameAlgo-Key": this.gameKey
|
|
898
|
+
}
|
|
899
|
+
});
|
|
900
|
+
if (!n.ok) throw await te(n);
|
|
901
|
+
return n;
|
|
902
|
+
}
|
|
903
|
+
logUserId(e) {
|
|
904
|
+
this.didLogUserId || (this.didLogUserId = !0, this.log(`userId: ${e}`));
|
|
905
|
+
}
|
|
906
|
+
logAssignments(e, t) {
|
|
907
|
+
for (let n of e) this.log(`${t}: ${n.key} -> ${n.variant}`);
|
|
908
|
+
}
|
|
909
|
+
log(e) {
|
|
910
|
+
var t;
|
|
911
|
+
(t = this.logger) == null || t.call(this, `[GameAlgoSDK] ${e}`);
|
|
912
|
+
}
|
|
913
|
+
}, D = class {
|
|
914
|
+
constructor(e) {
|
|
915
|
+
var t, n;
|
|
916
|
+
if (u(this, "uploadEvents", void 0), u(this, "reportDiagnostic", void 0), u(this, "platform", void 0), u(this, "sdkVersion", void 0), u(this, "appVersion", void 0), u(this, "maxBatchSize", void 0), u(this, "maxBatchBytes", void 0), u(this, "queueLimit", void 0), u(this, "persistOnEnqueue", void 0), u(this, "flushIntervalMs", void 0), u(this, "now", void 0), u(this, "storage", void 0), u(this, "persistenceKey", void 0), u(this, "milestonePersistenceKey", void 0), u(this, "milestoneRestorePromise", void 0), u(this, "restorePromise", void 0), u(this, "userId", void 0), u(this, "sessionId", M()), u(this, "contextId", void 0), u(this, "timezone", void 0), u(this, "userCreatedAt", void 0), u(this, "accountUserId", void 0), u(this, "isDebug", void 0), u(this, "currentExperiments", {}), u(this, "queue", []), u(this, "retryBatch", []), u(this, "inflightBatch", []), u(this, "urgentEventIds", /* @__PURE__ */ new Set()), u(this, "urgentFlushes", /* @__PURE__ */ new Map()), u(this, "pendingIdentityAds", []), u(this, "timersStopped", !1), u(this, "onAdIdentityState", void 0), u(this, "restored", !1), u(this, "flushTimer", void 0), u(this, "activeFlush", void 0), u(this, "sessionStartMs", void 0), u(this, "sessionDurationTimer", void 0), u(this, "sampledForegroundTimeMs", 0), u(this, "sessionForegroundStartMs", 0), u(this, "sessionDurationSampleAt", 0), u(this, "sessionDurationVisible", void 0), u(this, "sessionDurationWasVisible", !0), u(this, "sessionHasEnd", !1), u(this, "consecutiveFailures", 0), u(this, "hasPersistedQueue", !1), u(this, "queueRestorePending", !1), u(this, "incrementalQueue", void 0), u(this, "queuePersistActive", void 0), u(this, "queuePersistRevision", 0), u(this, "customCounts", /* @__PURE__ */ new Map()), u(this, "ordinaryCounts", /* @__PURE__ */ new Map()), u(this, "eventQuota", void 0), u(this, "diagnosticKeys", /* @__PURE__ */ new Set()), u(this, "diagnosticCount", 0), u(this, "reachedMilestoneKeys", /* @__PURE__ */ new Set()), u(this, "pendingMilestoneKeys", /* @__PURE__ */ new Set()), u(this, "milestonePersistPromise", Promise.resolve()), u(this, "queuePersistPromise", Promise.resolve()), this.uploadEvents = e.uploadEvents, this.reportDiagnostic = e.reportDiagnostic, this.platform = e.platform, this.sdkVersion = e.sdkVersion, this.appVersion = e.appVersion, this.timezone = (t = P(e.timezone)) == null ? L() : t, this.isDebug = e.isDebug, this.flushIntervalMs = e.flushIntervalMs, this.maxBatchSize = Math.max(1, Math.min(e.maxBatchSize, 100)), this.maxBatchBytes = Number.isFinite(e.maxBatchBytes) && Number(e.maxBatchBytes) > 0 ? Math.floor(Number(e.maxBatchBytes)) : void 0, this.queueLimit = Math.max(e.queueLimit, this.maxBatchSize), this.persistOnEnqueue = (n = e.persistOnEnqueue) != null && n, this.now = e.now, this.onAdIdentityState = e.onAdIdentityState, this.sessionStartMs = this.now(), this.sessionDurationSampleAt = this.sessionStartMs, J(this.platform)) {
|
|
917
|
+
var r, i;
|
|
918
|
+
this.sessionDurationTimer = setInterval(() => this.sampleSessionDuration(), 1e3), (r = (i = this.sessionDurationTimer).unref) == null || r.call(i);
|
|
919
|
+
}
|
|
920
|
+
this.storage = e.storage, this.persistenceKey = P(e.persistenceKey), this.persistOnEnqueue && this.storage && this.persistenceKey && (this.incrementalQueue = new C(this.storage, this.persistenceKey)), this.milestonePersistenceKey = this.persistenceKey ? `${this.persistenceKey}:milestones` : void 0, this.milestoneRestorePromise = this.restorePersistedMilestones(), this.restorePromise = Promise.all([this.restorePersistedQueue(), this.milestoneRestorePromise]).then(() => {
|
|
921
|
+
this.restored = !0;
|
|
922
|
+
});
|
|
923
|
+
}
|
|
924
|
+
identify(e, t, n, r) {
|
|
925
|
+
if (P(e) && (this.userId = e), P(t) && t !== this.sessionId && (this.sessionId = t, this.sessionHasEnd = !1, this.sessionStartMs = this.now(), this.resetSessionDuration(this.sessionStartMs)), P(n) && (this.userCreatedAt = n), P(r) && (this.accountUserId = r), P(this.userId)) {
|
|
926
|
+
let e = this.pendingIdentityAds.splice(0);
|
|
927
|
+
if (e.length) try {
|
|
928
|
+
var i;
|
|
929
|
+
(i = this.onAdIdentityState) == null || i.call(this, "released", e.length);
|
|
930
|
+
} catch {}
|
|
931
|
+
for (let t of e) this.track("ad_view", t.payload, t.options);
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
newSession(e = M()) {
|
|
935
|
+
var t;
|
|
936
|
+
if (e = (t = P(e)) == null ? M() : t, e === this.sessionId) return;
|
|
937
|
+
let n = this.sessionId, r = this.now(), i = P(this.userId);
|
|
938
|
+
if (i && !this.sessionHasEnd) {
|
|
939
|
+
var a;
|
|
940
|
+
this.enqueue({
|
|
941
|
+
eventId: M(),
|
|
942
|
+
contextId: (a = this.contextId) == null ? "" : a,
|
|
943
|
+
userId: i,
|
|
944
|
+
sessionId: n,
|
|
945
|
+
eventType: "session_end",
|
|
946
|
+
isDebug: this.isDebug,
|
|
947
|
+
timestamp: new Date(r).toISOString(),
|
|
948
|
+
createdLocalAt: k(r),
|
|
949
|
+
accountUserId: this.accountUserId,
|
|
950
|
+
payload: {
|
|
951
|
+
reason: "new_session",
|
|
952
|
+
sessionDurationMs: this.sessionDuration(r)
|
|
953
|
+
}
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
this.sessionId = e, this.sessionHasEnd = !1, this.ordinaryCounts.delete(`pending:${n}`), this.diagnosticKeys.clear(), this.diagnosticCount = 0, this.pendingMilestoneKeys.clear(), this.contextId = void 0, this.eventQuota = void 0, this.sessionStartMs = r, this.resetSessionDuration(r), this.hasPersistedQueue = !0, this.persistPendingQueue().catch(() => void 0), J(this.platform) && this.flushUrgent().catch(() => void 0), this.flush().catch(() => void 0);
|
|
957
|
+
}
|
|
958
|
+
currentSessionId() {
|
|
959
|
+
return this.sessionId;
|
|
960
|
+
}
|
|
961
|
+
async waitForRestore() {
|
|
962
|
+
await this.restorePromise;
|
|
963
|
+
}
|
|
964
|
+
setContextId(e) {
|
|
965
|
+
this.bindSessionContext(this.sessionId, e);
|
|
966
|
+
}
|
|
967
|
+
bindSessionContext(e, t) {
|
|
968
|
+
let n = P(t);
|
|
969
|
+
if (e === this.sessionId && (this.contextId = n), !n) return;
|
|
970
|
+
this.mergeCustomCountBucket(`pending:${e}`, `context:${n}`), this.mergeOrdinaryCountBucket(`pending:${e}`, `context:${n}`);
|
|
971
|
+
let r = (t) => !P(t.contextId) && t.sessionId === e ? {
|
|
972
|
+
...t,
|
|
973
|
+
contextId: n
|
|
974
|
+
} : t;
|
|
975
|
+
this.retryBatch = this.retryBatch.map(r), this.queue = this.queue.map(r), this.rememberBoundMilestones(n), this.hasPersistedQueue && this.persistPendingQueue().catch(() => void 0), J(this.platform) && this.flushUrgent().catch(() => void 0), [...this.retryBatch, ...this.queue].some((t) => t.sessionId === e && t.eventType === "session_end") && this.flush().catch(() => void 0);
|
|
976
|
+
}
|
|
977
|
+
setDebug(e) {
|
|
978
|
+
this.isDebug = e;
|
|
979
|
+
}
|
|
980
|
+
setTimezone(e) {
|
|
981
|
+
var t;
|
|
982
|
+
this.timezone = (t = P(e)) == null ? L() : t;
|
|
983
|
+
}
|
|
984
|
+
setAssignments(e) {
|
|
985
|
+
this.currentExperiments = {};
|
|
986
|
+
for (let t of e) this.currentExperiments[t.key] = t.variant;
|
|
987
|
+
}
|
|
988
|
+
setEventQuota(e) {
|
|
989
|
+
let t = Math.floor(Number(e == null ? void 0 : e.ordinaryEventLimit));
|
|
990
|
+
this.eventQuota = (e == null ? void 0 : e.mode) === "enforce" && Number.isSafeInteger(t) && t >= 0 ? {
|
|
991
|
+
...e,
|
|
992
|
+
ordinaryEventLimit: t,
|
|
993
|
+
exemptEventTypes: Array.isArray(e.exemptEventTypes) ? e.exemptEventTypes.filter((e) => typeof e == "string" && e.length > 0) : []
|
|
994
|
+
} : void 0;
|
|
995
|
+
}
|
|
996
|
+
markSessionStarted() {
|
|
997
|
+
this.sessionStartMs = this.now(), this.resetSessionDuration(this.sessionStartMs);
|
|
998
|
+
}
|
|
999
|
+
getForegroundTimeMs() {
|
|
1000
|
+
return Math.round(this.sampledForegroundTimeMs);
|
|
1001
|
+
}
|
|
1002
|
+
setSessionDurationVisibility(e) {
|
|
1003
|
+
this.sessionDurationVisible = e, this.sessionDurationWasVisible = e();
|
|
1004
|
+
}
|
|
1005
|
+
observeSessionDurationVisibility() {
|
|
1006
|
+
J(this.platform) && this.sampleSessionDuration();
|
|
1007
|
+
}
|
|
1008
|
+
resetSessionDuration(e) {
|
|
1009
|
+
J(this.platform) && this.sampleSessionDuration(e), this.sessionForegroundStartMs = this.getForegroundTimeMs();
|
|
1010
|
+
}
|
|
1011
|
+
sampleSessionDuration(e = this.now()) {
|
|
1012
|
+
var t, n;
|
|
1013
|
+
if (!Number.isFinite(e)) return;
|
|
1014
|
+
let r = (t = (n = this.sessionDurationVisible) == null ? void 0 : n.call(this)) == null || t;
|
|
1015
|
+
this.sessionDurationWasVisible && (this.sampledForegroundTimeMs += Math.min(1e3, Math.max(0, e - this.sessionDurationSampleAt))), this.sessionDurationWasVisible = r, this.sessionDurationSampleAt = e;
|
|
1016
|
+
}
|
|
1017
|
+
sessionDuration(e) {
|
|
1018
|
+
var t;
|
|
1019
|
+
return J(this.platform) ? (this.sampleSessionDuration(e), this.getForegroundTimeMs() - this.sessionForegroundStartMs) : Math.max(0, e - ((t = this.sessionStartMs) == null ? e : t));
|
|
1020
|
+
}
|
|
1021
|
+
track(e, t = {}, n = {}) {
|
|
1022
|
+
var r, i, a, o, s, c;
|
|
1023
|
+
let l = P((r = n.userId) == null ? this.userId : r);
|
|
1024
|
+
if (!l) {
|
|
1025
|
+
var u, d, f, p;
|
|
1026
|
+
if (!J(this.platform) || e !== "ad_view" || n.userId !== void 0) return !1;
|
|
1027
|
+
let r = (u = n.timestamp) == null ? new Date(this.now()).toISOString() : u;
|
|
1028
|
+
this.pendingIdentityAds.push({
|
|
1029
|
+
payload: z(t),
|
|
1030
|
+
options: {
|
|
1031
|
+
...n,
|
|
1032
|
+
sessionId: (d = P(n.sessionId)) == null ? this.sessionId : d,
|
|
1033
|
+
isDebug: (f = n.isDebug) == null ? this.isDebug : f,
|
|
1034
|
+
timestamp: r,
|
|
1035
|
+
createdLocalAt: (p = n.createdLocalAt) == null ? k(Date.parse(r)) : p
|
|
1036
|
+
}
|
|
1037
|
+
}), this.pendingIdentityAds.length > this.queueLimit && this.pendingIdentityAds.shift();
|
|
1038
|
+
try {
|
|
1039
|
+
var m;
|
|
1040
|
+
(m = this.onAdIdentityState) == null || m.call(this, "buffered", this.pendingIdentityAds.length);
|
|
1041
|
+
} catch {}
|
|
1042
|
+
return !0;
|
|
1043
|
+
}
|
|
1044
|
+
let h = (i = P(n.sessionId)) == null ? this.sessionId : i, g = P((a = n.contextId) == null ? h === this.sessionId ? this.contextId : void 0 : a), _ = n.timestamp ? Date.parse(n.timestamp) : this.now(), v = (o = n.isDebug) == null ? this.isDebug : o, y = z(t), b = e === "milestone" ? this.prepareMilestone(l, g, v, y, _) : void 0;
|
|
1045
|
+
return b != null && b.duplicate || !this.consumeOrdinaryEventQuota(e, g, h, !1) || !this.consumeCustomEventQuota(e, g, h) ? !1 : (this.consumeOrdinaryEventQuota(e, g, h, !0), this.enqueue({
|
|
1046
|
+
eventId: M(),
|
|
1047
|
+
contextId: g == null ? "" : g,
|
|
1048
|
+
userId: l,
|
|
1049
|
+
sessionId: h,
|
|
1050
|
+
eventType: e,
|
|
1051
|
+
isDebug: v,
|
|
1052
|
+
timestamp: (s = n.timestamp) == null ? new Date(this.now()).toISOString() : s,
|
|
1053
|
+
createdLocalAt: (c = n.createdLocalAt) == null ? k(n.timestamp ? Date.parse(n.timestamp) : this.now()) : c,
|
|
1054
|
+
accountUserId: this.accountUserId,
|
|
1055
|
+
payload: y
|
|
1056
|
+
}), b != null && b.key && this.rememberMilestone(b.key, b.durable), J(this.platform) && e === "ad_view" ? this.flushUrgent().then(() => this.flush()).catch(() => void 0) : J(this.platform) && e === "session_end" && this.flushUrgent().catch(() => void 0), !0);
|
|
1057
|
+
}
|
|
1058
|
+
prepareMilestone(e, t, n, r, i) {
|
|
1059
|
+
let a = this.userCreatedAt ? Date.parse(this.userCreatedAt) : NaN;
|
|
1060
|
+
delete r.elapsedSinceRegistrationMs, Number.isFinite(i) && Number.isFinite(a) && (r.elapsedSinceRegistrationMs = Math.max(0, Math.floor(i - a)));
|
|
1061
|
+
let o = P(typeof r.milestoneType == "string" ? r.milestoneType : void 0), s = P(typeof r.milestonePoint == "string" ? r.milestonePoint : void 0);
|
|
1062
|
+
if (!o || !s) return {
|
|
1063
|
+
duplicate: !1,
|
|
1064
|
+
durable: !1
|
|
1065
|
+
};
|
|
1066
|
+
let c = JSON.stringify([
|
|
1067
|
+
n ? "debug" : "live",
|
|
1068
|
+
e,
|
|
1069
|
+
o,
|
|
1070
|
+
s
|
|
1071
|
+
]), l = !!t;
|
|
1072
|
+
return {
|
|
1073
|
+
duplicate: (l ? this.reachedMilestoneKeys : this.pendingMilestoneKeys).has(c),
|
|
1074
|
+
key: c,
|
|
1075
|
+
durable: l
|
|
1076
|
+
};
|
|
1077
|
+
}
|
|
1078
|
+
rememberMilestone(e, t) {
|
|
1079
|
+
if (!t) {
|
|
1080
|
+
this.pendingMilestoneKeys.add(e);
|
|
1081
|
+
return;
|
|
1082
|
+
}
|
|
1083
|
+
this.reachedMilestoneKeys.has(e) || (this.reachedMilestoneKeys.add(e), this.persistReachedMilestones());
|
|
1084
|
+
}
|
|
1085
|
+
rememberBoundMilestones(e) {
|
|
1086
|
+
let t = !1;
|
|
1087
|
+
for (let i of [...this.retryBatch, ...this.queue]) {
|
|
1088
|
+
var n, r;
|
|
1089
|
+
if (i.contextId !== e || i.eventType !== "milestone") continue;
|
|
1090
|
+
let a = P(typeof ((n = i.payload) == null ? void 0 : n.milestoneType) == "string" ? i.payload.milestoneType : void 0), o = P(typeof ((r = i.payload) == null ? void 0 : r.milestonePoint) == "string" ? i.payload.milestonePoint : void 0);
|
|
1091
|
+
if (!a || !o) continue;
|
|
1092
|
+
let s = JSON.stringify([
|
|
1093
|
+
i.isDebug ? "debug" : "live",
|
|
1094
|
+
i.userId,
|
|
1095
|
+
a,
|
|
1096
|
+
o
|
|
1097
|
+
]);
|
|
1098
|
+
this.reachedMilestoneKeys.has(s) || (this.reachedMilestoneKeys.add(s), t = !0);
|
|
1099
|
+
}
|
|
1100
|
+
t && this.persistReachedMilestones();
|
|
1101
|
+
}
|
|
1102
|
+
consumeCustomEventQuota(e, t, n) {
|
|
1103
|
+
var r, i;
|
|
1104
|
+
if (w.has(e)) return !0;
|
|
1105
|
+
let a = t ? `context:${t}` : `pending:${n}`, o = (r = this.customCounts.get(a)) == null ? {
|
|
1106
|
+
total: 0,
|
|
1107
|
+
byType: /* @__PURE__ */ new Map()
|
|
1108
|
+
} : r, s = (i = o.byType.get(e)) == null ? 0 : i, c, l = 0;
|
|
1109
|
+
if (!o.byType.has(e) && o.byType.size >= 100 ? (c = "distinct_event_types", l = 100) : s >= 1e3 ? (c = "context_event_type", l = 1e3) : o.total >= 5e3 && (c = "context_total", l = 5e3), c) {
|
|
1110
|
+
let r = c === "context_total" ? o.total + 1 : c === "distinct_event_types" ? o.byType.size + 1 : s + 1;
|
|
1111
|
+
return this.reportQuotaDiagnostic(e, n, t, c, l, r), !1;
|
|
1112
|
+
}
|
|
1113
|
+
return o.total += 1, o.byType.set(e, s + 1), this.customCounts.set(a, o), !0;
|
|
1114
|
+
}
|
|
1115
|
+
mergeCustomCountBucket(e, t) {
|
|
1116
|
+
var n, r;
|
|
1117
|
+
let i = this.customCounts.get(e);
|
|
1118
|
+
if (!i) return;
|
|
1119
|
+
let a = (n = this.customCounts.get(t)) == null ? {
|
|
1120
|
+
total: 0,
|
|
1121
|
+
byType: /* @__PURE__ */ new Map()
|
|
1122
|
+
} : n;
|
|
1123
|
+
a.total += i.total;
|
|
1124
|
+
for (let [e, t] of i.byType) a.byType.set(e, ((r = a.byType.get(e)) == null ? 0 : r) + t);
|
|
1125
|
+
this.customCounts.set(t, a), this.customCounts.delete(e);
|
|
1126
|
+
}
|
|
1127
|
+
consumeOrdinaryEventQuota(e, t, n, r) {
|
|
1128
|
+
var i;
|
|
1129
|
+
let a = this.eventQuota;
|
|
1130
|
+
if (e === "purchase" || e === "ad_view" || a != null && a.exemptEventTypes.includes(e)) return !0;
|
|
1131
|
+
let o = t ? `context:${t}` : `pending:${n}`, s = (i = this.ordinaryCounts.get(o)) == null ? 0 : i;
|
|
1132
|
+
return a && s >= a.ordinaryEventLimit ? (this.reportContextQuotaDiagnostic(e, n, t, a, s + 1), !1) : (r && this.ordinaryCounts.set(o, s + 1), !0);
|
|
1133
|
+
}
|
|
1134
|
+
mergeOrdinaryCountBucket(e, t) {
|
|
1135
|
+
var n;
|
|
1136
|
+
let r = this.ordinaryCounts.get(e);
|
|
1137
|
+
r !== void 0 && (this.ordinaryCounts.set(t, ((n = this.ordinaryCounts.get(t)) == null ? 0 : n) + r), this.ordinaryCounts.delete(e));
|
|
1138
|
+
}
|
|
1139
|
+
reportContextQuotaDiagnostic(e, t, n, r, i) {
|
|
1140
|
+
let a = `${n == null ? `pending:${t}` : n}\u0000context_event_quota`;
|
|
1141
|
+
if (!this.reportDiagnostic || this.diagnosticKeys.has(a) || this.diagnosticCount >= 10) return;
|
|
1142
|
+
this.diagnosticKeys.add(a), this.diagnosticCount += 1;
|
|
1143
|
+
let o = new Date(this.now()).toISOString(), s = e.replace(/[;\r\n]/g, "_").slice(0, 96);
|
|
1144
|
+
this.reportDiagnostic({
|
|
1145
|
+
diagnosticId: M(),
|
|
1146
|
+
userId: this.userId,
|
|
1147
|
+
sessionId: t,
|
|
1148
|
+
contextId: n,
|
|
1149
|
+
platform: this.platform,
|
|
1150
|
+
sdkVersion: this.sdkVersion,
|
|
1151
|
+
appVersion: this.appVersion,
|
|
1152
|
+
stage: "event_guard",
|
|
1153
|
+
status: "degraded",
|
|
1154
|
+
reasonCode: "context_event_quota_exceeded",
|
|
1155
|
+
reasonDetail: `eventType=${s};scope=context_total;limit=${r.ordinaryEventLimit};observed=${i};policyVersion=${r.policyVersion};quotaDate=${r.quotaDate};dropped=1`,
|
|
1156
|
+
createdAt: o,
|
|
1157
|
+
createdLocalAt: k(this.now()),
|
|
1158
|
+
isDebug: this.isDebug
|
|
1159
|
+
}).catch(() => void 0);
|
|
1160
|
+
}
|
|
1161
|
+
reportQuotaDiagnostic(e, t, n, r, i, a) {
|
|
1162
|
+
let o = `${t}\u0000${e}\u0000${r}`;
|
|
1163
|
+
if (!this.reportDiagnostic || this.diagnosticKeys.has(o) || this.diagnosticCount >= 10) return;
|
|
1164
|
+
this.diagnosticKeys.add(o), this.diagnosticCount += 1;
|
|
1165
|
+
let s = new Date(this.now()).toISOString(), c = e.replace(/[;\r\n]/g, "_").slice(0, 96);
|
|
1166
|
+
this.reportDiagnostic({
|
|
1167
|
+
diagnosticId: M(),
|
|
1168
|
+
userId: this.userId,
|
|
1169
|
+
sessionId: t,
|
|
1170
|
+
contextId: n,
|
|
1171
|
+
platform: this.platform,
|
|
1172
|
+
sdkVersion: this.sdkVersion,
|
|
1173
|
+
appVersion: this.appVersion,
|
|
1174
|
+
stage: "event_guard",
|
|
1175
|
+
status: "degraded",
|
|
1176
|
+
reasonCode: "custom_event_quota_exceeded",
|
|
1177
|
+
reasonDetail: `eventType=${c};scope=${r};limit=${i};observed=${a};dropped=1`,
|
|
1178
|
+
createdAt: s,
|
|
1179
|
+
createdLocalAt: k(this.now()),
|
|
1180
|
+
isDebug: this.isDebug
|
|
1181
|
+
}).catch(() => void 0);
|
|
1182
|
+
}
|
|
1183
|
+
trackEvent(e, t = {}, n = {}) {
|
|
1184
|
+
return this.track(e.startsWith("_") ? e : `_${e}`, t, n);
|
|
1185
|
+
}
|
|
1186
|
+
trackSessionEnd(e = {}) {
|
|
1187
|
+
let t = R(e);
|
|
1188
|
+
return this.sessionStartMs !== void 0 && (t.sessionDurationMs = this.sessionDuration(this.now())), this.track("session_end", t);
|
|
1189
|
+
}
|
|
1190
|
+
trackSessionEndSnapshot(e = {}) {
|
|
1191
|
+
var t;
|
|
1192
|
+
let n = P(this.userId);
|
|
1193
|
+
if (!n) return !1;
|
|
1194
|
+
let r = this.now(), i = R(e);
|
|
1195
|
+
return i.sessionDurationMs = this.sessionDuration(r), this.hasPersistedQueue = !0, this.enqueue({
|
|
1196
|
+
eventId: M(),
|
|
1197
|
+
contextId: (t = this.contextId) == null ? "" : t,
|
|
1198
|
+
userId: n,
|
|
1199
|
+
sessionId: this.sessionId,
|
|
1200
|
+
eventType: "session_end",
|
|
1201
|
+
isDebug: this.isDebug,
|
|
1202
|
+
timestamp: new Date(r).toISOString(),
|
|
1203
|
+
createdLocalAt: k(r),
|
|
1204
|
+
accountUserId: this.accountUserId,
|
|
1205
|
+
payload: z(i)
|
|
1206
|
+
}, !0), this.persistOnEnqueue || this.persistPendingQueue().catch(() => void 0), J(this.platform) && this.flushUrgent().catch(() => void 0), !0;
|
|
1207
|
+
}
|
|
1208
|
+
trackLevelStart(e = {}) {
|
|
1209
|
+
return this.track("level_start", e);
|
|
1210
|
+
}
|
|
1211
|
+
trackLevelEnd(e = {}) {
|
|
1212
|
+
return this.track("level_end", e);
|
|
1213
|
+
}
|
|
1214
|
+
trackMilestone(e, t, n = {}) {
|
|
1215
|
+
let r = R(n);
|
|
1216
|
+
return r.milestoneType = e, r.milestonePoint = t, this.track("milestone", r);
|
|
1217
|
+
}
|
|
1218
|
+
trackAdPlacement(e, t, n, r = {}) {
|
|
1219
|
+
if (typeof e != "string" || e.length !== 36 || !/^gap_[a-f0-9]{32}$/.test(e) || !Number.isFinite(t)) return !1;
|
|
1220
|
+
let i = typeof n == "string" ? n : void 0, a = R(typeof n == "string" || n == null ? r : n);
|
|
1221
|
+
return delete a.placement, delete a.adType, delete a.currency, a.adPlacementId = e, a.revenue = t, i && (a.network = i), this.track("ad_view", a);
|
|
1222
|
+
}
|
|
1223
|
+
trackPurchase(e, t, n, r = {}) {
|
|
1224
|
+
let i = R(r);
|
|
1225
|
+
return e && (i.productId = e), t !== void 0 && (i.revenue = t), n && (i.currency = n), this.track("purchase", i);
|
|
1226
|
+
}
|
|
1227
|
+
flush() {
|
|
1228
|
+
if (this.activeFlush) return this.activeFlush;
|
|
1229
|
+
let e = this.drain();
|
|
1230
|
+
return this.activeFlush = e, e.finally(() => {
|
|
1231
|
+
this.activeFlush === e && (this.activeFlush = void 0);
|
|
1232
|
+
}).catch(() => void 0), e;
|
|
1233
|
+
}
|
|
1234
|
+
async drain() {
|
|
1235
|
+
await this.restorePromise, await this.milestonePersistPromise, await this.queuePersistPromise, this.queueRestorePending && await this.persistPendingQueue().catch(() => void 0);
|
|
1236
|
+
let e = [];
|
|
1237
|
+
for (; this.retryBatch.length > 0 || this.queue.length > 0;) {
|
|
1238
|
+
let t = [...this.retryBatch, ...this.queue], n = t.filter((e) => !this.urgentEventIds.has(e.eventId) && P(e.contextId)), r = new Set(n.map((e) => e.eventId)), i = t.filter((e) => !r.has(e.eventId));
|
|
1239
|
+
if (n.length === 0) return e;
|
|
1240
|
+
let a = B(n, this.maxBatchSize, this.maxBatchBytes);
|
|
1241
|
+
this.retryBatch = [], this.queue = [...i, ...n.slice(a.length)], this.inflightBatch = a;
|
|
1242
|
+
try {
|
|
1243
|
+
e.push(await this.uploadEvents(a)), this.inflightBatch = [], this.consecutiveFailures = 0, this.hasPersistedQueue && await this.persistPendingQueue().catch(() => void 0);
|
|
1244
|
+
} catch (e) {
|
|
1245
|
+
if (this.inflightBatch = [], this.retryBatch = [...a, ...this.retryBatch], this.consecutiveFailures += 1, this.hasPersistedQueue || this.consecutiveFailures >= 3) {
|
|
1246
|
+
this.hasPersistedQueue = !0;
|
|
1247
|
+
try {
|
|
1248
|
+
await this.persistPendingQueue();
|
|
1249
|
+
} catch {}
|
|
1250
|
+
}
|
|
1251
|
+
throw e;
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
return await this.queuePersistPromise, this.hasPersistedQueue && this.retryBatch.length === 0 && this.queue.length === 0 && await this.clearPersistedQueue().catch(() => void 0), e;
|
|
1255
|
+
}
|
|
1256
|
+
async flushUrgent() {
|
|
1257
|
+
let e = ["session_end", "ad_view"].map((e) => {
|
|
1258
|
+
let t = this.urgentFlushes.get(e);
|
|
1259
|
+
if (t) return t;
|
|
1260
|
+
if (!this.urgentCandidates(e).length) return Promise.resolve([]);
|
|
1261
|
+
let n = this.drainUrgent(e);
|
|
1262
|
+
return this.urgentFlushes.set(e, n), n;
|
|
1263
|
+
});
|
|
1264
|
+
return (await Promise.all(e)).flat();
|
|
1265
|
+
}
|
|
1266
|
+
urgentCandidates(e) {
|
|
1267
|
+
return [...this.retryBatch, ...this.queue].filter((t) => t.eventType === e && !this.urgentEventIds.has(t.eventId) && (P(t.contextId) || t.sessionId === this.sessionId && P(this.contextId)));
|
|
1268
|
+
}
|
|
1269
|
+
async drainUrgent(e) {
|
|
1270
|
+
let t = [];
|
|
1271
|
+
try {
|
|
1272
|
+
for (this.restored || await this.restorePromise;;) {
|
|
1273
|
+
let n = this.urgentCandidates(e);
|
|
1274
|
+
if (n.length === 0) return t;
|
|
1275
|
+
let r = B(n, this.maxBatchSize, this.maxBatchBytes), i = new Set(r.map((e) => e.eventId));
|
|
1276
|
+
for (let e of i) this.urgentEventIds.add(e);
|
|
1277
|
+
let a = r.map((e) => P(e.contextId) ? e : {
|
|
1278
|
+
...e,
|
|
1279
|
+
contextId: this.contextId
|
|
1280
|
+
});
|
|
1281
|
+
try {
|
|
1282
|
+
t.push(await this.uploadEvents(a)), this.retryBatch = this.retryBatch.filter((e) => !i.has(e.eventId)), this.queue = this.queue.filter((e) => !i.has(e.eventId)), this.hasPersistedQueue && this.persistPendingQueue().catch(() => void 0);
|
|
1283
|
+
} catch (e) {
|
|
1284
|
+
let t = new Set([
|
|
1285
|
+
...this.retryBatch,
|
|
1286
|
+
...this.queue,
|
|
1287
|
+
...this.inflightBatch
|
|
1288
|
+
].map((e) => e.eventId));
|
|
1289
|
+
throw this.retryBatch = [...a.filter((e) => !t.has(e.eventId)), ...this.retryBatch], this.hasPersistedQueue && this.persistPendingQueue().catch(() => void 0), e;
|
|
1290
|
+
} finally {
|
|
1291
|
+
for (let e of i) this.urgentEventIds.delete(e);
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
} finally {
|
|
1295
|
+
this.urgentFlushes.delete(e);
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
close() {
|
|
1299
|
+
this.timersStopped = !0, this.sessionDurationTimer && (clearInterval(this.sessionDurationTimer), this.sessionDurationTimer = void 0), this.flushTimer && (clearInterval(this.flushTimer), this.flushTimer = void 0);
|
|
1300
|
+
}
|
|
1301
|
+
enqueue(e, t = !1) {
|
|
1302
|
+
this.queue.push(e), !t && e.eventType === "session_end" && e.sessionId === this.sessionId && (this.sessionHasEnd = !0), this.queue.length > this.queueLimit && this.queue.splice(0, this.queue.length - this.queueLimit), this.persistOnEnqueue && (this.hasPersistedQueue = !0, this.persistPendingQueue().catch(() => void 0)), this.startTimer(), this.queue.length >= this.maxBatchSize && this.flush().catch(() => void 0);
|
|
1303
|
+
}
|
|
1304
|
+
startTimer() {
|
|
1305
|
+
var e, t;
|
|
1306
|
+
this.timersStopped || this.flushTimer || this.flushIntervalMs <= 0 || (this.flushTimer = setInterval(() => {
|
|
1307
|
+
this.flush().catch(() => void 0);
|
|
1308
|
+
}, this.flushIntervalMs), (e = (t = this.flushTimer).unref) == null || e.call(t));
|
|
1309
|
+
}
|
|
1310
|
+
async restorePersistedQueue() {
|
|
1311
|
+
if (!this.storage || !this.persistenceKey) return;
|
|
1312
|
+
let e = this.storage, t;
|
|
1313
|
+
try {
|
|
1314
|
+
var n;
|
|
1315
|
+
t = await ((n = e.getItemDurably) == null ? e.getItem : n).call(e, this.persistenceKey);
|
|
1316
|
+
} catch {
|
|
1317
|
+
this.queueRestorePending = !0, this.hasPersistedQueue = !0;
|
|
1318
|
+
return;
|
|
1319
|
+
}
|
|
1320
|
+
if (this.queueRestorePending = !1, t) {
|
|
1321
|
+
if (this.incrementalQueue) {
|
|
1322
|
+
let e;
|
|
1323
|
+
try {
|
|
1324
|
+
e = await this.incrementalQueue.load(t);
|
|
1325
|
+
} catch {
|
|
1326
|
+
var r, i;
|
|
1327
|
+
this.incrementalQueue.reset(), await ((r = (i = this.storage).removeItem) == null ? void 0 : r.call(i, this.persistenceKey));
|
|
1328
|
+
return;
|
|
1329
|
+
}
|
|
1330
|
+
let n = new Set([
|
|
1331
|
+
...this.retryBatch,
|
|
1332
|
+
...this.inflightBatch,
|
|
1333
|
+
...this.queue
|
|
1334
|
+
].map((e) => e.eventId));
|
|
1335
|
+
this.retryBatch = [...e.filter((e) => !n.has(e.eventId)), ...this.retryBatch], this.hasPersistedQueue = e.length > 0;
|
|
1336
|
+
return;
|
|
1337
|
+
}
|
|
1338
|
+
try {
|
|
1339
|
+
if (t.includes("\"format\":\"gamealgo-event-chunks-v2\"")) {
|
|
1340
|
+
this.incrementalQueue = new C(this.storage, this.persistenceKey);
|
|
1341
|
+
let e = await this.incrementalQueue.load(t), n = new Set([
|
|
1342
|
+
...this.retryBatch,
|
|
1343
|
+
...this.inflightBatch,
|
|
1344
|
+
...this.queue
|
|
1345
|
+
].map((e) => e.eventId));
|
|
1346
|
+
this.retryBatch = [...e.filter((e) => !n.has(e.eventId)), ...this.retryBatch], this.hasPersistedQueue = e.length > 0;
|
|
1347
|
+
return;
|
|
1348
|
+
}
|
|
1349
|
+
let e = t.split("\n").map((e) => e.trim()).filter(Boolean).map((e) => JSON.parse(e)).filter((e) => P(e.eventId) && P(e.userId) && P(e.sessionId) && P(e.eventType)), n = new Set([
|
|
1350
|
+
...this.retryBatch,
|
|
1351
|
+
...this.inflightBatch,
|
|
1352
|
+
...this.queue
|
|
1353
|
+
].map((e) => e.eventId));
|
|
1354
|
+
this.retryBatch = [...e.filter((e) => !n.has(e.eventId)), ...this.retryBatch], this.hasPersistedQueue = e.length > 0;
|
|
1355
|
+
} catch {
|
|
1356
|
+
var a, o;
|
|
1357
|
+
await ((a = (o = this.storage).removeItem) == null ? void 0 : a.call(o, this.persistenceKey));
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
async restorePersistedMilestones() {
|
|
1362
|
+
if (!this.storage || !this.milestonePersistenceKey) return;
|
|
1363
|
+
let e = await this.storage.getItem(this.milestonePersistenceKey);
|
|
1364
|
+
if (e) try {
|
|
1365
|
+
let t = JSON.parse(e);
|
|
1366
|
+
if (!Array.isArray(t)) throw Error("invalid milestone cache");
|
|
1367
|
+
for (let e of t) typeof e == "string" && e && this.reachedMilestoneKeys.add(e);
|
|
1368
|
+
} catch {
|
|
1369
|
+
var t, n;
|
|
1370
|
+
await ((t = (n = this.storage).removeItem) == null ? void 0 : t.call(n, this.milestonePersistenceKey));
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
persistReachedMilestones() {
|
|
1374
|
+
if (!this.storage || !this.milestonePersistenceKey) return Promise.resolve();
|
|
1375
|
+
let e = async () => {
|
|
1376
|
+
await this.milestoneRestorePromise, await this.storage.setItem(this.milestonePersistenceKey, JSON.stringify([...this.reachedMilestoneKeys].sort()));
|
|
1377
|
+
};
|
|
1378
|
+
return this.milestonePersistPromise = this.milestonePersistPromise.then(e, e), this.milestonePersistPromise;
|
|
1379
|
+
}
|
|
1380
|
+
persistPendingQueue() {
|
|
1381
|
+
if (!this.storage || !this.persistenceKey) return Promise.resolve();
|
|
1382
|
+
if (this.queuePersistRevision++, this.queuePersistActive) return this.queuePersistActive;
|
|
1383
|
+
let e = async () => {
|
|
1384
|
+
try {
|
|
1385
|
+
if (await this.restorePromise, this.queueRestorePending && (await this.restorePersistedQueue(), this.queueRestorePending)) throw Error("Event queue storage is unavailable");
|
|
1386
|
+
let n;
|
|
1387
|
+
do {
|
|
1388
|
+
n = this.queuePersistRevision;
|
|
1389
|
+
let r = [
|
|
1390
|
+
...this.retryBatch,
|
|
1391
|
+
...this.inflightBatch,
|
|
1392
|
+
...this.queue
|
|
1393
|
+
];
|
|
1394
|
+
if (this.incrementalQueue) this.hasPersistedQueue = await this.incrementalQueue.save(r);
|
|
1395
|
+
else if (r.length === 0) {
|
|
1396
|
+
var e, t;
|
|
1397
|
+
await ((e = (t = this.storage).removeItem) == null ? void 0 : e.call(t, this.persistenceKey)), this.hasPersistedQueue = !1;
|
|
1398
|
+
} else await this.storage.setItem(this.persistenceKey, r.map((e) => JSON.stringify(e)).join("\n")), this.hasPersistedQueue = !0;
|
|
1399
|
+
} while (n !== this.queuePersistRevision);
|
|
1400
|
+
} finally {
|
|
1401
|
+
this.queuePersistActive = void 0;
|
|
1402
|
+
}
|
|
1403
|
+
}, t = this.queuePersistPromise.then(e, e);
|
|
1404
|
+
return this.queuePersistActive = t, this.queuePersistPromise = t.catch(() => void 0), t;
|
|
1405
|
+
}
|
|
1406
|
+
async clearPersistedQueue() {
|
|
1407
|
+
if (!this.storage || !this.persistenceKey) {
|
|
1408
|
+
this.hasPersistedQueue = !1;
|
|
1409
|
+
return;
|
|
1410
|
+
}
|
|
1411
|
+
await this.persistPendingQueue();
|
|
1412
|
+
}
|
|
1413
|
+
}, O = class {
|
|
1414
|
+
constructor(e, t, n, r) {
|
|
1415
|
+
u(this, "key", void 0), u(this, "snapshotProvider", void 0), u(this, "scriptRuntime", void 0), u(this, "logger", void 0), this.key = e, this.snapshotProvider = t, this.scriptRuntime = n, this.logger = r;
|
|
1416
|
+
}
|
|
1417
|
+
get isReady() {
|
|
1418
|
+
return this.assignment() !== void 0;
|
|
1419
|
+
}
|
|
1420
|
+
assignment() {
|
|
1421
|
+
var e;
|
|
1422
|
+
return (e = this.snapshotProvider().config) == null ? void 0 : e.experiments.find((e) => e.key === this.key);
|
|
1423
|
+
}
|
|
1424
|
+
variant(e = "control") {
|
|
1425
|
+
var t, n;
|
|
1426
|
+
return (t = (n = this.assignment()) == null ? void 0 : n.variant) == null ? e : t;
|
|
1427
|
+
}
|
|
1428
|
+
config(e) {
|
|
1429
|
+
var t, n;
|
|
1430
|
+
return (t = (n = this.assignment()) == null ? void 0 : n.config) == null ? e : t;
|
|
1431
|
+
}
|
|
1432
|
+
value(e, t) {
|
|
1433
|
+
var n, r;
|
|
1434
|
+
let i = (n = this.assignment()) == null ? void 0 : n.config;
|
|
1435
|
+
return i === void 0 || (r = de(i, e)) == null ? t : r;
|
|
1436
|
+
}
|
|
1437
|
+
string(e, t = "") {
|
|
1438
|
+
let n = this.value(e, t);
|
|
1439
|
+
return typeof n == "string" ? n : t;
|
|
1440
|
+
}
|
|
1441
|
+
number(e, t = 0) {
|
|
1442
|
+
let n = this.value(e, t);
|
|
1443
|
+
return typeof n == "number" ? n : t;
|
|
1444
|
+
}
|
|
1445
|
+
bool(e, t = !1) {
|
|
1446
|
+
let n = this.value(e, t);
|
|
1447
|
+
return typeof n == "boolean" ? n : t;
|
|
1448
|
+
}
|
|
1449
|
+
async execute(e) {
|
|
1450
|
+
var t;
|
|
1451
|
+
let n = this.snapshotProvider(), r = this.assignment();
|
|
1452
|
+
if (!n.config || !r) {
|
|
1453
|
+
this.log(`execute skipped: ${this.key} is not ready`);
|
|
1454
|
+
return;
|
|
1455
|
+
}
|
|
1456
|
+
if (!r.script) return {
|
|
1457
|
+
payload: r.config,
|
|
1458
|
+
diagnostics: { mode: "config-only" },
|
|
1459
|
+
assignment: r
|
|
1460
|
+
};
|
|
1461
|
+
let i = n.configFiles.get(j(r.script));
|
|
1462
|
+
if (!i) {
|
|
1463
|
+
this.log(`execute skipped: script not loaded: ${r.key} -> ${r.script.name}${r.script.versionId ? ` (${r.script.versionId})` : ""}`);
|
|
1464
|
+
return;
|
|
1465
|
+
}
|
|
1466
|
+
try {
|
|
1467
|
+
await V(i.content, r.script.hash);
|
|
1468
|
+
} catch (e) {
|
|
1469
|
+
throw this.log(`execute skipped: script hash mismatch: ${r.key} -> ${r.script.name}`), e;
|
|
1470
|
+
}
|
|
1471
|
+
let a = {
|
|
1472
|
+
state: e,
|
|
1473
|
+
config: r.config,
|
|
1474
|
+
meta: {
|
|
1475
|
+
gameId: n.config.gameId,
|
|
1476
|
+
userId: (t = n.userId) == null ? "" : t,
|
|
1477
|
+
environment: n.config.environment,
|
|
1478
|
+
strategy: r.key,
|
|
1479
|
+
experimentId: r.experimentId,
|
|
1480
|
+
variant: r.variant
|
|
1481
|
+
}
|
|
1482
|
+
}, o = U(await this.scriptRuntime.execute(i.content, a));
|
|
1483
|
+
if (!o || typeof o != "object" || Array.isArray(o)) {
|
|
1484
|
+
this.log(`execute failed for ${r.key}: result must be an object`);
|
|
1485
|
+
return;
|
|
1486
|
+
}
|
|
1487
|
+
let s = o.payload, c = o.diagnostics;
|
|
1488
|
+
if (s === void 0) {
|
|
1489
|
+
this.log(`execute failed for ${r.key}: result must contain payload`);
|
|
1490
|
+
return;
|
|
1491
|
+
}
|
|
1492
|
+
return {
|
|
1493
|
+
payload: s,
|
|
1494
|
+
diagnostics: c == null ? {} : c,
|
|
1495
|
+
assignment: r
|
|
1496
|
+
};
|
|
1497
|
+
}
|
|
1498
|
+
log(e) {
|
|
1499
|
+
var t;
|
|
1500
|
+
(t = this.logger) == null || t.call(this, `[GameAlgoSDK] ${e}`);
|
|
1501
|
+
}
|
|
1502
|
+
}, ee = class {
|
|
1503
|
+
constructor(e) {
|
|
1504
|
+
u(this, "snapshotProvider", void 0), this.snapshotProvider = e;
|
|
1505
|
+
}
|
|
1506
|
+
file(e) {
|
|
1507
|
+
return this.snapshotProvider().configFiles.get(e);
|
|
1508
|
+
}
|
|
1509
|
+
jsonFile(e, t) {
|
|
1510
|
+
let n = this.file(e);
|
|
1511
|
+
if (!n) return t;
|
|
1512
|
+
try {
|
|
1513
|
+
return JSON.parse(n.content);
|
|
1514
|
+
} catch {
|
|
1515
|
+
return t;
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
value(e, t, n) {
|
|
1519
|
+
var r;
|
|
1520
|
+
let i = this.defaultJsonSource(n);
|
|
1521
|
+
return i === void 0 || (r = de(i, e)) == null ? t : r;
|
|
1522
|
+
}
|
|
1523
|
+
string(e, t = "", n) {
|
|
1524
|
+
let r = this.value(e, t, n);
|
|
1525
|
+
return typeof r == "string" ? r : t;
|
|
1526
|
+
}
|
|
1527
|
+
number(e, t = 0, n) {
|
|
1528
|
+
let r = this.value(e, t, n);
|
|
1529
|
+
return typeof r == "number" ? r : t;
|
|
1530
|
+
}
|
|
1531
|
+
bool(e, t = !1, n) {
|
|
1532
|
+
let r = this.value(e, t, n);
|
|
1533
|
+
return typeof r == "boolean" ? r : t;
|
|
1534
|
+
}
|
|
1535
|
+
defaultJsonSource(e) {
|
|
1536
|
+
if (e) return this.jsonFile(e, ue);
|
|
1537
|
+
let t = [...this.snapshotProvider().configFiles.values()].filter((e) => e.contentType.includes("application/json") || e.name.endsWith(".json"));
|
|
1538
|
+
if (t.length === 1) try {
|
|
1539
|
+
return JSON.parse(t[0].content);
|
|
1540
|
+
} catch {
|
|
1541
|
+
return;
|
|
1542
|
+
}
|
|
1543
|
+
}
|
|
1544
|
+
};
|
|
1545
|
+
function k(e) {
|
|
1546
|
+
let t = new Date(Number.isFinite(e) ? e : Date.now()), n = -t.getTimezoneOffset(), r = n >= 0 ? "+" : "-", i = Math.abs(n), a = `${r}${A(Math.floor(i / 60))}:${A(i % 60)}`;
|
|
1547
|
+
return `${t.getFullYear()}-${A(t.getMonth() + 1)}-${A(t.getDate())}T${A(t.getHours())}:${A(t.getMinutes())}:${A(t.getSeconds())}.${String(t.getMilliseconds()).padStart(3, "0")}${a}`;
|
|
1548
|
+
}
|
|
1549
|
+
function A(e) {
|
|
1550
|
+
return String(e).padStart(2, "0");
|
|
1551
|
+
}
|
|
1552
|
+
function j(e) {
|
|
1553
|
+
let t = P(e.versionId);
|
|
1554
|
+
if (!t) throw Error(`script versionId is required: ${e.name}`);
|
|
1555
|
+
return `script:${t}`;
|
|
1556
|
+
}
|
|
1557
|
+
async function te(e) {
|
|
1558
|
+
let t = `GameAlgo API returned ${e.status}`;
|
|
1559
|
+
try {
|
|
1560
|
+
var n, r;
|
|
1561
|
+
let i = await e.json();
|
|
1562
|
+
return new T(e.status, (n = (r = i.message) == null ? i.error : r) == null ? t : n, i.error);
|
|
1563
|
+
} catch {
|
|
1564
|
+
return new T(e.status, t);
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
function ne(e) {
|
|
1568
|
+
let t = e.trim();
|
|
1569
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9_.-]*$/.test(t) || t.includes("..")) throw Error("Invalid config file name");
|
|
1570
|
+
return t;
|
|
1571
|
+
}
|
|
1572
|
+
function M() {
|
|
1573
|
+
var e, t, n;
|
|
1574
|
+
return (e = (t = globalThis.crypto) == null || (n = t.randomUUID) == null ? void 0 : n.call(t)) == null ? `${Date.now()}-${Math.random().toString(16).slice(2)}` : e;
|
|
1575
|
+
}
|
|
1576
|
+
function N(e) {
|
|
1577
|
+
return `${e.origin}${e.pathname.replace(/\/+$/, "")}`;
|
|
1578
|
+
}
|
|
1579
|
+
function P(e) {
|
|
1580
|
+
return (e == null ? void 0 : e.trim()) || void 0;
|
|
1581
|
+
}
|
|
1582
|
+
function re(e, t) {
|
|
1583
|
+
let n = P(t);
|
|
1584
|
+
return !n || (e === "gaid" || e === "idfa") && /^0{8}-0{4}-0{4}-0{4}-0{12}$/i.test(n) ? null : n;
|
|
1585
|
+
}
|
|
1586
|
+
function F(e) {
|
|
1587
|
+
let t = e == null ? 0 : e;
|
|
1588
|
+
if (!Number.isSafeInteger(t) || t < 0) throw Error("experimentIntegrationVersion must be a non-negative integer");
|
|
1589
|
+
return t;
|
|
1590
|
+
}
|
|
1591
|
+
function ie(e) {
|
|
1592
|
+
if (e !== !1) return e == null ? ((e) => console.log(e)) : e;
|
|
1593
|
+
}
|
|
1594
|
+
function I(e) {
|
|
1595
|
+
return e instanceof Error ? e.message : String(e);
|
|
1596
|
+
}
|
|
1597
|
+
function L() {
|
|
1598
|
+
try {
|
|
1599
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
|
|
1600
|
+
} catch {
|
|
1601
|
+
return "UTC";
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
function ae() {
|
|
1605
|
+
var e;
|
|
1606
|
+
let t = {}, n = globalThis.navigator, r = globalThis.process;
|
|
1607
|
+
return r != null && (e = r.versions) != null && e.node ? (t.runtime = "node", t.nodeVersion = r.versions.node) : n && (t.runtime = "browser"), r != null && r.platform && (t.os = r.platform), r != null && r.arch && (t.arch = r.arch), n != null && n.language && (t.locale = n.language), n != null && n.platform && (t.browserPlatform = n.platform), n != null && n.userAgent && (t.userAgent = n.userAgent), t;
|
|
1608
|
+
}
|
|
1609
|
+
function R(e) {
|
|
1610
|
+
return e && typeof e == "object" && !Array.isArray(e) ? { ...e } : {};
|
|
1611
|
+
}
|
|
1612
|
+
function z(e) {
|
|
1613
|
+
let t = {}, n = R(e);
|
|
1614
|
+
for (let [e, r] of Object.entries(n)) {
|
|
1615
|
+
if (!e) continue;
|
|
1616
|
+
let n = se(r);
|
|
1617
|
+
n !== void 0 && (t[e] = n);
|
|
1618
|
+
}
|
|
1619
|
+
return t;
|
|
1620
|
+
}
|
|
1621
|
+
function B(e, t, n) {
|
|
1622
|
+
let r = e.slice(0, t);
|
|
1623
|
+
if (!n || r.length <= 1) return r;
|
|
1624
|
+
let i = [];
|
|
1625
|
+
for (let e of r) {
|
|
1626
|
+
let t = [...i, e];
|
|
1627
|
+
if (i.length > 0 && oe(JSON.stringify({ events: t })) > n) break;
|
|
1628
|
+
i.push(e);
|
|
1629
|
+
}
|
|
1630
|
+
return i;
|
|
1631
|
+
}
|
|
1632
|
+
function oe(t) {
|
|
1633
|
+
return e(t).byteLength;
|
|
1634
|
+
}
|
|
1635
|
+
function se(e) {
|
|
1636
|
+
if (e === null || typeof e == "string" || typeof e == "boolean") return e;
|
|
1637
|
+
if (typeof e == "number") return Number.isFinite(e) ? e : void 0;
|
|
1638
|
+
if (e !== void 0) return JSON.stringify(e);
|
|
1639
|
+
}
|
|
1640
|
+
async function V(e, t) {
|
|
1641
|
+
if (!/^sha256:[a-f0-9]{64}$/i.test(t)) throw Error("Script hash must use sha256:<64 lowercase hex characters>");
|
|
1642
|
+
let n = await H(e);
|
|
1643
|
+
if (!n) throw Error("SHA-256 is unavailable in this runtime");
|
|
1644
|
+
if (n.toLowerCase() !== t.toLowerCase()) throw Error(`Script hash mismatch: expected=${t} actual=${n}`);
|
|
1645
|
+
}
|
|
1646
|
+
async function H(n) {
|
|
1647
|
+
var r;
|
|
1648
|
+
if (!((r = globalThis.crypto) != null && r.subtle)) return `sha256:${t(n)}`;
|
|
1649
|
+
let i = await globalThis.crypto.subtle.digest("SHA-256", e(n));
|
|
1650
|
+
return `sha256:${[...new Uint8Array(i)].map((e) => e.toString(16).padStart(2, "0")).join("")}`;
|
|
1651
|
+
}
|
|
1652
|
+
function U(e) {
|
|
1653
|
+
return JSON.parse(JSON.stringify(e));
|
|
1654
|
+
}
|
|
1655
|
+
function ce(e) {
|
|
1656
|
+
let t = U(e);
|
|
1657
|
+
if (!t || typeof t != "object" || Array.isArray(t)) throw Error("attribution must be an object");
|
|
1658
|
+
return t;
|
|
1659
|
+
}
|
|
1660
|
+
function le(e, t, n) {
|
|
1661
|
+
let r = G(t);
|
|
1662
|
+
if (r === "organic") return "organic";
|
|
1663
|
+
if (K(r)) return "unknown";
|
|
1664
|
+
if (e.toLowerCase() === "adjust") {
|
|
1665
|
+
let e = W(n, "network"), t = W(n, "tracker_name") || W(n, "trackerName"), r = W(n, "tracker_token") || W(n, "trackerToken");
|
|
1666
|
+
if ([
|
|
1667
|
+
e,
|
|
1668
|
+
t,
|
|
1669
|
+
r
|
|
1670
|
+
].some(K)) return "unknown";
|
|
1671
|
+
if ([
|
|
1672
|
+
e,
|
|
1673
|
+
t,
|
|
1674
|
+
r
|
|
1675
|
+
].some((e) => e === "organic")) return "organic";
|
|
1676
|
+
}
|
|
1677
|
+
return t == null ? "attributed" : t;
|
|
1678
|
+
}
|
|
1679
|
+
function W(e, t) {
|
|
1680
|
+
let n = e[t];
|
|
1681
|
+
return typeof n == "string" ? G(n) : "";
|
|
1682
|
+
}
|
|
1683
|
+
function G(e) {
|
|
1684
|
+
return (e == null ? "" : e).trim().toLowerCase().replace(/[_-]+/g, " ").replace(/\s+/g, " ");
|
|
1685
|
+
}
|
|
1686
|
+
function K(e) {
|
|
1687
|
+
return e === "unknown" || e === "unattr" || e === "unattributed" || e === "no user consent";
|
|
1688
|
+
}
|
|
1689
|
+
function q(e) {
|
|
1690
|
+
return e === null || typeof e == "string" || typeof e == "number" || typeof e == "boolean" ? JSON.stringify(e) : Array.isArray(e) ? `[${e.map((e) => q(e)).join(",")}]` : `{${Object.keys(e).sort().map((t) => `${JSON.stringify(t)}:${q(e[t])}`).join(",")}}`;
|
|
1691
|
+
}
|
|
1692
|
+
var ue = void 0;
|
|
1693
|
+
function de(e, t) {
|
|
1694
|
+
if (!t) return e;
|
|
1695
|
+
let n = e;
|
|
1696
|
+
for (let e of t.split(".")) {
|
|
1697
|
+
if (Array.isArray(n)) {
|
|
1698
|
+
let t = Number(e);
|
|
1699
|
+
n = Number.isInteger(t) ? n[t] : void 0;
|
|
1700
|
+
} else if (n && typeof n == "object") n = n[e];
|
|
1701
|
+
else return;
|
|
1702
|
+
if (n === void 0) return;
|
|
1703
|
+
}
|
|
1704
|
+
return n;
|
|
1705
|
+
}
|
|
1706
|
+
function J(e) {
|
|
1707
|
+
return e === "web" || e === "wechat" || e === "crazygames";
|
|
1708
|
+
}
|
|
1709
|
+
//#endregion
|
|
1710
|
+
//#region src/ads.ts
|
|
1711
|
+
var Y = /* @__PURE__ */ new WeakSet(), fe = class {
|
|
1712
|
+
constructor(e, t, n, r = 18e4) {
|
|
1713
|
+
if (u(this, "closed", !1), u(this, "cancel", void 0), u(this, "host", void 0), u(this, "report", void 0), u(this, "logger", void 0), u(this, "timeoutMs", void 0), this.host = e, this.report = t, this.logger = n, this.timeoutMs = r, !Number.isFinite(r) || r <= 0) throw Error("adTimeoutMs must be positive");
|
|
1714
|
+
}
|
|
1715
|
+
createAdSpace({ adType: e }) {
|
|
1716
|
+
if (this.closed) throw Error("GamePropel client is closed");
|
|
1717
|
+
if (e !== "rewarded" && e !== "midgame") throw Error("Invalid CrazyGames ad type");
|
|
1718
|
+
return Object.freeze({ show: (t, n = {}, r = {}) => this.show(e, t, n, r) });
|
|
1719
|
+
}
|
|
1720
|
+
async show(e, t, n, r) {
|
|
1721
|
+
if (this.closed) throw Error("GamePropel client is closed");
|
|
1722
|
+
if (!/^gap_[a-f0-9]{32}$/.test(t)) throw Error("Invalid GamePropel adPlacementId");
|
|
1723
|
+
if (!n || typeof n != "object" || Array.isArray(n) || [
|
|
1724
|
+
n.onStarted,
|
|
1725
|
+
n.onFinished,
|
|
1726
|
+
n.onError
|
|
1727
|
+
].some((e) => e !== void 0 && typeof e != "function")) throw Error("Invalid CrazyGames ad callbacks");
|
|
1728
|
+
if (!r || typeof r != "object" || Array.isArray(r)) throw Error("Ad payload must be an object");
|
|
1729
|
+
let i = this.host.ad;
|
|
1730
|
+
if (Y.has(i)) throw Error("A CrazyGames video ad is already pending");
|
|
1731
|
+
let a = JSON.parse(JSON.stringify(r));
|
|
1732
|
+
return delete a.trackId, a.hostAdType = e, a.revenueStatus = "unavailable", a.hostEnvironment = this.host.environment, Y.add(i), new Promise((r, o) => {
|
|
1733
|
+
var s;
|
|
1734
|
+
let c = !1, l = !1, u = () => {
|
|
1735
|
+
try {
|
|
1736
|
+
var e;
|
|
1737
|
+
(e = this.logger) == null || e.call(this, "[GamePropelSDK] ad callback failed");
|
|
1738
|
+
} catch {}
|
|
1739
|
+
}, d = (e) => {
|
|
1740
|
+
try {
|
|
1741
|
+
Promise.resolve(e == null ? void 0 : e()).catch(u);
|
|
1742
|
+
} catch {
|
|
1743
|
+
u();
|
|
1744
|
+
}
|
|
1745
|
+
}, f = () => {
|
|
1746
|
+
c = !0, clearTimeout(m), Y.delete(i), this.cancel = void 0;
|
|
1747
|
+
}, p = (e) => {
|
|
1748
|
+
c || (f(), d(n.onError ? () => n.onError(e) : void 0), o(Object.assign(Error(e.message), { code: e.code })));
|
|
1749
|
+
}, m = setTimeout(() => p({
|
|
1750
|
+
code: "timeout",
|
|
1751
|
+
message: "CrazyGames ad callbacks timed out"
|
|
1752
|
+
}), this.timeoutMs);
|
|
1753
|
+
(s = m.unref) == null || s.call(m), this.cancel = () => {
|
|
1754
|
+
c || (f(), o(/* @__PURE__ */ Error("GamePropel client is closed")));
|
|
1755
|
+
};
|
|
1756
|
+
try {
|
|
1757
|
+
let o = i.requestAd(e, {
|
|
1758
|
+
adStarted: () => {
|
|
1759
|
+
if (!(c || l)) {
|
|
1760
|
+
l = !0;
|
|
1761
|
+
try {
|
|
1762
|
+
this.report(t, a);
|
|
1763
|
+
} catch {
|
|
1764
|
+
u();
|
|
1765
|
+
}
|
|
1766
|
+
d(n.onStarted);
|
|
1767
|
+
}
|
|
1768
|
+
},
|
|
1769
|
+
adFinished: () => {
|
|
1770
|
+
c || (f(), d(n.onFinished), r());
|
|
1771
|
+
},
|
|
1772
|
+
adError: (e) => p(X(e))
|
|
1773
|
+
});
|
|
1774
|
+
Promise.resolve(o).catch((e) => p(X(e)));
|
|
1775
|
+
} catch (e) {
|
|
1776
|
+
p(X(e));
|
|
1777
|
+
}
|
|
1778
|
+
});
|
|
1779
|
+
}
|
|
1780
|
+
close() {
|
|
1781
|
+
var e;
|
|
1782
|
+
this.closed = !0, (e = this.cancel) == null || e.call(this);
|
|
1783
|
+
}
|
|
1784
|
+
};
|
|
1785
|
+
function X(e) {
|
|
1786
|
+
let t = e;
|
|
1787
|
+
return {
|
|
1788
|
+
code: typeof (t == null ? void 0 : t.code) == "string" ? t.code : "other",
|
|
1789
|
+
message: typeof (t == null ? void 0 : t.message) == "string" ? t.message : "CrazyGames ad failed"
|
|
1790
|
+
};
|
|
1791
|
+
}
|
|
1792
|
+
//#endregion
|
|
1793
|
+
//#region src/host.ts
|
|
1794
|
+
function pe(e) {
|
|
1795
|
+
var t;
|
|
1796
|
+
let n = e == null ? (t = globalThis.CrazyGames) == null ? void 0 : t.SDK : e;
|
|
1797
|
+
if (!n || typeof n.init != "function") throw Error("Load https://sdk.crazygames.com/crazygames-sdk-v3.js before initializing GamePropel");
|
|
1798
|
+
return n;
|
|
1799
|
+
}
|
|
1800
|
+
//#endregion
|
|
1801
|
+
//#region ../rest-api/src/browser-storage.ts
|
|
1802
|
+
var me = class {
|
|
1803
|
+
constructor(e = {}) {
|
|
1804
|
+
var t, n, r, i;
|
|
1805
|
+
u(this, "databaseName", void 0), u(this, "storeName", void 0), u(this, "indexedDB", void 0), u(this, "localStorage", void 0), u(this, "memory", /* @__PURE__ */ new Map()), u(this, "databasePromise", void 0), u(this, "logger", void 0), u(this, "operationSequence", 0), this.databaseName = (t = e.databaseName) == null ? "gamealgo-sdk" : t, this.storeName = (n = e.storeName) == null ? "kv" : n, this.indexedDB = e.indexedDB === null ? void 0 : (r = e.indexedDB) == null ? ve() : r, this.localStorage = e.localStorage === null ? void 0 : (i = e.localStorage) == null ? he() : i, this.logger = typeof e.logger == "function" ? e.logger : void 0;
|
|
1806
|
+
try {
|
|
1807
|
+
var a;
|
|
1808
|
+
(a = this.logger) == null || a.call(this, `[GameAlgoSDK] storage capabilities indexedDB=${!!this.indexedDB} localStorage=${!!this.localStorage}`);
|
|
1809
|
+
} catch {}
|
|
1810
|
+
}
|
|
1811
|
+
async getItem(e) {
|
|
1812
|
+
return this.observe(`read ${Z(e)}`, () => this.readItem(e, !1));
|
|
1813
|
+
}
|
|
1814
|
+
async getItemDurably(e) {
|
|
1815
|
+
return this.observe(`read-durable ${Z(e)}`, () => this.readItem(e, !0));
|
|
1816
|
+
}
|
|
1817
|
+
async readItem(e, t) {
|
|
1818
|
+
let n;
|
|
1819
|
+
try {
|
|
1820
|
+
var r, i, a;
|
|
1821
|
+
let n = (r = this.localStorage) == null ? void 0 : r.getItem(`gamealgo:fallback:${e}`);
|
|
1822
|
+
if (n === "deleted") return;
|
|
1823
|
+
if (n === "value") return (i = (a = this.localStorage) == null ? void 0 : a.getItem(e)) == null ? t ? void 0 : this.memory.get(e) : i;
|
|
1824
|
+
} catch (e) {
|
|
1825
|
+
if (t) throw e;
|
|
1826
|
+
n = e;
|
|
1827
|
+
}
|
|
1828
|
+
if (this.indexedDB) try {
|
|
1829
|
+
let t = await this.readIndexedDB(e);
|
|
1830
|
+
if (typeof t == "string") return t;
|
|
1831
|
+
} catch (e) {
|
|
1832
|
+
if (t) throw e;
|
|
1833
|
+
n = e;
|
|
1834
|
+
}
|
|
1835
|
+
try {
|
|
1836
|
+
var o;
|
|
1837
|
+
let t = (o = this.localStorage) == null ? void 0 : o.getItem(e);
|
|
1838
|
+
if (t != null) return t;
|
|
1839
|
+
} catch (e) {
|
|
1840
|
+
n = e;
|
|
1841
|
+
}
|
|
1842
|
+
if (t && n) throw n;
|
|
1843
|
+
return t ? void 0 : this.memory.get(e);
|
|
1844
|
+
}
|
|
1845
|
+
async setItem(e, t) {
|
|
1846
|
+
return this.observe(`write ${Z(e)}`, () => this.writeItem(e, t, !1));
|
|
1847
|
+
}
|
|
1848
|
+
async setItemDurably(e, t) {
|
|
1849
|
+
return this.observe(`write-durable ${Z(e)}`, () => this.writeItem(e, t, !0));
|
|
1850
|
+
}
|
|
1851
|
+
async writeItem(e, t, n) {
|
|
1852
|
+
var r;
|
|
1853
|
+
if (n && ((r = this.localStorage) == null || r.getItem(`gamealgo:fallback:${e}`)), this.memory.set(e, t), this.indexedDB) try {
|
|
1854
|
+
await this.writeIndexedDB(e, t);
|
|
1855
|
+
try {
|
|
1856
|
+
var i, a;
|
|
1857
|
+
(i = this.localStorage) == null || i.removeItem(`gamealgo:fallback:${e}`), (a = this.localStorage) == null || a.removeItem(e);
|
|
1858
|
+
} catch (e) {
|
|
1859
|
+
if (n) throw e;
|
|
1860
|
+
}
|
|
1861
|
+
return;
|
|
1862
|
+
} catch {}
|
|
1863
|
+
try {
|
|
1864
|
+
var o, s;
|
|
1865
|
+
if (!this.localStorage) throw Error("Persistent browser storage is unavailable");
|
|
1866
|
+
(o = this.localStorage) == null || o.setItem(e, t), (s = this.localStorage) == null || s.setItem(`gamealgo:fallback:${e}`, "value");
|
|
1867
|
+
} catch (e) {
|
|
1868
|
+
if (n) throw e;
|
|
1869
|
+
}
|
|
1870
|
+
}
|
|
1871
|
+
async removeItem(e) {
|
|
1872
|
+
return this.observe(`delete ${Z(e)}`, () => this.deleteItem(e, !1));
|
|
1873
|
+
}
|
|
1874
|
+
async removeItemDurably(e) {
|
|
1875
|
+
return this.observe(`delete-durable ${Z(e)}`, () => this.deleteItem(e, !0));
|
|
1876
|
+
}
|
|
1877
|
+
async deleteItem(e, t) {
|
|
1878
|
+
this.memory.delete(e);
|
|
1879
|
+
let n = !1;
|
|
1880
|
+
if (this.indexedDB) try {
|
|
1881
|
+
await this.deleteIndexedDB(e), n = !0;
|
|
1882
|
+
} catch {}
|
|
1883
|
+
try {
|
|
1884
|
+
var r, i, a;
|
|
1885
|
+
if (!this.localStorage) {
|
|
1886
|
+
if (n) return;
|
|
1887
|
+
throw Error("Persistent browser storage is unavailable");
|
|
1888
|
+
}
|
|
1889
|
+
(r = this.localStorage) == null || r.removeItem(e), n ? (i = this.localStorage) == null || i.removeItem(`gamealgo:fallback:${e}`) : (a = this.localStorage) == null || a.setItem(`gamealgo:fallback:${e}`, "deleted");
|
|
1890
|
+
} catch (e) {
|
|
1891
|
+
if (t) throw e;
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
database() {
|
|
1895
|
+
return this.indexedDB ? (this.databasePromise || (this.databasePromise = this.observe("indexeddb.open", () => new Promise((e, t) => {
|
|
1896
|
+
let n = this.indexedDB.open(this.databaseName, 1);
|
|
1897
|
+
n.onupgradeneeded = () => {
|
|
1898
|
+
let e = n.result;
|
|
1899
|
+
e.objectStoreNames.contains(this.storeName) || e.createObjectStore(this.storeName);
|
|
1900
|
+
}, n.onsuccess = () => e(n.result), n.onerror = () => {
|
|
1901
|
+
var e;
|
|
1902
|
+
return t((e = n.error) == null ? /* @__PURE__ */ Error("Unable to open IndexedDB") : e);
|
|
1903
|
+
}, n.onblocked = () => t(/* @__PURE__ */ Error("IndexedDB upgrade is blocked"));
|
|
1904
|
+
}))), this.databasePromise) : Promise.reject(/* @__PURE__ */ Error("IndexedDB is unavailable"));
|
|
1905
|
+
}
|
|
1906
|
+
async readIndexedDB(e) {
|
|
1907
|
+
let t = await this.database();
|
|
1908
|
+
return this.observe(`indexeddb.read ${Z(e)}`, () => ge(t.transaction(this.storeName, "readonly").objectStore(this.storeName).get(e)));
|
|
1909
|
+
}
|
|
1910
|
+
async writeIndexedDB(e, t) {
|
|
1911
|
+
let n = await this.database();
|
|
1912
|
+
await this.observe(`indexeddb.write ${Z(e)}`, async () => {
|
|
1913
|
+
let r = n.transaction(this.storeName, "readwrite");
|
|
1914
|
+
r.objectStore(this.storeName).put(t, e), await _e(r);
|
|
1915
|
+
});
|
|
1916
|
+
}
|
|
1917
|
+
async deleteIndexedDB(e) {
|
|
1918
|
+
let t = await this.database();
|
|
1919
|
+
await this.observe(`indexeddb.delete ${Z(e)}`, async () => {
|
|
1920
|
+
let n = t.transaction(this.storeName, "readwrite");
|
|
1921
|
+
n.objectStore(this.storeName).delete(e), await _e(n);
|
|
1922
|
+
});
|
|
1923
|
+
}
|
|
1924
|
+
async observe(e, t) {
|
|
1925
|
+
var n;
|
|
1926
|
+
if (!this.logger) return t();
|
|
1927
|
+
let r = ++this.operationSequence, i = Date.now(), a = (t) => {
|
|
1928
|
+
try {
|
|
1929
|
+
var n;
|
|
1930
|
+
(n = this.logger) == null || n.call(this, `[GameAlgoSDK] storage op=${r} ${e} ${t} elapsedMs=${Math.max(0, Date.now() - i)}`);
|
|
1931
|
+
} catch {}
|
|
1932
|
+
};
|
|
1933
|
+
a("started");
|
|
1934
|
+
let o = setTimeout(() => a("pending"), 3e3);
|
|
1935
|
+
(n = o.unref) == null || n.call(o);
|
|
1936
|
+
try {
|
|
1937
|
+
let e = await t();
|
|
1938
|
+
return a(`complete resultType=${typeof e}`), e;
|
|
1939
|
+
} catch (e) {
|
|
1940
|
+
throw a(`failed errorType=${e instanceof Error ? e.name : typeof e}`), e;
|
|
1941
|
+
} finally {
|
|
1942
|
+
clearTimeout(o);
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
};
|
|
1946
|
+
function Z(e) {
|
|
1947
|
+
return e.startsWith("gamealgo:v1:identity:") ? e.endsWith(":user-id") ? "identity.userId" : e.endsWith(":created-at") ? "identity.createdAt" : e.endsWith(":created-local-at") ? "identity.createdLocalAt" : "identity" : e.startsWith("gamealgo_user_") ? "identity.legacy" : e.startsWith("gamealgo:v1:event-queue:") ? "event-queue" : e.startsWith("gamealgo:v1:snapshot:") ? "snapshot" : "cache";
|
|
1948
|
+
}
|
|
1949
|
+
function he() {
|
|
1950
|
+
if (!(typeof window > "u")) try {
|
|
1951
|
+
return window.localStorage;
|
|
1952
|
+
} catch {
|
|
1953
|
+
return;
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
function ge(e) {
|
|
1957
|
+
return new Promise((t, n) => {
|
|
1958
|
+
e.onsuccess = () => t(e.result), e.onerror = () => {
|
|
1959
|
+
var t;
|
|
1960
|
+
return n((t = e.error) == null ? /* @__PURE__ */ Error("IndexedDB request failed") : t);
|
|
1961
|
+
};
|
|
1962
|
+
});
|
|
1963
|
+
}
|
|
1964
|
+
function _e(e) {
|
|
1965
|
+
return new Promise((t, n) => {
|
|
1966
|
+
e.oncomplete = () => t(), e.onerror = () => {
|
|
1967
|
+
var t;
|
|
1968
|
+
return n((t = e.error) == null ? /* @__PURE__ */ Error("IndexedDB transaction failed") : t);
|
|
1969
|
+
}, e.onabort = () => {
|
|
1970
|
+
var t;
|
|
1971
|
+
return n((t = e.error) == null ? /* @__PURE__ */ Error("IndexedDB transaction was aborted") : t);
|
|
1972
|
+
};
|
|
1973
|
+
});
|
|
1974
|
+
}
|
|
1975
|
+
function ve() {
|
|
1976
|
+
try {
|
|
1977
|
+
return globalThis.indexedDB;
|
|
1978
|
+
} catch {
|
|
1979
|
+
return;
|
|
1980
|
+
}
|
|
1981
|
+
}
|
|
1982
|
+
//#endregion
|
|
1983
|
+
//#region src/storage.ts
|
|
1984
|
+
var ye = class {
|
|
1985
|
+
constructor(e = {}) {
|
|
1986
|
+
u(this, "storage", void 0), this.storage = new me({
|
|
1987
|
+
...e,
|
|
1988
|
+
databaseName: "gamepropel-crazygames",
|
|
1989
|
+
storeName: "telemetry",
|
|
1990
|
+
localStorage: null
|
|
1991
|
+
});
|
|
1992
|
+
}
|
|
1993
|
+
getItem(e) {
|
|
1994
|
+
return this.storage.getItemDurably(e);
|
|
1995
|
+
}
|
|
1996
|
+
setItem(e, t) {
|
|
1997
|
+
return this.storage.setItemDurably(e, t);
|
|
1998
|
+
}
|
|
1999
|
+
removeItem(e) {
|
|
2000
|
+
return this.storage.removeItemDurably(e);
|
|
2001
|
+
}
|
|
2002
|
+
}, be = class {
|
|
2003
|
+
constructor(t, n = 1e4) {
|
|
2004
|
+
if (u(this, "pending", /* @__PURE__ */ new Set()), u(this, "closed", !1), u(this, "fetch", void 0), !Number.isFinite(n) || n <= 0) throw Error("requestTimeoutMs must be positive");
|
|
2005
|
+
this.fetch = async (r, i = {}) => {
|
|
2006
|
+
var a;
|
|
2007
|
+
if (this.closed) throw Error("GamePropel client is closed");
|
|
2008
|
+
let o = new AbortController(), s = /\/v1\/events\/batch(?:\?|$)/.test(r) && e((a = i.body) == null ? "" : a).length <= 61440, c, l, u = new Promise((e, t) => {
|
|
2009
|
+
c = () => {
|
|
2010
|
+
o.abort(), t(/* @__PURE__ */ Error("GamePropel client is closed"));
|
|
2011
|
+
}, l = setTimeout(() => {
|
|
2012
|
+
o.abort(), t(/* @__PURE__ */ Error("GamePropel request timed out"));
|
|
2013
|
+
}, n);
|
|
2014
|
+
});
|
|
2015
|
+
this.pending.add(c);
|
|
2016
|
+
try {
|
|
2017
|
+
let e = (async () => {
|
|
2018
|
+
let e = await t(r, {
|
|
2019
|
+
...i,
|
|
2020
|
+
keepalive: s,
|
|
2021
|
+
signal: o.signal
|
|
2022
|
+
}), n = await e.text();
|
|
2023
|
+
return {
|
|
2024
|
+
ok: e.ok,
|
|
2025
|
+
status: e.status,
|
|
2026
|
+
headers: e.headers,
|
|
2027
|
+
text: async () => n,
|
|
2028
|
+
json: async () => JSON.parse(n)
|
|
2029
|
+
};
|
|
2030
|
+
})();
|
|
2031
|
+
return await Promise.race([e, u]);
|
|
2032
|
+
} finally {
|
|
2033
|
+
l && clearTimeout(l), this.pending.delete(c);
|
|
2034
|
+
}
|
|
2035
|
+
};
|
|
2036
|
+
}
|
|
2037
|
+
close() {
|
|
2038
|
+
this.closed = !0;
|
|
2039
|
+
for (let e of this.pending) e();
|
|
2040
|
+
this.pending.clear();
|
|
2041
|
+
}
|
|
2042
|
+
}, Q = "1.0.0", xe = Object.freeze({
|
|
2043
|
+
config: !0,
|
|
2044
|
+
parameterExperiments: !0,
|
|
2045
|
+
remoteScripts: !1,
|
|
2046
|
+
multiplayer: !1,
|
|
2047
|
+
rewardedAds: !0,
|
|
2048
|
+
midgameAds: !0,
|
|
2049
|
+
impressionRevenue: !1
|
|
2050
|
+
}), Se = class e extends E {
|
|
2051
|
+
static async init(t) {
|
|
2052
|
+
var n;
|
|
2053
|
+
let r = pe(t.crazyGames);
|
|
2054
|
+
if (t.adTimeoutMs !== void 0 && (!Number.isFinite(t.adTimeoutMs) || t.adTimeoutMs <= 0)) throw Error("adTimeoutMs must be positive");
|
|
2055
|
+
let i = (n = t.hostInitTimeoutMs) == null ? 1e4 : n;
|
|
2056
|
+
if (!Number.isFinite(i) || i <= 0) throw Error("hostInitTimeoutMs must be positive");
|
|
2057
|
+
let a;
|
|
2058
|
+
try {
|
|
2059
|
+
await Promise.race([r.init(), new Promise((e, t) => {
|
|
2060
|
+
a = setTimeout(() => t(/* @__PURE__ */ Error("CrazyGames initialization timed out")), i);
|
|
2061
|
+
})]);
|
|
2062
|
+
} finally {
|
|
2063
|
+
a && clearTimeout(a);
|
|
2064
|
+
}
|
|
2065
|
+
if (r.environment !== "crazygames" && r.environment !== "local") throw Error("CrazyGames SDK is disabled on this domain");
|
|
2066
|
+
return new e(t, r);
|
|
2067
|
+
}
|
|
2068
|
+
constructor(e, t) {
|
|
2069
|
+
var n, r, i, a, o, s, c, l, d;
|
|
2070
|
+
let f = (n = e.fetchImpl) == null ? (r = globalThis.fetch) == null ? void 0 : r.bind(globalThis) : n;
|
|
2071
|
+
if (!f) throw Error("Browser fetch is required");
|
|
2072
|
+
let p = new be(f, e.requestTimeoutMs);
|
|
2073
|
+
super({
|
|
2074
|
+
...e,
|
|
2075
|
+
platform: "crazygames",
|
|
2076
|
+
sdkVersion: Q,
|
|
2077
|
+
storage: (i = e.storage) == null ? new ye(e.browserStorage) : i,
|
|
2078
|
+
fetchImpl: p.fetch,
|
|
2079
|
+
scriptRuntime: { execute() {
|
|
2080
|
+
throw Error("CrazyGames SDK 1.x supports parameter experiments; remote scripts are unavailable");
|
|
2081
|
+
} },
|
|
2082
|
+
isDebug: t.environment === "local" || e.isDebug,
|
|
2083
|
+
eventFlushIntervalMs: (a = e.eventFlushIntervalMs) == null ? 5e3 : a,
|
|
2084
|
+
eventMaxBatchSize: Math.min((o = e.eventMaxBatchSize) == null ? 20 : o, 20),
|
|
2085
|
+
eventMaxBatchBytes: Math.min((s = e.eventMaxBatchBytes) == null ? 49152 : s, 49152),
|
|
2086
|
+
eventQueueLimit: (c = e.eventQueueLimit) == null ? 500 : c,
|
|
2087
|
+
eventPersistOnEnqueue: !0,
|
|
2088
|
+
logger: (l = e.logger) != null && l,
|
|
2089
|
+
device: $(e.device)
|
|
2090
|
+
}), u(this, "host", void 0), u(this, "transport", void 0), u(this, "ads", void 0), u(this, "win", typeof window > "u" ? void 0 : window), u(this, "doc", typeof document > "u" ? void 0 : document), u(this, "releases", []), u(this, "autoSessionLifecycle", void 0), u(this, "preload", void 0), u(this, "deviceOverrides", void 0), u(this, "closed", !1), u(this, "endedSessionId", void 0), u(this, "onVisibility", () => {
|
|
2091
|
+
var e, t;
|
|
2092
|
+
((e = this.doc) == null ? void 0 : e.visibilityState) === "hidden" ? this.endSession("visibility_hidden") : ((t = this.doc) == null ? void 0 : t.visibilityState) === "visible" && this.resume();
|
|
2093
|
+
}), u(this, "onPageHide", () => this.endSession("pagehide")), u(this, "onPageShow", () => {
|
|
2094
|
+
var e;
|
|
2095
|
+
((e = this.doc) == null ? void 0 : e.visibilityState) !== "hidden" && this.resume();
|
|
2096
|
+
}), u(this, "onOnline", () => this.flushInBackground()), this.host = t, this.deviceOverrides = $(e.device), this.transport = p, this.autoSessionLifecycle = e.autoSessionLifecycle !== !1, this.preload = (d = e.preloadConfigFiles) == null || d, this.tracker.setSessionDurationVisibility(() => {
|
|
2097
|
+
var e;
|
|
2098
|
+
return ((e = this.doc) == null ? void 0 : e.visibilityState) !== "hidden";
|
|
2099
|
+
}), this.ads = new fe(t, (e, t) => {
|
|
2100
|
+
this.tracker.trackAdPlacement(e, 0, "crazygames", t);
|
|
2101
|
+
}, typeof e.logger == "function" ? e.logger : void 0, e.adTimeoutMs), e.autoLifecycle !== !1 && (this.listen(this.doc, "visibilitychange", this.onVisibility), this.listen(this.win, "pagehide", this.onPageHide), this.listen(this.win, "pageshow", this.onPageShow), this.listen(this.win, "online", this.onOnline));
|
|
2102
|
+
}
|
|
2103
|
+
createAdSpace(e) {
|
|
2104
|
+
return this.ads.createAdSpace(e);
|
|
2105
|
+
}
|
|
2106
|
+
getForegroundTimeMs() {
|
|
2107
|
+
return this.tracker.getForegroundTimeMs();
|
|
2108
|
+
}
|
|
2109
|
+
async flush() {
|
|
2110
|
+
await this.tracker.flushUrgent(), await this.tracker.flush();
|
|
2111
|
+
}
|
|
2112
|
+
fetchConfig(e = {}) {
|
|
2113
|
+
return this.closed ? Promise.reject(/* @__PURE__ */ Error("GamePropel client is closed")) : super.fetchConfig({
|
|
2114
|
+
...e,
|
|
2115
|
+
platform: "crazygames",
|
|
2116
|
+
sdkVersion: Q,
|
|
2117
|
+
isDebug: this.host.environment === "local" || e.isDebug,
|
|
2118
|
+
device: $(e.device)
|
|
2119
|
+
});
|
|
2120
|
+
}
|
|
2121
|
+
async refresh(e = {}) {
|
|
2122
|
+
var t;
|
|
2123
|
+
let n = await this.fetchConfig(e);
|
|
2124
|
+
if (this.closed) return;
|
|
2125
|
+
let r = (t = e.preloadConfigFiles) == null ? this.preload : t;
|
|
2126
|
+
if (r === !1) return;
|
|
2127
|
+
let i = n.configFiles.filter((e) => {
|
|
2128
|
+
var t;
|
|
2129
|
+
return e.name.endsWith(".json") || ((t = e.contentType) == null ? void 0 : t.includes("application/json"));
|
|
2130
|
+
}).filter((e) => !Array.isArray(r) || r.includes(e.name)).map((e) => e.name);
|
|
2131
|
+
await Promise.all([...new Set(i)].map((e) => this.fetchConfigFile(e)));
|
|
2132
|
+
}
|
|
2133
|
+
defaultDeviceContext() {
|
|
2134
|
+
let e = {
|
|
2135
|
+
...this.deviceOverrides,
|
|
2136
|
+
runtime: "crazygames",
|
|
2137
|
+
remoteScripts: !1,
|
|
2138
|
+
crazyGamesEnvironment: this.host.environment
|
|
2139
|
+
};
|
|
2140
|
+
try {
|
|
2141
|
+
var t, n, r, i, a, o;
|
|
2142
|
+
let s = (t = this.host.user) == null ? void 0 : t.systemInfo, c = {
|
|
2143
|
+
locale: s == null ? void 0 : s.locale,
|
|
2144
|
+
countryCode: s == null ? void 0 : s.countryCode,
|
|
2145
|
+
deviceType: s == null || (n = s.device) == null ? void 0 : n.type,
|
|
2146
|
+
osVersion: s == null || (r = s.os) == null ? void 0 : r.version,
|
|
2147
|
+
browser: s == null || (i = s.browser) == null ? void 0 : i.name,
|
|
2148
|
+
browserVersion: s == null || (a = s.browser) == null ? void 0 : a.version,
|
|
2149
|
+
applicationType: s == null ? void 0 : s.applicationType
|
|
2150
|
+
};
|
|
2151
|
+
for (let [t, n] of Object.entries(c)) typeof n == "string" && n.trim() && (e[t] = n.trim().slice(0, 128));
|
|
2152
|
+
let l = s == null || (o = s.os) == null || (o = o.name) == null ? void 0 : o.toLowerCase(), u = l && {
|
|
2153
|
+
ios: "ios",
|
|
2154
|
+
android: "android",
|
|
2155
|
+
windows: "windows",
|
|
2156
|
+
"mac os": "macos",
|
|
2157
|
+
macos: "macos",
|
|
2158
|
+
linux: "linux",
|
|
2159
|
+
"chromium os": "chromeos",
|
|
2160
|
+
"chrome os": "chromeos"
|
|
2161
|
+
}[l];
|
|
2162
|
+
u && (e.os = u);
|
|
2163
|
+
} catch {}
|
|
2164
|
+
return e;
|
|
2165
|
+
}
|
|
2166
|
+
onConfigReady() {
|
|
2167
|
+
var e;
|
|
2168
|
+
((e = this.doc) == null ? void 0 : e.visibilityState) === "hidden" && this.endSession("visibility_hidden");
|
|
2169
|
+
}
|
|
2170
|
+
close() {
|
|
2171
|
+
if (!this.closed) {
|
|
2172
|
+
this.closed = !0;
|
|
2173
|
+
for (let e of this.releases.splice(0)) e();
|
|
2174
|
+
this.ads.close(), this.tracker.close(), this.transport.close();
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
listen(e, t, n) {
|
|
2178
|
+
e == null || e.addEventListener(t, n), this.releases.push(() => e == null ? void 0 : e.removeEventListener(t, n));
|
|
2179
|
+
}
|
|
2180
|
+
endSession(e) {
|
|
2181
|
+
if (this.closed) return;
|
|
2182
|
+
this.tracker.observeSessionDurationVisibility();
|
|
2183
|
+
let t = this.tracker.currentSessionId();
|
|
2184
|
+
this.autoSessionLifecycle && this.endedSessionId !== t && this.tracker.trackSessionEndSnapshot({ reason: e }) && (this.endedSessionId = t), this.flushInBackground();
|
|
2185
|
+
}
|
|
2186
|
+
resume() {
|
|
2187
|
+
if (this.closed) return;
|
|
2188
|
+
this.tracker.observeSessionDurationVisibility();
|
|
2189
|
+
let e = this.endedSessionId !== void 0;
|
|
2190
|
+
this.endedSessionId = void 0, this.flushInBackground(), e && this.refresh({ forceRefresh: !0 }).catch(() => void 0);
|
|
2191
|
+
}
|
|
2192
|
+
flushInBackground() {
|
|
2193
|
+
this.closed || (this.tracker.flushUrgent().catch(() => void 0), this.tracker.flush().catch(() => void 0));
|
|
2194
|
+
}
|
|
2195
|
+
};
|
|
2196
|
+
function $(e) {
|
|
2197
|
+
let t = { ...e };
|
|
2198
|
+
for (let e of [
|
|
2199
|
+
"runtime",
|
|
2200
|
+
"remoteScripts",
|
|
2201
|
+
"runtimeMiniappId",
|
|
2202
|
+
"runtimeProjectId",
|
|
2203
|
+
"runtimeAppId",
|
|
2204
|
+
"runtimeIdentitySource",
|
|
2205
|
+
"wechatAppId",
|
|
2206
|
+
"crazyGamesEnvironment",
|
|
2207
|
+
"os",
|
|
2208
|
+
"osVersion",
|
|
2209
|
+
"locale",
|
|
2210
|
+
"countryCode",
|
|
2211
|
+
"deviceType",
|
|
2212
|
+
"browser",
|
|
2213
|
+
"browserVersion",
|
|
2214
|
+
"applicationType"
|
|
2215
|
+
]) delete t[e];
|
|
2216
|
+
return t;
|
|
2217
|
+
}
|
|
2218
|
+
function Ce(e) {
|
|
2219
|
+
return Se.init(e);
|
|
2220
|
+
}
|
|
2221
|
+
//#endregion
|
|
2222
|
+
export { xe as GAMEPROPEL_CRAZYGAMES_CAPABILITIES, Q as GAMEPROPEL_CRAZYGAMES_SDK_VERSION, Se as GamePropelCrazyGamesClient, ye as GamePropelCrazyGamesStorage, Ce as initGamePropelCrazyGames };
|
|
2223
|
+
|
|
2224
|
+
//# sourceMappingURL=index.js.map
|