Skip to content

Commit

Permalink
Fix scoretag
Browse files Browse the repository at this point in the history
  • Loading branch information
schwarper committed Sep 30, 2024
1 parent 22264fd commit 2743889
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 120 deletions.
2 changes: 2 additions & 0 deletions TagsApi/ITagsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public interface ITagApi
public void ResetPlayerColor(CCSPlayerController player, Tags_Colors color);
public bool GetPlayerChatSound(CCSPlayerController player);
public void SetPlayerChatSound(CCSPlayerController controller, bool value);
public bool GetPlayerToggleTags(CCSPlayerController player);
public void SetPlayerToggleTags(CCSPlayerController player, bool value);
public void ReloadTags();
}
9 changes: 8 additions & 1 deletion cs2-tags/src/api/api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public string GetPlayerTag(CCSPlayerController player, Tags_Tags tag)
{
return player.GetTag(tag);
}

public void SetPlayerTag(CCSPlayerController player, Tags_Tags tag, string newtag)
{
player.SetTag(tag, newtag);
Expand Down Expand Up @@ -47,6 +46,14 @@ public void SetPlayerChatSound(CCSPlayerController player, bool value)
{
player.SetChatSound(value);
}
public bool GetPlayerToggleTags(CCSPlayerController player)
{
return player.GetToggleTags();
}
public void SetPlayerToggleTags(CCSPlayerController player, bool value)
{
player.SetToggleTags(value);
}
public void ReloadTags()
{
Reload();
Expand Down
112 changes: 59 additions & 53 deletions cs2-tags/src/config/config.cs
Original file line number Diff line number Diff line change
@@ -1,74 +1,96 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using System.Reflection;
using Tomlyn;
using Tomlyn.Model;
using static Tags.Tags;
using static Tags.TagsLibrary;
using static TagsApi.Tags;

namespace Tags;

public static class Config_Config
{
public class Settings
{
public string DeadName { get; set; } = string.Empty;
public string NoneName { get; set; } = string.Empty;
public string SpecName { get; set; } = string.Empty;
public string TName { get; set; } = string.Empty;
public string CTName { get; set; } = string.Empty;
}

public class Cfg
{
public Settings Settings { get; set; } = new();
public Tag DefaultTags { get; set; } = new();
public Dictionary<string, Tag> Tags { get; set; } = [];
}

public static Cfg Config { get; set; } = new Cfg();
private static string? ConfigPath;

public static void Load()
{
string assemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? "";
string cfgPath = $"{Server.GameDirectory}/csgo/addons/counterstrikesharp/configs/plugins/{assemblyName}";
if (ConfigPath == null)
{
string assemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? string.Empty;

ConfigPath = Path.Combine(Server.GameDirectory,
"csgo",
"addons",
"counterstrikesharp",
"configs",
"plugins",
assemblyName,
"config.toml"
);

if (!File.Exists(ConfigPath))
{
throw new FileNotFoundException($"Configuration file not found: {ConfigPath}");
}
}

LoadConfig($"{cfgPath}/config.toml");
LoadConfig(ConfigPath);
}

public static void Reload()
{
Config.Tags.Clear();
PlayerDataList.Clear();
PlayerTagsList.Clear();

Load();
LoadPlayersTag();
}

public static void LoadPlayersTag()
{
const string playerdesignername = "cs_player_controller";
var players = GetPlayers();

for (int i = 0; i < Server.MaxPlayers; i++)
foreach (var player in players)
{
CCSPlayerController? player = Utilities.GetEntityFromIndex<CCSPlayerController>(i + 1);

if (player?.DesignerName != playerdesignername || player.IsBot)
{
continue;
}

player.LoadTag();
}
}

private static void LoadConfig(string configPath)
{
if (!File.Exists(configPath))
{
throw new FileNotFoundException($"Configuration file not found: {configPath}");
}

string configText = File.ReadAllText(configPath);
TomlTable model = Toml.ToModel(configText);

TomlTable settingsTable = (TomlTable)model["Settings"];
Config.Settings.DeadName = settingsTable["DeadName"].ToString()!;
Config.Settings.NoneName = settingsTable["NoneName"].ToString()!;
Config.Settings.SpecName = settingsTable["SpecName"].ToString()!;
Config.Settings.TName = settingsTable["TName"].ToString()!;
Config.Settings.CTName = settingsTable["CTName"].ToString()!;
TomlTable table = (TomlTable)model["Settings"];
Config.Settings.DeadName = table["DeadName"].ToString()!;
Config.Settings.NoneName = table["NoneName"].ToString()!;
Config.Settings.SpecName = table["SpecName"].ToString()!;
Config.Settings.TName = table["TName"].ToString()!;
Config.Settings.CTName = table["CTName"].ToString()!;

TomlTable defaultTable = (TomlTable)model["Default"];
Config.DefaultTags.ScoreTag = defaultTable["ScoreTag"].ToString()!;
Config.DefaultTags.ChatTag = defaultTable["ChatTag"].ToString()!;
Config.DefaultTags.ChatColor = defaultTable["ChatColor"].ToString()!;
Config.DefaultTags.NameColor = defaultTable["NameColor"].ToString()!;
Config.DefaultTags.ChatSound = bool.Parse(defaultTable["ChatSound"].ToString()!);
table = (TomlTable)model["Default"];
Config.DefaultTags.ScoreTag = table["ScoreTag"].ToString()!;
Config.DefaultTags.ChatTag = table["ChatTag"].ToString()!;
Config.DefaultTags.ChatColor = table["ChatColor"].ToString()!;
Config.DefaultTags.NameColor = table["NameColor"].ToString()!;
Config.DefaultTags.ChatSound = bool.Parse(table["ChatSound"].ToString()!);

foreach (KeyValuePair<string, object> tags in model)
{
Expand All @@ -79,33 +101,17 @@ private static void LoadConfig(string configPath)
continue;
}

TomlTable value = (TomlTable)tags.Value;
table = (TomlTable)tags.Value;

Tag configTag = new();

value.SetIfPresent("ScoreTag", (string stag) => configTag.ScoreTag = stag);
value.SetIfPresent("ChatTag", (string ctag) => configTag.ChatTag = ctag);
value.SetIfPresent("ChatColor", (string ccolor) => configTag.ChatColor = ccolor);
value.SetIfPresent("NameColor", (string ncolor) => configTag.NameColor = ncolor);
value.SetIfPresent("ChatSound", (bool csound) => configTag.ChatSound = csound);
table.SetIfPresent("ScoreTag", (string stag) => configTag.ScoreTag = stag);
table.SetIfPresent("ChatTag", (string ctag) => configTag.ChatTag = ctag);
table.SetIfPresent("ChatColor", (string ccolor) => configTag.ChatColor = ccolor);
table.SetIfPresent("NameColor", (string ncolor) => configTag.NameColor = ncolor);
table.SetIfPresent("ChatSound", (bool csound) => configTag.ChatSound = csound);

Config.Tags.Add(key, configTag);
}
}

public class Settings
{
public string DeadName { get; set; } = string.Empty;
public string NoneName { get; set; } = string.Empty;
public string SpecName { get; set; } = string.Empty;
public string TName { get; set; } = string.Empty;
public string CTName { get; set; } = string.Empty;
}

public class Cfg
{
public Settings Settings { get; set; } = new();
public Tag DefaultTags { get; set; } = new();
public Dictionary<string, Tag> Tags { get; set; } = [];
}
}
78 changes: 39 additions & 39 deletions cs2-tags/src/cs2-tag.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Core.Capabilities;
Expand All @@ -9,23 +9,19 @@
using System.Collections.Concurrent;
using TagsApi;
using static Tags.Config_Config;
using static Tags.TagsLibrary;
using static TagsApi.Tags;

namespace Tags;

public partial class Tags : BasePlugin
{
public override string ModuleName => "Tags";
public override string ModuleVersion => "0.0.8";
public override string ModuleVersion => "0.0.9";
public override string ModuleAuthor => "schwarper";

public class PlayerData
{
public Tag PlayerTag { get; set; } = null!;
public bool ToggleTags { get; set; }
}

public static ConcurrentDictionary<ulong, PlayerData> PlayerDataList { get; set; } = [];
public static ConcurrentDictionary<ulong, Tag> PlayerTagsList { get; set; } = [];
public static HashSet<ulong> PlayerToggleTagsList { get; set; } = [];
public TagsAPI Api { get; set; } = null!;

public override void Load(bool hotReload)
Expand Down Expand Up @@ -67,29 +63,31 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo
return HookResult.Continue;
}

PlayerDataList.TryRemove(player.SteamID, out _);
var steamid = player.SteamID;
PlayerTagsList.TryRemove(steamid, out _);
PlayerToggleTagsList.Remove(steamid);
return HookResult.Continue;
}

