final fix

This commit is contained in:
erik 2025-06-09 02:15:27 +02:00
parent 57b2f0400e
commit 1142a012ef
2 changed files with 31 additions and 16 deletions

View file

@ -36,6 +36,11 @@ namespace MosswartMassacre
private const int IntervalSec = 5;
private static string SessionId = "";
// ─── cached prismatic taper count ───────────
private static int _cachedPrismaticTaperCount = 0;
private static DateTime _lastPrismaticTaperUpdate = DateTime.MinValue;
private static readonly TimeSpan PrismaticTaperCacheInterval = TimeSpan.FromMinutes(10);
// ─── runtime state ──────────────────────────
private static ClientWebSocket _ws;
private static CancellationTokenSource _cts;
@ -164,16 +169,12 @@ namespace MosswartMassacre
{
try
{
PluginCore.WriteToChat("[WebSocket] Building telemetry payload...");
var json = BuildPayloadJson();
PluginCore.WriteToChat($"[WebSocket] Payload built ({json.Length} chars), sending...");
await SendEncodedAsync(json, _cts.Token);
PluginCore.WriteToChat("[WebSocket] Telemetry sent successfully");
}
catch (Exception ex)
{
PluginCore.WriteToChat($"[WebSocket] Telemetry failed: {ex.Message}");
PluginCore.WriteToChat($"[WebSocket] Stack trace: {ex.StackTrace}");
break; // Exit telemetry loop on failure
}
@ -293,16 +294,8 @@ namespace MosswartMassacre
await _sendLock.WaitAsync(token);
try
{
if (_ws == null)
{
PluginCore.WriteToChat("[WebSocket] Send failed - WebSocket is null");
if (_ws == null || _ws.State != WebSocketState.Open)
return;
}
if (_ws.State != WebSocketState.Open)
{
PluginCore.WriteToChat($"[WebSocket] Send failed - State: {_ws.State}");
return;
}
var bytes = Encoding.UTF8.GetBytes(text);
await _ws.SendAsync(new ArraySegment<byte>(bytes),
@ -312,7 +305,7 @@ namespace MosswartMassacre
}
catch (Exception ex)
{
PluginCore.WriteToChat($"[WebSocket] Send error: {ex.Message} (State: {_ws?.State})");
PluginCore.WriteToChat($"[WebSocket] Send error: {ex.Message}");
_ws?.Abort();
_ws?.Dispose();
_ws = null;
@ -325,7 +318,29 @@ namespace MosswartMassacre
// ─── payload builder ──────────────────────────────
private static void UpdatePrismaticTaperCache()
{
try
{
_cachedPrismaticTaperCount = Utils.GetItemStackSize("Prismatic Taper");
_lastPrismaticTaperUpdate = DateTime.Now;
PluginCore.WriteToChat($"[WebSocket] Updated prismatic taper cache: {_cachedPrismaticTaperCount}");
}
catch (Exception ex)
{
PluginCore.WriteToChat($"[WebSocket] Failed to update prismatic taper cache: {ex.Message}");
}
}
private static int GetCachedPrismaticTaperCount()
{
// Update cache if it's stale (older than 2 minutes)
if (DateTime.Now - _lastPrismaticTaperUpdate > PrismaticTaperCacheInterval)
{
UpdatePrismaticTaperCache();
}
return _cachedPrismaticTaperCount;
}
private static string BuildPayloadJson()
{
@ -346,7 +361,7 @@ namespace MosswartMassacre
onlinetime = (DateTime.Now - PluginCore.statsStartTime).ToString(@"dd\.hh\:mm\:ss"),
deaths = PluginCore.sessionDeaths.ToString(),
total_deaths = PluginCore.totalDeaths.ToString(),
prismatic_taper_count = Utils.GetItemStackSize("Prismatic Taper").ToString(),
prismatic_taper_count = GetCachedPrismaticTaperCount().ToString(),
vt_state = VtankControl.VtGetMetaState(),
mem_mb = tele.MemoryBytes,
cpu_pct = tele.GetCpuUsage(),