Skip to content

Commit

Permalink
refactor: Optimise gamerule based functions (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ryuu committed Nov 24, 2023
1 parent a5dd3d8 commit a9c0260
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
-- 2023.10.24 - V1.3.4

- refactor: Optimise gamerule based functions (#39)
- fix: Visual bugs (#43)
- fix: Disconnect lag spike (#46)
- fix: Bots being loaded into DB rarely (#45)
- fix: Data load lag spikes

-- 2023.10.21 - V1.3.3

Expand Down
11 changes: 11 additions & 0 deletions src/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ public partial class K4System
{
private void SetupGameEvents()
{
RegisterListener<Listeners.OnMapStart>((mapName) =>
{
Log("Processing start of map", LogLevel.Debug);
AddTimer(0.5f, () =>
{
globalGameRules = K4ryuu.GameRules();
});
Log("Start of map processing completed", LogLevel.Debug);
});
RegisterListener<Listeners.OnMapEnd>(() =>
{
Log("Processing end of map for all players", LogLevel.Debug);
Expand Down
10 changes: 8 additions & 2 deletions src/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,10 @@ public bool IsPointsAllowed()
List<CCSPlayerController> players = Utilities.GetPlayers();
int notBots = players.Count(player => !player.IsBot);

bool isAllowed = Config.GeneralSettings.ModuleRanks && (!K4ryuu.GameRules().WarmupPeriod || Config.RankSettings.WarmupPoints) && (Config.RankSettings.MinPlayers <= notBots);
if (globalGameRules == null)
globalGameRules = K4ryuu.GameRules();

bool isAllowed = Config.GeneralSettings.ModuleRanks && (!globalGameRules.WarmupPeriod || Config.RankSettings.WarmupPoints) && (Config.RankSettings.MinPlayers <= notBots);

Log($"Points are {(isAllowed ? "allowed" : "not allowed")}.", LogLevel.Info);

Expand All @@ -500,7 +503,10 @@ public bool IsStatsAllowed()
List<CCSPlayerController> players = Utilities.GetPlayers();
int notBots = players.Count(player => !player.IsBot);

bool isAllowed = Config.GeneralSettings.ModuleStats && (!K4ryuu.GameRules().WarmupPeriod || Config.StatisticSettings.WarmupStats) && (Config.StatisticSettings.MinPlayers <= notBots);
if (globalGameRules == null)
globalGameRules = K4ryuu.GameRules();

bool isAllowed = Config.GeneralSettings.ModuleStats && (!globalGameRules.WarmupPeriod || Config.StatisticSettings.WarmupStats) && (Config.StatisticSettings.MinPlayers <= notBots);

Log($"Stats are {(isAllowed ? "allowed" : "not allowed")}.", LogLevel.Info);

Expand Down
1 change: 1 addition & 0 deletions src/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ public partial class K4System
private Dictionary<string, Rank> ranks = new Dictionary<string, Rank>();
internal static PlayerCache<User> PlayerSummaries = new PlayerCache<User>();
internal string noneRank = "None";
internal CCSGameRules? globalGameRules;
}
}
2 changes: 1 addition & 1 deletion src/K4ryuuHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ internal static class CCSPlayerControllerEx
{
internal static bool IsValidPlayer(this CCSPlayerController? controller)
{
return controller != null && controller.IsValid && !controller.IsBot;
return controller != null && controller.IsValid && !controller.IsBot && !controller.IsHLTV;
}
}

Expand Down

0 comments on commit a9c0260

Please sign in to comment.