From 620e6b15072c280d60e72d4535b3456219336df5 Mon Sep 17 00:00:00 2001 From: Daniel Wallace Date: Sun, 15 Nov 2026 08:25:02 -0800 Subject: [PATCH] RynthChat: server default tab planning and Global room discovery Co-Authored-By: Claude Opus 5.5 --- .../UI/Data/ChatServerTabs.cs | 96 +++++++++++++++++++ tools/ChatFilterTests/ChatFilterTests.csproj | 3 + tools/ChatFilterTests/Program.cs | 80 ++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 src/RynthCore.Engine/UI/Data/ChatServerTabs.cs diff --git a/src/RynthCore.Engine/UI/Data/ChatServerTabs.cs b/src/RynthCore.Engine/UI/Data/ChatServerTabs.cs new file mode 100644 index 0000000..b3b02b1 --- /dev/null +++ b/src/RynthCore.Engine/UI/Data/ChatServerTabs.cs @@ -0,0 +1,96 @@ +// ============================================================================ +// RynthCore.Engine - UI/Data/ChatServerTabs.cs +// Server default chat tabs (2026-11-15): what a server plugin's "chatTabs" +// manifest entry turns into in RynthChat's settings. Pure decisions only (no +// ChatModel, ImGui or SectionRegistry): ChatModel.ApplyServerTabs gathers the +// state, calls Plan, and applies the result; tools/ChatFilterTests compiles +// this file as is. +// +// An entry's rules are owned by it ("owner" = "/"), so a +// refresh rewrites only those rules and a renamed tab keeps them. A tab is +// seeded once (seededTabs); a tab the player deleted stays deleted, because +// deleting disables its rules and the seed mark remains. An older engine that +// rewrites the shared settings file strips the new fields: the seed then +// recognises its own rules by owner, or an existing tab of the same name, and +// adopts it instead of creating a second one. +// ============================================================================ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; +using RynthCore.Engine.UI.Sections; + +namespace RynthCore.Engine.UI.Data; + +internal static class ChatServerTabs +{ + /// What Plan needs to know about the current settings. + internal sealed record SeedState(IReadOnlyCollection Seeded, IReadOnlyCollection ExistingTabs, IReadOnlyList OwnedRules); + + /// What to do: create/pin the tab, mark it seeded, and the full replacement set of this owner's rules. + internal sealed record SeedPlan(bool CreateTab, string TabName, bool Pin, bool MarkSeeded, RynthChatFilterDto[] Rules); + + // A remote speaker's room line: [Trade] Ann@coldeve says, "..." (optionally Ann@coldeve (Discord)). + // Anchored at the line start and the speaker may not contain a quote, so a tell or a say that merely + // quotes such a line does not match. + private static readonly Regex RoomLine = new( + "^\\[(General|Trade|LFG|Roleplay)\\] [^\"]+@[a-z0-9-]+( \\(discord\\))? says, \"", + RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.NonBacktracking); + + /// The owner key of an entry: "server/id". + internal static string OwnerKey(SectionChatTabDecl d) => d.Server + "/" + d.Id; + + /// The Global room a remote speaker's line names (canonical capitalisation), or null. + internal static string? DiscoverRoom(string text) + { + if (string.IsNullOrEmpty(text)) return null; + Match m = RoomLine.Match(text); + if (!m.Success) return null; + return m.Groups[1].Value.ToLowerInvariant() switch + { + "general" => "General", + "trade" => "Trade", + "lfg" => "LFG", + "roleplay" => "Roleplay", + _ => null, + }; + } + + /// The "startswith" pattern a Global-room rule uses for . + internal static string GlobalPattern(string room) => "[" + room + "] "; + + internal static SeedPlan Plan(SectionChatTabDecl d, SeedState s, string? discoveredRoom) + { + string key = OwnerKey(d); + bool seeded = s.Seeded.Contains(key, StringComparer.OrdinalIgnoreCase); + bool hasOwned = s.OwnedRules.Count > 0; + bool tabNamed = s.ExistingTabs.Contains(d.Name, StringComparer.OrdinalIgnoreCase); + + // Follow a rename: the owned rules carry the tab's current name. + string tabName = d.Name; + foreach (RynthChatFilterDto owned in s.OwnedRules) + if (!string.IsNullOrEmpty(owned.Tab)) { tabName = owned.Tab!; break; } + + bool create = !seeded && !hasOwned && !tabNamed; + string room = discoveredRoom ?? d.GlobalRoomFallback; + var rules = new RynthChatFilterDto[d.Rules.Length]; + for (int i = 0; i < rules.Length; i++) + { + SectionChatRuleDecl r = d.Rules[i]; + rules[i] = new RynthChatFilterDto + { + Owner = key, + Server = d.Server, + Tab = tabName, + Action = r.Action, + When = r.When, + Mode = r.GlobalRoom ? "startswith" : r.Mode, + Pattern = r.GlobalRoom ? GlobalPattern(room) : r.Pattern, + GlobalRoom = r.GlobalRoom ? true : null, + Enabled = i < s.OwnedRules.Count ? s.OwnedRules[i].Enabled : true, + }; + } + return new SeedPlan(create, tabName, create && d.Pinned, !seeded, rules); + } +} diff --git a/tools/ChatFilterTests/ChatFilterTests.csproj b/tools/ChatFilterTests/ChatFilterTests.csproj index e870212..9b050bb 100644 --- a/tools/ChatFilterTests/ChatFilterTests.csproj +++ b/tools/ChatFilterTests/ChatFilterTests.csproj @@ -36,6 +36,9 @@ + + + diff --git a/tools/ChatFilterTests/Program.cs b/tools/ChatFilterTests/Program.cs index 12407ea..b1fb8ae 100644 --- a/tools/ChatFilterTests/Program.cs +++ b/tools/ChatFilterTests/Program.cs @@ -1,6 +1,7 @@ using System.Text.Json; using System.Text.RegularExpressions; using RynthCore.Engine.UI.Data; +using RynthCore.Engine.UI.Sections; namespace ChatFilterTests; @@ -54,6 +55,8 @@ internal static class Program RetailSetting(); TabsAcrossSetting(); ServerTabSettingsRoundTrip(); + ServerTabDiscovery(); + ServerTabPlans(); SharedFileReload(); LineColorTags(); LineColorKinds(); @@ -1143,6 +1146,83 @@ internal static class Program Check(old.Filters!.All(f => f.Server == null && f.Owner == null && f.GlobalRoom == null), "an old file's rules have none of the rule fields"); } + // ── Server default tabs (2026-11-15) ── + + private static SectionChatTabDecl NetworkDecl() => new( + "dreamweave", "dereth-network", "Network", true, "Roleplay", + new[] + { + new SectionChatRuleDecl("copy", "type:3", "", "^[^\"]+@[a-z0-9-]+( \\(discord\\))? tells you, \"", false), + new SectionChatRuleDecl("copy", "type:4", "", "^You tell [^\"]+@[a-z0-9-]+( \\(discord\\))?, \"", false), + new SectionChatRuleDecl("copy", "", "startswith", "", true), + }); + + private static ChatServerTabs.SeedState State(string[]? seeded = null, string[]? tabs = null, RynthChatFilterDto[]? owned = null) + => new(seeded ?? Array.Empty(), tabs ?? new[] { "All", "Chat" }, owned ?? Array.Empty()); + + private static void ServerTabDiscovery() + { + Case("Discover_FindsRemoteSpeaker"); + Check(ChatServerTabs.DiscoverRoom("[Roleplay] Bob@coldeve says, \"hi\"") == "Roleplay", "Roleplay, remote speaker"); + Check(ChatServerTabs.DiscoverRoom("[trade] Ann Smith@leaf-tide (Discord) says, \"x\"") == "Trade", "lower-case tag, Discord speaker, canonical Trade"); + Check(ChatServerTabs.DiscoverRoom("[LFG] Zed@dw says, \"x\"") == "LFG" && ChatServerTabs.DiscoverRoom("[general] Zed@dw says, \"x\"") == "General", "LFG and General"); + Case("Discover_IgnoresLocalSpeaker"); + Check(ChatServerTabs.DiscoverRoom("[Roleplay] Ann says, \"hi\"") == null, "a local speaker"); + Check(ChatServerTabs.DiscoverRoom("[Roleplay] Ann (Discord) says, \"hi\"") == null, "a Discord speaker with no @world"); + Case("Discover_IgnoresQuotedRoomText"); + Check(ChatServerTabs.DiscoverRoom("Bob says, \"[Trade] Ann@coldeve says, \\\"x\\\"\"") == null, "a say quoting a room line"); + Check(ChatServerTabs.DiscoverRoom("Bob@coldeve tells you, \"[Trade] Ann@coldeve says, \\\"x\\\"\"") == null, "a tell carrying a room line"); + Check(ChatServerTabs.DiscoverRoom("") == null && ChatServerTabs.DiscoverRoom("[Sports] Bob@coldeve says, \"x\"") == null, "empty and an unknown room"); + } + + private static void ServerTabPlans() + { + SectionChatTabDecl d = NetworkDecl(); + string key = ChatServerTabs.OwnerKey(d); + Case("Plan_SeedsFresh"); + Check(key == "dreamweave/dereth-network", "owner key"); + ChatServerTabs.SeedPlan p = ChatServerTabs.Plan(d, State(), null); + Check(p.CreateTab && p.TabName == "Network" && p.Pin && p.MarkSeeded, "creates, names, pins and marks the tab"); + Check(p.Rules.Length == 3 && p.Rules.All(r => r.Owner == key && r.Server == "dreamweave" && r.Tab == "Network" && r.Enabled), "3 owned rules for the tab"); + RynthChatFilterDto g = p.Rules[2]; + Check(g.GlobalRoom == true && g.Mode == "startswith" && g.Pattern == "[Roleplay] ", "the Global rule uses the fallback room"); + Check(p.Rules[0].GlobalRoom == null && p.Rules[0].Pattern == d.Rules[0].Pattern && p.Rules[0].When == "type:3", "other rules copy the declaration"); + + Case("Plan_UsesDiscoveredRoom"); + Check(ChatServerTabs.Plan(d, State(), "General").Rules[2].Pattern == "[General] ", "the discovered room wins over the fallback"); + + Case("Plan_AdoptsExistingTabByName"); + p = ChatServerTabs.Plan(d, State(tabs: new[] { "All", "Chat", "Network" }), null); + Check(!p.CreateTab && !p.Pin && p.MarkSeeded && p.TabName == "Network", "an existing Network tab is adopted, not duplicated or re-pinned"); + p = ChatServerTabs.Plan(d, State(tabs: new[] { "network" }), null); + Check(!p.CreateTab, "the name match ignores case"); + var strayRule = new RynthChatFilterDto { Owner = key, Tab = "DN" }; + p = ChatServerTabs.Plan(d, State(owned: new[] { strayRule }), null); + Check(!p.CreateTab && !p.Pin && p.MarkSeeded && p.TabName == "DN", "owned rules without a seed mark (older engine) are adopted too"); + + Case("Refresh_FollowsRenamedTab"); + RynthChatFilterDto[] owned = ChatServerTabs.Plan(d, State(), null).Rules; + foreach (RynthChatFilterDto r in owned) r.Tab = "DN"; + p = ChatServerTabs.Plan(d, State(seeded: new[] { key }, tabs: new[] { "All", "Chat", "DN" }, owned: owned), "Trade"); + Check(p.TabName == "DN" && !p.CreateTab && !p.Pin && !p.MarkSeeded, "seeded: no create, no pin, no new mark"); + Check(p.Rules.All(r => r.Tab == "DN"), "every rule follows the rename"); + Check(p.Rules[2].Pattern == "[Trade] ", "a refresh rewrites the Global pattern"); + + Case("Refresh_KeepsDeletedTabDeleted"); + owned = ChatServerTabs.Plan(d, State(), null).Rules; + foreach (RynthChatFilterDto r in owned) r.Enabled = false; + p = ChatServerTabs.Plan(d, State(seeded: new[] { key }, owned: owned), null); + Check(!p.CreateTab && !p.Pin, "a seeded tab that is gone is not re-created"); + Check(p.Rules.All(r => !r.Enabled), "its rules stay disabled"); + + Case("Plan rules compile"); + foreach (RynthChatFilterDto r in ChatServerTabs.Plan(d, State(), "LFG").Rules) + { + System.Text.RegularExpressions.Regex? rx = ChatRuleMatching.Compile(ChatRuleMatching.Parse(r.Mode), r.Pattern, 5000, out string? err); + Check(err == null && rx != null, $"rule '{r.Pattern}' compiles: {err}"); + } + } + private static void TabsAcrossSetting() { Case("tabs across the top: off (the drawer) unless a file says true");