Cleaned up websockets

This commit is contained in:
erik 2025-05-09 23:07:24 +02:00
parent 56b09f509a
commit 33fb228654
2 changed files with 45 additions and 26 deletions

View file

@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Timers;
using Decal.Adapter;
@ -128,31 +130,49 @@ namespace MosswartMassacre
}
private void OnSpawn(object sender, CreateObjectEventArgs e)
private async void OnSpawn(object sender, CreateObjectEventArgs e)
{
// e.New is the wrapped WorldObject that just appeared
var mob = e.New;
if (mob.ObjectClass != ObjectClass.Monster) return;
var coords = mob.Coordinates();
float ns = (float)coords.NorthSouth;
float ew = (float)coords.EastWest;
try
{
var coords = mob.Coordinates();
const string fmt = "F7";
string ns = coords.NorthSouth.ToString(fmt, CultureInfo.InvariantCulture);
string ew = coords.EastWest.ToString(fmt, CultureInfo.InvariantCulture);
WriteToChat($"[Spawn] {mob.Name} @ (NS={ns:F1}, EW={ew:F1})");
// TODO: record (ew, ns) for your heatmap
await WebSocket.SendSpawnAsync(ns, ew, mob.Name);
}
catch (Exception ex)
{
PluginCore.WriteToChat($"[WS] Spawn send failed: {ex}");
}
}
private void OnDespawn(object sender, ReleaseObjectEventArgs e)
{
// e.Released is the wrapped WorldObject that just disappeared
var mob = e.Released;
if (mob.ObjectClass != ObjectClass.Monster) return;
var coords = mob.Coordinates();
float ns = (float)coords.NorthSouth;
float ew = (float)coords.EastWest;
// var c = mob.Coordinates();
// PluginCore.WriteToChat(
// $"[Despawn] {mob.Name} @ (NS={c.NorthSouth:F1}, EW={c.EastWest:F1})");
}
WriteToChat($"[Despawn] {mob.Name} @ (NS={ns:F1}, EW={ew:F1})");
private async void AllChatText(object sender, ChatTextInterceptEventArgs e)
{
try
{
string cleaned = NormalizeChatLine(e.Text);
await WebSocket.SendChatTextAsync(e.Color, cleaned);
}
catch (Exception ex)
{
PluginCore.WriteToChat($"[WS] Chat send failed: {ex}");
}
}
private static string NormalizeChatLine(string raw)
@ -171,20 +191,6 @@ namespace MosswartMassacre
return collapsed;
}
private void AllChatText(object sender, ChatTextInterceptEventArgs e)
{
try
{
var cleaned = NormalizeChatLine(e.Text);
_ = WebSocket.SendChatTextAsync(e.Color, cleaned);
}
catch (Exception ex)
{
PluginCore.WriteToChat("Error sending chat over WS: " + ex.Message);
}
}
private void HandleServerCommand(CommandEnvelope env)
{
// Skicka commands

View file

@ -193,6 +193,19 @@ namespace MosswartMassacre
var json = JsonConvert.SerializeObject(envelope);
await SendEncodedAsync(json, CancellationToken.None);
}
public static async Task SendSpawnAsync(string nsCoord, string ewCoord, string monster)
{
var envelope = new
{
type = "spawn",
mob = monster,
ns = nsCoord,
ew = ewCoord
};
var json = JsonConvert.SerializeObject(envelope);
await SendEncodedAsync(json, CancellationToken.None);
}
// ─── shared send helper with locking ───────────────