public HookResult OnMessage(UserMessage um)
{
int entityIndex = um.ReadInt("entityindex");

CCSPlayerController? player = Utilities.GetPlayerFromIndex(entityIndex);

if (player == null || player.IsBot)
if (Utilities.GetPlayerFromIndex(entityIndex) is not CCSPlayerController player || player.IsBot)
{
return HookResult.Continue;
}

if (!PlayerDataList.TryGetValue(player.SteamID, out PlayerData? playerData))
var steamid = player.SteamID;
Tag playerData;

if (PlayerToggleTagsList.Contains(steamid))
{
return HookResult.Continue;
playerData = Config.DefaultTags;
}

if (!playerData.ToggleTags)
else
{
playerData.PlayerTag = Config.DefaultTags;
playerData = PlayerTagsList[steamid];
}

Api.PlayerChat(um);
Expand All @@ -109,13 +107,13 @@ public HookResult OnMessage(UserMessage um)

string deadname = player.PawnIsAlive ? string.Empty : Config.Settings.DeadName;
string teamname = isTeamMessage ? player.Team.Name() : string.Empty;
string tag = playerData.PlayerTag.ChatTag;
string namecolor = playerData.PlayerTag.NameColor;
string chatcolor = playerData.PlayerTag.ChatColor;
string tag = playerData.ChatTag;
string namecolor = playerData.NameColor;
string chatcolor = playerData.ChatColor;

string formattedMessage = TagsLibrary.FormatMessage(deadname, teamname, tag, namecolor, chatcolor, playername, cleanedMessage, player.Team);
um.SetString("messagename", formattedMessage);
um.SetBool("chat", playerData.PlayerTag.ChatSound);
um.SetBool("chat", playerData.ChatSound);

return HookResult.Changed;
}
Expand All @@ -138,40 +136,42 @@ public void Command_Toggletags(CCSPlayerController? player, CommandInfo info)
return;
}

