From f7a951dc9d32538dec9533d686ebe7beea05fa13 Mon Sep 17 00:00:00 2001 From: schwarper Date: Thu, 29 Aug 2024 06:25:25 +0300 Subject: [PATCH] Player clan update --- cs2-tags/src/config/config.cs | 11 ++++- cs2-tags/src/cs2-tag.cs | 76 ++++++++++++++++++++++------------- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/cs2-tags/src/config/config.cs b/cs2-tags/src/config/config.cs index eae74b7..d850f7c 100644 --- a/cs2-tags/src/config/config.cs +++ b/cs2-tags/src/config/config.cs @@ -23,17 +23,24 @@ public static void Load() public static void Reload() { - Config.Settings = new(); - Config.DefaultTags = new(); Config.Tags.Clear(); PlayerDataList.Clear(); Load(); + LoadPlayersTag(); + } + public static void LoadPlayersTag() + { List players = Utilities.GetPlayers(); foreach (CCSPlayerController player in players) { + if (player.IsBot) + { + continue; + } + PlayerDataList.Add(player.SteamID, new PlayerData { PlayerTag = GetTag(player), diff --git a/cs2-tags/src/cs2-tag.cs b/cs2-tags/src/cs2-tag.cs index c49e422..4d3c777 100644 --- a/cs2-tags/src/cs2-tag.cs +++ b/cs2-tags/src/cs2-tag.cs @@ -47,7 +47,7 @@ public HookResult OnPlayerConnect(EventPlayerConnectFull @event, GameEventInfo i { CCSPlayerController? player = @event.Userid; - if (player == null) + if (player == null || player.IsBot) { return HookResult.Continue; } @@ -66,7 +66,7 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo { CCSPlayerController? player = @event.Userid; - if (player == null) + if (player == null || player.IsBot) { return HookResult.Continue; } @@ -75,7 +75,7 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo return HookResult.Continue; } - public HookResult OnMessage(UserMessage um) + public static HookResult OnMessage(UserMessage um) { int entityIndex = um.ReadInt("entityindex"); @@ -100,6 +100,13 @@ public HookResult OnMessage(UserMessage um) string playername = um.ReadString("param1"); string message = um.ReadString("param2"); + string cleanedMessage = RemoveCurlyBraceContent(message); + + if (string.IsNullOrWhiteSpace(cleanedMessage)) + { + return HookResult.Handled; + } + bool isTeamMessage = !msgT.Contains("All"); string deadname = player.PawnIsAlive ? string.Empty : Config.Settings.DeadName; @@ -108,34 +115,39 @@ public HookResult OnMessage(UserMessage um) string namecolor = playerData.PlayerTag.NameColor; string chatcolor = playerData.PlayerTag.ChatColor; - string formattedMessage = FormatMessage(deadname, teamname, tag, namecolor, chatcolor, playername, message, player.Team); + string formattedMessage = FormatMessage(deadname, teamname, tag, namecolor, chatcolor, playername, cleanedMessage, player.Team); um.SetString("messagename", formattedMessage); return HookResult.Changed; + } - static string ReplaceTags(string message, CsTeam team) - { - string modifiedValue = StringExtensions.ReplaceColorTags(message) - .Replace("{TeamColor}", ChatColors.ForTeam(team).ToString()); + private static string RemoveCurlyBraceContent(string message) + { + return System.Text.RegularExpressions.Regex.Replace(message, @"\{.*?\}", string.Empty); + } - return modifiedValue; - } + static string ReplaceTags(string message, CsTeam team) + { + string modifiedValue = StringExtensions.ReplaceColorTags(message) + .Replace("{TeamColor}", ChatColors.ForTeam(team).ToString()); - static string TeamName(CsTeam team) - { - return team switch - { - CsTeam.Spectator => ReplaceTags(Config.Settings.SpecName, CsTeam.Spectator), - CsTeam.Terrorist => ReplaceTags(Config.Settings.TName, CsTeam.Terrorist), - CsTeam.CounterTerrorist => ReplaceTags(Config.Settings.CTName, CsTeam.CounterTerrorist), - CsTeam.None => ReplaceTags(Config.Settings.NoneName, CsTeam.None), - _ => ReplaceTags(Config.Settings.NoneName, CsTeam.None) - }; - } + return modifiedValue; + } - static string FormatMessage(string deadIcon, string teamname, string tag, string namecolor, string chatcolor, string playername, string message, CsTeam team) - { - return ReplaceTags($" {deadIcon}{teamname}{tag}{namecolor}{playername}{ChatColors.Default}: {chatcolor}{message}", team); - } + static string TeamName(CsTeam team) + { + return team switch + { + CsTeam.Spectator => ReplaceTags(Config.Settings.SpecName, CsTeam.Spectator), + CsTeam.Terrorist => ReplaceTags(Config.Settings.TName, CsTeam.Terrorist), + CsTeam.CounterTerrorist => ReplaceTags(Config.Settings.CTName, CsTeam.CounterTerrorist), + CsTeam.None => ReplaceTags(Config.Settings.NoneName, CsTeam.None), + _ => ReplaceTags(Config.Settings.NoneName, CsTeam.None) + }; + } + + static string FormatMessage(string deadIcon, string teamname, string tag, string namecolor, string chatcolor, string playername, string message, CsTeam team) + { + return ReplaceTags($" {deadIcon}{teamname}{tag}{namecolor}{playername}{ChatColors.Default}: {chatcolor}{message}", team); } [ConsoleCommand("css_tags_reload")] @@ -179,15 +191,25 @@ public static void UpdateTags() { foreach (var kvp in PlayerDataList) { - CCSPlayerController player = Utilities.GetPlayerFromSteamId(kvp.Key)!; + CCSPlayerController? player = Utilities.GetPlayerFromSteamId(kvp.Key); + + if (player == null) + { + continue; + } + var scoretag = kvp.Value.PlayerTag.ScoreTag; - if (string.IsNullOrEmpty(scoretag)) + if (string.IsNullOrEmpty(scoretag) || player.Clan == scoretag) { continue; } player.Clan = scoretag; + Utilities.SetStateChanged(player, "CCSPlayerController", "m_szClan"); + + var fakeEvent = new EventNextlevelChanged(false); + fakeEvent.FireEventToClient(player); } } }