-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandCenterMod.cs
More file actions
451 lines (391 loc) · 20.2 KB
/
CommandCenterMod.cs
File metadata and controls
451 lines (391 loc) · 20.2 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
using System.Net;
using System.Net.Sockets;
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.DI;
using SPTarkov.Server.Core.Models.Spt.Config;
using SPTarkov.Server.Core.Models.Utils;
using SPTarkov.Server.Core.Servers;
using SPTarkov.Server.Core.Services;
using ZSlayerCommandCenter.Models;
using ZSlayerCommandCenter.Services;
namespace ZSlayerCommandCenter;
[Injectable(TypePriority = OnLoadOrder.PostSptModLoader + 1)]
public class CommandCenterMod(
ConfigService configService,
ConsoleBufferService consoleBufferService,
HeadlessLogService headlessLogService,
ActivityLogService activityLogService,
OfferRegenerationService offerRegenerationService,
TraderApplyService traderApplyService,
QuestOverrideService questOverrideService,
ProgressionControlService progressionControlService,
TelemetryService telemetryService,
SeasonalEventService seasonalEventService,
ConfigServer configServer,
ISptLogger<CommandCenterMod> logger) : IOnLoad
{
/// <summary>Detected server URLs, available for mail/API use.</summary>
public static string ServerUrls { get; private set; } = "";
private static readonly string[] StartupQuotes =
[
"It's dangerous to go alone! Take this Toz.",
"I used to be a PMC, then I took a buckshot to the knee.",
"Would you kindly... adjust the XP multiplier?",
"Snake? Snake?! SNAAAAKE! ...oh wait, wrong game.",
"Remember: no Russian. Unless it's Prapor.",
"The Labs weren't built in a day. But this mod was.",
"Thank you PMC! But your loot is in another stash.",
"Kept you waiting, huh? — Peacekeeper, probably",
"Do you get to the Labs very often? Oh, what am I saying, of course you don't.",
"A man chooses, a Scav obeys.",
"The right man in the wrong raid can make all the difference.",
"Protocol 16-23: Always check your corners. And your server config.",
"Mission failed. We'll get 'em next wipe.",
"Had to be me. Someone else might have gotten the loot wrong.",
"Ah shit, here we go again.",
"I need a weapon. — Master Chief, browsing the Items tab",
"Boy! ...bring me my Red Rebel. — Kratos, probably",
"Nothing is true, everything is permitted. Especially with admin tools.",
"Praise the sun! And the XP multiplier.",
"You died. But at least your server is configured properly.",
"Rise and shine, Mr. Admin. Rise and... shine.",
"The Scavs are coming. And they're not happy about the loot tables."
];
public Task OnLoad()
{
configService.LoadConfig();
var config = configService.GetConfig();
// Recover Watchdog token from file if config lost it (e.g. after config upgrade)
var tokenFilePath = Path.Combine(configService.ModPath, "watchdog-token.txt");
if (string.IsNullOrEmpty(config.Watchdog.WatchdogToken) && File.Exists(tokenFilePath))
{
try
{
var saved = File.ReadAllText(tokenFilePath).Trim();
if (!string.IsNullOrEmpty(saved))
{
config.Watchdog.WatchdogToken = saved;
configService.SaveConfig();
logger.Info("[ZSlayerHQ] Restored Watchdog auth token from watchdog-token.txt");
}
}
catch { /* ignore */ }
}
// Generate new token only if truly missing (first run)
if (string.IsNullOrEmpty(config.Watchdog.WatchdogToken))
{
config.Watchdog.WatchdogToken = WatchdogManager.GenerateToken();
configService.SaveConfig();
logger.Info("[ZSlayerHQ] Generated new Watchdog auth token");
}
// Write token to file (source of truth for Watchdog auto-discovery)
try
{
File.WriteAllText(tokenFilePath, config.Watchdog.WatchdogToken);
}
catch (Exception ex)
{
logger.Warning($"[ZSlayerHQ] Failed to write watchdog-token.txt: {ex.Message}");
}
// Wire up late-bound services
telemetryService.SetSeasonService(seasonalEventService);
// Load persisted raid history from disk
telemetryService.Initialize();
// Configure and install console interceptor
consoleBufferService.Configure(config.Dashboard.ConsoleBufferSize);
consoleBufferService.InstallConsoleInterceptor();
// Configure headless log reader
headlessLogService.Configure(config.Dashboard.HeadlessLogPath, configService.ModPath);
// Apply flea market settings (globals, prices, tax) on startup
offerRegenerationService.ApplyGlobalsAndConfig();
// Initialize trader control (discover traders, snapshot, apply config)
traderApplyService.Initialize();
// Initialize quest editor (discover quests, snapshot, apply config)
questOverrideService.Initialize();
// Initialize progression controls (snapshot globals, apply config)
progressionControlService.Initialize();
// Clean up old activity logs
activityLogService.CleanupOldLogs();
activityLogService.LogAction(ActionType.ServerStart, "", "Server started");
// Detect bound IP and build URLs based on bind mode
var httpCfg = configServer.GetConfig<HttpConfig>();
var boundIp = httpCfg.Ip;
var port = httpCfg.Port;
string MakeUrl(string ip) => $"https://{ip}:{port}/zslayer/cc/";
var urlLines = new List<string>();
var mailLines = new List<string> { "ZSlayer Command Center URLs:" };
string? publicIp = null;
string footer;
if (boundIp == "0.0.0.0")
{
// Default bind — accepts connections from anywhere
var lanIp = GetLanIp();
publicIp = GetPublicIp();
urlLines.Add($"Local: {MakeUrl("127.0.0.1")}");
mailLines.Add($" Local: {MakeUrl("127.0.0.1")}");
if (lanIp != null) { urlLines.Add($"LAN: {MakeUrl(lanIp)}"); mailLines.Add($" LAN: {MakeUrl(lanIp)}"); }
if (publicIp != null) { urlLines.Add($"Public: {MakeUrl(publicIp)}"); mailLines.Add($" Public: {MakeUrl(publicIp)}"); }
else mailLines.Add(" Public IP unknown — ask the server host for the public IP.");
footer = "Share the LAN or Public URL with players to access the Command Center";
}
else if (IsPrivateIp(boundIp))
{
// Bound to a LAN IP — loopback won't work
publicIp = GetPublicIp();
urlLines.Add($"LAN: {MakeUrl(boundIp)}");
mailLines.Add($" LAN: {MakeUrl(boundIp)}");
if (publicIp != null) { urlLines.Add($"Public: {MakeUrl(publicIp)}"); mailLines.Add($" Public: {MakeUrl(publicIp)}"); }
else mailLines.Add(" Public IP unknown — ask the server host for the public IP.");
footer = "Share the Public URL with remote players, or use the LAN URL on your local network";
}
else if (boundIp == "127.0.0.1")
{
// Localhost only
urlLines.Add($"Local: {MakeUrl("127.0.0.1")}");
mailLines.Add($" Local: {MakeUrl("127.0.0.1")}");
footer = "Server bound to localhost — only accessible from this machine";
}
else
{
// Specific IP (VPN, etc.)
urlLines.Add($"Connect: {MakeUrl(boundIp)}");
mailLines.Add($" Connect: {MakeUrl(boundIp)}");
footer = "Share this URL with players connected to the same network";
}
PrintStartupBanner(urlLines, publicIp, footer);
ServerUrls = string.Join("\n", mailLines);
// Watchdog WebSocket endpoint is auto-registered by SPT DI
logger.Info("[ZSlayerHQ] Watchdog WebSocket endpoint active at /ws/watchdog");
var token = config.Watchdog.WatchdogToken;
var maskedToken = token.Length > 5 ? token[..^5] + "*****" : new string('*', token.Length);
logger.Info($"[ZSlayerHQ] Watchdog auth token: {maskedToken}");
return Task.CompletedTask;
}
private void PrintStartupBanner(List<string> urlLines, string? publicIp, string footer)
{
const string cyan = "\x1b[96m";
const string gold = "\x1b[93m";
const string dim = "\x1b[90m";
const string reset = "\x1b[0m";
const string green = "\x1b[92m";
const string white = "\x1b[97m";
var title = $"⚡ ZSlayerHQ Command Center v{ModMetadata.StaticVersion} ⚡";
var subtitle = "Open in your browser to manage your server:";
var quote = $"\"{StartupQuotes[Random.Shared.Next(StartupQuotes.Length)]}\"";
// Headless info line — Watchdog manages process lifecycle now
string? headlessLine = "Headless managed via Watchdog — connect Watchdog app to enable controls";
string? noPublicLine = publicIp == null
? "Public IP not detected — Google 'what is my IP' to find it"
: null;
// Pre-compute flea table layout for width calculation and rendering
var fd = offerRegenerationService.StartupDisplay;
var fleaTableWidthLines = new List<string>();
var fleaDataRows = new List<(string LP, string LC, string RP, string RC)>();
var fleaSepPos = 0;
string fleaHdrRP = "", fleaHdrRC = "";
if (fd is { HasModifiers: true })
{
// Buy price: single combined line with category breakdown if applicable
string buyVP, buyVC;
if (fd.ExampleMultSource == "global×category" && fd.ExampleBasePrice > 0)
{
var catMult = fd.ExampleEffectiveMult / fd.BuyMultiplier;
buyVP = $"{fd.ExampleEffectiveMult:F2}x ({fd.BuyMultiplier:F2}x global × {catMult:F2}x category)";
buyVC = $"{green}{fd.ExampleEffectiveMult:F2}x {dim}({white}{fd.BuyMultiplier:F2}x {dim}global × {white}{catMult:F2}x {dim}category){reset}";
}
else if (fd.ExampleMultSource == "item" && fd.ExampleBasePrice > 0)
{
buyVP = $"{fd.ExampleEffectiveMult:F2}x (per-item override)";
buyVC = $"{green}{fd.ExampleEffectiveMult:F2}x {dim}(per-item override){reset}";
}
else
{
buyVP = $"{fd.BuyMultiplier:F2}x";
buyVC = $"{cyan}{fd.BuyMultiplier:F2}x{reset}";
}
// Right-side price/tax examples with aligned arrows
string prRP = "", prRC = "", txRP = "", txRC = "";
if (fd.ExampleBasePrice > 0)
{
var bpStr = $"{fd.ExampleBasePrice:N0}";
var btStr = fd.ExampleBaseTax > 0 ? $"{fd.ExampleBaseTax:N0}" : "";
var maxBW = Math.Max(bpStr.Length, btStr.Length);
prRP = $"₽ {bpStr.PadRight(maxBW)} → ₽ {fd.ExampleModifiedPrice:N0}";
prRC = $"{white}₽ {bpStr.PadRight(maxBW)} {dim}→ {cyan}₽ {fd.ExampleModifiedPrice:N0}{reset}";
if (fd.ExampleBaseTax > 0)
{
txRP = $"₽ {btStr.PadRight(maxBW)} → ₽ {fd.ExampleModifiedTax:N0}";
txRC = $"{white}₽ {btStr.PadRight(maxBW)} {dim}→ {cyan}₽ {fd.ExampleModifiedTax:N0}{reset}";
}
}
// Example header
if (fd.ExampleName.Length > 0)
{
fleaHdrRP = $"Example: {fd.ExampleName}";
fleaHdrRC = $"{cyan}Example: {fd.ExampleName}{reset}";
}
// Row data: label, value plain/colored, right plain/colored
var rows = new (string Lbl, string VP, string VC, string RP, string RC)[]
{
("Buy Price:", buyVP, buyVC, prRP, prRC),
("Tax Multiplier:", $"{fd.TaxMultiplier:F2}x", $"{cyan}{fd.TaxMultiplier:F2}x{reset}", txRP, txRC),
("Max Player Offers:", $"{fd.MaxOffers}", $"{cyan}{fd.MaxOffers}{reset}", "", ""),
("Listing Duration:", $"{fd.DurationHours}h", $"{cyan}{fd.DurationHours}h{reset}", "", ""),
("Barter Offers:", $"{fd.BarterPercent}%", $"{cyan}{fd.BarterPercent}%{reset}", "", ""),
("Prices Modified:", $"{fd.ModifiedPrices:N0}/{fd.TotalPrices:N0}", $"{cyan}{fd.ModifiedPrices:N0}/{fd.TotalPrices:N0}{reset}", "", ""),
};
const int lblW = 19; // "Max Player Offers:" is longest
var maxVLen = rows.Max(r => r.VP.Length);
fleaSepPos = 3 + lblW + 1 + maxVLen + 2;
foreach (var (lbl, vp, vc, rp, rc) in rows)
{
var lp = $" {lbl.PadRight(lblW)} {vp}";
var lc = $" {white}{lbl.PadRight(lblW)}{reset} {vc}";
fleaDataRows.Add((lp, lc, rp, rc));
}
// Plain text lines for box width calculation
var hdr = " Changes".PadRight(fleaSepPos) + "│" + (fleaHdrRP.Length > 0 ? $" {fleaHdrRP} " : " ");
fleaTableWidthLines.Add(hdr);
foreach (var (lp, _, rp, _) in fleaDataRows)
{
var line = lp.PadRight(fleaSepPos) + "│" + (rp.Length > 0 ? $" {rp} " : " ");
fleaTableWidthLines.Add(line);
}
}
// Determine box inner width from ALL content lines
var allContent = new List<string> { title, subtitle, footer, quote };
allContent.AddRange(urlLines);
if (headlessLine != null) allContent.Add(headlessLine);
if (noPublicLine != null) allContent.Add(noPublicLine);
allContent.AddRange(fleaTableWidthLines);
var innerWidth = allContent.Max(s => s.Length) + 6;
string Center(string text) =>
text.PadLeft((innerWidth + text.Length) / 2).PadRight(innerWidth);
string LeftAlign(string text) =>
(" " + text).PadRight(innerWidth);
var bar = new string('═', innerWidth);
// Print box
logger.Info($"{gold}╔{bar}╗{reset}");
// ⚡ emojis render as 2 columns each in console but count as 1 in string.Length — subtract 2
var titlePad = Center(title);
titlePad = titlePad.Length >= 2 ? titlePad[..^2] : titlePad;
logger.Info($"{gold}║{reset}{cyan}{titlePad}{reset}{gold}║{reset}");
logger.Info($"{gold}╠{bar}╣{reset}");
logger.Info($"{gold}║{reset}{Center("")}{gold}║{reset}");
logger.Info($"{gold}║{reset}{Center(subtitle)}{gold}║{reset}");
logger.Info($"{gold}║{reset}{Center("")}{gold}║{reset}");
var maxUrlLen = urlLines.Max(u => u.Length);
var urlLeftPad = (innerWidth - maxUrlLen) / 2;
foreach (var url in urlLines)
{
var urlRightPad = innerWidth - urlLeftPad - url.Length;
logger.Info($"{gold}║{reset}{cyan}{new string(' ', urlLeftPad)}{url}{new string(' ', Math.Max(0, urlRightPad))}{reset}{gold}║{reset}");
}
logger.Info($"{gold}║{reset}{Center("")}{gold}║{reset}");
if (noPublicLine != null)
{
logger.Info($"{gold}║{reset}\x1b[33m{Center(noPublicLine)}{reset}{gold}║{reset}");
logger.Info($"{gold}║{reset}{Center("")}{gold}║{reset}");
}
logger.Info($"{gold}║{reset}{Center(footer)}{gold}║{reset}");
logger.Info($"{gold}║{reset}{Center("")}{gold}║{reset}");
if (headlessLine != null)
{
var centered = Center(headlessLine);
logger.Info($"{gold}║{reset}{cyan}{centered}{reset}{gold}║{reset}");
logger.Info($"{gold}║{reset}{Center("")}{gold}║{reset}");
}
logger.Info($"{gold}╠{bar}╣{reset}");
logger.Info($"{gold}║{reset}{dim}{Center(quote)}{reset}{gold}║{reset}");
// ── Helpers ──
void BlankLine() =>
logger.Info($"{gold}║{reset}{new string(' ', innerWidth)}{gold}║{reset}");
void StatusLine(string plainText, string coloredText)
{
var pad = Math.Max(0, innerWidth - 3 - plainText.Length);
logger.Info($"{gold}║{reset} {coloredText}{new string(' ', pad)}{reset}{gold}║{reset}");
}
// ═══ Config status line ═══
logger.Info($"{gold}╠{bar}╣{reset}");
BlankLine();
var plainParts = new List<string> { "Config loaded", "Watchdog WebSocket ready" };
var colorParts = new List<string> { $"{green}Config {white}loaded", $"{green}Watchdog WebSocket {white}ready" };
var configPlain = string.Join(" | ", plainParts);
var configColored = string.Join($" {dim}│{reset} ", colorParts);
var configLeftPad = (innerWidth - configPlain.Length) / 2;
var configRightPad = innerWidth - configPlain.Length - configLeftPad;
logger.Info($"{gold}║{reset}{new string(' ', configLeftPad)}{configColored}{new string(' ', configRightPad)}{reset}{gold}║{reset}");
BlankLine();
// ═══ Flea Modifiers Section ═══
if (fd is { HasModifiers: true })
{
logger.Info($"{gold}╠{bar}╣{reset}");
BlankLine();
// Title + underline (centered)
var fleaTitle = "Flea Modifiers Applied";
var ftlp = (innerWidth - fleaTitle.Length) / 2;
var ftrp = innerWidth - fleaTitle.Length - ftlp;
logger.Info($"{gold}║{reset}{new string(' ', ftlp)}{green}{fleaTitle}{reset}{new string(' ', ftrp)}{gold}║{reset}");
logger.Info($"{gold}║{reset}{new string(' ', ftlp)}{dim}{new string('─', fleaTitle.Length)}{reset}{new string(' ', ftrp)}{gold}║{reset}");
BlankLine();
// Table header
var hLeftPad = Math.Max(0, fleaSepPos - " Changes".Length);
var hRP = fleaHdrRP.Length > 0 ? " " + fleaHdrRP : "";
var hRC = fleaHdrRC.Length > 0 ? " " + fleaHdrRC : "";
var hRightPad = Math.Max(0, innerWidth - fleaSepPos - 1 - hRP.Length);
logger.Info($"{gold}║{reset} {cyan}Changes{reset}{new string(' ', hLeftPad)}{dim}│{reset}{hRC}{new string(' ', hRightPad)}{gold}║{reset}");
// Separator
var rightDash = Math.Max(0, innerWidth - fleaSepPos - 1);
logger.Info($"{gold}║{reset}{dim}{new string('─', fleaSepPos)}┼{new string('─', rightDash)}{reset}{gold}║{reset}");
// Data rows
foreach (var (lp, lc, rp, rc) in fleaDataRows)
{
var leftPad = Math.Max(0, fleaSepPos - lp.Length);
var rPlain = rp.Length > 0 ? " " + rp : "";
var rColor = rc.Length > 0 ? " " + rc : "";
var rightPad = Math.Max(0, innerWidth - fleaSepPos - 1 - rPlain.Length);
logger.Info($"{gold}║{reset}{lc}{new string(' ', leftPad)}{dim}│{reset}{rColor}{new string(' ', rightPad)}{gold}║{reset}");
}
BlankLine();
}
else if (fd != null)
{
logger.Info($"{gold}╠{bar}╣{reset}");
BlankLine();
StatusLine("Flea: Default settings (no modifiers)", $"{dim}Flea: Default settings (no modifiers)");
BlankLine();
}
logger.Info($"{gold}╚{bar}╝{reset}");
}
private static bool IsPrivateIp(string ip)
{
if (!IPAddress.TryParse(ip, out var addr)) return false;
var bytes = addr.GetAddressBytes();
if (bytes.Length != 4) return false;
return bytes[0] == 10
|| (bytes[0] == 172 && bytes[1] >= 16 && bytes[1] <= 31)
|| (bytes[0] == 192 && bytes[1] == 168);
}
private static string? GetLanIp()
{
try
{
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Connect("8.8.8.8", 80);
return (socket.LocalEndPoint as IPEndPoint)?.Address.ToString();
}
catch { return null; }
}
private static string? GetPublicIp()
{
try
{
using var client = new HttpClient { Timeout = TimeSpan.FromSeconds(3) };
using var request = new HttpRequestMessage(HttpMethod.Get, "https://api.ipify.org");
using var response = client.Send(request);
using var reader = new StreamReader(response.Content.ReadAsStream());
return reader.ReadToEnd().Trim();
}
catch { return null; }
}
}