-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
628 lines (575 loc) · 41.4 KB
/
Copy pathserver.js
File metadata and controls
628 lines (575 loc) · 41.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
const http = require("http");
const fs = require("fs");
const path = require("path");
const host = process.env.HOST || "0.0.0.0";
const port = Number(process.env.PORT || 8787);
let serverRoot = __dirname;
const clients = new Set();
let overlayState = null;
let timestampSession = null;
const proximityBySourceId = new Map();
const monsterConfig = {
levels: {
1: ["Peeper", "Shadow Child", "Gnomes", "Apex Predator", "Spewer", "Bella", "Birthday Boy", "Elsa", "Tick"],
2: ["Rugrat", "Animal", "Upscream", "Chef", "Hidden", "Bowtie", "Mentalist", "Banger", "Gambit", "Headgrab", "Heart Hugger", "Oogly"],
3: ["Headman", "Robe", "Huntsman", "Reaper", "Clown", "Trudge", "Cleanup Crew", "Loom"]
},
strength: {
Banger: 0, Gnome: 0, Gnomes: 0, Animal: 4, "Birthday Boy": 4, Headgrab: 4, Mentalist: 4, Hidden: 4,
Rugrat: 4, Spewer: 4, Upscream: 4, Bowtie: 7, Bella: 9, Chef: 9, Gambit: 9, "Heart Hugger": 9,
Huntsman: 9, Oogly: 9, Reaper: 9, "Shadow Child": 9, "Cleanup Crew": 13, Clown: 13, Headman: 13,
Loom: 13, Robe: 13, Trudge: 13, "Apex Predator": "impossible", Elsa: "impossible", Peeper: "impossible", Tick: "impossible"
},
aliases: {
apexpredator: "Apex Predator", animal: "Animal", animals: "Animals", banger: "Banger", bangers: "Bangers",
bella: "Bella", birthdayboy: "Birthday Boy", bowtie: "Bowtie", bowties: "Bowties", chef: "Chef", cheffrog: "Chef",
chefs: "Chefs", cleanupcrew: "Cleanup Crew", clown: "Clown", clownbeamer: "Clown", elsa: "Elsa", gambit: "Gambit",
gnome: "Gnomes", gnomes: "Gnomes", headgrab: "Headgrab", headman: "Headman", hearthugger: "Heart Hugger",
hidden: "Hidden", huntsman: "Huntsman", loom: "Loom", mentalist: "Mentalist", mentalists: "Mentalists",
oogly: "Oogly", peeper: "Peeper", peepers: "Peepers", reaper: "Reaper", robe: "Robe", rugrat: "Rugrat",
shadowchild: "Shadow Child", spewer: "Spewer", spewers: "Spewers", tick: "Tick", trudge: "Trudge",
upscream: "Upscream", upscreams: "Upscreams"
}
};
const levelMonsterCounts = {
"1-2": { level1: 1, level2: 0, level3: 1 },
"3-5": { level1: 1, level2: 1, level3: 1 },
"6-8": { level1: 2, level2: 2, level3: 2 },
"9": { level1: 2, level2: 3, level3: 2 },
"10-19": { level1: 2, level2: 3, level3: 3 },
"20+": { level1: 3, level2: 4, level3: 4 }
};
const defaultPlayerUpgrades = {
strength: 0, tumbleLaunch: 0, range: 0, sprintSpeed: 0, tumbleWings: 0,
crouchRest: 0, extraJump: 0, tumbleClimb: 0, health: 0, stamina: 0,
mapPlayerCount: 0, deathHeadBattery: 0
};
const allUpgradeKeys = Object.keys(defaultPlayerUpgrades);
const defaultUpgradeVisibility = allUpgradeKeys.reduce((accumulator, key) => {
accumulator[key] = (key !== "health" && key !== "stamina"); // <-- Замени на эту строчку
return accumulator;
}, {});
const defaultOverlayState = {
level: 1, gameplayVisible: false, tabHidden: false, players: {},
upgradeVisibility: { ...defaultUpgradeVisibility }, style: 1, interfaceLanguage: "ru",
bgEnabled: false, timerVisible: true, upgradesVisible: true, upgradeLayout: "inline",
compactModeEnabled: true, upgradeRows: "double", mapValueVisible: true, lostValueVisible: true,
valueWrapEnabled: true, monsterIconsVisible: true, levelBadgeVisible: true,
upgradeTooltipsVisible: false, monsterHealthBarsVisible: true, monsterProximityWavesVisible: true,
monsterStrengthVisible: true, respawnTimerVisible: true, respawnIndicatorVisible: true,
onlyAliveMonstersVisible: false, onlyAliveIncludeUndetected: false, squareSize: 54, upgradeSize: 24,
overlayScaleVersion: 3, columnsCount: 11, columnsLayoutVersion: 2, overlayDefaultsVersion: 4,
overlayAlignment: "center", overlayPosition: { left: 0, top: 0, anchorX: "center", anchorY: "top" },
controlsPosition: null, hoverOpacity: 50, seconds: 0, running: false, startedAt: null,
monsters: [], roster: [], rosterPending: false, mapValue: 0, mapValueInitial: 0, mapValueGoal: null, lostValue: 0,
upgradesAlignment: "center", showInShop: true, levelName: ""
};
const levelNameAliases = { swiftbroomacademy: "Wizard", wizard: "Wizard", macjannekstation: "Arctic", arctic: "Arctic", headmanmanor: "Manor", manor: "Manor", museumofhumanarts: "Museum", museum: "Museum" };
const respawnReductionFlashThresholdSeconds = 2.5;
const respawnReductionFlashDurationMs = 1100;
function normalizeOverlayState(rawState) {
if (!rawState) return null;
const source = { ...defaultOverlayState, ...rawState };
const sourceSquareSize = Number(source.squareSize);
const sourceUpgradeSize = Number(source.upgradeSize);
const shouldMigrateDefaultSizes = Number(source.overlayScaleVersion) < 3;
const sourceOverlayDefaultsVersion = Number(rawState.overlayDefaultsVersion || 0);
const shouldMigrateOverlayDefaults = sourceOverlayDefaultsVersion < 2;
const shouldMigrateTooltipDefault = sourceOverlayDefaultsVersion < 3;
const shouldMigrateValueWrapDefault = sourceOverlayDefaultsVersion < 4;
const squareSize = Number.isFinite(sourceSquareSize) ? (shouldMigrateDefaultSizes && (sourceSquareSize === 50 || sourceSquareSize === 64) ? defaultOverlayState.squareSize : sourceSquareSize) : defaultOverlayState.squareSize;
const upgradeSize = Number.isFinite(sourceUpgradeSize) ? (shouldMigrateDefaultSizes && sourceUpgradeSize === 32 ? defaultOverlayState.upgradeSize : sourceUpgradeSize) : defaultOverlayState.upgradeSize;
const sourceColumnsCount = Number(source.columnsCount);
const columnsCount = Number.isFinite(sourceColumnsCount) ? (shouldMigrateOverlayDefaults && sourceColumnsCount === 7 ? defaultOverlayState.columnsCount : sourceColumnsCount) : defaultOverlayState.columnsCount;
const hoverOpacity = Number.isFinite(Number(source.hoverOpacity)) ? Math.min(100, Math.max(20, Number(source.hoverOpacity))) : defaultOverlayState.hoverOpacity;
const interfaceLanguage = source.interfaceLanguage === "en" ? "en" : defaultOverlayState.interfaceLanguage;
const normalizedOverlayAlignment = ["left", "center", "right"].includes(source.overlayAlignment) ? source.overlayAlignment : defaultOverlayState.overlayAlignment;
const overlayAlignment = shouldMigrateOverlayDefaults && normalizedOverlayAlignment === "left" ? defaultOverlayState.overlayAlignment : normalizedOverlayAlignment;
const hasCompactModeEnabled = Object.prototype.hasOwnProperty.call(rawState, "compactModeEnabled");
const normalizedCompactModeEnabled = hasCompactModeEnabled && typeof source.compactModeEnabled === "boolean" ? source.compactModeEnabled : source.upgradeLayout === "inline";
const compactModeEnabled = shouldMigrateOverlayDefaults && !normalizedCompactModeEnabled && source.upgradeLayout !== "inline" ? defaultOverlayState.compactModeEnabled : normalizedCompactModeEnabled;
const normalizedUpgradeRows = ["single", "double"].includes(source.upgradeRows) ? source.upgradeRows : defaultOverlayState.upgradeRows;
const upgradeRows = shouldMigrateOverlayDefaults && normalizedUpgradeRows === "single" ? defaultOverlayState.upgradeRows : normalizedUpgradeRows;
const normalizedOverlayPosition = normalizePosition(source.overlayPosition);
const overlayPosition = shouldMigrateOverlayDefaults && !normalizedOverlayPosition ? normalizePosition(defaultOverlayState.overlayPosition) : normalizedOverlayPosition;
const controlsPosition = normalizePosition(source.controlsPosition);
const timerVisible = shouldMigrateOverlayDefaults && source.timerVisible === false ? defaultOverlayState.timerVisible : Boolean(source.timerVisible);
const upgradeTooltipsVisible = shouldMigrateTooltipDefault && source.upgradeTooltipsVisible === true ? defaultOverlayState.upgradeTooltipsVisible : Boolean(source.upgradeTooltipsVisible);
const valueWrapEnabled = shouldMigrateValueWrapDefault && source.valueWrapEnabled === false ? defaultOverlayState.valueWrapEnabled : Boolean(source.valueWrapEnabled);
const mapValue = normalizeCurrencyValue(source.mapValue, defaultOverlayState.mapValue);
const mapValueInitial = normalizeCurrencyValue(source.mapValueInitial, defaultOverlayState.mapValueInitial);
const mapValueGoal = normalizeCurrencyValue(source.mapValueGoal, defaultOverlayState.mapValueGoal);
const lostValue = normalizeCurrencyValue(source.lostValue, defaultOverlayState.lostValue);
const upgradeVisibility = normalizeUpgradeVisibility(source.upgradeVisibility);
const players = source.players && typeof source.players === "object" ? source.players : {};
return {
...source, style: 1, squareSize, upgradeSize, overlayScaleVersion: defaultOverlayState.overlayScaleVersion,
columnsCount, columnsLayoutVersion: defaultOverlayState.columnsLayoutVersion, overlayDefaultsVersion: defaultOverlayState.overlayDefaultsVersion,
hoverOpacity, interfaceLanguage, overlayAlignment, compactModeEnabled, upgradeLayout: compactModeEnabled ? "inline" : "stacked",
upgradeRows, overlayPosition, controlsPosition, timerVisible, upgradeTooltipsVisible, valueWrapEnabled, upgradeVisibility,
players, monsters: Array.isArray(source.monsters) ? source.monsters : [], roster: Array.isArray(source.roster) ? source.roster : [],
mapValue, mapValueInitial, mapValueGoal, lostValue
};
}
function normalizeCurrencyValue(value, fallback) {
if (value === null && fallback === null) return null;
const parsed = Number(value);
if (!Number.isFinite(parsed)) return fallback;
return Math.max(0, Math.round(parsed));
}
function normalizeUpgradeVisibility(rawVisibility) {
const source = rawVisibility && typeof rawVisibility === "object" ? rawVisibility : {};
return allUpgradeKeys.reduce((accumulator, key) => { accumulator[key] = Object.prototype.hasOwnProperty.call(source, key) ? Boolean(source[key]) : true; return accumulator; }, {});
}
function normalizePosition(position) {
if (!position || typeof position !== "object") return null;
const left = Number(position.left), top = Number(position.top);
if (!Number.isFinite(left) || !Number.isFinite(top)) return null;
return { left: Math.max(0, Math.round(left)), top: Math.max(0, Math.round(top)), anchorX: ["left", "center", "right"].includes(position.anchorX) ? position.anchorX : null, anchorY: ["top", "center", "bottom"].includes(position.anchorY) ? position.anchorY : null };
}
const mimeTypes = { ".html": "text/html; charset=utf-8", ".css": "text/css; charset=utf-8", ".js": "application/javascript; charset=utf-8", ".json": "application/json; charset=utf-8", ".ttf": "font/ttf", ".otf": "font/otf", ".png": "image/png", ".ico": "image/x-icon", ".webp": "image/webp" };
function sendJson(response, statusCode, payload) {
response.writeHead(statusCode, { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "no-store" });
response.end(JSON.stringify(payload));
}
function readBody(request) {
return new Promise((resolve, reject) => {
let body = "";
request.on("data", (chunk) => { body += chunk; if (body.length > 1024 * 1024) { request.destroy(); reject(new Error("Request body is too large")); } });
request.on("end", () => resolve(body));
request.on("error", reject);
});
}
function broadcastState() {
const data = `data: ${JSON.stringify(getNormalizedOverlayState())}\n\n`;
for (const client of clients) client.write(data);
}
function getNormalizedOverlayState() {
overlayState = normalizeOverlayState(overlayState) || { ...defaultOverlayState };
return overlayState;
}
function normalizeMonsterKey(name) { return String(name || "").toLowerCase().replace(/[^a-z0-9]/g, ""); }
function normalizeMonsterName(name) { const key = normalizeMonsterKey(name); return monsterConfig.aliases[key] || null; }
function getCountsForLevel(level) { if (level <= 2) return levelMonsterCounts["1-2"]; if (level <= 5) return levelMonsterCounts["3-5"]; if (level <= 8) return levelMonsterCounts["6-8"]; if (level === 9) return levelMonsterCounts["9"]; if (level <= 19) return levelMonsterCounts["10-19"]; return levelMonsterCounts["20+"]; }
function normalizeLevel(value) { const level = Number(value); if (!Number.isFinite(level) || level < 1) return null; return Math.floor(level); }
function getMonsterLevel(monsterName) { for (let level = 1; level <= 3; level++) { if (monsterConfig.levels[level].includes(monsterName)) return level; } return 3; }
function getMonsterFileName(monsterName) {
const replacements = { peeper: "eye_monster", peepers: "eye_monster", "shadow child": "shadow_child", "shadow children": "shadow_child", gnomes: "gnome", "apex predator": "apex_predator", "apex predators": "apex_predator", chef: "chef_frog", chefs: "chef_frog", clown: "clown_beamer", animals: "animal", upscreams: "upscream", bowties: "bowtie", mentalists: "mentalist", spewers: "spewer", bangers: "banger" };
const key = monsterName.toLowerCase();
return replacements[key] || key;
}
function getMonsterCount(monsterName, level, isReplacement = false) {
if (monsterName === "Gnomes" && level !== 3) return 4;
if ((monsterName === "Bangers" || monsterName === "Banger") && level === 2) return 3;
if (level !== 3 || !isReplacement) return null;
if (["Animals", "Upscreams", "Bowties", "Rugrat", "Mentalists", "Peepers", "Chefs"].includes(monsterName)) return 3;
if (monsterName === "Hidden") return 2;
if (["Apex Predators", "Spewers", "Shadow Children"].includes(monsterName)) return 4;
if (monsterName === "Bangers") return 6;
if (monsterName === "Gnomes") return 10;
if (["Bella", "Birthday Boy", "Elsa", "Headgrab", "Tick"].includes(monsterName)) return 3;
if (["Gambit", "Heart Hugger", "Oogly"].includes(monsterName)) return 2;
return null;
}
function getMonsterStrength(monsterName) {
if (monsterConfig.strength[monsterName] !== undefined) return monsterConfig.strength[monsterName];
if (monsterName.endsWith("s")) return monsterConfig.strength[monsterName.slice(0, -1)];
return undefined;
}
function createMonsterEntry(monsterName, level, isReplacement, sourceId = null) {
return { id: `${Date.now()}-${Math.random().toString(16).slice(2)}`, level, name: monsterName, image: `assets/monsters/${getMonsterFileName(monsterName)}.webp`, count: getMonsterCount(monsterName, level, isReplacement), strength: getMonsterStrength(monsterName), sourceId };
}
function addSeenMonster(rawName, rawSourceId) {
const monsterName = normalizeMonsterName(rawName);
if (!monsterName) return { ok: false, statusCode: 422, payload: { error: "Unknown monster", received: rawName } };
const state = { ...defaultOverlayState, ...(normalizeOverlayState(overlayState) || {}), monsters: Array.isArray(overlayState?.monsters) ? overlayState.monsters : [], roster: Array.isArray(overlayState?.roster) ? overlayState.roster : [] };
const level = getMonsterLevel(monsterName);
const isReplacement = level === 3 && !monsterConfig.levels[3].includes(monsterName);
const monsterCount = getMonsterCount(monsterName, level, isReplacement);
const rawId = rawSourceId == null ? null : String(rawSourceId);
const rosterSlot = rawId == null ? null : state.roster.find((slot) => Array.isArray(slot.sourceIds) && slot.sourceIds.includes(rawId));
const sourceId = rosterSlot?.id || (rawId == null ? null : `enemy:${rawId}`);
if ((sourceId != null && state.monsters.some((monster) => monster.sourceId === sourceId)) || (state.roster.length === 0 && monsterCount != null && state.monsters.some((monster) => normalizeMonsterKey(monster.name) === normalizeMonsterKey(monsterName)))) {
overlayState = state;
return { ok: true, statusCode: 200, payload: { ok: true, added: false, monster: monsterName, reason: "already-known" } };
}
if (state.roster.length === 0) {
const counts = getCountsForLevel(state.level);
const currentLevelCount = state.monsters.filter((monster) => monster.level === level).length;
if (currentLevelCount >= counts[`level${level}`]) {
overlayState = state;
return { ok: true, statusCode: 200, payload: { ok: true, added: false, monster: monsterName, reason: "level-slot-limit" } };
}
}
overlayState = { ...state, monsters: [...state.monsters, createMonsterEntry(monsterName, level, isReplacement, sourceId)] };
return { ok: true, statusCode: 200, payload: { ok: true, added: true, monster: monsterName, level } };
}
function updateMonsterRoster(rawMonsters) {
if (!Array.isArray(rawMonsters)) return { ok: false, statusCode: 422, payload: { error: "Invalid monster roster" } };
const state = { ...defaultOverlayState, ...(normalizeOverlayState(overlayState) || {}), monsters: Array.isArray(overlayState?.monsters) ? overlayState.monsters : [], roster: Array.isArray(overlayState?.roster) ? overlayState.roster : [] };
const roster = state.roster.map((slot) => ({ ...slot, sourceIds: Array.isArray(slot.sourceIds) ? [...slot.sourceIds] : [] }));
for (const rawMonster of rawMonsters) {
const monsterName = normalizeMonsterName(rawMonster?.name);
if (!monsterName || rawMonster?.id == null) continue;
const sourceId = String(rawMonster.id), level = getMonsterLevel(monsterName), isReplacement = level === 3 && !monsterConfig.levels[3].includes(monsterName);
const groupSize = getMonsterCount(monsterName, level, isReplacement), groupKey = groupSize == null ? null : normalizeMonsterKey(monsterName);
let slot = roster.find((entry) => Array.isArray(entry.sourceIds) && entry.sourceIds.includes(sourceId));
if (!slot && groupKey != null) {
const groupSlots = roster.filter((entry) => entry.groupKey === groupKey);
slot = groupSlots.find((entry) => entry.sourceIds.length < groupSize);
if (!slot) { slot = { id: `group:${groupKey}:${groupSlots.length + 1}`, level, groupKey, sourceIds: [] }; roster.push(slot); }
}
const slotId = `enemy:${sourceId}`;
if (!slot && groupKey == null) slot = roster.find((entry) => entry.id === slotId);
if (!slot) { slot = { id: slotId, level, sourceIds: [] }; roster.push(slot); }
if (!slot.sourceIds.includes(sourceId)) slot.sourceIds.push(sourceId);
}
roster.sort((left, right) => left.level - right.level || left.id.localeCompare(right.id));
const rosterSourceIds = new Set(roster.flatMap((slot) => slot.sourceIds || []).map(String));
for (const sourceId of proximityBySourceId.keys()) { if (!rosterSourceIds.has(sourceId)) proximityBySourceId.delete(sourceId); }
overlayState = { ...state, roster, rosterPending: false };
return { ok: true, statusCode: 200, payload: { ok: true, slots: roster.length } };
}
function getPortableRoot() { return path.basename(serverRoot).toLowerCase() === "resources" ? path.dirname(serverRoot) : serverRoot; }
function padTimePart(value) { return String(value).padStart(2, "0"); }
function createTimestampFilePath() {
const now = new Date();
const fileName = [now.getFullYear(), padTimePart(now.getMonth() + 1), padTimePart(now.getDate())].join("-") + "_" + [padTimePart(now.getHours()), padTimePart(now.getMinutes()), padTimePart(now.getSeconds())].join("-") + ".txt";
return path.join(getPortableRoot(), "timestamps", fileName);
}
function formatLocalClockTime(date = new Date()) { return `${padTimePart(date.getHours())}:${padTimePart(date.getMinutes())}:${padTimePart(date.getSeconds())}`; }
function normalizeLevelName(value) { const raw = String(value || "").trim(); if (!raw) return "?"; const compact = raw.replace(/[^a-z0-9]/gi, "").toLowerCase(); if (levelNameAliases[compact]) return levelNameAliases[compact]; for (const [name, levelName] of Object.entries(levelNameAliases)) { if (compact.includes(name)) return levelName; } return "?"; }
function createTimestampSession() { timestampSession = { filePath: createTimestampFilePath(), lines: [], activeLineIndex: null }; return timestampSession; }
function getTimestampSession() { if (!timestampSession) return createTimestampSession(); return timestampSession; }
function writeTimestampSession() { if (!timestampSession) return; try { fs.mkdirSync(path.dirname(timestampSession.filePath), { recursive: true }); fs.writeFileSync(timestampSession.filePath, `${timestampSession.lines.join("\n")}\n`, "utf8"); } catch (error) { console.warn(`Could not write timestamps file: ${error.message}`); } }
function formatMonsterTimestampList(monsters) { return getMonsterTotals(monsters).map(({ name, count }) => count > 1 ? `${name} x${count}` : name); }
function getMonsterTotals(monsters) {
const totals = new Map();
for (const monster of Array.isArray(monsters) ? monsters : []) {
const name = String(monster?.name || "").trim();
if (!name) continue;
const count = Number(monster.count), visibleCount = Number.isFinite(count) && count > 1 ? Math.floor(count) : 1;
totals.set(name, (totals.get(name) || 0) + visibleCount);
}
return [...totals.entries()].map(([name, count]) => ({ name, count }));
}
function finalizeActiveTimestampLine() {
if (!timestampSession || timestampSession.activeLineIndex == null) return;
const monsters = formatMonsterTimestampList(overlayState?.monsters);
const line = timestampSession.lines[timestampSession.activeLineIndex] || "";
timestampSession.lines[timestampSession.activeLineIndex] = monsters.length > 0 ? `${line.replace(/\s+\[[^\]]*\]$/, "")} [${monsters.join(", ")}]` : line.replace(/\s+\[[^\]]*\]$/, "");
timestampSession.activeLineIndex = null;
writeTimestampSession();
}
function startTimestampLine(level, rawLevelName) {
finalizeActiveTimestampLine();
const session = getTimestampSession();
const line = `${formatLocalClockTime()} Level ${level} ${normalizeLevelName(rawLevelName)}`;
session.lines.push(line);
session.activeLineIndex = session.lines.length - 1;
writeTimestampSession();
}
function updateMonsterStatuses(rawStatuses) {
if (!Array.isArray(rawStatuses)) return { ok: false, statusCode: 422, payload: { error: "Invalid monster statuses" } };
const state = { ...defaultOverlayState, ...(normalizeOverlayState(overlayState) || {}), monsters: Array.isArray(overlayState?.monsters) ? overlayState.monsters : [], roster: Array.isArray(overlayState?.roster) ? overlayState.roster : [] };
const statuses = new Map();
for (const rawStatus of rawStatuses) {
if (rawStatus?.id == null || typeof rawStatus.alive !== "boolean") continue;
const remaining = Number(rawStatus.respawnRemaining), health = Number(rawStatus.health), maxHealth = Number(rawStatus.maxHealth);
const hasHealth = rawStatus.health != null && Number.isFinite(health), hasMaxHealth = rawStatus.maxHealth != null && Number.isFinite(maxHealth);
const sourceId = String(rawStatus.id);
const previousProximity = proximityBySourceId.get(sourceId) || { playerClose: false, playerVeryClose: false };
const proximity = { playerClose: typeof rawStatus.playerClose === "boolean" ? rawStatus.playerClose : previousProximity.playerClose, playerVeryClose: typeof rawStatus.playerVeryClose === "boolean" ? rawStatus.playerVeryClose : previousProximity.playerVeryClose };
proximityBySourceId.set(sourceId, proximity);
statuses.set(sourceId, { alive: rawStatus.alive, respawnRemaining: Number.isFinite(remaining) ? Math.max(0, remaining) : 0, health: hasHealth ? Math.max(0, health) : null, maxHealth: hasMaxHealth ? Math.max(0, maxHealth) : null, ...proximity });
}
// НОВЫЙ БЛОК: Авто-создание слотов ростера для клиентов в мультиплеере
for (const sourceId of statuses.keys()) {
let slot = state.roster.find((s) => Array.isArray(s.sourceIds) && s.sourceIds.includes(sourceId));
if (!slot) {
const monster = state.monsters.find((m) => m.sourceId === `enemy:${sourceId}` || m.sourceId === sourceId);
if (monster) {
const level = getMonsterLevel(monster.name);
const isReplacement = level === 3 && !monsterConfig.levels[3].includes(monster.name);
const groupSize = getMonsterCount(monster.name, level, isReplacement);
const groupKey = groupSize == null ? null : normalizeMonsterKey(monster.name);
if (groupKey != null) {
const groupSlots = state.roster.filter((entry) => entry.groupKey === groupKey);
slot = groupSlots.find((entry) => entry.sourceIds.length < groupSize);
if (!slot) {
slot = { id: `group:${groupKey}:${groupSlots.length + 1}`, level, groupKey, sourceIds: [] };
state.roster.push(slot);
}
} else {
slot = { id: `enemy:${sourceId}`, level, sourceIds: [] };
state.roster.push(slot);
}
slot.sourceIds.push(sourceId);
}
}
}
const now = Date.now();
let updatedSlots = 0;
const roster = state.roster.map((slot) => {
const sourceIds = Array.isArray(slot.sourceIds) ? slot.sourceIds.map(String) : [];
const sourceStatuses = sourceIds.map((id) => statuses.get(id)).filter(Boolean);
const proximityStatuses = sourceIds.map((id) => proximityBySourceId.get(id)).filter(Boolean);
const proximityPatch = { playerClose: proximityStatuses.some((status) => status.playerClose), playerVeryClose: proximityStatuses.some((status) => status.playerVeryClose) };
if (sourceStatuses.length === 0 || sourceStatuses.length !== sourceIds.length) { if (sourceStatuses.length > 0) { updatedSlots++; return { ...slot, ...proximityPatch }; } return slot; }
updatedSlots++;
const healthStatuses = sourceStatuses.filter((status) => status.health != null);
const healthPatch = healthStatuses.length > 0 ? { health: healthStatuses.reduce((total, status) => total + status.health, 0), maxHealth: healthStatuses.some((status) => status.maxHealth != null) ? healthStatuses.reduce((total, status) => total + (status.maxHealth || 0), 0) : null } : {};
const clearFlashPatch = { respawnFlashAt: null, respawnFlashUntil: null, respawnFlashAmount: null };
if (sourceStatuses.some((status) => status.alive)) { return { ...slot, ...healthPatch, ...proximityPatch, ...clearFlashPatch, alive: true, respawnEndsAt: null, respawnDuration: null }; }
const remainingValues = sourceStatuses.map((status) => status.respawnRemaining).filter((remaining) => remaining > 0);
let remaining = 0;
if (remainingValues.length > 0) {
remaining = Math.min(...remainingValues);
} else if (slot.respawnEndsAt != null) {
remaining = Math.max(0, (Number(slot.respawnEndsAt) - now) / 1000);
} else {
remaining = 0;
}
const existingEnd = Number(slot.respawnEndsAt), projectedRemaining = Number.isFinite(existingEnd) ? Math.max(0, (existingEnd - now) / 1000) : null;
const suddenReduction = slot.alive === false && projectedRemaining != null ? projectedRemaining - remaining : 0;
const hasRespawnReductionFlash = suddenReduction >= respawnReductionFlashThresholdSeconds;
const keepExistingEnd = slot.alive === false && projectedRemaining != null && Math.abs(projectedRemaining - remaining) < 1.5;
const respawnEndsAt = keepExistingEnd ? existingEnd : now + remaining * 1000;
const previousDuration = Number(slot.respawnDuration), respawnDuration = slot.alive === false && Number.isFinite(previousDuration) ? Math.max(previousDuration, remaining) : remaining;
const flashPatch = hasRespawnReductionFlash ? { respawnFlashAt: now, respawnFlashUntil: now + respawnReductionFlashDurationMs, respawnFlashAmount: Math.max(1, Math.round(suddenReduction)) } : Number(slot.respawnFlashUntil) > now ? { respawnFlashAt: slot.respawnFlashAt, respawnFlashUntil: slot.respawnFlashUntil, respawnFlashAmount: slot.respawnFlashAmount } : { respawnFlashAt: null, respawnFlashUntil: null, respawnFlashAmount: null };
return { ...slot, ...healthPatch, ...proximityPatch, ...flashPatch, alive: false, respawnEndsAt, respawnDuration };
});
overlayState = { ...state, roster };
return { ok: true, statusCode: 200, payload: { ok: true, updatedSlots } };
}
function setGameLevel(rawLevel, rawLevelName) {
const level = normalizeLevel(rawLevel);
if (level == null) return { ok: false, statusCode: 422, payload: { error: "Invalid level", received: rawLevel } };
startTimestampLine(level, rawLevelName);
proximityBySourceId.clear();
const currentState = normalizeOverlayState(overlayState) || {};
overlayState = {
...defaultOverlayState,
...currentState,
level,
levelName: String(rawLevelName || ""),
gameplayVisible: true,
// Убрано жесткое стирание players и апгрейдов, чтобы они сохранялись при переходе в магазин!
seconds: 0,
running: true,
startedAt: Date.now(),
monsters: [],
roster: [],
rosterPending: false,
mapValue: 0,
mapValueInitial: 0,
mapValueGoal: null,
lostValue: 0
};
return { ok: true, statusCode: 200, payload: { ok: true, level } };
}
function setGameplayVisibility(rawVisible) {
if (typeof rawVisible !== "boolean") return { ok: false, statusCode: 422, payload: { error: "Invalid visibility", received: rawVisible } };
if (!rawVisible) finalizeActiveTimestampLine();
overlayState = { ...defaultOverlayState, ...(normalizeOverlayState(overlayState) || {}), gameplayVisible: rawVisible };
return { ok: true, statusCode: 200, payload: { ok: true, gameplayVisible: rawVisible } };
}
function setPlayerStrength(rawStrength) {
const strength = Number(rawStrength);
if (!Number.isFinite(strength) || strength < 0) return { ok: false, statusCode: 422, payload: { error: "Invalid strength", received: rawStrength } };
overlayState = { ...defaultOverlayState, ...(normalizeOverlayState(overlayState) || {}), strength: Math.floor(strength) };
return { ok: true, statusCode: 200, payload: { ok: true, strength: overlayState.strength } };
}
function setTabHidden(rawHidden) {
if (typeof rawHidden !== "boolean") return { ok: false, statusCode: 422, payload: { error: "Invalid Tab visibility", received: rawHidden } };
overlayState = { ...defaultOverlayState, ...(normalizeOverlayState(overlayState) || {}), tabHidden: rawHidden };
return { ok: true, statusCode: 200, payload: { ok: true, tabHidden: rawHidden } };
}
function setTumbleLaunch(rawTumbleLaunch) {
const tumbleLaunch = Number(rawTumbleLaunch);
if (!Number.isFinite(tumbleLaunch) || tumbleLaunch < 0) return { ok: false, statusCode: 422, payload: { error: "Invalid Tumble Launch", received: rawTumbleLaunch } };
overlayState = { ...defaultOverlayState, ...(normalizeOverlayState(overlayState) || {}), tumbleLaunch: Math.floor(tumbleLaunch) };
return { ok: true, statusCode: 200, payload: { ok: true, tumbleLaunch: overlayState.tumbleLaunch } };
}
function setPlayerUpgrades(rawPayload) {
if (!rawPayload || !Array.isArray(rawPayload.players)) return { ok: false, statusCode: 422, payload: { error: "Invalid players payload" } };
const state = { ...defaultOverlayState, ...(normalizeOverlayState(overlayState) || {}) };
const players = state.players || {};
const activeSteamIds = new Set(rawPayload.players.map(p => p.steamId));
for (const steamId of Object.keys(players)) {
if (!activeSteamIds.has(steamId)) {
delete players[steamId];
}
}
for (const playerData of rawPayload.players) {
const steamId = playerData.steamId;
const playerName = playerData.name;
const rawUpgrades = playerData.upgrades || {};
const updates = {};
for (const key of Object.keys(defaultPlayerUpgrades)) {
const value = (key in rawUpgrades) ? Number(rawUpgrades[key]) : 0;
if (Number.isFinite(value) && value >= 0) updates[key] = Math.floor(value);
}
players[steamId] = { ...defaultPlayerUpgrades, ...updates, name: playerName };
}
// ИСПРАВЛЕНИЕ: Блок присвоения силы и локального ID
if (rawPayload.localSteamId !== undefined) {
state.localSteamId = rawPayload.localSteamId;
if (players[state.localSteamId] && players[state.localSteamId].strength !== undefined) {
state.strength = players[state.localSteamId].strength;
}
}
overlayState = { ...state, players };
return { ok: true, statusCode: 200, payload: { ok: true, processedPlayers: rawPayload.players.length } };
}
function setMapValue(rawPayload) {
if (!rawPayload || typeof rawPayload !== "object" || Array.isArray(rawPayload)) return { ok: false, statusCode: 422, payload: { error: "Invalid map value payload" } };
const mapValue = normalizeCurrencyValue(rawPayload.value ?? rawPayload.mapValue ?? rawPayload.current, null);
if (mapValue == null) return { ok: false, statusCode: 422, payload: { error: "Invalid map value", received: rawPayload.value ?? rawPayload.mapValue ?? rawPayload.current } };
const updates = { mapValue };
const mapValueInitial = normalizeCurrencyValue(rawPayload.initial ?? rawPayload.mapValueInitial, null);
const mapValueGoal = normalizeCurrencyValue(rawPayload.goal ?? rawPayload.mapValueGoal, null);
const lostValue = normalizeCurrencyValue(rawPayload.lost ?? rawPayload.lostValue, null);
if (mapValueInitial != null) updates.mapValueInitial = mapValueInitial;
if (mapValueGoal != null) updates.mapValueGoal = mapValueGoal;
if (lostValue != null) updates.lostValue = lostValue;
overlayState = { ...defaultOverlayState, ...(normalizeOverlayState(overlayState) || {}), ...updates };
return { ok: true, statusCode: 200, payload: { ok: true, mapValue: overlayState.mapValue, mapValueInitial: overlayState.mapValueInitial, mapValueGoal: overlayState.mapValueGoal, lostValue: overlayState.lostValue } };
}
function serveFile(request, response) {
const requestUrl = new URL(request.url, `http://${request.headers.host}`);
const pathname = decodeURIComponent(requestUrl.pathname === "/" ? "/overlay.html" : requestUrl.pathname);
const filePath = path.normalize(path.join(serverRoot, pathname));
if (!filePath.startsWith(serverRoot)) { response.writeHead(403); response.end("Forbidden"); return; }
fs.stat(filePath, (statError, stats) => {
if (statError || !stats.isFile()) { response.writeHead(404); response.end("Not found"); return; }
const ext = path.extname(filePath).toLowerCase();
response.writeHead(200, { "Content-Type": mimeTypes[ext] || "application/octet-stream", "Cache-Control": "no-store" });
fs.createReadStream(filePath).pipe(response);
});
}
const server = http.createServer(async (request, response) => {
try {
if (request.method === "GET" && request.url === "/api/state") { sendJson(response, 200, { state: getNormalizedOverlayState() }); return; }
if (request.method === "POST" && request.url === "/api/state") {
const body = await readBody(request);
overlayState = normalizeOverlayState(JSON.parse(body || "{}").state);
sendJson(response, 200, { ok: true });
broadcastState();
return;
}
if (request.method === "POST" && request.url === "/api/monster-seen") {
const payload = JSON.parse(await readBody(request) || "{}");
const result = addSeenMonster(payload.name || payload.monster || payload.enemy, payload.id ?? payload.instanceId);
sendJson(response, result.statusCode, result.payload);
if (result.ok && result.payload.added) broadcastState();
return;
}
if (request.method === "POST" && request.url === "/api/roster") {
const payload = JSON.parse(await readBody(request) || "{}");
const result = updateMonsterRoster(payload.monsters);
sendJson(response, result.statusCode, result.payload);
if (result.ok) broadcastState();
return;
}
if (request.method === "POST" && request.url === "/api/monster-status") {
const payload = JSON.parse(await readBody(request) || "{}");
const result = updateMonsterStatuses(payload.statuses);
sendJson(response, result.statusCode, result.payload);
if (result.ok && result.payload.updatedSlots > 0) broadcastState();
return;
}
if (request.method === "POST" && request.url === "/api/level") {
const payload = JSON.parse(await readBody(request) || "{}");
const result = setGameLevel(payload.level, payload.levelName || payload.mapName || payload.map || payload.location);
sendJson(response, result.statusCode, result.payload);
if (result.ok) broadcastState();
return;
}
if (request.method === "POST" && request.url === "/api/visibility") {
const payload = JSON.parse(await readBody(request) || "{}");
const result = setGameplayVisibility(payload.visible ?? payload.gameplayVisible);
sendJson(response, result.statusCode, result.payload);
if (result.ok) broadcastState();
return;
}
if (request.method === "POST" && request.url === "/api/strength") {
const payload = JSON.parse(await readBody(request) || "{}");
const result = setPlayerStrength(payload.strength);
sendJson(response, result.statusCode, result.payload);
if (result.ok) broadcastState();
return;
}
if (request.method === "POST" && request.url === "/api/cursor") {
const payload = JSON.parse(await readBody(request) || "{}");
const state = { ...defaultOverlayState, ...(normalizeOverlayState(overlayState) || {}) };
state.cursorVisible = Boolean(payload.visible);
overlayState = state;
sendJson(response, 200, { ok: true, cursorVisible: state.cursorVisible });
broadcastState();
return;
}
if (request.method === "POST" && request.url === "/api/tab-hidden") {
const payload = JSON.parse(await readBody(request) || "{}");
const result = setTabHidden(payload.hidden ?? payload.tabHidden);
sendJson(response, result.statusCode, result.payload);
if (result.ok) broadcastState();
return;
}
if (request.method === "POST" && request.url === "/api/tumble-launch") {
const payload = JSON.parse(await readBody(request) || "{}");
const result = setTumbleLaunch(payload.tumbleLaunch ?? payload.value);
sendJson(response, result.statusCode, result.payload);
if (result.ok) broadcastState();
return;
}
if (request.method === "POST" && request.url === "/api/upgrades") {
const payload = JSON.parse(await readBody(request) || "{}");
const result = setPlayerUpgrades(payload);
sendJson(response, result.statusCode, result.payload);
if (result.ok) broadcastState();
return;
}
if (request.method === "POST" && request.url === "/api/map-value") {
const payload = JSON.parse(await readBody(request) || "{}");
const result = setMapValue(payload);
sendJson(response, result.statusCode, result.payload);
if (result.ok) broadcastState();
return;
}
if (request.method === "GET" && request.url === "/api/events") {
response.writeHead(200, { "Content-Type": "text/event-stream; charset=utf-8", "Cache-Control": "no-store", Connection: "keep-alive" });
response.write("\n");
clients.add(response);
request.on("close", () => clients.delete(response));
return;
}
if (request.method === "GET") { serveFile(request, response); return; }
response.writeHead(405); response.end("Method not allowed");
} catch (error) { sendJson(response, 500, { error: error.message }); }
});
function startOverlayServer(options = {}) {
if (server.listening) return Promise.resolve(server);
serverRoot = path.resolve(options.root || __dirname);
const listenHost = options.host || host, listenPort = Number(options.port || port);
return new Promise((resolve, reject) => {
const handleError = (error) => reject(error);
server.once("error", handleError);
server.listen(listenPort, listenHost, () => {
server.off("error", handleError);
console.log(`Overlay: http://${listenHost}:${listenPort}/overlay.html`);
resolve(server);
});
});
}
function stopOverlayServer() {
finalizeActiveTimestampLine();
for (const client of clients) client.end();
clients.clear();
if (!server.listening) return Promise.resolve();
return new Promise((resolve) => server.close(resolve));
}
if (require.main === module) {
startOverlayServer().catch((error) => { console.error(`Overlay server failed: ${error.message}`); process.exitCode = 1; });
}
module.exports = { startOverlayServer, stopOverlayServer };