RynthChat: server default tab planning and Global room discovery
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
626cc9dfc3
commit
620e6b1507
3 changed files with 179 additions and 0 deletions
96
src/RynthCore.Engine/UI/Data/ChatServerTabs.cs
Normal file
96
src/RynthCore.Engine/UI/Data/ChatServerTabs.cs
Normal file
|
|
@ -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" = "<server>/<entry id>"), 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
|
||||
{
|
||||
/// <summary>What Plan needs to know about the current settings.</summary>
|
||||
internal sealed record SeedState(IReadOnlyCollection<string> Seeded, IReadOnlyCollection<string> ExistingTabs, IReadOnlyList<RynthChatFilterDto> OwnedRules);
|
||||
|
||||
/// <summary>What to do: create/pin the tab, mark it seeded, and the full replacement set of this owner's rules.</summary>
|
||||
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);
|
||||
|
||||
/// <summary>The owner key of an entry: "server/id".</summary>
|
||||
internal static string OwnerKey(SectionChatTabDecl d) => d.Server + "/" + d.Id;
|
||||
|
||||
/// <summary>The Global room a remote speaker's line names (canonical capitalisation), or null.</summary>
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>The "startswith" pattern a Global-room rule uses for <paramref name="room"/>.</summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue