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

Commit

Permalink
Patch v4.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ryuu committed May 9, 2024
1 parent a8b1aa0 commit e9a4b11
Show file tree
Hide file tree
Showing 19 changed files with 218 additions and 55 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
-- 2023.05.09 - V4.2.0

- feat: Toplist placement for ranks can now be displayed on scoreboard with new settings
- feat: Auto team balancer by rank points (experimental)
- fix: Removed debug messages lefover
- fix: Commands show up even if the module disabled
- fix: Compile warning from CSS updates
- fix: Headshot is now only counted if it was a kill
- fix: Points by playtime not counts anymore after 2-3 ticks
- fix: Game win and lose stats not counted properly

-- 2023.04.16 - V4.1.9

- fix: Collection was modified
Expand Down
7 changes: 1 addition & 6 deletions K4-System/src/Models/PlayerModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ namespace K4System.Models;

public class K4Player
{
//** ? Main */
private readonly Plugin Plugin;

//** ? Player */
public readonly CCSPlayerController Controller;
public readonly ulong SteamID;
Expand All @@ -25,10 +22,8 @@ public class K4Player
public TimeData? timeData { get; set; }
public (int killStreak, DateTime lastKillTime) KillStreak = (0, DateTime.MinValue);

public K4Player(Plugin plugin, CCSPlayerController playerController)
public K4Player(CCSPlayerController playerController)
{
Plugin = plugin;

Controller = playerController;
SteamID = playerController.SteamID;
PlayerName = playerController.PlayerName;
Expand Down
75 changes: 65 additions & 10 deletions K4-System/src/Module/ModuleRank.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace K4System
using CounterStrikeSharp.API.Modules.Timers;
using CounterStrikeSharp.API.Modules.Utils;
using K4System.Models;
using Dapper;

public partial class ModuleRank : IModuleRank
{
Expand All @@ -16,12 +17,16 @@ public ModuleRank(ILogger<ModuleRank> logger, IPluginContext pluginContext)
this.pluginContext = pluginContext;
}

public Timer? reservePlayTimeTimer = null;
public Timer? reservePlacementTimer = null;

public void Initialize(bool hotReload)
{
this.plugin = (pluginContext.Plugin as Plugin)!;
this.Config = plugin.Config;

this.Logger.LogInformation("Initializing '{0}'", this.GetType().Name);
if (Config.GeneralSettings.LoadMessages)
this.Logger.LogInformation("Initializing '{0}'", this.GetType().Name);

//** ? Register Module Parts */

Expand All @@ -32,22 +37,72 @@ public void Initialize(bool hotReload)

//** ? Register Timers */

plugin.AddTimer(Config.PointSettings.PlaytimeMinutes * 60, () =>
if (Config.PointSettings.PlaytimeMinutes > 0)
{
reservePlayTimeTimer = plugin.AddTimer(Config.PointSettings.PlaytimeMinutes * 60, () =>
{
foreach (K4Player k4player in plugin.K4Players)
{
if (!k4player.IsValid || !k4player.IsPlayer)
continue;
if (k4player.Controller.Team == CsTeam.Terrorist)
ModifyPlayerPoints(k4player, Config.PointSettings.PlaytimePoints, "k4.phrases.playtime");
}
}, TimerFlags.REPEAT);
}

if (Config.RankSettings.DisplayToplistPlacement)
{
foreach (K4Player k4player in plugin.K4Players)
reservePlacementTimer = plugin.AddTimer(5, () =>
{
if (!k4player.IsValid || !k4player.IsPlayer)
continue;
if (k4player.Controller.Team == CsTeam.Terrorist)
ModifyPlayerPoints(k4player, Config.PointSettings.PlaytimePoints, "k4.phrases.playtime");
}
}, TimerFlags.REPEAT);
string query = $@"SELECT steam_id,
(SELECT COUNT(*) FROM `{Config.DatabaseSettings.TablePrefix}k4ranks`
WHERE `points` > (SELECT `points` FROM `{Config.DatabaseSettings.TablePrefix}k4ranks` WHERE `steam_id` = t.steam_id)) AS playerPlace
FROM `{Config.DatabaseSettings.TablePrefix}k4ranks` t
WHERE steam_id IN @SteamIds";
var steamIds = plugin.K4Players.Where(p => p.IsValid && p.IsPlayer && p.rankData != null)
.Select(p => p.SteamID)
.ToArray();
Task.Run(async () =>
{
try
{
using (var connection = plugin.CreateConnection(Config))
{
await connection.OpenAsync();
var result = await connection.QueryAsync(query, new { SteamIds = steamIds });
foreach (var row in result)
{
string steamId = row.steam_id;
int playerPlace = (int)row.playerPlace + 1;
K4Player? k4player = plugin.K4Players.FirstOrDefault(p => p.SteamID == ulong.Parse(steamId));
if (k4player != null && k4player.rankData != null)
{
k4player.rankData.TopPlacement = playerPlace;
}
}
}
}
catch (Exception ex)
{
Server.NextFrame(() => Logger.LogError($"A problem occurred while fetching player placements: {ex.Message}"));
}
});
}, TimerFlags.REPEAT);
}

}

