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

Commit

Permalink
feat: Module switches
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ryuu committed Nov 13, 2023
1 parent 04d1d61 commit e1cc9a4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
9 changes: 9 additions & 0 deletions src/CFG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ public class GeneralSettings

[JsonPropertyName("log-level")]
public int LogLevel { get; set; } = 0;

[JsonPropertyName("module_ranks")]
public bool ModuleRanks { get; set; } = true;

[JsonPropertyName("module_stats")]
public bool ModuleStats { get; set; } = true;

[JsonPropertyName("module_times")]
public bool ModuleTimes { get; set; } = true;
}

public class DatabaseSettings
Expand Down
30 changes: 30 additions & 0 deletions src/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public void OnCommandCheckRank(CCSPlayerController? player, CommandInfo command)
if (!player.IsValidPlayer())
return;

if (!Config.GeneralSettings.ModuleRanks)
return;

string playerName = player!.PlayerName;
int playerPoints = PlayerSummaries[player].Points;

Expand Down Expand Up @@ -69,6 +72,9 @@ public void OnCommandResetMyRank(CCSPlayerController? player, CommandInfo comman
if (!player.IsValidPlayer())
return;

if (!Config.GeneralSettings.ModuleRanks)
return;

if (!PlayerSummaries.ContainsPlayer(player!))
LoadPlayerData(player!);

Expand All @@ -86,6 +92,9 @@ public void OnCommandCheckRankTopFive(CCSPlayerController? player, CommandInfo c
if (!player.IsValidPlayer())
return;

if (!Config.GeneralSettings.ModuleRanks)
return;

PrintTopXPlayers(player!, 5);
}

Expand All @@ -96,6 +105,9 @@ public void OnCommandCheckRankTopTen(CCSPlayerController? player, CommandInfo co
if (!player.IsValidPlayer())
return;

if (!Config.GeneralSettings.ModuleRanks)
return;

PrintTopXPlayers(player!, 10);
}

Expand All @@ -107,6 +119,9 @@ public void OnCommandCheckStatistics(CCSPlayerController? player, CommandInfo co
if (!player.IsValidPlayer())
return;

if (!Config.GeneralSettings.ModuleStats)
return;

MySqlQueryResult result = MySql!.Table("k4stats").Where(MySqlQueryCondition.New("steam_id", "=", player!.SteamID.ToString())).Select();

if (result.Rows > 0)
Expand All @@ -128,6 +143,9 @@ public void OnCommandResetOtherRank(CCSPlayerController? player, CommandInfo com
if (!player.IsValidPlayer())
return;

if (!Config.GeneralSettings.ModuleRanks)
return;

List<CCSPlayerController> players = Utilities.GetPlayers();
foreach (CCSPlayerController target in players)
{
Expand Down Expand Up @@ -157,6 +175,9 @@ public void OnCommandSetPoints(CCSPlayerController? player, CommandInfo command)
if (!player.IsValidPlayer())
return;

if (!Config.GeneralSettings.ModuleRanks)
return;

if (int.TryParse(command.ArgByIndex(2), out int parsedInt))
{
List<CCSPlayerController> players = Utilities.GetPlayers();
Expand Down Expand Up @@ -193,6 +214,9 @@ public void OnCommandGivePoints(CCSPlayerController? player, CommandInfo command
if (!player.IsValidPlayer())
return;

if (!Config.GeneralSettings.ModuleRanks)
return;

if (int.TryParse(command.ArgByIndex(2), out int parsedInt))
{
List<CCSPlayerController> players = Utilities.GetPlayers();
Expand Down Expand Up @@ -229,6 +253,9 @@ public void OnCommandRemovePoints(CCSPlayerController? player, CommandInfo comma
if (!player.IsValidPlayer())
return;

if (!Config.GeneralSettings.ModuleRanks)
return;

if (int.TryParse(command.ArgByIndex(2), out int parsedInt))
{
List<CCSPlayerController> players = Utilities.GetPlayers();
Expand Down Expand Up @@ -270,6 +297,9 @@ public void OnCommandCheckPlaytime(CCSPlayerController? player, CommandInfo comm
if (player == null || !player.IsValid)
return;

if (!Config.GeneralSettings.ModuleTimes)
return;

SaveClientTime(player);

MySqlQueryResult result = MySql!.Table("k4times").Where($"`steam_id` = '{player.SteamID}'").Select();
Expand Down
28 changes: 18 additions & 10 deletions src/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ private void SetupGameEvents()
{
CCSPlayerController playerController = @event.Userid;
playerKillStreaks[playerController.UserId ?? 0] = (0, DateTime.MinValue);
if (Config.GeneralSettings.ModuleRanks)
playerKillStreaks[playerController.UserId ?? 0] = (0, DateTime.MinValue);
if (playerController.IsValidPlayer())
LoadPlayerData(playerController);
Expand Down Expand Up @@ -61,6 +62,9 @@ private void SetupGameEvents()
});
RegisterEventHandler<EventPlayerTeam>((@event, info) =>
{
if (Config.GeneralSettings.ModuleTimes)
return HookResult.Continue;
CCSPlayerController player = @event.Userid;
if (player == null || !player.IsValid || player.IsBot || @event.Oldteam == @event.Team)
return HookResult.Continue;
Expand All @@ -77,13 +81,15 @@ private void SetupGameEvents()
RegisterEventHandler<EventRoundMvp>((@event, info) =>
{
CCSPlayerController player = @event.Userid;
if (player == null || !player.IsValid || player.IsBot)
return HookResult.Continue;
MySql!.ExecuteNonQueryAsync($"UPDATE `k4stats` SET `mvp` = (`mvp` + 1) WHERE `steam_id` = {player.SteamID};");
if (Config.GeneralSettings.ModuleStats)
MySql!.ExecuteNonQueryAsync($"UPDATE `k4stats` SET `mvp` = (`mvp` + 1) WHERE `steam_id` = {player.SteamID};");
if (Config.GeneralSettings.ModuleRanks)
ModifyClientPoints(@event.Userid, CHANGE_MODE.GIVE, Config.PointSettings.MVP, "Round MVP");
ModifyClientPoints(@event.Userid, CHANGE_MODE.GIVE, Config.PointSettings.MVP, "Round MVP");
return HookResult.Continue;
});
RegisterEventHandler<EventRoundStart>((@event, info) =>
Expand Down Expand Up @@ -114,9 +120,6 @@ private void SetupGameEvents()
{
CsTeam winnerTeam = (CsTeam)@event.Winner;
if (!IsPointsAllowed())
return HookResult.Continue;
List<CCSPlayerController> players = Utilities.GetPlayers();
foreach (CCSPlayerController player in players)
{
Expand All @@ -132,14 +135,16 @@ private void SetupGameEvents()
if (IsStatsAllowed())
MySql!.ExecuteNonQueryAsync($"UPDATE `k4stats` SET `round_win` = (`round_win` + 1) WHERE `steam_id` = {player.SteamID};");
ModifyClientPoints(player, CHANGE_MODE.GIVE, Config.PointSettings.RoundWin, "Round Win");
if (IsPointsAllowed())
ModifyClientPoints(player, CHANGE_MODE.GIVE, Config.PointSettings.RoundWin, "Round Win");
}
else
{
if (IsStatsAllowed())
MySql!.ExecuteNonQueryAsync($"UPDATE `k4stats` SET `round_lose` = (`round_lose` + 1) WHERE `steam_id` = {player.SteamID};");
ModifyClientPoints(player, CHANGE_MODE.REMOVE, Config.PointSettings.RoundLose, "Round Lose");
if (IsPointsAllowed())
ModifyClientPoints(player, CHANGE_MODE.REMOVE, Config.PointSettings.RoundLose, "Round Lose");
}
}
Expand All @@ -150,6 +155,9 @@ private void SetupGameEvents()
});
RegisterEventHandler<EventPlayerSpawn>((@event, info) =>
{
if (!Config.GeneralSettings.ModuleTimes)
return HookResult.Continue;
CCSPlayerController player = @event.Userid;
if (player == null || !player.IsValid || player.IsBot)
return HookResult.Continue;
Expand Down Expand Up @@ -429,7 +437,7 @@ private void SetupGameEvents()
}
}
if (assisterController.IsValidPlayer())
if (assisterController.IsValidPlayer() && IsPointsAllowed())
{
if (!PlayerSummaries.ContainsPlayer(assisterController))
LoadPlayerData(assisterController);
Expand Down
4 changes: 2 additions & 2 deletions src/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,15 @@ public bool IsPointsAllowed()
List<CCSPlayerController> players = Utilities.GetPlayers();
int notBots = players.Count(player => !player.IsBot);

return (!K4ryuu.GameRules().WarmupPeriod || Config.RankSettings.WarmupPoints) && (Config.RankSettings.MinPlayers <= notBots);
return Config.GeneralSettings.ModuleRanks && (!K4ryuu.GameRules().WarmupPeriod || Config.RankSettings.WarmupPoints) && (Config.RankSettings.MinPlayers <= notBots);
}

public bool IsStatsAllowed()
{
List<CCSPlayerController> players = Utilities.GetPlayers();
int notBots = players.Count(player => !player.IsBot);

return (!K4ryuu.GameRules().WarmupPeriod || Config.StatisticSettings.WarmupStats) && (Config.StatisticSettings.MinPlayers <= notBots);
return Config.GeneralSettings.ModuleStats && (!K4ryuu.GameRules().WarmupPeriod || Config.StatisticSettings.WarmupStats) && (Config.StatisticSettings.MinPlayers <= notBots);
}

private void UpdatePlayerData(CCSPlayerController player, string field, double value)
Expand Down

0 comments on commit e1cc9a4

Please sign in to comment.