Skip to content

Commit

Permalink
Code editing
Browse files Browse the repository at this point in the history
  • Loading branch information
schwarper committed Aug 28, 2024
1 parent baddca2 commit 075edf0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vs/
**/obj
**/bin
36 changes: 18 additions & 18 deletions cs2-tags/src/api/api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public class TagsAPI : ITagApi
{
public string GetPlayerTag(CCSPlayerController player, Tags_Tags tag)
{
if (PlayerTags.TryGetValue(player.SteamID, out Tag? playerTag))
if (PlayerDataList.TryGetValue(player.SteamID, out var playerData))
{
return tag switch
{
Tags_Tags.ScoreTag => playerTag.ScoreTag,
Tags_Tags.ChatTag => playerTag.ChatTag,
Tags_Tags.ScoreTag => playerData.PlayerTag.ScoreTag,
Tags_Tags.ChatTag => playerData.PlayerTag.ChatTag,
_ => string.Empty
};
}
Expand All @@ -24,45 +24,45 @@ public string GetPlayerTag(CCSPlayerController player, Tags_Tags tag)

public void SetPlayerTag(CCSPlayerController player, Tags_Tags tag, string newtag)
{
if (PlayerTags.TryGetValue(player.SteamID, out Tag? playertag))
if (PlayerDataList.TryGetValue(player.SteamID, out var playerData))
{
switch (tag)
{
case Tags_Tags.ScoreTag:
playertag.ScoreTag = newtag;
playerData.PlayerTag.ScoreTag = newtag;
break;
case Tags_Tags.ChatTag:
playertag.ChatTag = newtag;
playerData.PlayerTag.ChatTag = newtag;
break;
}
}
}

public void ResetPlayerTag(CCSPlayerController player, Tags_Tags tag)
{
if (PlayerTags.TryGetValue(player.SteamID, out Tag? playertag))
if (PlayerDataList.TryGetValue(player.SteamID, out var playerData))
{
Tag defaultTag = GetTag(player);
switch (tag)
{
case Tags_Tags.ScoreTag:
playertag.ScoreTag = defaultTag.ScoreTag;
playerData.PlayerTag.ScoreTag = defaultTag.ScoreTag;
break;
case Tags_Tags.ChatTag:
playertag.ChatTag = defaultTag.ChatTag;
playerData.PlayerTag.ChatTag = defaultTag.ChatTag;
break;
}
}
}

public string GetPlayerColor(CCSPlayerController player, Tags_Colors color)
{
if (PlayerTags.TryGetValue(player.SteamID, out Tag? playertag))
if (PlayerDataList.TryGetValue(player.SteamID, out var playerData))
{
return color switch
{
Tags_Colors.NameColor => playertag.NameColor,
Tags_Colors.ChatColor => playertag.ChatColor,
Tags_Colors.NameColor => playerData.PlayerTag.NameColor,
Tags_Colors.ChatColor => playerData.PlayerTag.ChatColor,
_ => string.Empty
};
}
Expand All @@ -72,32 +72,32 @@ public string GetPlayerColor(CCSPlayerController player, Tags_Colors color)

public void SetPlayerColor(CCSPlayerController player, Tags_Colors color, string newcolor)
{
if (PlayerTags.TryGetValue(player.SteamID, out Tag? playertag))
if (PlayerDataList.TryGetValue(player.SteamID, out var playerData))
{
switch (color)
{
case Tags_Colors.NameColor:
playertag.NameColor = newcolor;
playerData.PlayerTag.NameColor = newcolor;
break;
case Tags_Colors.ChatColor:
playertag.ChatColor = newcolor;
playerData.PlayerTag.ChatColor = newcolor;
break;
}
}
}

public void ResetPlayerColor(CCSPlayerController player, Tags_Colors color)
{
if (PlayerTags.TryGetValue(player.SteamID, out Tag? playertag))
if (PlayerDataList.TryGetValue(player.SteamID, out var playerData))
{
Tag defaultTag = GetTag(player);
switch (color)
{
case Tags_Colors.NameColor:
playertag.NameColor = defaultTag.NameColor;
playerData.PlayerTag.NameColor = defaultTag.NameColor;
break;
case Tags_Colors.ChatColor:
playertag.ChatColor = defaultTag.ChatColor;
playerData.PlayerTag.ChatColor = defaultTag.ChatColor;
break;
}
}
Expand Down
12 changes: 7 additions & 5 deletions cs2-tags/src/config/config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,27 @@ public static void Reload()
Config.Settings = new();
Config.DefaultTags = new();
Config.Tags.Clear();
PlayerTags.Clear();
PlayerToggleTags.Clear();
PlayerDataList.Clear();

Load();

List<CCSPlayerController> players = Utilities.GetPlayers();

foreach (CCSPlayerController player in players)
{
PlayerTags.Add(player.SteamID, GetTag(player));
PlayerToggleTags.Add(player.SteamID, true);
PlayerDataList.Add(player.SteamID, new PlayerData
{
PlayerTag = GetTag(player),
ToggleTags = true
});
}
}

public static Tag GetTag(CCSPlayerController player)
{
Dictionary<string, Tag> tags = Config.Tags;

Tag steamidTag = tags.FirstOrDefault(tag => tag.Key == player.SteamID.ToString()).Value;
Tag steamidTag = tags.SingleOrDefault(tag => tag.Key == player.SteamID.ToString()).Value;

if (steamidTag != null)
{
Expand Down
54 changes: 31 additions & 23 deletions cs2-tags/src/cs2-tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ namespace Tags;
public class Tags : BasePlugin
{
public override string ModuleName => "Tags";
public override string ModuleVersion => "0.0.5";
public override string ModuleVersion => "0.0.6";
public override string ModuleAuthor => "schwarper";

public static Dictionary<ulong, Tag> PlayerTags { get; set; } = [];
public static Dictionary<ulong, bool> PlayerToggleTags { get; set; } = [];
public class PlayerData
{
public Tag PlayerTag { get; set; } = null!;
public bool ToggleTags { get; set; }
}

public static Dictionary<ulong, PlayerData> PlayerDataList { get; set; } = [];

public override void Load(bool hotReload)
{
Expand Down Expand Up @@ -47,8 +52,12 @@ public HookResult OnPlayerConnect(EventPlayerConnectFull @event, GameEventInfo i
return HookResult.Continue;
}

PlayerTags.Add(player.SteamID, GetTag(player));
PlayerToggleTags.Add(player.SteamID, true);
PlayerDataList.Add(player.SteamID, new PlayerData
{
PlayerTag = GetTag(player),
ToggleTags = true
});

return HookResult.Continue;
}

Expand All @@ -57,13 +66,12 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo
{
CCSPlayerController? player = @event.Userid;

if (player == null || !PlayerTags.ContainsKey(player.SteamID))
if (player == null)
{
return HookResult.Continue;
}

PlayerTags.Remove(player.SteamID);
PlayerToggleTags.Remove(player.SteamID);
PlayerDataList.Remove(player.SteamID);
return HookResult.Continue;
}

Expand All @@ -78,14 +86,14 @@ public HookResult OnMessage(UserMessage um)
return HookResult.Continue;
}

if (!PlayerTags.TryGetValue(player.SteamID, out Tag? playerTag) || playerTag == null)
if (!PlayerDataList.TryGetValue(player.SteamID, out var playerData))
{
return HookResult.Continue;
}

if (!PlayerToggleTags[player.SteamID])
if (!playerData.ToggleTags)
{
playerTag = Config.DefaultTags;
playerData.PlayerTag = Config.DefaultTags;
}

string msgT = um.ReadString("messagename");
Expand All @@ -96,9 +104,9 @@ public HookResult OnMessage(UserMessage um)

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

string formattedMessage = FormatMessage(deadname, teamname, tag, namecolor, chatcolor, playername, message, player.Team);
um.SetString("messagename", formattedMessage);
Expand Down Expand Up @@ -148,38 +156,38 @@ public void Command_Toggletags(CCSPlayerController? player, CommandInfo info)
return;
}

if (!PlayerToggleTags.TryGetValue(player.SteamID, out bool value))
if (!PlayerDataList.TryGetValue(player.SteamID, out var playerData))
{
return;
}

var value = playerData.ToggleTags;

if (value)
{
PlayerToggleTags[player.SteamID] = false;

playerData.ToggleTags = false;
info.ReplyToCommand("[cs2-tags] Toggletags is false");
}
else
{
PlayerToggleTags[player.SteamID] = true;

playerData.ToggleTags = true;
info.ReplyToCommand("[cs2-tags] Toggletags is true");
}
}

public static void UpdateTags()
{
foreach (KeyValuePair<ulong, Tag> kvp in PlayerTags)
foreach (var kvp in PlayerDataList)
{
CCSPlayerController player = Utilities.GetPlayerFromSteamId(kvp.Key)!;
Tag tag = kvp.Value;
var scoretag = kvp.Value.PlayerTag.ScoreTag;

if (string.IsNullOrEmpty(tag.ScoreTag))
if (string.IsNullOrEmpty(scoretag))
{
continue;
}

player.Clan = tag.ScoreTag;
player.Clan = scoretag;
}
}
}

0 comments on commit 075edf0

Please sign in to comment.