public void Release(bool hotReload)
{
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
if (Config.GeneralSettings.LoadMessages)
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
}
}
}
6 changes: 4 additions & 2 deletions K4-System/src/Module/ModuleStat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public void Initialize(bool hotReload)
this.plugin = (pluginContext.Plugin as Plugin)!;
this.Config = plugin.Config;

this.Logger.LogInformation("Initializing '{0}'", this.GetType().Name);
if (Config.GeneralSettings.LoadMessages)
this.Logger.LogInformation("Initializing '{0}'", this.GetType().Name);

//** ? Register Module Parts */

Expand All @@ -27,7 +28,8 @@ public void Initialize(bool hotReload)

public void Release(bool hotReload)
{
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
if (Config.GeneralSettings.LoadMessages)
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
}
}
}
6 changes: 4 additions & 2 deletions K4-System/src/Module/ModuleTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public void Initialize(bool hotReload)
this.plugin = (pluginContext.Plugin as Plugin)!;
this.Config = plugin.Config;

this.Logger.LogInformation("Initializing '{0}'", this.GetType().Name);
if (Config.GeneralSettings.LoadMessages)
this.Logger.LogInformation("Initializing '{0}'", this.GetType().Name);

//** ? Register Module Parts */

Expand All @@ -27,7 +28,8 @@ public void Initialize(bool hotReload)

public void Release(bool hotReload)
{
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
if (Config.GeneralSettings.LoadMessages)
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
}
}
}
6 changes: 4 additions & 2 deletions K4-System/src/Module/ModuleUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public void Initialize(bool hotReload)
this.plugin = (pluginContext.Plugin as Plugin)!;
this.Config = plugin.Config;

this.Logger.LogInformation("Initializing '{0}'", this.GetType().Name);
if (Config.GeneralSettings.LoadMessages)
this.Logger.LogInformation("Initializing '{0}'", this.GetType().Name);

//** ? Register Module Parts */

Expand All @@ -26,7 +27,8 @@ public void Initialize(bool hotReload)

public void Release(bool hotReload)
{
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
if (Config.GeneralSettings.LoadMessages)
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
}
}
}
2 changes: 0 additions & 2 deletions K4-System/src/Module/Rank/RankCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ public void OnCommandTop(CCSPlayerController? player, CommandInfo info)
printCount = Math.Clamp(parsedInt, 1, 25);
}

Logger.LogInformation($"Player {k4player.PlayerName} requested top {printCount} players.");

Task.Run(async () =>
{
Console.WriteLine("Saving all players data");
Expand Down
43 changes: 43 additions & 0 deletions K4-System/src/Module/Rank/RankEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,49 @@ public void Initialize_Events()
return HookResult.Continue;
}, HookMode.Post);

plugin.RegisterEventHandler((EventRoundPrestart @event, GameEventInfo info) =>
{
if (Config.RankSettings.RankBasedTeamBalance)
{
int team1Size = plugin.K4Players.Count(p => p.Controller.Team == CsTeam.Terrorist);
int team2Size = plugin.K4Players.Count(p => p.Controller.Team == CsTeam.CounterTerrorist);
if (Math.Abs(team1Size - team2Size) > 1)
{
var team1Players = plugin.K4Players.Where(p => p.Controller.Team == CsTeam.Terrorist).ToList();
var team2Players = plugin.K4Players.Where(p => p.Controller.Team == CsTeam.CounterTerrorist).ToList();
var team1RankPoints = team1Players.Select(p => p.rankData?.Points ?? 0).Sum();
var team2RankPoints = team2Players.Select(p => p.rankData?.Points ?? 0).Sum();
while (Math.Abs(team1RankPoints - team2RankPoints) > Config.RankSettings.RankBasedTeamBalanceMaxDifference)
{
if (team1RankPoints > team2RankPoints)
{
var playerToSwitch = team1Players.OrderByDescending(p => p.rankData?.Points ?? 0).First();
team1Players.Remove(playerToSwitch);
team2Players.Add(playerToSwitch);
playerToSwitch.Controller.ChangeTeam(CsTeam.CounterTerrorist);
}
else
{
var playerToSwitch = team2Players.OrderByDescending(p => p.rankData?.Points ?? 0).First();
team2Players.Remove(playerToSwitch);
team1Players.Add(playerToSwitch);
playerToSwitch.Controller.ChangeTeam(CsTeam.Terrorist);
}
team1RankPoints = team1Players.Select(p => p.rankData?.Points ?? 0).Sum();
team2RankPoints = team2Players.Select(p => p.rankData?.Points ?? 0).Sum();
}
Server.PrintToChatAll($" {plugin.Localizer["k4.general.prefix"]} {plugin.Localizer["k4.ranks.rank.teamsbalanced"]}");
}
}
return HookResult.Continue;
}, HookMode.Post);

