using axXez.Extensions; using axXez.TS3.Protocol; using System.Collections.Generic; using System.Linq; namespace axXez.TS3.Bot.Modules { [Module(Namespace = "axXez")] internal class ExclusiveGroups : Module { public string[][] MutuallyExclusiveGroups { get; set; } = new string[0][]; private int[][] _resolvedGroups = new int[0][]; protected override void OnStart() { _resolvedGroups = MutuallyExclusiveGroups .Select(set => set .Select(entry => Server.FindServerGroup(entry, out var g) ? g.ID : -1) .Where(id => id != -1) .ToArray()) .ToArray(); foreach (Client c in Clients.Values.ToArray()) CorrectClientRights(c); } public override void OnClientJoined(Client client, Channel source, Channel target, EventReason reason) { CorrectClientRights(client); } public override void OnClientUpdated(Client client, TsEntityChangeInfo changes) { if (!changes.Has(nameof(Client.ServerGroups))) return; Log($"ClientServerGroupChanged: {client.Name} -> {client.ServerGroups}", LogType.Verbose); CorrectClientRights(client); } private void CorrectClientRights(Client client) { if (string.IsNullOrEmpty(client.ServerGroups)) return; List serverGroups = client.ServerGroups.Split(',').Select(int.Parse).ToList(); if (serverGroups.Count <= 1) return; foreach (int[] set in _resolvedGroups) { bool updated = false; int exclusiveGroup = -1; for (int i = set.Length - 1; i >= 0; i--) { if (!serverGroups.Contains(set[i])) continue; if (exclusiveGroup == -1) { exclusiveGroup = set[i]; } else { serverGroups.Remove(set[i]); updated = true; Query.RemoveClientFromServerGroup(set[i], client.DBID); } } if (updated) { var newServerGroups = string.Join(",", serverGroups); Log($"correct client rights for '{client.Name}': {client.ServerGroups} -> {newServerGroups}"); if (serverGroups.Count <= 1) return; } } } [Command("xgroups status", Description = "Shows all configured mutually exclusive group sets")] public CommandResponse Status(CommandContext ctx) { if (MutuallyExclusiveGroups.Length == 0) return ctx.Ok("No mutually exclusive group sets configured."); ctx.Reply("[B]Exclusive Groups[/B]"); for (int i = 0; i < MutuallyExclusiveGroups.Length; i++) { var set = MutuallyExclusiveGroups[i]; var resolved = i < _resolvedGroups.Length ? _resolvedGroups[i] : new int[0]; var entries = set.Select((entry, j) => { var id = j < resolved.Length ? resolved[j] : -1; return id != -1 ? $"[B]{(Server.FindServerGroup(id, out var g) ? g.Name : entry)}[/B] ([COLOR=gray]{id}[/COLOR])" : $"[B]{entry}[/B] [COLOR=red](not found)[/COLOR]"; }); ctx.Reply($" Set {i + 1}: {string.Join(", ", entries)}"); } return ctx.Ok(); } } }