Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Commit

Permalink
Patch v4.3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ryuu committed Jun 3, 2024
1 parent 80f81e0 commit 87dcf94
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 41 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
-- 2023.06.03 - V4.3.4

- fix: Country tag performance issues
- fix: Disconnect announcement not shown
- fix: Fixed the resetrank problem
- fix: Color replacing problems with DarkRed or DarkBlue and so

-- 2023.05.18 - V4.3.3

- fix: Threading problems
Expand Down
19 changes: 13 additions & 6 deletions K4-System/src/Module/Rank/RankFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,21 @@ public void SetPlayerClanTag(K4Player k4player)
}
}

if (Config.RankSettings.CountryTagEnabled)
string? playerIp = k4player.Controller?.IpAddress;
Task.Run(async () =>
{
string countryTag = plugin.GetPlayerCountryCode(k4player.Controller);
tag = tag.Length > 0 ? $"{countryTag} | {tag}" : countryTag;
}
if (Config.RankSettings.CountryTagEnabled)
{
string countryTag = await plugin.GetPlayerCountryCodeAsync(playerIp);
tag = tag.Length > 0 ? $"{countryTag} | {tag}" : countryTag;
}
if (tag.Length > 0)
k4player.ClanTag = tag;
Server.NextWorldUpdate(() =>
{
if (tag.Length > 0)
k4player.ClanTag = tag;
});
});
}
}
}
13 changes: 9 additions & 4 deletions K4-System/src/Module/Utils/UtilsEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ public void Initialize_Events()
{
K4Player? k4player = plugin.GetK4Player(@event.Userid);
if (k4player is null || !k4player.IsValid || !k4player.IsPlayer)
if (k4player is null || !k4player.IsPlayer)
return HookResult.Continue;
string? message = plugin.ReplacePlaceholders(k4player, plugin.Localizer["k4.announcement.disconnect"]);
if (message != null)
Server.PrintToChatAll(message);
plugin.ReplacePlaceholders(k4player, plugin.Localizer["k4.announcement.disconnect"], (result) =>
{
Server.NextWorldUpdate(() =>
{
if (result != null)
Server.NextWorldUpdate(() => Server.PrintToChatAll(result));
});
});
return HookResult.Continue;
});
Expand Down
2 changes: 1 addition & 1 deletion K4-System/src/Plugin/PluginBasics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public void OnCommandResetData(CCSPlayerController? player, CommandInfo info)
if (playerData is null)
return;

playerData.RoundPoints -= playerData.Points;
playerData.RoundPoints = 0;
playerData.Points = Config.RankSettings.StartPoints;
playerData.Rank = ModuleRank.GetNoneRank();
}
Expand Down
12 changes: 8 additions & 4 deletions K4-System/src/Plugin/PluginDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,14 @@ public void LoadPlayerRowToCache(K4Player k4player, dynamic row, bool all)

if (Config.UtilSettings.ConnectMessageEnable)
{
string? message = ReplacePlaceholders(k4player, Localizer["k4.announcement.connect"]);

if (message != null)
Server.NextWorldUpdate(() => Server.PrintToChatAll(message));
ReplacePlaceholders(k4player, Localizer["k4.announcement.connect"], (result) =>
{
Server.NextWorldUpdate(() =>
{
if (result != null)
Server.NextWorldUpdate(() => Server.PrintToChatAll(result));
});
});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion K4-System/src/Plugin/PluginManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed partial class Plugin : BasePlugin

public override string ModuleAuthor => "K4ryuu";

public override string ModuleVersion => "4.3.3 " +
public override string ModuleVersion => "4.3.4 " +
#if RELEASE
"(release)";
#else
Expand Down
73 changes: 48 additions & 25 deletions K4-System/src/Plugin/PluginStock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace K4System
using Microsoft.Extensions.Logging;
using MaxMind.GeoIP2.Exceptions;
using System.Reflection;
using System.Text.RegularExpressions;
using CounterStrikeSharp.API;

public sealed partial class Plugin : BasePlugin
{
Expand All @@ -20,10 +22,16 @@ public sealed partial class Plugin : BasePlugin

public string ApplyPrefixColors(string msg)
{
var chatColors = typeof(ChatColors).GetFields().Select(f => (f.Name, Value: f.GetValue(null)?.ToString()));
foreach (var (name, value) in chatColors)
var chatColors = typeof(ChatColors).GetFields()
.Select(f => new { f.Name, Value = f.GetValue(null)?.ToString() })
.OrderByDescending(c => c.Name.Length);

foreach (var color in chatColors)
{
msg = msg.Replace(name, value, StringComparison.OrdinalIgnoreCase);
if (color.Value != null)
{
msg = Regex.Replace(msg, $@"\b{color.Name}\b", color.Value, RegexOptions.IgnoreCase);
}
}

return msg;
Expand Down Expand Up @@ -98,10 +106,8 @@ public static bool PlayerHasPermission(K4Player k4player, string permission)
}
}

public string GetPlayerCountryCode(CCSPlayerController player)
public async Task<string> GetPlayerCountryCodeAsync(string? playerIp)
{
string? playerIp = player.IpAddress;

if (playerIp == null)
return "??";

Expand All @@ -115,48 +121,65 @@ public string GetPlayerCountryCode(CCSPlayerController player)
return "??";
}

using (DatabaseReader reader = new DatabaseReader(filePath))
return await Task.Run(() =>
{
try
{
MaxMind.GeoIP2.Responses.CountryResponse response = reader.Country(realIP);
return response.Country.IsoCode ?? "??";
}
catch (AddressNotFoundException)
using (DatabaseReader reader = new DatabaseReader(filePath))
{
Logger.LogError($"The address {realIP} is not in the database.");
return "??";
}
catch (GeoIP2Exception ex)
{
Logger.LogError($"Error: {ex.Message}");
return "??";
try
{
MaxMind.GeoIP2.Responses.CountryResponse response = reader.Country(realIP);
return response.Country.IsoCode ?? "??";
}
catch (AddressNotFoundException)
{
Logger.LogError($"The address {realIP} is not in the database.");
return "??";
}
catch (GeoIP2Exception ex)
{
Logger.LogError($"Error: {ex.Message}");
return "??";
}
}
}
});
}

public string? ReplacePlaceholders(K4Player k4player, string text)
public void ReplacePlaceholders(K4Player k4player, string text, Action<string> callback)
{
if (k4player == null)
return text;
{
callback(text);
return;
}

Dictionary<string, string> placeholders = new Dictionary<string, string>
{
{ "name", k4player.PlayerName },
{ "steamid", k4player.SteamID.ToString() },
{ "clantag", k4player.ClanTag },
{ "rank", k4player.rankData?.Rank?.Name ?? "Unranked" },
{ "country", GetPlayerCountryCode(k4player.Controller) },
{ "points", k4player.rankData?.Points.ToString() ?? "0" },
{ "topplacement", k4player.rankData?.TopPlacement.ToString() ?? "0" },
{ "playtime", k4player.timeData?.TimeFields["all"].ToString() ?? "0" },
};

string? playerIp = k4player.Controller.IpAddress;

Task.Run(async () =>
{
string country = await GetPlayerCountryCodeAsync(playerIp);
placeholders.Add("country", country);
callback(ReplaceTextPlaceholders(text, placeholders));
});
}

private string ReplaceTextPlaceholders(string text, Dictionary<string, string> placeholders)
{
foreach (var placeholder in placeholders)
{
text = text.Replace($"{{{placeholder.Key}}}", placeholder.Value);
}

return text;
}
}
Expand Down

0 comments on commit 87dcf94

Please sign in to comment.