@logludo/sdk 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/LICENSE +22 -0
- package/README.md +276 -2
- package/dist/capture.d.ts +24 -0
- package/dist/client.d.ts +166 -0
- package/dist/environment.d.ts +11 -0
- package/dist/index.cjs +2120 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.mjs +2098 -0
- package/dist/logludo.d.ts +373 -0
- package/dist/logludo.esm.js +2098 -0
- package/dist/logludo.iife.js +2 -0
- package/dist/path.d.ts +10 -0
- package/dist/storage.d.ts +10 -0
- package/dist/types.d.ts +145 -0
- package/package.json +67 -4
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2098 @@
|
|
|
1
|
+
/* @logludo/sdk v1.0.0 | Zero-Dependency JavaScript Game Analytics SDK | https://logludo.com */
|
|
2
|
+
|
|
3
|
+
// src/capture.ts
|
|
4
|
+
function resolveTargetCanvas(explicit) {
|
|
5
|
+
if (typeof explicit === "function") {
|
|
6
|
+
try {
|
|
7
|
+
const resolved = explicit();
|
|
8
|
+
if (resolved && typeof resolved.toDataURL === "function") return resolved;
|
|
9
|
+
} catch {
|
|
10
|
+
}
|
|
11
|
+
} else if (typeof explicit === "string") {
|
|
12
|
+
if (typeof document === "undefined") return null;
|
|
13
|
+
try {
|
|
14
|
+
const el = document.querySelector(explicit);
|
|
15
|
+
if (el && el.tagName === "CANVAS") return el;
|
|
16
|
+
} catch {
|
|
17
|
+
}
|
|
18
|
+
} else if (explicit && typeof explicit.toDataURL === "function") {
|
|
19
|
+
return explicit;
|
|
20
|
+
}
|
|
21
|
+
if (typeof document === "undefined") return null;
|
|
22
|
+
const canvases = Array.from(document.querySelectorAll("canvas"));
|
|
23
|
+
if (canvases.length === 0) return null;
|
|
24
|
+
let best = null;
|
|
25
|
+
let bestArea = -1;
|
|
26
|
+
for (const canvas of canvases) {
|
|
27
|
+
const rect = canvas.getBoundingClientRect();
|
|
28
|
+
const area = (rect.width || canvas.width || 0) * (rect.height || canvas.height || 0);
|
|
29
|
+
if (area > bestArea) {
|
|
30
|
+
bestArea = area;
|
|
31
|
+
best = canvas;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return best || canvases[0];
|
|
35
|
+
}
|
|
36
|
+
function canvasSliceToBase64(source, sx, sy, sw, sh) {
|
|
37
|
+
if (sx === 0 && sy === 0 && sw === source.width && sh === source.height) {
|
|
38
|
+
const dataUrl2 = source.toDataURL("image/png");
|
|
39
|
+
const idx2 = dataUrl2.indexOf(",");
|
|
40
|
+
return idx2 !== -1 ? dataUrl2.slice(idx2 + 1) : dataUrl2;
|
|
41
|
+
}
|
|
42
|
+
const tileCanvas = document.createElement("canvas");
|
|
43
|
+
tileCanvas.width = sw;
|
|
44
|
+
tileCanvas.height = sh;
|
|
45
|
+
const ctx = tileCanvas.getContext("2d");
|
|
46
|
+
if (!ctx) {
|
|
47
|
+
const dataUrl2 = source.toDataURL("image/png");
|
|
48
|
+
const idx2 = dataUrl2.indexOf(",");
|
|
49
|
+
return idx2 !== -1 ? dataUrl2.slice(idx2 + 1) : dataUrl2;
|
|
50
|
+
}
|
|
51
|
+
ctx.drawImage(source, sx, sy, sw, sh, 0, 0, sw, sh);
|
|
52
|
+
const dataUrl = tileCanvas.toDataURL("image/png");
|
|
53
|
+
const idx = dataUrl.indexOf(",");
|
|
54
|
+
return idx !== -1 ? dataUrl.slice(idx + 1) : dataUrl;
|
|
55
|
+
}
|
|
56
|
+
async function captureAndUploadCanvas(params) {
|
|
57
|
+
const { canvas, apiUrl, gameId, gameVersion, sceneName } = params;
|
|
58
|
+
const tileSize = Math.max(256, params.tileSize || 3e3);
|
|
59
|
+
const width = canvas.width || 1;
|
|
60
|
+
const height = canvas.height || 1;
|
|
61
|
+
const cols = Math.max(1, Math.ceil(width / tileSize));
|
|
62
|
+
const rows = Math.max(1, Math.ceil(height / tileSize));
|
|
63
|
+
const captureUrl = `${apiUrl.replace(/\/+$/, "")}/capture`;
|
|
64
|
+
for (let ty = 0; ty < rows; ty++) {
|
|
65
|
+
for (let tx = 0; tx < cols; tx++) {
|
|
66
|
+
const sx = tx * tileSize;
|
|
67
|
+
const sy = ty * tileSize;
|
|
68
|
+
const sw = Math.min(tileSize, width - sx);
|
|
69
|
+
const sh = Math.min(tileSize, height - sy);
|
|
70
|
+
const base64 = canvasSliceToBase64(canvas, sx, sy, sw, sh);
|
|
71
|
+
const tilePayload = {
|
|
72
|
+
game_id: gameId,
|
|
73
|
+
game_version: gameVersion,
|
|
74
|
+
room_name: sceneName,
|
|
75
|
+
image_base64: base64,
|
|
76
|
+
tile_x: tx,
|
|
77
|
+
tile_y: ty
|
|
78
|
+
};
|
|
79
|
+
const res = await fetch(captureUrl, {
|
|
80
|
+
method: "POST",
|
|
81
|
+
headers: { "Content-Type": "application/json" },
|
|
82
|
+
body: JSON.stringify(tilePayload)
|
|
83
|
+
});
|
|
84
|
+
if (!res.ok) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const manifestPayload = {
|
|
90
|
+
game_id: gameId,
|
|
91
|
+
game_version: gameVersion,
|
|
92
|
+
room_name: sceneName,
|
|
93
|
+
manifest: {
|
|
94
|
+
width,
|
|
95
|
+
height,
|
|
96
|
+
tileSize,
|
|
97
|
+
columns: cols,
|
|
98
|
+
rows
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const manifestRes = await fetch(captureUrl, {
|
|
102
|
+
method: "POST",
|
|
103
|
+
headers: { "Content-Type": "application/json" },
|
|
104
|
+
body: JSON.stringify(manifestPayload)
|
|
105
|
+
});
|
|
106
|
+
return manifestRes.ok;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// src/environment.ts
|
|
110
|
+
function detectDevMode(explicitDebug, explicitBuildType) {
|
|
111
|
+
if (explicitBuildType === "run") return true;
|
|
112
|
+
if (explicitBuildType === "build") return false;
|
|
113
|
+
if (typeof explicitDebug === "boolean") return explicitDebug;
|
|
114
|
+
try {
|
|
115
|
+
if (typeof globalThis !== "undefined") {
|
|
116
|
+
const loc = globalThis.location;
|
|
117
|
+
if (loc) {
|
|
118
|
+
const host = (loc.hostname || "").toLowerCase();
|
|
119
|
+
if (host === "localhost" || host === "127.0.0.1" || host === "[::1]" || host === "0.0.0.0" || host.endsWith(".local") || loc.protocol === "file:") {
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
if (typeof loc.search === "string" && /[?&]logludo_dev=(1|true)/i.test(loc.search)) {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
if (host.length > 0) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const proc = globalThis.process;
|
|
130
|
+
if (proc?.env?.NODE_ENV && proc.env.NODE_ENV !== "production") {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
} catch {
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
function parseBrowserName(ua) {
|
|
139
|
+
const lower = ua.toLowerCase();
|
|
140
|
+
if (lower.includes("electron")) return "Electron";
|
|
141
|
+
if (lower.includes("edg/") || lower.includes("edge/")) return "Edge";
|
|
142
|
+
if (lower.includes("opr/") || lower.includes("opera")) return "Opera";
|
|
143
|
+
if (lower.includes("chrome/") || lower.includes("crios/")) return "Chrome";
|
|
144
|
+
if (lower.includes("firefox/") || lower.includes("fxios/")) return "Firefox";
|
|
145
|
+
if (lower.includes("safari/")) {
|
|
146
|
+
return /iphone|ipad|ipod|mobile/i.test(ua) ? "Mobile Safari" : "Safari";
|
|
147
|
+
}
|
|
148
|
+
if (lower.includes("msie") || lower.includes("trident/")) return "IE";
|
|
149
|
+
return "Unknown";
|
|
150
|
+
}
|
|
151
|
+
function parseOsName(ua, platformHint) {
|
|
152
|
+
const source = `${platformHint || ""} ${ua}`.toLowerCase();
|
|
153
|
+
if (/iphone|ipad|ipod|ios/.test(source)) return "iOS";
|
|
154
|
+
if (/android/.test(source)) return "Android";
|
|
155
|
+
if (/cros/.test(source)) return "ChromeOS";
|
|
156
|
+
if (/win/.test(source)) return "Windows";
|
|
157
|
+
if (/mac|darwin/.test(source)) return "macOS";
|
|
158
|
+
if (/linux|x11/.test(source)) return "Linux";
|
|
159
|
+
return "Unknown";
|
|
160
|
+
}
|
|
161
|
+
function detectEnvironment() {
|
|
162
|
+
const fallback = {
|
|
163
|
+
platform: "Web",
|
|
164
|
+
clientType: "Web",
|
|
165
|
+
osInfo: "Unknown",
|
|
166
|
+
deviceModel: "Unknown",
|
|
167
|
+
domain: "",
|
|
168
|
+
url: ""
|
|
169
|
+
};
|
|
170
|
+
if (typeof globalThis === "undefined") return fallback;
|
|
171
|
+
const g = globalThis;
|
|
172
|
+
const nav = g.navigator;
|
|
173
|
+
const loc = g.location;
|
|
174
|
+
const ua = nav?.userAgent || "";
|
|
175
|
+
const uaPlatform = nav?.userAgentData?.platform || nav?.platform || "";
|
|
176
|
+
const isElectron = ua.includes("Electron") || Boolean(g.process?.versions?.electron);
|
|
177
|
+
const isTauri = Boolean(g.__TAURI__ || g.__TAURI_INTERNALS__);
|
|
178
|
+
const isNwjs = Boolean(g.nw);
|
|
179
|
+
const capacitor = g.Capacitor;
|
|
180
|
+
const isCapacitorNative = Boolean(capacitor?.isNativePlatform && capacitor.isNativePlatform());
|
|
181
|
+
const isCordova = Boolean(g.cordova);
|
|
182
|
+
const osInfo = parseOsName(ua, uaPlatform);
|
|
183
|
+
let deviceModel = ua ? parseBrowserName(ua) : "Node.js";
|
|
184
|
+
let platform = "Web";
|
|
185
|
+
let clientType = "Web";
|
|
186
|
+
if (isElectron || isTauri || isNwjs) {
|
|
187
|
+
platform = osInfo !== "Unknown" ? osInfo : "Desktop";
|
|
188
|
+
clientType = "Desktop";
|
|
189
|
+
if (isElectron) deviceModel = "Electron";
|
|
190
|
+
else if (isTauri) deviceModel = "Tauri";
|
|
191
|
+
} else if (isCapacitorNative || isCordova) {
|
|
192
|
+
const capPlat = capacitor?.getPlatform?.();
|
|
193
|
+
if (capPlat === "ios" || osInfo === "iOS") {
|
|
194
|
+
platform = "iOS";
|
|
195
|
+
clientType = "Mobile";
|
|
196
|
+
} else if (capPlat === "android" || osInfo === "Android") {
|
|
197
|
+
platform = "Android";
|
|
198
|
+
clientType = "Mobile";
|
|
199
|
+
} else {
|
|
200
|
+
platform = osInfo;
|
|
201
|
+
clientType = "Mobile";
|
|
202
|
+
}
|
|
203
|
+
} else if (!loc && typeof g.process !== "undefined") {
|
|
204
|
+
platform = "Desktop";
|
|
205
|
+
clientType = "Desktop";
|
|
206
|
+
const nodeProc = g.process;
|
|
207
|
+
if (nodeProc.platform === "darwin") fallback.osInfo = "macOS";
|
|
208
|
+
else if (nodeProc.platform === "win32") fallback.osInfo = "Windows";
|
|
209
|
+
else if (nodeProc.platform === "linux") fallback.osInfo = "Linux";
|
|
210
|
+
return {
|
|
211
|
+
...fallback,
|
|
212
|
+
platform: fallback.osInfo !== "Unknown" ? fallback.osInfo : "Desktop",
|
|
213
|
+
clientType: "Desktop",
|
|
214
|
+
deviceModel: "Node.js"
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
let domain = "";
|
|
218
|
+
let url = "";
|
|
219
|
+
if (loc) {
|
|
220
|
+
if (loc.hostname) domain = loc.hostname;
|
|
221
|
+
if (loc.href && (loc.protocol === "http:" || loc.protocol === "https:")) {
|
|
222
|
+
url = loc.href;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
platform,
|
|
227
|
+
clientType,
|
|
228
|
+
osInfo,
|
|
229
|
+
deviceModel,
|
|
230
|
+
domain,
|
|
231
|
+
url
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
function detectFramework(explicitFramework) {
|
|
235
|
+
if (explicitFramework && explicitFramework.trim()) {
|
|
236
|
+
return explicitFramework.trim();
|
|
237
|
+
}
|
|
238
|
+
if (typeof globalThis === "undefined") return "Vanilla";
|
|
239
|
+
const g = globalThis;
|
|
240
|
+
if (g.Phaser) return "Phaser";
|
|
241
|
+
if (g.PIXI) return "PixiJS";
|
|
242
|
+
if (g.THREE) return "Three.js";
|
|
243
|
+
if (g.BABYLON) return "Babylon.js";
|
|
244
|
+
if (g.kaplay || g.kaboom) return "KAPLAY";
|
|
245
|
+
if (g.ex && typeof g.ex.Engine === "function") return "Excalibur";
|
|
246
|
+
if (g.pc && typeof g.pc.Application === "function") return "PlayCanvas";
|
|
247
|
+
return "Vanilla";
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// src/path.ts
|
|
251
|
+
function formatCoord(val) {
|
|
252
|
+
const t = Math.round(val * 10);
|
|
253
|
+
if (t === 0) return "0";
|
|
254
|
+
const neg = t < 0;
|
|
255
|
+
const absT = Math.abs(t);
|
|
256
|
+
const whole = Math.floor(absT / 10);
|
|
257
|
+
const frac = absT % 10;
|
|
258
|
+
const str = frac === 0 ? String(whole) : `${whole}.${frac}`;
|
|
259
|
+
return neg ? `-${str}` : str;
|
|
260
|
+
}
|
|
261
|
+
function minifyPath(rawPath, ax = 0, ay = 0, curX, curY, maxPoints = 0) {
|
|
262
|
+
if (!Array.isArray(rawPath)) return "";
|
|
263
|
+
const rawLen = rawPath.length;
|
|
264
|
+
const startIdx = maxPoints > 0 && rawLen > maxPoints ? rawLen - maxPoints : 0;
|
|
265
|
+
const pxArr = [];
|
|
266
|
+
const pyArr = [];
|
|
267
|
+
let lastX = 0;
|
|
268
|
+
let lastY = 0;
|
|
269
|
+
for (let i = startIdx; i < rawLen; i++) {
|
|
270
|
+
const p = rawPath[i];
|
|
271
|
+
if (!Array.isArray(p) || p.length < 2) continue;
|
|
272
|
+
const rawX = Number(p[0]);
|
|
273
|
+
const rawY = Number(p[1]);
|
|
274
|
+
if (!Number.isFinite(rawX) || !Number.isFinite(rawY)) continue;
|
|
275
|
+
const px = rawX - ax;
|
|
276
|
+
const py = rawY - ay;
|
|
277
|
+
if (pxArr.length === 0 || Math.abs(px - lastX) >= 0.75 || Math.abs(py - lastY) >= 0.75) {
|
|
278
|
+
pxArr.push(px);
|
|
279
|
+
pyArr.push(py);
|
|
280
|
+
lastX = px;
|
|
281
|
+
lastY = py;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (typeof curX === "number" && typeof curY === "number" && Number.isFinite(curX) && Number.isFinite(curY)) {
|
|
285
|
+
if (pxArr.length === 0 || Math.abs(curX - lastX) >= 0.75 || Math.abs(curY - lastY) >= 0.75) {
|
|
286
|
+
pxArr.push(curX);
|
|
287
|
+
pyArr.push(curY);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
const n = pxArr.length;
|
|
291
|
+
if (n < 2) return "";
|
|
292
|
+
const keep = new Array(n).fill(false);
|
|
293
|
+
keep[0] = true;
|
|
294
|
+
keep[n - 1] = true;
|
|
295
|
+
if (n > 2) {
|
|
296
|
+
const stack = [[0, n - 1]];
|
|
297
|
+
const epsSq = 2.25;
|
|
298
|
+
while (stack.length > 0) {
|
|
299
|
+
const seg = stack.pop();
|
|
300
|
+
const i0 = seg[0];
|
|
301
|
+
const i1 = seg[1];
|
|
302
|
+
if (i1 - i0 <= 1) continue;
|
|
303
|
+
const x0 = pxArr[i0];
|
|
304
|
+
const y0 = pyArr[i0];
|
|
305
|
+
const x1 = pxArr[i1];
|
|
306
|
+
const y1 = pyArr[i1];
|
|
307
|
+
const dx = x1 - x0;
|
|
308
|
+
const dy = y1 - y0;
|
|
309
|
+
const lenSq = dx * dx + dy * dy;
|
|
310
|
+
let maxDSq = -1;
|
|
311
|
+
let maxIdx = -1;
|
|
312
|
+
for (let j = i0 + 1; j < i1; j++) {
|
|
313
|
+
const qx = pxArr[j];
|
|
314
|
+
const qy = pyArr[j];
|
|
315
|
+
let dSq;
|
|
316
|
+
if (lenSq <= 1e-4) {
|
|
317
|
+
const ox = qx - x0;
|
|
318
|
+
const oy = qy - y0;
|
|
319
|
+
dSq = ox * ox + oy * oy;
|
|
320
|
+
} else {
|
|
321
|
+
let t = ((qx - x0) * dx + (qy - y0) * dy) / lenSq;
|
|
322
|
+
if (t < 0) t = 0;
|
|
323
|
+
else if (t > 1) t = 1;
|
|
324
|
+
const ox = qx - (x0 + t * dx);
|
|
325
|
+
const oy = qy - (y0 + t * dy);
|
|
326
|
+
dSq = ox * ox + oy * oy;
|
|
327
|
+
}
|
|
328
|
+
if (dSq > maxDSq) {
|
|
329
|
+
maxDSq = dSq;
|
|
330
|
+
maxIdx = j;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (maxDSq > epsSq && maxIdx !== -1) {
|
|
334
|
+
keep[maxIdx] = true;
|
|
335
|
+
stack.push([i0, maxIdx]);
|
|
336
|
+
stack.push([maxIdx, i1]);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
const parts = [];
|
|
341
|
+
for (let k = 0; k < n; k++) {
|
|
342
|
+
if (keep[k]) {
|
|
343
|
+
parts.push(`${formatCoord(pxArr[k])},${formatCoord(pyArr[k])}`);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return parts.join(";");
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// src/storage.ts
|
|
350
|
+
function createSafeStorage() {
|
|
351
|
+
const memory = /* @__PURE__ */ new Map();
|
|
352
|
+
let browserStorage = null;
|
|
353
|
+
try {
|
|
354
|
+
if (typeof globalThis !== "undefined" && "localStorage" in globalThis && globalThis.localStorage) {
|
|
355
|
+
const testKey = "__logludo_storage_probe__";
|
|
356
|
+
globalThis.localStorage.setItem(testKey, "1");
|
|
357
|
+
globalThis.localStorage.removeItem(testKey);
|
|
358
|
+
browserStorage = globalThis.localStorage;
|
|
359
|
+
}
|
|
360
|
+
} catch {
|
|
361
|
+
browserStorage = null;
|
|
362
|
+
}
|
|
363
|
+
return {
|
|
364
|
+
getItem(key) {
|
|
365
|
+
if (browserStorage) {
|
|
366
|
+
try {
|
|
367
|
+
const val = browserStorage.getItem(key);
|
|
368
|
+
if (val !== null) return val;
|
|
369
|
+
} catch {
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return memory.has(key) ? memory.get(key) : null;
|
|
373
|
+
},
|
|
374
|
+
setItem(key, value) {
|
|
375
|
+
memory.set(key, value);
|
|
376
|
+
if (browserStorage) {
|
|
377
|
+
try {
|
|
378
|
+
browserStorage.setItem(key, value);
|
|
379
|
+
} catch {
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
removeItem(key) {
|
|
384
|
+
memory.delete(key);
|
|
385
|
+
if (browserStorage) {
|
|
386
|
+
try {
|
|
387
|
+
browserStorage.removeItem(key);
|
|
388
|
+
} catch {
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// src/client.ts
|
|
396
|
+
var SDK_VERSION = "js-1.0.0";
|
|
397
|
+
var DEFAULT_API_URL = "https://api.logludo.com/v0";
|
|
398
|
+
var GAME_ID_PLACEHOLDER = "__LOGLUDO_INJECT_GAME_ID_HERE__";
|
|
399
|
+
var ALLOWED_CATEGORIES = /* @__PURE__ */ new Set([
|
|
400
|
+
"session_start",
|
|
401
|
+
"session_end",
|
|
402
|
+
"error",
|
|
403
|
+
"progression",
|
|
404
|
+
"monetization",
|
|
405
|
+
"gameplay",
|
|
406
|
+
"design",
|
|
407
|
+
"performance"
|
|
408
|
+
]);
|
|
409
|
+
function getTodayUtcDateStr(nowSec) {
|
|
410
|
+
const d = new Date(nowSec * 1e3);
|
|
411
|
+
const y = d.getUTCFullYear();
|
|
412
|
+
const m = d.getUTCMonth() + 1;
|
|
413
|
+
const day = d.getUTCDate();
|
|
414
|
+
return `${y}-${m}-${day}`;
|
|
415
|
+
}
|
|
416
|
+
function isGameIdValid(id) {
|
|
417
|
+
if (typeof id !== "string") return false;
|
|
418
|
+
const trimmed = id.trim();
|
|
419
|
+
return trimmed.length > 0 && !trimmed.includes("INJECT_GAME_ID");
|
|
420
|
+
}
|
|
421
|
+
function clonePayload(src) {
|
|
422
|
+
if (!src || typeof src !== "object") return {};
|
|
423
|
+
const copy = { ...src };
|
|
424
|
+
if (src.scenelytics && typeof src.scenelytics === "object") {
|
|
425
|
+
copy.scenelytics = { ...src.scenelytics };
|
|
426
|
+
}
|
|
427
|
+
return copy;
|
|
428
|
+
}
|
|
429
|
+
function extractTargetCoords(target) {
|
|
430
|
+
if (!target || typeof target === "string") return null;
|
|
431
|
+
const obj = target;
|
|
432
|
+
if (typeof obj.x === "number" && typeof obj.y === "number" && Number.isFinite(obj.x) && Number.isFinite(obj.y)) {
|
|
433
|
+
return { x: obj.x, y: obj.y };
|
|
434
|
+
}
|
|
435
|
+
if (obj.position && typeof obj.position.x === "number" && typeof obj.position.y === "number" && Number.isFinite(obj.position.x) && Number.isFinite(obj.position.y)) {
|
|
436
|
+
return { x: obj.position.x, y: obj.position.y };
|
|
437
|
+
}
|
|
438
|
+
return null;
|
|
439
|
+
}
|
|
440
|
+
var LogLudoClient = class {
|
|
441
|
+
constructor(initialOptions) {
|
|
442
|
+
this.initialized = false;
|
|
443
|
+
this.sessionId = "";
|
|
444
|
+
this.deviceId = "";
|
|
445
|
+
this.bucket = 1;
|
|
446
|
+
this.daysSinceInstall = 0;
|
|
447
|
+
this.sessionCount = 0;
|
|
448
|
+
this.activeMask = 0;
|
|
449
|
+
this.firstOpenDate = 0;
|
|
450
|
+
this.installTime = 0;
|
|
451
|
+
this.isFirstSessionToday = false;
|
|
452
|
+
this.isDev = false;
|
|
453
|
+
this.framework = "Vanilla";
|
|
454
|
+
this._completedFunnelSteps = [];
|
|
455
|
+
this._completedFunnelDropoffs = [];
|
|
456
|
+
this._eventBuffer = [];
|
|
457
|
+
this._bufferRaws = [];
|
|
458
|
+
this._inFlightBatchCount = 0;
|
|
459
|
+
this._isFlushing = false;
|
|
460
|
+
this._flushQueued = false;
|
|
461
|
+
this._flushQueuedForce = false;
|
|
462
|
+
this._offlineDirty = false;
|
|
463
|
+
this._retryAfterTime = 0;
|
|
464
|
+
this._retryBackoffMs = 0;
|
|
465
|
+
this._recentEventHistory = [];
|
|
466
|
+
this._dedupHistoryLimit = 32;
|
|
467
|
+
this._dedupWarnedAt = -99999;
|
|
468
|
+
this._frame = 0;
|
|
469
|
+
this._spamTracker = /* @__PURE__ */ new Map();
|
|
470
|
+
this._pathCallTracker = /* @__PURE__ */ new Map();
|
|
471
|
+
this._rateWarnedAt = -99999;
|
|
472
|
+
this._eventsThisSecond = 0;
|
|
473
|
+
this._lastEventSecond = 0;
|
|
474
|
+
this._lastFlushTime = 0;
|
|
475
|
+
this._gameIdWarned = false;
|
|
476
|
+
this._activeExperiments = [];
|
|
477
|
+
this._experimentFlags = {};
|
|
478
|
+
this._experimentsLoaded = false;
|
|
479
|
+
this._experimentReadyCallbacks = [];
|
|
480
|
+
this._experimentWarned = /* @__PURE__ */ new Set();
|
|
481
|
+
this._expTimeoutHandle = null;
|
|
482
|
+
this._trackedPaths = /* @__PURE__ */ new Map();
|
|
483
|
+
this._targetKeyMap = /* @__PURE__ */ new WeakMap();
|
|
484
|
+
this._targetKeySeq = 0;
|
|
485
|
+
this._activeLinks = /* @__PURE__ */ new Map();
|
|
486
|
+
this._linkSeq = 0;
|
|
487
|
+
this._aggregates = /* @__PURE__ */ new Map();
|
|
488
|
+
this._sessionStartTimeMs = Date.now();
|
|
489
|
+
this._currentSceneName = "main";
|
|
490
|
+
this._sceneEnterTimeMs = Date.now();
|
|
491
|
+
this._scenePingSent = false;
|
|
492
|
+
this._lastScenePingMs = 0;
|
|
493
|
+
this._sessionEnded = false;
|
|
494
|
+
this._fpsSamples = [];
|
|
495
|
+
this._lastRafTime = 0;
|
|
496
|
+
this._rafId = null;
|
|
497
|
+
this._flushTimer = null;
|
|
498
|
+
this._offlineSaveTimer = null;
|
|
499
|
+
this._cleanupListeners = [];
|
|
500
|
+
this._explicitFlushInterval = false;
|
|
501
|
+
this._storage = createSafeStorage();
|
|
502
|
+
const isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
|
|
503
|
+
this._options = {
|
|
504
|
+
apiUrl: DEFAULT_API_URL,
|
|
505
|
+
gameId: GAME_ID_PLACEHOLDER,
|
|
506
|
+
gameVersion: "1.0.0",
|
|
507
|
+
logLevel: "info",
|
|
508
|
+
captureCrashes: true,
|
|
509
|
+
autoPerformance: true,
|
|
510
|
+
performanceDelayMs: 1e4,
|
|
511
|
+
performanceIntervalMs: 0,
|
|
512
|
+
captureHotkeysEnabled: true,
|
|
513
|
+
captureRequireModifier: true,
|
|
514
|
+
autoFlush: true,
|
|
515
|
+
batchLimit: isBrowser ? 4 : 10,
|
|
516
|
+
flushIntervalMs: 5e3,
|
|
517
|
+
dedupWindowMs: 2e3,
|
|
518
|
+
defaultExperimentPercentage: 50,
|
|
519
|
+
autoFlushOnSceneChange: false,
|
|
520
|
+
storagePrefix: "logludo",
|
|
521
|
+
anchorX: 0,
|
|
522
|
+
anchorY: 0
|
|
523
|
+
};
|
|
524
|
+
if (initialOptions) {
|
|
525
|
+
this._applyOptions(initialOptions);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
_logRank(level) {
|
|
529
|
+
switch (level) {
|
|
530
|
+
case "silent":
|
|
531
|
+
return 0;
|
|
532
|
+
case "errors":
|
|
533
|
+
return 1;
|
|
534
|
+
case "info":
|
|
535
|
+
return 2;
|
|
536
|
+
case "verbose":
|
|
537
|
+
return 3;
|
|
538
|
+
default:
|
|
539
|
+
return 2;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
_log(level, message, ...args) {
|
|
543
|
+
if (!this.isDev && level !== "errors") return;
|
|
544
|
+
if (this._logRank(this._options.logLevel) < this._logRank(level)) return;
|
|
545
|
+
if (level === "errors") {
|
|
546
|
+
console.error(message, ...args);
|
|
547
|
+
} else {
|
|
548
|
+
console.log(message, ...args);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
_storageKey(name) {
|
|
552
|
+
const gid = isGameIdValid(this._options.gameId) ? this._options.gameId : "default";
|
|
553
|
+
return `${this._options.storagePrefix}_${name}_${gid}`;
|
|
554
|
+
}
|
|
555
|
+
_applyOptions(options) {
|
|
556
|
+
const gameId = options.gameId ?? options.game_ID;
|
|
557
|
+
if (typeof gameId === "string" && gameId.trim()) {
|
|
558
|
+
this._options.gameId = gameId.trim();
|
|
559
|
+
}
|
|
560
|
+
const apiUrl = options.apiUrl ?? options.api_url;
|
|
561
|
+
if (typeof apiUrl === "string" && apiUrl.trim()) {
|
|
562
|
+
this._options.apiUrl = apiUrl.trim().replace(/\/+$/, "");
|
|
563
|
+
}
|
|
564
|
+
const gameVersion = options.gameVersion ?? options.appVersion;
|
|
565
|
+
if (typeof gameVersion === "string" && gameVersion.trim()) {
|
|
566
|
+
this._options.gameVersion = gameVersion.trim();
|
|
567
|
+
}
|
|
568
|
+
const logLevel = options.logLevel ?? options.log_level;
|
|
569
|
+
if (logLevel) {
|
|
570
|
+
this._options.logLevel = logLevel;
|
|
571
|
+
}
|
|
572
|
+
if (typeof options.debug === "boolean") {
|
|
573
|
+
this._options.explicitDebug = options.debug;
|
|
574
|
+
}
|
|
575
|
+
if (options.buildType) {
|
|
576
|
+
this._options.explicitBuildType = options.buildType;
|
|
577
|
+
}
|
|
578
|
+
if (typeof options.framework === "string" && options.framework.trim()) {
|
|
579
|
+
this._options.explicitFramework = options.framework.trim();
|
|
580
|
+
}
|
|
581
|
+
const captureCrashes = options.captureCrashes ?? options.capture_crashes;
|
|
582
|
+
if (typeof captureCrashes === "boolean") {
|
|
583
|
+
this._options.captureCrashes = captureCrashes;
|
|
584
|
+
}
|
|
585
|
+
if (typeof options.autoPerformance === "boolean") {
|
|
586
|
+
this._options.autoPerformance = options.autoPerformance;
|
|
587
|
+
}
|
|
588
|
+
if (typeof options.performanceDelayMs === "number") {
|
|
589
|
+
this._options.performanceDelayMs = Math.max(0, options.performanceDelayMs);
|
|
590
|
+
}
|
|
591
|
+
if (typeof options.performanceIntervalMs === "number") {
|
|
592
|
+
this._options.performanceIntervalMs = Math.max(0, options.performanceIntervalMs);
|
|
593
|
+
}
|
|
594
|
+
if (typeof options.captureHotkeysEnabled === "boolean") {
|
|
595
|
+
this._options.captureHotkeysEnabled = options.captureHotkeysEnabled;
|
|
596
|
+
}
|
|
597
|
+
if (typeof options.captureRequireModifier === "boolean") {
|
|
598
|
+
this._options.captureRequireModifier = options.captureRequireModifier;
|
|
599
|
+
}
|
|
600
|
+
if (options.canvas !== void 0) {
|
|
601
|
+
this._options.canvas = options.canvas;
|
|
602
|
+
}
|
|
603
|
+
if (typeof options.initialScene === "string" && options.initialScene.trim()) {
|
|
604
|
+
this._currentSceneName = options.initialScene.trim();
|
|
605
|
+
}
|
|
606
|
+
if (typeof options.autoFlush === "boolean") {
|
|
607
|
+
this._options.autoFlush = options.autoFlush;
|
|
608
|
+
}
|
|
609
|
+
if (typeof options.batchLimit === "number" && options.batchLimit > 0) {
|
|
610
|
+
this._options.batchLimit = Math.floor(options.batchLimit);
|
|
611
|
+
}
|
|
612
|
+
if (typeof options.flushIntervalMs === "number" && options.flushIntervalMs >= 250) {
|
|
613
|
+
this._options.flushIntervalMs = Math.floor(options.flushIntervalMs);
|
|
614
|
+
this._explicitFlushInterval = true;
|
|
615
|
+
}
|
|
616
|
+
if (typeof options.dedupWindowMs === "number" && options.dedupWindowMs >= 0) {
|
|
617
|
+
this._options.dedupWindowMs = Math.floor(options.dedupWindowMs);
|
|
618
|
+
}
|
|
619
|
+
if (typeof options.defaultExperimentPercentage === "number") {
|
|
620
|
+
this._options.defaultExperimentPercentage = Math.max(0, Math.min(100, options.defaultExperimentPercentage));
|
|
621
|
+
}
|
|
622
|
+
if (typeof options.autoFlushOnSceneChange === "boolean") {
|
|
623
|
+
this._options.autoFlushOnSceneChange = options.autoFlushOnSceneChange;
|
|
624
|
+
}
|
|
625
|
+
if (options.sessionStartPayload && typeof options.sessionStartPayload === "object") {
|
|
626
|
+
this._options.sessionStartPayload = { ...options.sessionStartPayload };
|
|
627
|
+
}
|
|
628
|
+
if (typeof options.userId === "string" && options.userId.trim()) {
|
|
629
|
+
this._options.userId = options.userId.trim();
|
|
630
|
+
}
|
|
631
|
+
if (typeof options.storagePrefix === "string" && options.storagePrefix.trim()) {
|
|
632
|
+
this._options.storagePrefix = options.storagePrefix.trim();
|
|
633
|
+
}
|
|
634
|
+
if (typeof options.anchorX === "number" && Number.isFinite(options.anchorX)) {
|
|
635
|
+
this._options.anchorX = options.anchorX;
|
|
636
|
+
}
|
|
637
|
+
if (typeof options.anchorY === "number" && Number.isFinite(options.anchorY)) {
|
|
638
|
+
this._options.anchorY = options.anchorY;
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
configure(options) {
|
|
642
|
+
const wasConfigured = isGameIdValid(this._options.gameId);
|
|
643
|
+
this._applyOptions(options);
|
|
644
|
+
this.isDev = detectDevMode(this._options.explicitDebug, this._options.explicitBuildType);
|
|
645
|
+
this.framework = detectFramework(this._options.explicitFramework);
|
|
646
|
+
if (this.initialized && !wasConfigured && isGameIdValid(this._options.gameId)) {
|
|
647
|
+
this._gameIdWarned = false;
|
|
648
|
+
this._loadOrCreateDeviceData(Math.floor(Date.now() / 1e3));
|
|
649
|
+
for (const envItem of this._eventBuffer) {
|
|
650
|
+
if (!isGameIdValid(envItem.game_id)) {
|
|
651
|
+
envItem.game_id = this._options.gameId;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
this.saveDeviceData();
|
|
655
|
+
void this.fetchExperiments();
|
|
656
|
+
if (this._eventBuffer.length > 0 && this._options.autoFlush) {
|
|
657
|
+
void this.flush(false);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
return this;
|
|
661
|
+
}
|
|
662
|
+
init(options) {
|
|
663
|
+
if (options) {
|
|
664
|
+
this._applyOptions(options);
|
|
665
|
+
}
|
|
666
|
+
if (this.initialized) {
|
|
667
|
+
return this;
|
|
668
|
+
}
|
|
669
|
+
this.initialized = true;
|
|
670
|
+
this._sessionEnded = false;
|
|
671
|
+
this.isDev = detectDevMode(this._options.explicitDebug, this._options.explicitBuildType);
|
|
672
|
+
this.framework = detectFramework(this._options.explicitFramework);
|
|
673
|
+
if (!this._explicitFlushInterval) {
|
|
674
|
+
this._options.flushIntervalMs = this.isDev ? 2e3 : 5e3;
|
|
675
|
+
}
|
|
676
|
+
const nowMs = Date.now();
|
|
677
|
+
const nowSec = Math.floor(nowMs / 1e3);
|
|
678
|
+
this._sessionStartTimeMs = nowMs;
|
|
679
|
+
this._sceneEnterTimeMs = nowMs;
|
|
680
|
+
this._scenePingSent = false;
|
|
681
|
+
this._lastScenePingMs = 0;
|
|
682
|
+
this._lastFlushTime = nowMs;
|
|
683
|
+
this._recentEventHistory = [];
|
|
684
|
+
this._bufferRaws = [];
|
|
685
|
+
this._spamTracker.clear();
|
|
686
|
+
this._pathCallTracker.clear();
|
|
687
|
+
this._retryAfterTime = 0;
|
|
688
|
+
this._retryBackoffMs = 0;
|
|
689
|
+
if (!options?.initialScene && typeof document !== "undefined" && document.title) {
|
|
690
|
+
this._currentSceneName = document.title.trim() || "main";
|
|
691
|
+
}
|
|
692
|
+
const randSuffix = Math.floor(Math.random() * 1e6);
|
|
693
|
+
this.sessionId = `sess_${nowSec}_${nowMs % 1e5}_${randSuffix}`;
|
|
694
|
+
this._loadOfflineBuffer();
|
|
695
|
+
this._loadOrCreateDeviceData(nowSec);
|
|
696
|
+
if (!isGameIdValid(this._options.gameId) && !this._gameIdWarned) {
|
|
697
|
+
this._gameIdWarned = true;
|
|
698
|
+
this._log(
|
|
699
|
+
"errors",
|
|
700
|
+
">> LOGLUDO WARNING: gameId is not configured. Pass { gameId: '...' } to LOGLUDO.init(). Telemetry uploads are paused until configured."
|
|
701
|
+
);
|
|
702
|
+
}
|
|
703
|
+
this._loadCachedExperiments();
|
|
704
|
+
void this.fetchExperiments();
|
|
705
|
+
this._flushPendingCrashFromStorage();
|
|
706
|
+
this._attachBrowserListeners();
|
|
707
|
+
this._startTimers();
|
|
708
|
+
this._ensureRafLoop();
|
|
709
|
+
const envInfo = detectEnvironment();
|
|
710
|
+
this.sendEvent("session_start", {
|
|
711
|
+
app_version: this._options.gameVersion,
|
|
712
|
+
sdk_version: SDK_VERSION,
|
|
713
|
+
sdk_engine: "JavaScript",
|
|
714
|
+
framework: this.framework,
|
|
715
|
+
platform: envInfo.platform,
|
|
716
|
+
client_type: envInfo.clientType,
|
|
717
|
+
os_info: envInfo.osInfo,
|
|
718
|
+
device_model: envInfo.deviceModel,
|
|
719
|
+
...envInfo.domain ? { domain: envInfo.domain } : {},
|
|
720
|
+
...envInfo.url ? { url: envInfo.url } : {},
|
|
721
|
+
days_since_install: this.daysSinceInstall,
|
|
722
|
+
test_bucket: this.bucket,
|
|
723
|
+
session_count: this.sessionCount,
|
|
724
|
+
active_mask: this.activeMask,
|
|
725
|
+
first_open_date: this.firstOpenDate,
|
|
726
|
+
is_first_session_today: this.isFirstSessionToday,
|
|
727
|
+
scene_name: this._currentSceneName,
|
|
728
|
+
...this._options.sessionStartPayload || {}
|
|
729
|
+
});
|
|
730
|
+
return this;
|
|
731
|
+
}
|
|
732
|
+
_loadOfflineBuffer() {
|
|
733
|
+
const raw = this._storage.getItem(this._storageKey("offline"));
|
|
734
|
+
if (!raw) return;
|
|
735
|
+
try {
|
|
736
|
+
const parsed = JSON.parse(raw);
|
|
737
|
+
if (Array.isArray(parsed)) {
|
|
738
|
+
const sliced = parsed.length > 400 ? parsed.slice(parsed.length - 400) : parsed;
|
|
739
|
+
this._eventBuffer = sliced.filter(
|
|
740
|
+
(e) => Boolean(e && typeof e === "object" && typeof e.event_name === "string")
|
|
741
|
+
);
|
|
742
|
+
this._bufferRaws = new Array(this._eventBuffer.length).fill("");
|
|
743
|
+
this._offlineDirty = this._eventBuffer.length > 0;
|
|
744
|
+
}
|
|
745
|
+
} catch {
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
_loadOrCreateDeviceData(nowSec) {
|
|
749
|
+
const todayStr = getTodayUtcDateStr(nowSec);
|
|
750
|
+
let reset = false;
|
|
751
|
+
this.installTime = nowSec;
|
|
752
|
+
const raw = this._storage.getItem(this._storageKey("device"));
|
|
753
|
+
if (raw) {
|
|
754
|
+
try {
|
|
755
|
+
const data = JSON.parse(raw);
|
|
756
|
+
if (isGameIdValid(this._options.gameId) && data.game_id && isGameIdValid(data.game_id) && data.game_id !== this._options.gameId) {
|
|
757
|
+
reset = true;
|
|
758
|
+
} else {
|
|
759
|
+
if (typeof data.bucket === "number") this.bucket = data.bucket;
|
|
760
|
+
if (typeof data.device_id === "string") this.deviceId = data.device_id;
|
|
761
|
+
if (typeof data.install_time === "number") this.installTime = data.install_time;
|
|
762
|
+
if (typeof data.session_count === "number") this.sessionCount = data.session_count;
|
|
763
|
+
if (typeof data.active_mask === "number") this.activeMask = data.active_mask;
|
|
764
|
+
if (typeof data.first_open_date === "number") this.firstOpenDate = data.first_open_date;
|
|
765
|
+
this._completedFunnelSteps = Array.isArray(data.completed_funnel_steps) ? data.completed_funnel_steps.map(String) : [];
|
|
766
|
+
this._completedFunnelDropoffs = Array.isArray(data.completed_funnel_dropoffs) ? data.completed_funnel_dropoffs.map(String) : [];
|
|
767
|
+
if (!Number.isFinite(this.firstOpenDate) || this.firstOpenDate < 1e9) {
|
|
768
|
+
this.firstOpenDate = nowSec;
|
|
769
|
+
}
|
|
770
|
+
this.isFirstSessionToday = data.last_session_date !== todayStr;
|
|
771
|
+
}
|
|
772
|
+
} catch {
|
|
773
|
+
reset = true;
|
|
774
|
+
}
|
|
775
|
+
} else {
|
|
776
|
+
reset = true;
|
|
777
|
+
}
|
|
778
|
+
if (reset) {
|
|
779
|
+
this.bucket = Math.floor(Math.random() * 10) + 1;
|
|
780
|
+
this.firstOpenDate = nowSec;
|
|
781
|
+
this.deviceId = this._generateDeviceId(nowSec);
|
|
782
|
+
this.sessionCount = 0;
|
|
783
|
+
this.activeMask = 0;
|
|
784
|
+
this.installTime = nowSec;
|
|
785
|
+
this.isFirstSessionToday = true;
|
|
786
|
+
this._completedFunnelSteps = [];
|
|
787
|
+
this._completedFunnelDropoffs = [];
|
|
788
|
+
} else if (!this.deviceId) {
|
|
789
|
+
this.deviceId = this._generateDeviceId(this.firstOpenDate || nowSec);
|
|
790
|
+
}
|
|
791
|
+
this.sessionCount += 1;
|
|
792
|
+
this.daysSinceInstall = Math.max(0, Math.floor((nowSec - this.firstOpenDate) / 86400));
|
|
793
|
+
if (this.daysSinceInstall <= 30) {
|
|
794
|
+
this.activeMask = this.activeMask | 1 << this.daysSinceInstall;
|
|
795
|
+
}
|
|
796
|
+
this.saveDeviceData();
|
|
797
|
+
}
|
|
798
|
+
_generateDeviceId(seedSec) {
|
|
799
|
+
const cryptoObj = typeof globalThis !== "undefined" ? globalThis.crypto : void 0;
|
|
800
|
+
if (cryptoObj && typeof cryptoObj.randomUUID === "function") {
|
|
801
|
+
return `dev_${seedSec}_${cryptoObj.randomUUID().slice(0, 12)}`;
|
|
802
|
+
}
|
|
803
|
+
const randA = Math.floor(1e5 + Math.random() * 9e5);
|
|
804
|
+
const randB = Math.floor(Math.random() * 1e6);
|
|
805
|
+
return `dev_${seedSec}_${randA}_${randB}`;
|
|
806
|
+
}
|
|
807
|
+
saveDeviceData() {
|
|
808
|
+
const todayStr = getTodayUtcDateStr(Math.floor(Date.now() / 1e3));
|
|
809
|
+
const deviceData = {
|
|
810
|
+
game_id: this._options.gameId,
|
|
811
|
+
bucket: this.bucket,
|
|
812
|
+
device_id: this.deviceId,
|
|
813
|
+
install_time: this.installTime,
|
|
814
|
+
session_count: this.sessionCount,
|
|
815
|
+
active_mask: this.activeMask,
|
|
816
|
+
first_open_date: this.firstOpenDate,
|
|
817
|
+
last_session_date: todayStr,
|
|
818
|
+
completed_funnel_steps: this._completedFunnelSteps,
|
|
819
|
+
completed_funnel_dropoffs: this._completedFunnelDropoffs
|
|
820
|
+
};
|
|
821
|
+
this._storage.setItem(this._storageKey("device"), JSON.stringify(deviceData));
|
|
822
|
+
}
|
|
823
|
+
saveOffline() {
|
|
824
|
+
if (!this._offlineDirty) return;
|
|
825
|
+
this._storage.setItem(this._storageKey("offline"), JSON.stringify(this._eventBuffer));
|
|
826
|
+
this._offlineDirty = false;
|
|
827
|
+
}
|
|
828
|
+
_loadCachedExperiments() {
|
|
829
|
+
const raw = this._storage.getItem(this._storageKey("experiments"));
|
|
830
|
+
if (!raw) return;
|
|
831
|
+
try {
|
|
832
|
+
const parsed = JSON.parse(raw);
|
|
833
|
+
if (parsed && typeof parsed === "object") {
|
|
834
|
+
if (Array.isArray(parsed.experiments)) {
|
|
835
|
+
this._activeExperiments = parsed.experiments.map(String);
|
|
836
|
+
}
|
|
837
|
+
if (parsed.flags && typeof parsed.flags === "object") {
|
|
838
|
+
this._experimentFlags = { ...parsed.flags };
|
|
839
|
+
}
|
|
840
|
+
this._experimentsLoaded = true;
|
|
841
|
+
}
|
|
842
|
+
} catch {
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
_flushPendingCrashFromStorage() {
|
|
846
|
+
const raw = this._storage.getItem(this._storageKey("crash"));
|
|
847
|
+
if (!raw) return;
|
|
848
|
+
try {
|
|
849
|
+
const crashData = JSON.parse(raw);
|
|
850
|
+
this.sendEvent("error", crashData);
|
|
851
|
+
} catch {
|
|
852
|
+
}
|
|
853
|
+
this._storage.removeItem(this._storageKey("crash"));
|
|
854
|
+
}
|
|
855
|
+
_attachBrowserListeners() {
|
|
856
|
+
if (typeof window === "undefined" || typeof document === "undefined") return;
|
|
857
|
+
this._detachListeners();
|
|
858
|
+
const onError = (event) => {
|
|
859
|
+
if (!this._options.captureCrashes) return;
|
|
860
|
+
const errObj = event.error instanceof Error ? event.error : void 0;
|
|
861
|
+
const msg = event.message || errObj?.message || "Uncaught error";
|
|
862
|
+
const stack = errObj?.stack || (event.filename ? `at ${event.filename}:${event.lineno || 0}:${event.colno || 0}` : "");
|
|
863
|
+
this.handleException({ message: msg, stack }, { action: "fatal_crash", severity: "fatal_crash" });
|
|
864
|
+
};
|
|
865
|
+
const onRejection = (event) => {
|
|
866
|
+
if (!this._options.captureCrashes) return;
|
|
867
|
+
const reason = event.reason;
|
|
868
|
+
const msg = reason instanceof Error ? reason.message : typeof reason === "string" ? reason : JSON.stringify(reason || "Unhandled promise rejection");
|
|
869
|
+
const stack = reason instanceof Error ? reason.stack || "" : "";
|
|
870
|
+
this.handleException(
|
|
871
|
+
{ message: msg, stack },
|
|
872
|
+
{ action: "unhandled_exception", severity: "unhandled_exception" }
|
|
873
|
+
);
|
|
874
|
+
};
|
|
875
|
+
const onVisibilityChange = () => {
|
|
876
|
+
if (document.visibilityState === "hidden") {
|
|
877
|
+
this.saveOffline();
|
|
878
|
+
void this.flush(false, true);
|
|
879
|
+
}
|
|
880
|
+
};
|
|
881
|
+
const onPageHide = () => {
|
|
882
|
+
this._endSession("page_hide");
|
|
883
|
+
};
|
|
884
|
+
const onBeforeUnload = () => {
|
|
885
|
+
this._endSession("normal_quit");
|
|
886
|
+
};
|
|
887
|
+
const onPageShow = (event) => {
|
|
888
|
+
if (event.persisted && this._sessionEnded) {
|
|
889
|
+
this._sessionEnded = false;
|
|
890
|
+
this._sessionStartTimeMs = Date.now();
|
|
891
|
+
this._sceneEnterTimeMs = Date.now();
|
|
892
|
+
}
|
|
893
|
+
};
|
|
894
|
+
const onKeyDown = (event) => {
|
|
895
|
+
if (!this.isDev || !this._options.captureHotkeysEnabled) return;
|
|
896
|
+
if (event.key !== "F9") return;
|
|
897
|
+
const modOk = !this._options.captureRequireModifier || (event.ctrlKey || event.metaKey) && event.shiftKey;
|
|
898
|
+
if (!modOk) return;
|
|
899
|
+
event.preventDefault();
|
|
900
|
+
void this.captureScene();
|
|
901
|
+
};
|
|
902
|
+
window.addEventListener("error", onError);
|
|
903
|
+
window.addEventListener("unhandledrejection", onRejection);
|
|
904
|
+
document.addEventListener("visibilitychange", onVisibilityChange);
|
|
905
|
+
window.addEventListener("pagehide", onPageHide);
|
|
906
|
+
window.addEventListener("beforeunload", onBeforeUnload);
|
|
907
|
+
window.addEventListener("pageshow", onPageShow);
|
|
908
|
+
window.addEventListener("keydown", onKeyDown);
|
|
909
|
+
this._cleanupListeners.push(() => {
|
|
910
|
+
window.removeEventListener("error", onError);
|
|
911
|
+
window.removeEventListener("unhandledrejection", onRejection);
|
|
912
|
+
document.removeEventListener("visibilitychange", onVisibilityChange);
|
|
913
|
+
window.removeEventListener("pagehide", onPageHide);
|
|
914
|
+
window.removeEventListener("beforeunload", onBeforeUnload);
|
|
915
|
+
window.removeEventListener("pageshow", onPageShow);
|
|
916
|
+
window.removeEventListener("keydown", onKeyDown);
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
_detachListeners() {
|
|
920
|
+
for (const fn of this._cleanupListeners) {
|
|
921
|
+
try {
|
|
922
|
+
fn();
|
|
923
|
+
} catch {
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
this._cleanupListeners = [];
|
|
927
|
+
}
|
|
928
|
+
_startTimers() {
|
|
929
|
+
this._stopTimers();
|
|
930
|
+
if (typeof setInterval === "undefined") return;
|
|
931
|
+
if (this._options.autoFlush) {
|
|
932
|
+
this._flushTimer = setInterval(() => {
|
|
933
|
+
if (Date.now() - this._lastFlushTime >= this._options.flushIntervalMs) {
|
|
934
|
+
if (this._eventBuffer.length > 0 && !this._isFlushing) {
|
|
935
|
+
void this.flush(false);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
}, Math.min(2e3, this._options.flushIntervalMs));
|
|
939
|
+
if (typeof this._flushTimer?.unref === "function") {
|
|
940
|
+
this._flushTimer.unref();
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
this._offlineSaveTimer = setInterval(() => {
|
|
944
|
+
this.saveOffline();
|
|
945
|
+
}, 5e3);
|
|
946
|
+
if (typeof this._offlineSaveTimer?.unref === "function") {
|
|
947
|
+
this._offlineSaveTimer.unref();
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
_stopTimers() {
|
|
951
|
+
if (this._flushTimer !== null) {
|
|
952
|
+
clearInterval(this._flushTimer);
|
|
953
|
+
this._flushTimer = null;
|
|
954
|
+
}
|
|
955
|
+
if (this._offlineSaveTimer !== null) {
|
|
956
|
+
clearInterval(this._offlineSaveTimer);
|
|
957
|
+
this._offlineSaveTimer = null;
|
|
958
|
+
}
|
|
959
|
+
if (this._expTimeoutHandle !== null) {
|
|
960
|
+
clearTimeout(this._expTimeoutHandle);
|
|
961
|
+
this._expTimeoutHandle = null;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
_ensureRafLoop() {
|
|
965
|
+
if (this._rafId !== null) return;
|
|
966
|
+
if (typeof globalThis === "undefined" || typeof globalThis.requestAnimationFrame !== "function") return;
|
|
967
|
+
if (!this._options.autoPerformance && this._trackedPaths.size === 0) return;
|
|
968
|
+
this._lastRafTime = typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
969
|
+
const tick = (now) => {
|
|
970
|
+
if (!this.initialized) {
|
|
971
|
+
this._rafId = null;
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
this._frame += 1;
|
|
975
|
+
if (this._options.autoPerformance) {
|
|
976
|
+
const dt = now - this._lastRafTime;
|
|
977
|
+
this._lastRafTime = now;
|
|
978
|
+
if (dt > 0 && dt < 1e3) {
|
|
979
|
+
const fps = Math.min(240, 1e3 / dt);
|
|
980
|
+
this._fpsSamples.push(fps);
|
|
981
|
+
if (this._fpsSamples.length > 300) {
|
|
982
|
+
this._fpsSamples.shift();
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
const nowMs = Date.now();
|
|
986
|
+
const sceneElapsedMs = nowMs - this._sceneEnterTimeMs;
|
|
987
|
+
if (!this._scenePingSent && sceneElapsedMs >= this._options.performanceDelayMs) {
|
|
988
|
+
this._emitPerformancePing(nowMs);
|
|
989
|
+
this._scenePingSent = true;
|
|
990
|
+
this._lastScenePingMs = nowMs;
|
|
991
|
+
} else if (this._scenePingSent && this._options.performanceIntervalMs > 0 && nowMs - this._lastScenePingMs >= this._options.performanceIntervalMs) {
|
|
992
|
+
this._emitPerformancePing(nowMs);
|
|
993
|
+
this._lastScenePingMs = nowMs;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
if (this._trackedPaths.size > 0) {
|
|
997
|
+
for (const tracker of this._trackedPaths.values()) {
|
|
998
|
+
const coords = extractTargetCoords(tracker.target);
|
|
999
|
+
if (!coords) continue;
|
|
1000
|
+
const rx = Math.round(coords.x * 100) / 100;
|
|
1001
|
+
const ry = Math.round(coords.y * 100) / 100;
|
|
1002
|
+
if (tracker.lastX === void 0 || tracker.lastY === void 0 || Math.abs(rx - tracker.lastX) >= 0.75 || Math.abs(ry - tracker.lastY) >= 0.75) {
|
|
1003
|
+
tracker.lastX = rx;
|
|
1004
|
+
tracker.lastY = ry;
|
|
1005
|
+
tracker.path.push([rx, ry]);
|
|
1006
|
+
if (tracker.path.length >= tracker.maxLen * 2) {
|
|
1007
|
+
tracker.path.splice(0, tracker.path.length - tracker.maxLen);
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
if (this._options.autoPerformance || this._trackedPaths.size > 0) {
|
|
1013
|
+
this._rafId = globalThis.requestAnimationFrame(tick);
|
|
1014
|
+
} else {
|
|
1015
|
+
this._rafId = null;
|
|
1016
|
+
}
|
|
1017
|
+
};
|
|
1018
|
+
this._rafId = globalThis.requestAnimationFrame(tick);
|
|
1019
|
+
}
|
|
1020
|
+
_emitPerformancePing(nowMs) {
|
|
1021
|
+
const samples = this._fpsSamples;
|
|
1022
|
+
let avgFps = 60;
|
|
1023
|
+
let minFps = 60;
|
|
1024
|
+
let maxFps = 60;
|
|
1025
|
+
if (samples.length > 0) {
|
|
1026
|
+
let sum = 0;
|
|
1027
|
+
minFps = Infinity;
|
|
1028
|
+
maxFps = -Infinity;
|
|
1029
|
+
for (const s of samples) {
|
|
1030
|
+
sum += s;
|
|
1031
|
+
if (s < minFps) minFps = s;
|
|
1032
|
+
if (s > maxFps) maxFps = s;
|
|
1033
|
+
}
|
|
1034
|
+
avgFps = Math.round(sum / samples.length * 10) / 10;
|
|
1035
|
+
minFps = Math.round(minFps);
|
|
1036
|
+
maxFps = Math.round(maxFps);
|
|
1037
|
+
this._fpsSamples = [];
|
|
1038
|
+
}
|
|
1039
|
+
const perfPayload = {
|
|
1040
|
+
action: "ping",
|
|
1041
|
+
average_fps: avgFps,
|
|
1042
|
+
min_fps: minFps,
|
|
1043
|
+
max_fps: maxFps,
|
|
1044
|
+
current_scene: this._currentSceneName,
|
|
1045
|
+
scene_name: this._currentSceneName
|
|
1046
|
+
};
|
|
1047
|
+
const perfObj = typeof performance !== "undefined" ? performance : void 0;
|
|
1048
|
+
if (perfObj?.memory?.usedJSHeapSize) {
|
|
1049
|
+
perfPayload.memory_mb = Math.round(perfObj.memory.usedJSHeapSize / (1024 * 1024) * 10) / 10;
|
|
1050
|
+
}
|
|
1051
|
+
this._lastFlushTime = Math.min(this._lastFlushTime, nowMs);
|
|
1052
|
+
this.sendEvent("performance", perfPayload);
|
|
1053
|
+
}
|
|
1054
|
+
enterScene(sceneName) {
|
|
1055
|
+
if (!this.initialized) this.init();
|
|
1056
|
+
const clean = typeof sceneName === "string" ? sceneName.trim() : "";
|
|
1057
|
+
if (!clean || clean === this._currentSceneName) {
|
|
1058
|
+
return this;
|
|
1059
|
+
}
|
|
1060
|
+
const nowMs = Date.now();
|
|
1061
|
+
const elapsedSec = Math.max(0, Math.round((nowMs - this._sceneEnterTimeMs) / 100) / 10);
|
|
1062
|
+
if (this._currentSceneName && elapsedSec >= 0.2) {
|
|
1063
|
+
this.sendEvent("gameplay", {
|
|
1064
|
+
action: "scene_exit",
|
|
1065
|
+
room_name: this._currentSceneName,
|
|
1066
|
+
scene_name: this._currentSceneName,
|
|
1067
|
+
next_scene: clean,
|
|
1068
|
+
duration_seconds: elapsedSec,
|
|
1069
|
+
is_session_exit: false
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
this._currentSceneName = clean;
|
|
1073
|
+
this._sceneEnterTimeMs = nowMs;
|
|
1074
|
+
this._scenePingSent = false;
|
|
1075
|
+
this._lastScenePingMs = 0;
|
|
1076
|
+
this._activeLinks.clear();
|
|
1077
|
+
if (this._options.autoFlushOnSceneChange) {
|
|
1078
|
+
void this.flush(false);
|
|
1079
|
+
}
|
|
1080
|
+
return this;
|
|
1081
|
+
}
|
|
1082
|
+
enter_scene(sceneName) {
|
|
1083
|
+
return this.enterScene(sceneName);
|
|
1084
|
+
}
|
|
1085
|
+
setScene(sceneName) {
|
|
1086
|
+
return this.enterScene(sceneName);
|
|
1087
|
+
}
|
|
1088
|
+
set_scene(sceneName) {
|
|
1089
|
+
return this.enterScene(sceneName);
|
|
1090
|
+
}
|
|
1091
|
+
identify(userId, traits) {
|
|
1092
|
+
if (!this.initialized) this.init();
|
|
1093
|
+
if (typeof userId === "string" && userId.trim()) {
|
|
1094
|
+
this._options.userId = userId.trim();
|
|
1095
|
+
}
|
|
1096
|
+
if (traits && typeof traits === "object") {
|
|
1097
|
+
this._userTraits = { ...this._userTraits || {}, ...traits };
|
|
1098
|
+
}
|
|
1099
|
+
return this;
|
|
1100
|
+
}
|
|
1101
|
+
// --- EXPERIMENTS & FEATURE FLAGS ---
|
|
1102
|
+
isExperimentsReady() {
|
|
1103
|
+
if (!this.initialized) this.init();
|
|
1104
|
+
return this._experimentsLoaded;
|
|
1105
|
+
}
|
|
1106
|
+
is_experiments_ready() {
|
|
1107
|
+
return this.isExperimentsReady();
|
|
1108
|
+
}
|
|
1109
|
+
_hashExperimentBucket(name) {
|
|
1110
|
+
const devId = this.deviceId || String(this.firstOpenDate);
|
|
1111
|
+
const seed = `${devId}:${name}`;
|
|
1112
|
+
let hash = 0;
|
|
1113
|
+
for (let i = 0; i < seed.length; i++) {
|
|
1114
|
+
hash = hash * 31 + seed.charCodeAt(i) | 0;
|
|
1115
|
+
}
|
|
1116
|
+
return Math.abs(hash) % 100;
|
|
1117
|
+
}
|
|
1118
|
+
_flushExperimentCallbacks() {
|
|
1119
|
+
if (this._expTimeoutHandle !== null) {
|
|
1120
|
+
clearTimeout(this._expTimeoutHandle);
|
|
1121
|
+
this._expTimeoutHandle = null;
|
|
1122
|
+
}
|
|
1123
|
+
if (this._experimentReadyCallbacks.length === 0) return;
|
|
1124
|
+
const cbs = this._experimentReadyCallbacks;
|
|
1125
|
+
this._experimentReadyCallbacks = [];
|
|
1126
|
+
for (const cb of cbs) {
|
|
1127
|
+
try {
|
|
1128
|
+
cb();
|
|
1129
|
+
} catch (err) {
|
|
1130
|
+
this._log("errors", ">> LOGLUDO ERROR in whenExperimentsReady callback:", err);
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
whenExperimentsReady(callback, timeoutMs = 500) {
|
|
1135
|
+
if (typeof callback !== "function") return;
|
|
1136
|
+
if (!this.initialized) this.init();
|
|
1137
|
+
if (this._experimentsLoaded) {
|
|
1138
|
+
callback();
|
|
1139
|
+
return;
|
|
1140
|
+
}
|
|
1141
|
+
this._experimentReadyCallbacks.push(callback);
|
|
1142
|
+
if (timeoutMs > 0 && this._expTimeoutHandle === null) {
|
|
1143
|
+
this._expTimeoutHandle = setTimeout(() => {
|
|
1144
|
+
this._flushExperimentCallbacks();
|
|
1145
|
+
}, Math.max(50, timeoutMs));
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
when_experiments_ready(callback, timeoutMs = 500) {
|
|
1149
|
+
this.whenExperimentsReady(callback, timeoutMs);
|
|
1150
|
+
}
|
|
1151
|
+
experiment(name, defaultOrPct, fallbackPct) {
|
|
1152
|
+
if (!this.initialized) this.init();
|
|
1153
|
+
if (Object.prototype.hasOwnProperty.call(this._experimentFlags, name)) {
|
|
1154
|
+
return Boolean(this._experimentFlags[name]);
|
|
1155
|
+
}
|
|
1156
|
+
if (this._activeExperiments.includes(name)) {
|
|
1157
|
+
return true;
|
|
1158
|
+
}
|
|
1159
|
+
const explicitDefault = typeof defaultOrPct === "boolean" ? defaultOrPct : false;
|
|
1160
|
+
let pctToUse;
|
|
1161
|
+
if (typeof fallbackPct === "number" && Number.isFinite(fallbackPct)) {
|
|
1162
|
+
pctToUse = Math.max(0, Math.min(100, fallbackPct));
|
|
1163
|
+
} else if (typeof defaultOrPct === "number" && Number.isFinite(defaultOrPct)) {
|
|
1164
|
+
pctToUse = Math.max(0, Math.min(100, defaultOrPct));
|
|
1165
|
+
} else if (defaultOrPct === void 0) {
|
|
1166
|
+
pctToUse = Math.max(0, Math.min(100, this._options.defaultExperimentPercentage));
|
|
1167
|
+
}
|
|
1168
|
+
if (!this._experimentsLoaded && pctToUse !== void 0) {
|
|
1169
|
+
const bucketVal = this._hashExperimentBucket(name);
|
|
1170
|
+
const assigned = pctToUse >= 100 ? true : pctToUse <= 0 ? false : bucketVal < pctToUse;
|
|
1171
|
+
this._experimentFlags[name] = assigned;
|
|
1172
|
+
if (assigned && !this._activeExperiments.includes(name)) {
|
|
1173
|
+
this._activeExperiments.push(name);
|
|
1174
|
+
}
|
|
1175
|
+
if (this.isDev && !this._experimentWarned.has(name)) {
|
|
1176
|
+
this._experimentWarned.add(name);
|
|
1177
|
+
this._log(
|
|
1178
|
+
"info",
|
|
1179
|
+
`>> LOGLUDO: experiment("${name}") assigned locally at ${pctToUse}% default (${assigned ? "experiment/true" : "control/false"}) while awaiting server flags.`
|
|
1180
|
+
);
|
|
1181
|
+
}
|
|
1182
|
+
return assigned;
|
|
1183
|
+
}
|
|
1184
|
+
if (!this._experimentsLoaded && this.isDev && !this._experimentWarned.has(name)) {
|
|
1185
|
+
this._experimentWarned.add(name);
|
|
1186
|
+
this._log(
|
|
1187
|
+
"info",
|
|
1188
|
+
`>> LOGLUDO WARNING: experiment("${name}") called before server experiments loaded; returning default (${explicitDefault}).`
|
|
1189
|
+
);
|
|
1190
|
+
}
|
|
1191
|
+
return explicitDefault;
|
|
1192
|
+
}
|
|
1193
|
+
async fetchExperiments() {
|
|
1194
|
+
if (!isGameIdValid(this._options.gameId)) {
|
|
1195
|
+
this._experimentsLoaded = true;
|
|
1196
|
+
this._flushExperimentCallbacks();
|
|
1197
|
+
return;
|
|
1198
|
+
}
|
|
1199
|
+
const devId = this.deviceId || String(this.firstOpenDate);
|
|
1200
|
+
const url = `${this._options.apiUrl}/experiments?game_id=${encodeURIComponent(
|
|
1201
|
+
this._options.gameId
|
|
1202
|
+
)}&bucket=${encodeURIComponent(String(this.bucket))}&device_id=${encodeURIComponent(devId)}`;
|
|
1203
|
+
try {
|
|
1204
|
+
if (typeof fetch !== "function") {
|
|
1205
|
+
this._experimentsLoaded = true;
|
|
1206
|
+
this._flushExperimentCallbacks();
|
|
1207
|
+
return;
|
|
1208
|
+
}
|
|
1209
|
+
const res = await fetch(url, { method: "GET" });
|
|
1210
|
+
if (res.ok) {
|
|
1211
|
+
const text = await res.text();
|
|
1212
|
+
const data = JSON.parse(text);
|
|
1213
|
+
if (data && data.success) {
|
|
1214
|
+
if (Array.isArray(data.experiments)) {
|
|
1215
|
+
this._activeExperiments = data.experiments.map(String);
|
|
1216
|
+
}
|
|
1217
|
+
if (data.flags && typeof data.flags === "object") {
|
|
1218
|
+
this._experimentFlags = { ...data.flags };
|
|
1219
|
+
}
|
|
1220
|
+
this._retroactivelyTagBufferedExperiments();
|
|
1221
|
+
this._storage.setItem(this._storageKey("experiments"), text);
|
|
1222
|
+
this._log("info", `>> LOGLUDO: Experiments loaded (${this._activeExperiments.length} active)`);
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
} catch (err) {
|
|
1226
|
+
this._log("info", ">> LOGLUDO: Failed to fetch experiments:", err);
|
|
1227
|
+
} finally {
|
|
1228
|
+
this._experimentsLoaded = true;
|
|
1229
|
+
this._flushExperimentCallbacks();
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
_retroactivelyTagBufferedExperiments() {
|
|
1233
|
+
const hasExps = this._activeExperiments.length > 0;
|
|
1234
|
+
const hasFlags = Object.keys(this._experimentFlags).length > 0;
|
|
1235
|
+
if (!hasExps && !hasFlags) return;
|
|
1236
|
+
for (const item of this._eventBuffer) {
|
|
1237
|
+
if (!item.payload) continue;
|
|
1238
|
+
if (hasExps && !("experiments" in item.payload)) {
|
|
1239
|
+
item.payload.experiments = [...this._activeExperiments];
|
|
1240
|
+
}
|
|
1241
|
+
if (hasFlags && !("experiment_flags" in item.payload)) {
|
|
1242
|
+
item.payload.experiment_flags = { ...this._experimentFlags };
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
_attachExperimentContext(payload) {
|
|
1247
|
+
if (this._activeExperiments.length > 0 && !("experiments" in payload)) {
|
|
1248
|
+
payload.experiments = [...this._activeExperiments];
|
|
1249
|
+
}
|
|
1250
|
+
if (Object.keys(this._experimentFlags).length > 0 && !("experiment_flags" in payload)) {
|
|
1251
|
+
payload.experiment_flags = { ...this._experimentFlags };
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
// --- SPATIAL PATH TRACKING & LINKING ---
|
|
1255
|
+
_resolveTrackKey(target) {
|
|
1256
|
+
if (typeof target === "string") return `s:${target}`;
|
|
1257
|
+
if (target && typeof target === "object") {
|
|
1258
|
+
if ("id" in target && target.id !== void 0 && target.id !== null) {
|
|
1259
|
+
return `id:${String(target.id)}`;
|
|
1260
|
+
}
|
|
1261
|
+
let existing = this._targetKeyMap.get(target);
|
|
1262
|
+
if (!existing) {
|
|
1263
|
+
this._targetKeySeq += 1;
|
|
1264
|
+
existing = `obj_${this._targetKeySeq}`;
|
|
1265
|
+
this._targetKeyMap.set(target, existing);
|
|
1266
|
+
}
|
|
1267
|
+
return existing;
|
|
1268
|
+
}
|
|
1269
|
+
return "default";
|
|
1270
|
+
}
|
|
1271
|
+
trackPath(target, maxLength = 60) {
|
|
1272
|
+
if (!this.initialized) this.init();
|
|
1273
|
+
if (!target) return this;
|
|
1274
|
+
const key = this._resolveTrackKey(target);
|
|
1275
|
+
if (this._trackedPaths.has(key)) {
|
|
1276
|
+
const existing = this._trackedPaths.get(key);
|
|
1277
|
+
existing.maxLen = Math.max(2, maxLength);
|
|
1278
|
+
existing.target = target;
|
|
1279
|
+
const pt = this._pathCallTracker.get(key);
|
|
1280
|
+
if (pt) {
|
|
1281
|
+
const df = this._frame - pt.lastFrame;
|
|
1282
|
+
if (df >= 1 && df <= 2) pt.consec += 1;
|
|
1283
|
+
else if (df > 2) pt.consec = 1;
|
|
1284
|
+
pt.lastFrame = this._frame;
|
|
1285
|
+
if (pt.consec >= 4 && Date.now() - pt.warnedAt >= 1e4) {
|
|
1286
|
+
pt.warnedAt = Date.now();
|
|
1287
|
+
this._log(
|
|
1288
|
+
"errors",
|
|
1289
|
+
">> LOGLUDO WARNING: LOGLUDO.trackPath() is being called every frame (likely in update()). Call it once in create/init instead."
|
|
1290
|
+
);
|
|
1291
|
+
}
|
|
1292
|
+
} else {
|
|
1293
|
+
this._pathCallTracker.set(key, { lastFrame: this._frame, consec: 1, warnedAt: -99999 });
|
|
1294
|
+
}
|
|
1295
|
+
return this;
|
|
1296
|
+
}
|
|
1297
|
+
this._trackedPaths.set(key, {
|
|
1298
|
+
key,
|
|
1299
|
+
target,
|
|
1300
|
+
path: [],
|
|
1301
|
+
maxLen: Math.max(2, maxLength)
|
|
1302
|
+
});
|
|
1303
|
+
this._ensureRafLoop();
|
|
1304
|
+
return this;
|
|
1305
|
+
}
|
|
1306
|
+
track_path(target, maxLength = 60) {
|
|
1307
|
+
return this.trackPath(target, maxLength);
|
|
1308
|
+
}
|
|
1309
|
+
samplePath(target, x, y) {
|
|
1310
|
+
if (!this.initialized) this.init();
|
|
1311
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) return this;
|
|
1312
|
+
const key = this._resolveTrackKey(target);
|
|
1313
|
+
let tracker = this._trackedPaths.get(key);
|
|
1314
|
+
if (!tracker) {
|
|
1315
|
+
tracker = { key, target, path: [], maxLen: 60 };
|
|
1316
|
+
this._trackedPaths.set(key, tracker);
|
|
1317
|
+
}
|
|
1318
|
+
const rx = Math.round(x * 100) / 100;
|
|
1319
|
+
const ry = Math.round(y * 100) / 100;
|
|
1320
|
+
if (tracker.lastX === void 0 || tracker.lastY === void 0 || Math.abs(rx - tracker.lastX) >= 0.75 || Math.abs(ry - tracker.lastY) >= 0.75) {
|
|
1321
|
+
tracker.lastX = rx;
|
|
1322
|
+
tracker.lastY = ry;
|
|
1323
|
+
tracker.path.push([rx, ry]);
|
|
1324
|
+
if (tracker.path.length >= tracker.maxLen * 2) {
|
|
1325
|
+
tracker.path.splice(0, tracker.path.length - tracker.maxLen);
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
return this;
|
|
1329
|
+
}
|
|
1330
|
+
sample_path(target, x, y) {
|
|
1331
|
+
return this.samplePath(target, x, y);
|
|
1332
|
+
}
|
|
1333
|
+
stopTrackingPath(target) {
|
|
1334
|
+
const key = this._resolveTrackKey(target);
|
|
1335
|
+
this._trackedPaths.delete(key);
|
|
1336
|
+
this._pathCallTracker.delete(key);
|
|
1337
|
+
return this;
|
|
1338
|
+
}
|
|
1339
|
+
stop_tracking_path(target) {
|
|
1340
|
+
return this.stopTrackingPath(target);
|
|
1341
|
+
}
|
|
1342
|
+
startLink(scope = "default") {
|
|
1343
|
+
this._linkSeq += 1;
|
|
1344
|
+
const key = String(scope || "default");
|
|
1345
|
+
const prefix = key === "default" ? "l" : key;
|
|
1346
|
+
const linkId = `${prefix}_${this._linkSeq}`;
|
|
1347
|
+
this._activeLinks.set(key, linkId);
|
|
1348
|
+
return linkId;
|
|
1349
|
+
}
|
|
1350
|
+
start_link(scope = "default") {
|
|
1351
|
+
return this.startLink(scope);
|
|
1352
|
+
}
|
|
1353
|
+
getLink(scope = "default") {
|
|
1354
|
+
return this._activeLinks.get(String(scope || "default"));
|
|
1355
|
+
}
|
|
1356
|
+
get_link(scope = "default") {
|
|
1357
|
+
return this.getLink(scope);
|
|
1358
|
+
}
|
|
1359
|
+
clearLink(scope = "default") {
|
|
1360
|
+
this._activeLinks.delete(String(scope || "default"));
|
|
1361
|
+
}
|
|
1362
|
+
clear_link(scope = "default") {
|
|
1363
|
+
this.clearLink(scope);
|
|
1364
|
+
}
|
|
1365
|
+
// --- IN-MEMORY AGGREGATORS ---
|
|
1366
|
+
_formatAggregate(agg) {
|
|
1367
|
+
if (agg.type === "sum") return agg.value ?? 0;
|
|
1368
|
+
if (agg.type === "average") return agg.count && agg.count > 0 ? (agg.sum ?? 0) / agg.count : 0;
|
|
1369
|
+
if (agg.type === "frequency") {
|
|
1370
|
+
const arr = [];
|
|
1371
|
+
for (const [item, count] of (agg.map || /* @__PURE__ */ new Map()).entries()) {
|
|
1372
|
+
arr.push({ item, count });
|
|
1373
|
+
}
|
|
1374
|
+
arr.sort((a, b) => b.count - a.count);
|
|
1375
|
+
return arr.slice(0, 10);
|
|
1376
|
+
}
|
|
1377
|
+
return void 0;
|
|
1378
|
+
}
|
|
1379
|
+
aggregateAdd(name, value, type = "sum") {
|
|
1380
|
+
let agg = this._aggregates.get(name);
|
|
1381
|
+
if (!agg) {
|
|
1382
|
+
agg = { type };
|
|
1383
|
+
if (type === "sum") agg.value = 0;
|
|
1384
|
+
else if (type === "average") {
|
|
1385
|
+
agg.sum = 0;
|
|
1386
|
+
agg.count = 0;
|
|
1387
|
+
} else if (type === "frequency") {
|
|
1388
|
+
agg.map = /* @__PURE__ */ new Map();
|
|
1389
|
+
}
|
|
1390
|
+
this._aggregates.set(name, agg);
|
|
1391
|
+
}
|
|
1392
|
+
if (agg.type === "sum") {
|
|
1393
|
+
const num = Number(value);
|
|
1394
|
+
if (Number.isFinite(num)) agg.value = (agg.value ?? 0) + num;
|
|
1395
|
+
} else if (agg.type === "average") {
|
|
1396
|
+
const num = Number(value);
|
|
1397
|
+
if (Number.isFinite(num)) {
|
|
1398
|
+
agg.sum = (agg.sum ?? 0) + num;
|
|
1399
|
+
agg.count = (agg.count ?? 0) + 1;
|
|
1400
|
+
}
|
|
1401
|
+
} else if (agg.type === "frequency") {
|
|
1402
|
+
const key = String(value);
|
|
1403
|
+
agg.map = agg.map || /* @__PURE__ */ new Map();
|
|
1404
|
+
agg.map.set(key, (agg.map.get(key) ?? 0) + 1);
|
|
1405
|
+
}
|
|
1406
|
+
return this;
|
|
1407
|
+
}
|
|
1408
|
+
aggregate_add(name, value, type = "sum") {
|
|
1409
|
+
return this.aggregateAdd(name, value, type);
|
|
1410
|
+
}
|
|
1411
|
+
aggregatePop(name) {
|
|
1412
|
+
const agg = this._aggregates.get(name);
|
|
1413
|
+
if (!agg) return void 0;
|
|
1414
|
+
const formatted = this._formatAggregate(agg);
|
|
1415
|
+
this._aggregates.delete(name);
|
|
1416
|
+
return formatted;
|
|
1417
|
+
}
|
|
1418
|
+
aggregate_pop(name) {
|
|
1419
|
+
return this.aggregatePop(name);
|
|
1420
|
+
}
|
|
1421
|
+
aggregateGetAll(pop = false) {
|
|
1422
|
+
const out = {};
|
|
1423
|
+
for (const [k, agg] of this._aggregates.entries()) {
|
|
1424
|
+
const formatted = this._formatAggregate(agg);
|
|
1425
|
+
if (formatted !== void 0) out[k] = formatted;
|
|
1426
|
+
}
|
|
1427
|
+
if (pop) {
|
|
1428
|
+
this._aggregates.clear();
|
|
1429
|
+
}
|
|
1430
|
+
return out;
|
|
1431
|
+
}
|
|
1432
|
+
aggregate_get_all(pop = false) {
|
|
1433
|
+
return this.aggregateGetAll(pop);
|
|
1434
|
+
}
|
|
1435
|
+
// --- PROGRESSION FUNNELS ---
|
|
1436
|
+
funnelStep(stepId, payload = {}) {
|
|
1437
|
+
if (!this.initialized) this.init();
|
|
1438
|
+
if (!stepId || typeof stepId !== "string") return false;
|
|
1439
|
+
const stepKey = stepId.trim().toLowerCase();
|
|
1440
|
+
if (!stepKey) return false;
|
|
1441
|
+
if (this._completedFunnelSteps.includes(stepKey)) {
|
|
1442
|
+
return false;
|
|
1443
|
+
}
|
|
1444
|
+
const prior = [...this._completedFunnelSteps];
|
|
1445
|
+
this._completedFunnelSteps.push(stepKey);
|
|
1446
|
+
this.saveDeviceData();
|
|
1447
|
+
const cloned = clonePayload(payload);
|
|
1448
|
+
cloned.action = "funnel_step";
|
|
1449
|
+
cloned.step_id = stepId;
|
|
1450
|
+
cloned.step_name = stepId;
|
|
1451
|
+
cloned.funnel_once = true;
|
|
1452
|
+
if (!("prior_steps" in cloned)) {
|
|
1453
|
+
cloned.prior_steps = prior;
|
|
1454
|
+
}
|
|
1455
|
+
return this.sendEvent("progression", cloned);
|
|
1456
|
+
}
|
|
1457
|
+
funnel_step(stepId, payload = {}) {
|
|
1458
|
+
return this.funnelStep(stepId, payload);
|
|
1459
|
+
}
|
|
1460
|
+
funnelDropoff(stepId, reason = "abandoned", payload = {}) {
|
|
1461
|
+
if (!this.initialized) this.init();
|
|
1462
|
+
if (!stepId || typeof stepId !== "string") return false;
|
|
1463
|
+
const stepKey = stepId.trim().toLowerCase();
|
|
1464
|
+
if (!stepKey) return false;
|
|
1465
|
+
if (this._completedFunnelSteps.includes(stepKey) || this._completedFunnelDropoffs.includes(stepKey)) {
|
|
1466
|
+
return false;
|
|
1467
|
+
}
|
|
1468
|
+
this._completedFunnelDropoffs.push(stepKey);
|
|
1469
|
+
this.saveDeviceData();
|
|
1470
|
+
const cloned = clonePayload(payload);
|
|
1471
|
+
cloned.action = "dropoff";
|
|
1472
|
+
cloned.step_id = stepId;
|
|
1473
|
+
cloned.step_name = stepId;
|
|
1474
|
+
cloned.dropoff_reason = reason;
|
|
1475
|
+
cloned.funnel_once = true;
|
|
1476
|
+
return this.sendEvent("progression", cloned);
|
|
1477
|
+
}
|
|
1478
|
+
funnel_dropoff(stepId, reason = "abandoned", payload = {}) {
|
|
1479
|
+
return this.funnelDropoff(stepId, reason, payload);
|
|
1480
|
+
}
|
|
1481
|
+
// --- EVENT SENDING & SAFEGUARDS ---
|
|
1482
|
+
_purgeUnflushedSpam(name, actionStr) {
|
|
1483
|
+
const locked = this._isFlushing ? this._inFlightBatchCount : 0;
|
|
1484
|
+
for (let i = this._eventBuffer.length - 1; i >= locked; i--) {
|
|
1485
|
+
const ev = this._eventBuffer[i];
|
|
1486
|
+
if (ev.event_name === name) {
|
|
1487
|
+
const evAct = typeof ev.payload?.action === "string" ? ev.payload.action : "";
|
|
1488
|
+
if (evAct === actionStr) {
|
|
1489
|
+
this._eventBuffer.splice(i, 1);
|
|
1490
|
+
if (i < this._bufferRaws.length) {
|
|
1491
|
+
this._bufferRaws.splice(i, 1);
|
|
1492
|
+
}
|
|
1493
|
+
this._offlineDirty = true;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
_checkStepSpam(name, actionStr, isExempt) {
|
|
1499
|
+
if (isExempt) return false;
|
|
1500
|
+
const nowMs = Date.now();
|
|
1501
|
+
if (this._spamTracker.size > 64) {
|
|
1502
|
+
for (const [k, rec2] of this._spamTracker.entries()) {
|
|
1503
|
+
if (nowMs - rec2.winStart > 1e4 && nowMs >= rec2.blockedUntil) {
|
|
1504
|
+
this._spamTracker.delete(k);
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
const sig = `${name}|${actionStr}`;
|
|
1509
|
+
let rec = this._spamTracker.get(sig);
|
|
1510
|
+
if (rec) {
|
|
1511
|
+
if (this._frame > 0) {
|
|
1512
|
+
const df = this._frame - rec.lastFrame;
|
|
1513
|
+
if (df >= 1 && df <= 2) rec.consecFrames += 1;
|
|
1514
|
+
else if (df > 2) rec.consecFrames = 1;
|
|
1515
|
+
rec.lastFrame = this._frame;
|
|
1516
|
+
}
|
|
1517
|
+
if (nowMs - rec.winStart >= 1e3) {
|
|
1518
|
+
rec.winStart = nowMs;
|
|
1519
|
+
rec.winCount = 1;
|
|
1520
|
+
} else {
|
|
1521
|
+
rec.winCount += 1;
|
|
1522
|
+
}
|
|
1523
|
+
} else {
|
|
1524
|
+
rec = {
|
|
1525
|
+
lastFrame: this._frame,
|
|
1526
|
+
consecFrames: 1,
|
|
1527
|
+
winStart: nowMs,
|
|
1528
|
+
winCount: 1,
|
|
1529
|
+
warnedAt: -99999,
|
|
1530
|
+
blockedUntil: 0
|
|
1531
|
+
};
|
|
1532
|
+
this._spamTracker.set(sig, rec);
|
|
1533
|
+
}
|
|
1534
|
+
const isLoopSpam = rec.consecFrames >= 4 || rec.winCount > 12 && (this._frame === 0 || rec.consecFrames >= 3) || nowMs < rec.blockedUntil;
|
|
1535
|
+
if (isLoopSpam) {
|
|
1536
|
+
rec.blockedUntil = nowMs + 2e3;
|
|
1537
|
+
this._purgeUnflushedSpam(name, actionStr);
|
|
1538
|
+
if (nowMs - rec.warnedAt >= 1e4) {
|
|
1539
|
+
rec.warnedAt = nowMs;
|
|
1540
|
+
const label = actionStr ? `${name} / ${actionStr}` : name;
|
|
1541
|
+
this._log(
|
|
1542
|
+
"errors",
|
|
1543
|
+
`>> LOGLUDO WARNING: Event '${label}' is being called repeatedly in a loop (likely inside update/requestAnimationFrame). Blocked to prevent spam. Move it out of the loop or use LOGLUDO.aggregateAdd().`
|
|
1544
|
+
);
|
|
1545
|
+
}
|
|
1546
|
+
return true;
|
|
1547
|
+
}
|
|
1548
|
+
return false;
|
|
1549
|
+
}
|
|
1550
|
+
sendEvent(name, payload = {}) {
|
|
1551
|
+
if (!this.initialized) this.init();
|
|
1552
|
+
if (!ALLOWED_CATEGORIES.has(name)) {
|
|
1553
|
+
this._log(
|
|
1554
|
+
"errors",
|
|
1555
|
+
`>> LOGLUDO WARNING: Unknown event category "${name}". Allowed categories: session_start, session_end, error, progression, monetization, gameplay, design, performance.`
|
|
1556
|
+
);
|
|
1557
|
+
}
|
|
1558
|
+
const p = clonePayload(payload);
|
|
1559
|
+
const actionStr = typeof p.action === "string" ? p.action : "";
|
|
1560
|
+
const isFunnel = Boolean(p.funnel_once);
|
|
1561
|
+
const isLifecycle = name === "session_start" || name === "session_end";
|
|
1562
|
+
const isCritical = isLifecycle || name === "error" || name === "monetization" || isFunnel;
|
|
1563
|
+
if (this._checkStepSpam(name, actionStr, isLifecycle || isFunnel)) {
|
|
1564
|
+
return false;
|
|
1565
|
+
}
|
|
1566
|
+
const nowMs = Date.now();
|
|
1567
|
+
const curSec = Math.floor(nowMs / 1e3);
|
|
1568
|
+
if (curSec !== this._lastEventSecond) {
|
|
1569
|
+
this._lastEventSecond = curSec;
|
|
1570
|
+
this._eventsThisSecond = 0;
|
|
1571
|
+
}
|
|
1572
|
+
this._eventsThisSecond += 1;
|
|
1573
|
+
if (this._eventsThisSecond > 20 && !isCritical) {
|
|
1574
|
+
if (nowMs - this._rateWarnedAt >= 5e3) {
|
|
1575
|
+
this._rateWarnedAt = nowMs;
|
|
1576
|
+
this._log(
|
|
1577
|
+
"errors",
|
|
1578
|
+
`>> LOGLUDO WARNING: Event rate limit (>20/sec) exceeded while logging '${name}'. Dropping excess events.`
|
|
1579
|
+
);
|
|
1580
|
+
}
|
|
1581
|
+
return false;
|
|
1582
|
+
}
|
|
1583
|
+
if (this._eventBuffer.length >= (isCritical ? 400 : 150)) {
|
|
1584
|
+
return false;
|
|
1585
|
+
}
|
|
1586
|
+
const rawScene = p.scenelytics;
|
|
1587
|
+
const trackToExtract = p.track_id ?? p.trackId ?? rawScene?.track_id ?? rawScene?.trackId;
|
|
1588
|
+
const hasSpatialCoords = "x" in p || "y" in p || "endX" in p || "endY" in p || trackToExtract !== void 0;
|
|
1589
|
+
if (hasSpatialCoords && !p.scenelytics) {
|
|
1590
|
+
p.scenelytics = {};
|
|
1591
|
+
}
|
|
1592
|
+
const ax = this._options.anchorX;
|
|
1593
|
+
const ay = this._options.anchorY;
|
|
1594
|
+
if (p.scenelytics && typeof p.scenelytics === "object") {
|
|
1595
|
+
const scene = p.scenelytics;
|
|
1596
|
+
for (const k of ["x", "y", "endX", "endY"]) {
|
|
1597
|
+
if (k in p) {
|
|
1598
|
+
scene[k] = p[k];
|
|
1599
|
+
delete p[k];
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
if ("link_id" in p || "linkId" in p) {
|
|
1603
|
+
scene.link_id = p.link_id ?? p.linkId;
|
|
1604
|
+
delete p.link_id;
|
|
1605
|
+
delete p.linkId;
|
|
1606
|
+
} else if ("linkId" in scene && !("link_id" in scene)) {
|
|
1607
|
+
scene.link_id = scene.linkId;
|
|
1608
|
+
}
|
|
1609
|
+
delete scene.linkId;
|
|
1610
|
+
if ("link_scope" in p || "linkScope" in p) {
|
|
1611
|
+
if (!("link_scope" in scene)) scene.link_scope = p.link_scope ?? p.linkScope;
|
|
1612
|
+
delete p.link_scope;
|
|
1613
|
+
delete p.linkScope;
|
|
1614
|
+
} else if ("linkScope" in scene && !("link_scope" in scene)) {
|
|
1615
|
+
scene.link_scope = scene.linkScope;
|
|
1616
|
+
}
|
|
1617
|
+
delete scene.linkScope;
|
|
1618
|
+
if (typeof scene.link_scope === "string") {
|
|
1619
|
+
const resolved = this.getLink(scene.link_scope);
|
|
1620
|
+
if (resolved && !scene.link_id) {
|
|
1621
|
+
scene.link_id = resolved;
|
|
1622
|
+
}
|
|
1623
|
+
delete scene.link_scope;
|
|
1624
|
+
}
|
|
1625
|
+
if ("link_id" in scene) {
|
|
1626
|
+
if (!scene.link_id) {
|
|
1627
|
+
delete scene.link_id;
|
|
1628
|
+
} else {
|
|
1629
|
+
scene.link_id = String(scene.link_id);
|
|
1630
|
+
}
|
|
1631
|
+
}
|
|
1632
|
+
if (trackToExtract !== void 0) {
|
|
1633
|
+
const coords = extractTargetCoords(trackToExtract);
|
|
1634
|
+
if (coords) {
|
|
1635
|
+
if (scene.x === void 0) scene.x = coords.x;
|
|
1636
|
+
if (scene.y === void 0) scene.y = coords.y;
|
|
1637
|
+
} else {
|
|
1638
|
+
const tKey = this._resolveTrackKey(trackToExtract);
|
|
1639
|
+
const tr = this._trackedPaths.get(tKey);
|
|
1640
|
+
if (tr && tr.path.length > 0) {
|
|
1641
|
+
const last = tr.path[tr.path.length - 1];
|
|
1642
|
+
if (scene.x === void 0) scene.x = last[0];
|
|
1643
|
+
if (scene.y === void 0) scene.y = last[1];
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
delete p.track_id;
|
|
1647
|
+
delete p.trackId;
|
|
1648
|
+
delete scene.track_id;
|
|
1649
|
+
delete scene.trackId;
|
|
1650
|
+
}
|
|
1651
|
+
for (const k of ["x", "y", "endX", "endY"]) {
|
|
1652
|
+
if (typeof scene[k] === "number" && Number.isFinite(scene[k])) {
|
|
1653
|
+
const offset = k === "x" || k === "endX" ? ax : ay;
|
|
1654
|
+
scene[k] = Math.round((scene[k] - offset) * 100) / 100;
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
if (!("room_name" in p) && !("scene_name" in p)) {
|
|
1658
|
+
p.room_name = this._currentSceneName;
|
|
1659
|
+
p.scene_name = this._currentSceneName;
|
|
1660
|
+
} else if (!("room_name" in p) && typeof p.scene_name === "string") {
|
|
1661
|
+
p.room_name = p.scene_name;
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
p.build_type = this.isDev ? "run" : "build";
|
|
1665
|
+
if (!("sdk_version" in p)) p.sdk_version = SDK_VERSION;
|
|
1666
|
+
if (!("sdk_engine" in p)) p.sdk_engine = "JavaScript";
|
|
1667
|
+
if (this._options.userId && !("user_id" in p)) {
|
|
1668
|
+
p.user_id = this._options.userId;
|
|
1669
|
+
if (this._userTraits) {
|
|
1670
|
+
Object.assign(p, this._userTraits);
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
const envInfo = detectEnvironment();
|
|
1674
|
+
if (envInfo.domain && !("domain" in p)) p.domain = envInfo.domain;
|
|
1675
|
+
if (envInfo.url && !("url" in p)) p.url = envInfo.url;
|
|
1676
|
+
this._attachExperimentContext(p);
|
|
1677
|
+
const dedupStr = JSON.stringify(p);
|
|
1678
|
+
const checkStart = Math.max(0, Math.min(this._eventBuffer.length, this._bufferRaws.length) - 20);
|
|
1679
|
+
for (let i = Math.min(this._eventBuffer.length, this._bufferRaws.length) - 1; i >= checkStart; i--) {
|
|
1680
|
+
if (this._bufferRaws[i] === dedupStr && this._eventBuffer[i].event_name === name) {
|
|
1681
|
+
return false;
|
|
1682
|
+
}
|
|
1683
|
+
}
|
|
1684
|
+
const dedupKey = `${name}|${dedupStr}`;
|
|
1685
|
+
for (let i = this._recentEventHistory.length - 1; i >= 0; i--) {
|
|
1686
|
+
const entry = this._recentEventHistory[i];
|
|
1687
|
+
if (nowMs - entry.time >= this._options.dedupWindowMs) {
|
|
1688
|
+
this._recentEventHistory.splice(i, 1);
|
|
1689
|
+
} else if (entry.key === dedupKey) {
|
|
1690
|
+
entry.time = nowMs;
|
|
1691
|
+
if (this.isDev && nowMs - this._dedupWarnedAt >= 2e3) {
|
|
1692
|
+
this._dedupWarnedAt = nowMs;
|
|
1693
|
+
this._log(
|
|
1694
|
+
"info",
|
|
1695
|
+
`>> LOGLUDO: Suppressed duplicate event '${name}' within ${this._options.dedupWindowMs}ms dedup window.`
|
|
1696
|
+
);
|
|
1697
|
+
}
|
|
1698
|
+
return false;
|
|
1699
|
+
}
|
|
1700
|
+
}
|
|
1701
|
+
this._recentEventHistory.push({ key: dedupKey, time: nowMs });
|
|
1702
|
+
if (this._recentEventHistory.length > this._dedupHistoryLimit) {
|
|
1703
|
+
this._recentEventHistory.shift();
|
|
1704
|
+
}
|
|
1705
|
+
if (p.scenelytics && typeof p.scenelytics === "object" && trackToExtract !== void 0) {
|
|
1706
|
+
const scene = p.scenelytics;
|
|
1707
|
+
const tKey = this._resolveTrackKey(trackToExtract);
|
|
1708
|
+
const tracker = this._trackedPaths.get(tKey);
|
|
1709
|
+
if (tracker) {
|
|
1710
|
+
const curX = typeof scene.x === "number" ? scene.x : void 0;
|
|
1711
|
+
const curY = typeof scene.y === "number" ? scene.y : void 0;
|
|
1712
|
+
const minified = minifyPath(tracker.path, ax, ay, curX, curY, tracker.maxLen);
|
|
1713
|
+
if (minified) {
|
|
1714
|
+
scene.recent_path = minified;
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
this._eventBuffer.push({
|
|
1719
|
+
game_id: this._options.gameId,
|
|
1720
|
+
game_version: this._options.gameVersion,
|
|
1721
|
+
session_id: this.sessionId,
|
|
1722
|
+
event_name: name,
|
|
1723
|
+
payload: p
|
|
1724
|
+
});
|
|
1725
|
+
this._bufferRaws.push(dedupStr);
|
|
1726
|
+
this._offlineDirty = true;
|
|
1727
|
+
const isBrowser = typeof window !== "undefined";
|
|
1728
|
+
const flushNow = this._options.autoFlush && (this.isDev || isBrowser && isCritical || this._eventBuffer.length >= this._options.batchLimit);
|
|
1729
|
+
if (flushNow && !this._isFlushing && nowMs >= this._retryAfterTime) {
|
|
1730
|
+
void this.flush(false);
|
|
1731
|
+
}
|
|
1732
|
+
return true;
|
|
1733
|
+
}
|
|
1734
|
+
send_event(name, payload = {}) {
|
|
1735
|
+
return this.sendEvent(name, payload);
|
|
1736
|
+
}
|
|
1737
|
+
async flush(force = false, useKeepalive = false) {
|
|
1738
|
+
if (!this.initialized) this.init();
|
|
1739
|
+
if (!isGameIdValid(this._options.gameId)) {
|
|
1740
|
+
if (!this._gameIdWarned) {
|
|
1741
|
+
this._gameIdWarned = true;
|
|
1742
|
+
this._log("errors", ">> LOGLUDO WARNING: Skipping flush because gameId is not configured.");
|
|
1743
|
+
}
|
|
1744
|
+
return false;
|
|
1745
|
+
}
|
|
1746
|
+
if (this._eventBuffer.length === 0) return true;
|
|
1747
|
+
if (this._isFlushing) {
|
|
1748
|
+
this._flushQueued = true;
|
|
1749
|
+
if (force) this._flushQueuedForce = true;
|
|
1750
|
+
return false;
|
|
1751
|
+
}
|
|
1752
|
+
const nowMs = Date.now();
|
|
1753
|
+
if (!force && nowMs < this._retryAfterTime) {
|
|
1754
|
+
return false;
|
|
1755
|
+
}
|
|
1756
|
+
this._flushQueued = false;
|
|
1757
|
+
this._flushQueuedForce = false;
|
|
1758
|
+
this._lastFlushTime = nowMs;
|
|
1759
|
+
const count = Math.min(this._eventBuffer.length, 50);
|
|
1760
|
+
const batch = this._eventBuffer.slice(0, count);
|
|
1761
|
+
this._inFlightBatchCount = count;
|
|
1762
|
+
this._isFlushing = true;
|
|
1763
|
+
const json = JSON.stringify(batch);
|
|
1764
|
+
this._log("verbose", ">> LOGLUDO PACKET OUTBOUND:", json);
|
|
1765
|
+
try {
|
|
1766
|
+
if (typeof fetch !== "function") {
|
|
1767
|
+
this._isFlushing = false;
|
|
1768
|
+
this._inFlightBatchCount = 0;
|
|
1769
|
+
return false;
|
|
1770
|
+
}
|
|
1771
|
+
const res = await fetch(this._options.apiUrl, {
|
|
1772
|
+
method: "POST",
|
|
1773
|
+
headers: { "Content-Type": "application/json" },
|
|
1774
|
+
body: json,
|
|
1775
|
+
keepalive: useKeepalive
|
|
1776
|
+
});
|
|
1777
|
+
if (res.ok) {
|
|
1778
|
+
this._log("info", `>> LOGLUDO: Batch Accepted! (${res.status})`);
|
|
1779
|
+
this._clearPending(count);
|
|
1780
|
+
return true;
|
|
1781
|
+
}
|
|
1782
|
+
this._log("errors", `>> LOGLUDO ERROR: ${res.status} ${res.statusText}`);
|
|
1783
|
+
if (res.status >= 400 && res.status < 500 && res.status !== 429) {
|
|
1784
|
+
this._log(
|
|
1785
|
+
"errors",
|
|
1786
|
+
`>> LOGLUDO: Permanent 4xx client error (${res.status}). Discarding rejected batch to prevent retry storm.`
|
|
1787
|
+
);
|
|
1788
|
+
this._clearPending(count);
|
|
1789
|
+
return false;
|
|
1790
|
+
}
|
|
1791
|
+
this._markFlushFailed();
|
|
1792
|
+
return false;
|
|
1793
|
+
} catch (err) {
|
|
1794
|
+
if (useKeepalive && typeof navigator !== "undefined" && typeof navigator.sendBeacon === "function") {
|
|
1795
|
+
try {
|
|
1796
|
+
const ok = navigator.sendBeacon(
|
|
1797
|
+
this._options.apiUrl,
|
|
1798
|
+
new Blob([json], { type: "application/json" })
|
|
1799
|
+
);
|
|
1800
|
+
if (ok) {
|
|
1801
|
+
this._clearPending(count);
|
|
1802
|
+
return true;
|
|
1803
|
+
}
|
|
1804
|
+
} catch {
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
this._log("errors", ">> LOGLUDO NETWORK ERROR:", err);
|
|
1808
|
+
this._markFlushFailed();
|
|
1809
|
+
return false;
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
flushEvents(force = false) {
|
|
1813
|
+
return this.flush(force);
|
|
1814
|
+
}
|
|
1815
|
+
flush_events(force = false) {
|
|
1816
|
+
return this.flush(force);
|
|
1817
|
+
}
|
|
1818
|
+
_markFlushFailed() {
|
|
1819
|
+
this._isFlushing = false;
|
|
1820
|
+
this._inFlightBatchCount = 0;
|
|
1821
|
+
this._retryBackoffMs = this._retryBackoffMs <= 0 ? 2e3 : Math.min(this._retryBackoffMs * 2, 3e4);
|
|
1822
|
+
this._retryAfterTime = Date.now() + this._retryBackoffMs;
|
|
1823
|
+
this.saveOffline();
|
|
1824
|
+
}
|
|
1825
|
+
_clearPending(count) {
|
|
1826
|
+
const delCount = Math.min(count, this._eventBuffer.length);
|
|
1827
|
+
if (delCount > 0) {
|
|
1828
|
+
this._eventBuffer.splice(0, delCount);
|
|
1829
|
+
this._bufferRaws.splice(0, Math.min(delCount, this._bufferRaws.length));
|
|
1830
|
+
this._offlineDirty = true;
|
|
1831
|
+
}
|
|
1832
|
+
this._inFlightBatchCount = 0;
|
|
1833
|
+
this._isFlushing = false;
|
|
1834
|
+
this._retryAfterTime = 0;
|
|
1835
|
+
this._retryBackoffMs = 0;
|
|
1836
|
+
this.saveOffline();
|
|
1837
|
+
if (this._flushQueued || this._eventBuffer.length >= this._options.batchLimit) {
|
|
1838
|
+
const useForce = this._flushQueuedForce;
|
|
1839
|
+
this._flushQueued = false;
|
|
1840
|
+
this._flushQueuedForce = false;
|
|
1841
|
+
void this.flush(useForce);
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
// --- CRASH HANDLING & SESSION END ---
|
|
1845
|
+
handleException(err, extra = {}) {
|
|
1846
|
+
if (!this.initialized) this.init();
|
|
1847
|
+
const errorObj = err instanceof Error ? err : err;
|
|
1848
|
+
const message = errorObj && typeof errorObj.message === "string" && errorObj.message || (typeof err === "string" ? err : "Unhandled exception");
|
|
1849
|
+
const stack = errorObj && typeof errorObj.stack === "string" && errorObj.stack || "";
|
|
1850
|
+
const envInfo = detectEnvironment();
|
|
1851
|
+
const sceneName = extra.currentScene || this._currentSceneName;
|
|
1852
|
+
const crashPayload = {
|
|
1853
|
+
action: extra.action || "fatal_crash",
|
|
1854
|
+
severity: extra.severity || "fatal_crash",
|
|
1855
|
+
message,
|
|
1856
|
+
error_message: message,
|
|
1857
|
+
stack_trace: stack,
|
|
1858
|
+
current_scene: sceneName,
|
|
1859
|
+
room_name: sceneName,
|
|
1860
|
+
device_model: envInfo.deviceModel,
|
|
1861
|
+
os_info: envInfo.osInfo
|
|
1862
|
+
};
|
|
1863
|
+
if (this._options.captureCrashes) {
|
|
1864
|
+
this._storage.setItem(this._storageKey("crash"), JSON.stringify(crashPayload));
|
|
1865
|
+
}
|
|
1866
|
+
this.sendEvent("error", crashPayload);
|
|
1867
|
+
if (this._options.autoFlush) {
|
|
1868
|
+
void this.flush(true, true);
|
|
1869
|
+
}
|
|
1870
|
+
}
|
|
1871
|
+
handle_exception(err, extra) {
|
|
1872
|
+
this.handleException(err, extra);
|
|
1873
|
+
}
|
|
1874
|
+
_endSession(exitState) {
|
|
1875
|
+
if (!this.initialized || this._sessionEnded) return;
|
|
1876
|
+
this._sessionEnded = true;
|
|
1877
|
+
const nowMs = Date.now();
|
|
1878
|
+
const sceneElapsedSec = Math.max(0, Math.round((nowMs - this._sceneEnterTimeMs) / 100) / 10);
|
|
1879
|
+
if (this._currentSceneName) {
|
|
1880
|
+
this.sendEvent("gameplay", {
|
|
1881
|
+
action: "scene_exit",
|
|
1882
|
+
room_name: this._currentSceneName,
|
|
1883
|
+
scene_name: this._currentSceneName,
|
|
1884
|
+
next_scene: "",
|
|
1885
|
+
duration_seconds: sceneElapsedSec,
|
|
1886
|
+
is_session_exit: true,
|
|
1887
|
+
exit_state: exitState
|
|
1888
|
+
});
|
|
1889
|
+
}
|
|
1890
|
+
const payload = {
|
|
1891
|
+
duration_seconds: Math.max(0, Math.round((nowMs - this._sessionStartTimeMs) / 1e3)),
|
|
1892
|
+
exit_state: exitState,
|
|
1893
|
+
last_scene: this._currentSceneName || "main"
|
|
1894
|
+
};
|
|
1895
|
+
const aggs = this.aggregateGetAll(true);
|
|
1896
|
+
for (const [k, v] of Object.entries(aggs)) {
|
|
1897
|
+
payload[k] = v;
|
|
1898
|
+
}
|
|
1899
|
+
this.sendEvent("session_end", payload);
|
|
1900
|
+
this.saveOffline();
|
|
1901
|
+
if (isGameIdValid(this._options.gameId) && this._eventBuffer.length > 0 && typeof navigator !== "undefined" && typeof navigator.sendBeacon === "function") {
|
|
1902
|
+
const batch = this._eventBuffer.slice(0, 50);
|
|
1903
|
+
const sent = navigator.sendBeacon(
|
|
1904
|
+
this._options.apiUrl,
|
|
1905
|
+
new Blob([JSON.stringify(batch)], { type: "application/json" })
|
|
1906
|
+
);
|
|
1907
|
+
if (sent) {
|
|
1908
|
+
this._eventBuffer.splice(0, batch.length);
|
|
1909
|
+
this._bufferRaws.splice(0, Math.min(batch.length, this._bufferRaws.length));
|
|
1910
|
+
this._offlineDirty = true;
|
|
1911
|
+
this.saveOffline();
|
|
1912
|
+
return;
|
|
1913
|
+
}
|
|
1914
|
+
}
|
|
1915
|
+
void this.flush(true, true);
|
|
1916
|
+
}
|
|
1917
|
+
// --- SCENELYTICS CANVAS CAPTURE ---
|
|
1918
|
+
async captureScene(sceneNameOrOptions, maybeOptions) {
|
|
1919
|
+
if (!this.initialized) this.init();
|
|
1920
|
+
if (!isGameIdValid(this._options.gameId)) {
|
|
1921
|
+
this._log("errors", ">> LOGLUDO: Cannot capture scene before gameId is configured.");
|
|
1922
|
+
return false;
|
|
1923
|
+
}
|
|
1924
|
+
const opts = typeof sceneNameOrOptions === "string" ? { ...maybeOptions || {}, sceneName: sceneNameOrOptions } : sceneNameOrOptions || maybeOptions || {};
|
|
1925
|
+
const sceneName = (opts.sceneName || this._currentSceneName || "main").trim();
|
|
1926
|
+
const canvas = resolveTargetCanvas(opts.canvas ?? this._options.canvas);
|
|
1927
|
+
if (!canvas) {
|
|
1928
|
+
this._log("errors", ">> LOGLUDO: No <canvas> element found for SceneLytics capture.");
|
|
1929
|
+
return false;
|
|
1930
|
+
}
|
|
1931
|
+
const doCapture = async () => {
|
|
1932
|
+
try {
|
|
1933
|
+
const ok = await captureAndUploadCanvas({
|
|
1934
|
+
canvas,
|
|
1935
|
+
apiUrl: this._options.apiUrl,
|
|
1936
|
+
gameId: this._options.gameId,
|
|
1937
|
+
gameVersion: this._options.gameVersion,
|
|
1938
|
+
sceneName,
|
|
1939
|
+
tileSize: opts.tileSize
|
|
1940
|
+
});
|
|
1941
|
+
if (ok) {
|
|
1942
|
+
this._log("info", `>> LOGLUDO: SceneLytics capture uploaded for '${sceneName}'.`);
|
|
1943
|
+
}
|
|
1944
|
+
return ok;
|
|
1945
|
+
} catch (err) {
|
|
1946
|
+
this._log("errors", ">> LOGLUDO ERROR (Capture):", err);
|
|
1947
|
+
return false;
|
|
1948
|
+
}
|
|
1949
|
+
};
|
|
1950
|
+
if (opts.waitForRender !== false && typeof globalThis.requestAnimationFrame === "function") {
|
|
1951
|
+
return new Promise((resolve) => {
|
|
1952
|
+
globalThis.requestAnimationFrame(() => {
|
|
1953
|
+
void doCapture().then(resolve);
|
|
1954
|
+
});
|
|
1955
|
+
});
|
|
1956
|
+
}
|
|
1957
|
+
return doCapture();
|
|
1958
|
+
}
|
|
1959
|
+
capture_room(sceneNameOrOptions, maybeOptions) {
|
|
1960
|
+
return this.captureScene(sceneNameOrOptions, maybeOptions);
|
|
1961
|
+
}
|
|
1962
|
+
// --- FRAMEWORK ADAPTERS (PHASER 3+) ---
|
|
1963
|
+
/**
|
|
1964
|
+
* Zero-dependency adapter for Phaser 3 (`Phaser.Game`).
|
|
1965
|
+
* Automatically attaches canvas capture, version metadata, and Phaser scene transitions.
|
|
1966
|
+
*/
|
|
1967
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1968
|
+
attachPhaser(phaserGame, options = {}) {
|
|
1969
|
+
if (!phaserGame || typeof phaserGame !== "object") {
|
|
1970
|
+
return () => {
|
|
1971
|
+
};
|
|
1972
|
+
}
|
|
1973
|
+
this.framework = "Phaser";
|
|
1974
|
+
this._options.explicitFramework = "Phaser";
|
|
1975
|
+
const cfgVersion = options.gameVersion || phaserGame.config?.gameVersion || void 0;
|
|
1976
|
+
if (!this.initialized) {
|
|
1977
|
+
this.init({
|
|
1978
|
+
...cfgVersion ? { gameVersion: String(cfgVersion) } : {},
|
|
1979
|
+
framework: "Phaser",
|
|
1980
|
+
...phaserGame.canvas ? { canvas: phaserGame.canvas } : {}
|
|
1981
|
+
});
|
|
1982
|
+
} else if (phaserGame.canvas && !this._options.canvas) {
|
|
1983
|
+
this._options.canvas = phaserGame.canvas;
|
|
1984
|
+
}
|
|
1985
|
+
const trackScenes = options.trackScenes !== false;
|
|
1986
|
+
const hookedKeys = /* @__PURE__ */ new Set();
|
|
1987
|
+
const unsubs = [];
|
|
1988
|
+
const hookScene = (scene) => {
|
|
1989
|
+
if (!scene || !scene.sys || !scene.sys.settings) return;
|
|
1990
|
+
const key = String(scene.sys.settings.key || "main");
|
|
1991
|
+
if (!trackScenes || hookedKeys.has(key)) return;
|
|
1992
|
+
hookedKeys.add(key);
|
|
1993
|
+
const sysEvents = scene.sys.events;
|
|
1994
|
+
if (sysEvents && typeof sysEvents.on === "function") {
|
|
1995
|
+
const onStart = () => {
|
|
1996
|
+
this.enterScene(key);
|
|
1997
|
+
};
|
|
1998
|
+
const onWake = () => {
|
|
1999
|
+
this.enterScene(key);
|
|
2000
|
+
};
|
|
2001
|
+
sysEvents.on("start", onStart);
|
|
2002
|
+
sysEvents.on("wake", onWake);
|
|
2003
|
+
unsubs.push(() => {
|
|
2004
|
+
if (typeof sysEvents.off === "function") {
|
|
2005
|
+
sysEvents.off("start", onStart);
|
|
2006
|
+
sysEvents.off("wake", onWake);
|
|
2007
|
+
}
|
|
2008
|
+
});
|
|
2009
|
+
}
|
|
2010
|
+
if (options.autoStartInitialScene !== false && scene.sys.isActive?.()) {
|
|
2011
|
+
this.enterScene(key);
|
|
2012
|
+
}
|
|
2013
|
+
};
|
|
2014
|
+
const sceneMgr = phaserGame.scene;
|
|
2015
|
+
if (sceneMgr && Array.isArray(sceneMgr.scenes)) {
|
|
2016
|
+
for (const s of sceneMgr.scenes) {
|
|
2017
|
+
hookScene(s);
|
|
2018
|
+
}
|
|
2019
|
+
}
|
|
2020
|
+
const gameEvents = phaserGame.events;
|
|
2021
|
+
if (gameEvents && typeof gameEvents.on === "function") {
|
|
2022
|
+
const onStep = () => {
|
|
2023
|
+
if (sceneMgr && Array.isArray(sceneMgr.scenes)) {
|
|
2024
|
+
for (const s of sceneMgr.scenes) {
|
|
2025
|
+
hookScene(s);
|
|
2026
|
+
}
|
|
2027
|
+
}
|
|
2028
|
+
};
|
|
2029
|
+
const onReady = () => {
|
|
2030
|
+
if (phaserGame.canvas) {
|
|
2031
|
+
this._options.canvas = phaserGame.canvas;
|
|
2032
|
+
}
|
|
2033
|
+
onStep();
|
|
2034
|
+
};
|
|
2035
|
+
const onDestroy = () => {
|
|
2036
|
+
cleanup();
|
|
2037
|
+
};
|
|
2038
|
+
gameEvents.on("ready", onReady);
|
|
2039
|
+
gameEvents.on("step", onStep);
|
|
2040
|
+
gameEvents.once?.("destroy", onDestroy);
|
|
2041
|
+
unsubs.push(() => {
|
|
2042
|
+
if (typeof gameEvents.off === "function") {
|
|
2043
|
+
gameEvents.off("ready", onReady);
|
|
2044
|
+
gameEvents.off("step", onStep);
|
|
2045
|
+
gameEvents.off("destroy", onDestroy);
|
|
2046
|
+
}
|
|
2047
|
+
});
|
|
2048
|
+
}
|
|
2049
|
+
const cleanup = () => {
|
|
2050
|
+
for (const fn of unsubs) {
|
|
2051
|
+
try {
|
|
2052
|
+
fn();
|
|
2053
|
+
} catch {
|
|
2054
|
+
}
|
|
2055
|
+
}
|
|
2056
|
+
};
|
|
2057
|
+
return cleanup;
|
|
2058
|
+
}
|
|
2059
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2060
|
+
attach_phaser(phaserGame, options = {}) {
|
|
2061
|
+
return this.attachPhaser(phaserGame, options);
|
|
2062
|
+
}
|
|
2063
|
+
getQueuedEventCount() {
|
|
2064
|
+
return this._eventBuffer.length;
|
|
2065
|
+
}
|
|
2066
|
+
/** Current periodic flush cadence in milliseconds. */
|
|
2067
|
+
getFlushIntervalMs() {
|
|
2068
|
+
return this._options.flushIntervalMs;
|
|
2069
|
+
}
|
|
2070
|
+
destroy() {
|
|
2071
|
+
if (this.initialized) {
|
|
2072
|
+
this._endSession("normal_quit");
|
|
2073
|
+
}
|
|
2074
|
+
this._stopTimers();
|
|
2075
|
+
this._detachListeners();
|
|
2076
|
+
if (this._rafId !== null && typeof globalThis !== "undefined" && typeof globalThis.cancelAnimationFrame === "function") {
|
|
2077
|
+
globalThis.cancelAnimationFrame(this._rafId);
|
|
2078
|
+
this._rafId = null;
|
|
2079
|
+
}
|
|
2080
|
+
this.initialized = false;
|
|
2081
|
+
}
|
|
2082
|
+
};
|
|
2083
|
+
var LOGLUDO = new LogLudoClient();
|
|
2084
|
+
|
|
2085
|
+
// src/index.ts
|
|
2086
|
+
var index_default = LOGLUDO;
|
|
2087
|
+
export {
|
|
2088
|
+
DEFAULT_API_URL,
|
|
2089
|
+
LOGLUDO,
|
|
2090
|
+
LogLudoClient,
|
|
2091
|
+
SDK_VERSION,
|
|
2092
|
+
index_default as default,
|
|
2093
|
+
detectDevMode,
|
|
2094
|
+
detectEnvironment,
|
|
2095
|
+
detectFramework,
|
|
2096
|
+
formatCoord,
|
|
2097
|
+
minifyPath
|
|
2098
|
+
};
|