Skip to content

Commit

Permalink
Player clan update
Browse files Browse the repository at this point in the history
  • Loading branch information
schwarper committed Aug 29, 2024
1 parent 075edf0 commit f7a951d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
11 changes: 9 additions & 2 deletions cs2-tags/src/config/config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CCSPlayerController> players = Utilities.GetPlayers();

foreach (CCSPlayerController player in players)
{
if (player.IsBot)
{
continue;
}

PlayerDataList.Add(player.SteamID, new PlayerData
{
PlayerTag = GetTag(player),
Expand Down
76 changes: 49 additions & 27 deletions cs2-tags/src/cs2-tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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");

Expand All @@ -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;
Expand All @@ -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")]
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit f7a951d

Please sign in to comment.