plugin.RegisterEventHandler((EventBombPlanted @event, GameEventInfo info) =>
{
ModifyPlayerPointsConnector(@event.Userid, Config.PointSettings.BombPlant, "k4.phrases.bombplanted");
Expand Down
10 changes: 9 additions & 1 deletion K4-System/src/Module/Rank/RankFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public Rank GetPlayerRank(int points)
return rankDictionary.LastOrDefault(kv => points >= kv.Value.Point).Value ?? noneRank!;
}

public void ModifyPlayerPointsConnector(CCSPlayerController player, int amount, string reason, string? extraInfo = null)
public void ModifyPlayerPointsConnector(CCSPlayerController? player, int amount, string reason, string? extraInfo = null)
{
K4Player? k4player = plugin.GetK4Player(player);
if (k4player is null)
Expand Down Expand Up @@ -264,6 +264,14 @@ public void SetPlayerClanTag(K4Player k4player)
}
}

if (Config.RankSettings.DisplayToplistPlacement && k4player.rankData != null)
{
if (k4player.rankData.TopPlacement != 0 && k4player.rankData.TopPlacement <= Config.RankSettings.DisplayToplistPlacementMax)
{
tag = $"{Config.RankSettings.DisplayToplistPlacementTag.Replace("{placement}", k4player.rankData.TopPlacement.ToString())} {tag}";
}
}

if (Config.RankSettings.CountryTagEnabled)
{
string countryTag = GetPlayerCountryCode(k4player.Controller);
Expand Down
1 change: 1 addition & 0 deletions K4-System/src/Module/Rank/RankGlobals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class RankData
public required int RoundPoints { get; set; }
public required bool HideAdminTag { get; set; }
public required bool MuteMessages { get; set; }
public int TopPlacement { get; set; }
}

public required Plugin plugin;
Expand Down
18 changes: 9 additions & 9 deletions K4-System/src/Module/Stat/StatEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace K4System
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using K4System.Models;
using Microsoft.Extensions.Logging;

public partial class ModuleStat : IModuleStat
{
Expand Down Expand Up @@ -60,11 +61,11 @@ public void Initialize_Events()
if (@event.Assistedflash)
ModifyPlayerStats(k4attacker, "assist_flash", 1);
if (@event.Headshot)
ModifyPlayerStats(k4attacker, "headshots", 1);
switch (@event.Hitgroup)
{
case (int)HitGroup_t.HITGROUP_HEAD:
ModifyPlayerStats(k4attacker, "headshots", 1);
break;
case (int)HitGroup_t.HITGROUP_CHEST:
ModifyPlayerStats(k4attacker, "chest_hits", 1);
break;
Expand Down Expand Up @@ -138,11 +139,6 @@ public void Initialize_Events()
if (k4attacker != null && k4attacker.IsValid && k4attacker.IsPlayer)
{
ModifyPlayerStats(k4attacker, "hits_given", 1);
if (@event.Hitgroup == 1)
{
ModifyPlayerStats(k4attacker, "headshots", 1);
}
}
return HookResult.Continue;
Expand Down Expand Up @@ -277,10 +273,14 @@ public void Initialize_Events()
{
k4players.Where(p => p.Controller.Team > CsTeam.Spectator)
.ToList()
.ForEach(p => ModifyPlayerStats(p, p.Controller.Team == winnerTeam ? "game_win" : "game_lose", 1));
.ForEach(p =>
{
ModifyPlayerStats(p, p.Controller.Team == winnerTeam ? "game_win" : "game_lose", 1);
});
}
}
Task.Run(plugin.SaveAllPlayersDataAsync);
return HookResult.Continue;
});
}
Expand Down
1 change: 1 addition & 0 deletions K4-System/src/Module/Stat/StatFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace K4System
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using K4System.Models;
using Microsoft.Extensions.Logging;

public partial class ModuleStat : IModuleStat
{
Expand Down
Loading

0 comments on commit e9a4b11

Please sign in to comment.