if (!PlayerDataList.TryGetValue(player.SteamID, out PlayerData? playerData))
{
return;
}

bool value = playerData.ToggleTags;
bool value = PlayerToggleTagsList.Add(player.SteamID);

if (value)
{
playerData.ToggleTags = false;
info.ReplyToCommand("[cs2-tags] Toggletags is false");
info.ReplyToCommand("[cs2-tags] Tags are now hidden.");
}
else
{
playerData.ToggleTags = true;
info.ReplyToCommand("[cs2-tags] Toggletags is true");
PlayerToggleTagsList.Remove(player.SteamID);
info.ReplyToCommand("[cs2-tags] Tags are now visible.");
}
}

public static void UpdateTags()
{
foreach (KeyValuePair<ulong, PlayerData> kvp in PlayerDataList)
{
CCSPlayerController? player = Utilities.GetPlayerFromSteamId(kvp.Key);
var players = GetPlayers();

if (player == null)
foreach ((ulong steamid, Tag tag) in PlayerTagsList)
{
if (players.FirstOrDefault(p => p.SteamID == steamid) is not CCSPlayerController player)
{
continue;
}

string scoretag = kvp.Value.ToggleTags ? Config.DefaultTags.ScoreTag :
kvp.Value.PlayerTag.ScoreTag;
string scoretag;

if (PlayerToggleTagsList.Contains(steamid))
{
scoretag = Config.DefaultTags.ScoreTag;
}
else
{
scoretag = tag.ScoreTag;
}

if (!string.IsNullOrEmpty(scoretag) && player.Clan != scoretag)
if (player.Clan != scoretag)
{
player.SetTag(scoretag);
}
Expand Down
Loading

0 comments on commit 2743889

Please sign in to comment.