Skip to content
This repository was archived by the owner on Jan 25, 2025. It is now read-only.

Commit e9a4b11

Browse files
committed
Patch v4.2.0
1 parent a8b1aa0 commit e9a4b11

19 files changed

+218
-55
lines changed

CHANGELOG

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
-- 2023.05.09 - V4.2.0
2+
3+
- feat: Toplist placement for ranks can now be displayed on scoreboard with new settings
4+
- feat: Auto team balancer by rank points (experimental)
5+
- fix: Removed debug messages lefover
6+
- fix: Commands show up even if the module disabled
7+
- fix: Compile warning from CSS updates
8+
- fix: Headshot is now only counted if it was a kill
9+
- fix: Points by playtime not counts anymore after 2-3 ticks
10+
- fix: Game win and lose stats not counted properly
11+
112
-- 2023.04.16 - V4.1.9
213

314
- fix: Collection was modified

K4-System/src/Models/PlayerModel.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ namespace K4System.Models;
1010

1111
public class K4Player
1212
{
13-
//** ? Main */
14-
private readonly Plugin Plugin;
15-
1613
//** ? Player */
1714
public readonly CCSPlayerController Controller;
1815
public readonly ulong SteamID;
@@ -25,10 +22,8 @@ public class K4Player
2522
public TimeData? timeData { get; set; }
2623
public (int killStreak, DateTime lastKillTime) KillStreak = (0, DateTime.MinValue);
2724

28-
public K4Player(Plugin plugin, CCSPlayerController playerController)
25+
public K4Player(CCSPlayerController playerController)
2926
{
30-
Plugin = plugin;
31-
3227
Controller = playerController;
3328
SteamID = playerController.SteamID;
3429
PlayerName = playerController.PlayerName;

K4-System/src/Module/ModuleRank.cs

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace K4System
77
using CounterStrikeSharp.API.Modules.Timers;
88
using CounterStrikeSharp.API.Modules.Utils;
99
using K4System.Models;
10+
using Dapper;
1011

1112
public partial class ModuleRank : IModuleRank
1213
{
@@ -16,12 +17,16 @@ public ModuleRank(ILogger<ModuleRank> logger, IPluginContext pluginContext)
1617
this.pluginContext = pluginContext;
1718
}
1819

20+
public Timer? reservePlayTimeTimer = null;
21+
public Timer? reservePlacementTimer = null;
22+
1923
public void Initialize(bool hotReload)
2024
{
2125
this.plugin = (pluginContext.Plugin as Plugin)!;
2226
this.Config = plugin.Config;
2327

24-
this.Logger.LogInformation("Initializing '{0}'", this.GetType().Name);
28+
if (Config.GeneralSettings.LoadMessages)
29+
this.Logger.LogInformation("Initializing '{0}'", this.GetType().Name);
2530

2631
//** ? Register Module Parts */
2732

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

3338
//** ? Register Timers */
3439

35-
plugin.AddTimer(Config.PointSettings.PlaytimeMinutes * 60, () =>
40+
if (Config.PointSettings.PlaytimeMinutes > 0)
41+
{
42+
reservePlayTimeTimer = plugin.AddTimer(Config.PointSettings.PlaytimeMinutes * 60, () =>
43+
{
44+
foreach (K4Player k4player in plugin.K4Players)
45+
{
46+
if (!k4player.IsValid || !k4player.IsPlayer)
47+
continue;
48+
49+
if (k4player.Controller.Team == CsTeam.Terrorist)
50+
ModifyPlayerPoints(k4player, Config.PointSettings.PlaytimePoints, "k4.phrases.playtime");
51+
}
52+
}, TimerFlags.REPEAT);
53+
}
54+
55+
if (Config.RankSettings.DisplayToplistPlacement)
3656
{
37-
foreach (K4Player k4player in plugin.K4Players)
57+
reservePlacementTimer = plugin.AddTimer(5, () =>
3858
{
39-
if (!k4player.IsValid || !k4player.IsPlayer)
40-
continue;
4159

42-
if (k4player.Controller.Team == CsTeam.Terrorist)
43-
ModifyPlayerPoints(k4player, Config.PointSettings.PlaytimePoints, "k4.phrases.playtime");
44-
}
45-
}, TimerFlags.REPEAT);
60+
string query = $@"SELECT steam_id,
61+
(SELECT COUNT(*) FROM `{Config.DatabaseSettings.TablePrefix}k4ranks`
62+
WHERE `points` > (SELECT `points` FROM `{Config.DatabaseSettings.TablePrefix}k4ranks` WHERE `steam_id` = t.steam_id)) AS playerPlace
63+
FROM `{Config.DatabaseSettings.TablePrefix}k4ranks` t
64+
WHERE steam_id IN @SteamIds";
65+
66+
var steamIds = plugin.K4Players.Where(p => p.IsValid && p.IsPlayer && p.rankData != null)
67+
.Select(p => p.SteamID)
68+
.ToArray();
69+
70+
Task.Run(async () =>
71+
{
72+
try
73+
{
74+
using (var connection = plugin.CreateConnection(Config))
75+
{
76+
await connection.OpenAsync();
77+
var result = await connection.QueryAsync(query, new { SteamIds = steamIds });
78+
79+
foreach (var row in result)
80+
{
81+
string steamId = row.steam_id;
82+
int playerPlace = (int)row.playerPlace + 1;
83+
84+
K4Player? k4player = plugin.K4Players.FirstOrDefault(p => p.SteamID == ulong.Parse(steamId));
85+
if (k4player != null && k4player.rankData != null)
86+
{
87+
k4player.rankData.TopPlacement = playerPlace;
88+
}
89+
}
90+
}
91+
}
92+
catch (Exception ex)
93+
{
94+
Server.NextFrame(() => Logger.LogError($"A problem occurred while fetching player placements: {ex.Message}"));
95+
}
96+
});
97+
}, TimerFlags.REPEAT);
98+
}
99+
46100
}
47101

48102
public void Release(bool hotReload)
49103
{
50-
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
104+
if (Config.GeneralSettings.LoadMessages)
105+
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
51106
}
52107
}
53108
}

K4-System/src/Module/ModuleStat.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public void Initialize(bool hotReload)
1717
this.plugin = (pluginContext.Plugin as Plugin)!;
1818
this.Config = plugin.Config;
1919

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

2223
//** ? Register Module Parts */
2324

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

2829
public void Release(bool hotReload)
2930
{
30-
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
31+
if (Config.GeneralSettings.LoadMessages)
32+
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
3133
}
3234
}
3335
}

K4-System/src/Module/ModuleTime.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public void Initialize(bool hotReload)
1717
this.plugin = (pluginContext.Plugin as Plugin)!;
1818
this.Config = plugin.Config;
1919

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

2223
//** ? Register Module Parts */
2324

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

2829
public void Release(bool hotReload)
2930
{
30-
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
31+
if (Config.GeneralSettings.LoadMessages)
32+
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
3133
}
3234
}
3335
}

K4-System/src/Module/ModuleUtils.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public void Initialize(bool hotReload)
1717
this.plugin = (pluginContext.Plugin as Plugin)!;
1818
this.Config = plugin.Config;
1919

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

2223
//** ? Register Module Parts */
2324

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

2728
public void Release(bool hotReload)
2829
{
29-
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
30+
if (Config.GeneralSettings.LoadMessages)
31+
this.Logger.LogInformation("Releasing '{0}'", this.GetType().Name);
3032
}
3133
}
3234
}

K4-System/src/Module/Rank/RankCommands.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,6 @@ public void OnCommandTop(CCSPlayerController? player, CommandInfo info)
200200
printCount = Math.Clamp(parsedInt, 1, 25);
201201
}
202202

203-
Logger.LogInformation($"Player {k4player.PlayerName} requested top {printCount} players.");
204-
205203
Task.Run(async () =>
206204
{
207205
Console.WriteLine("Saving all players data");

K4-System/src/Module/Rank/RankEvents.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,49 @@ public void Initialize_Events()
150150
return HookResult.Continue;
151151
}, HookMode.Post);
152152

153+
plugin.RegisterEventHandler((EventRoundPrestart @event, GameEventInfo info) =>
154+
{
155+
if (Config.RankSettings.RankBasedTeamBalance)
156+
{
157+
int team1Size = plugin.K4Players.Count(p => p.Controller.Team == CsTeam.Terrorist);
158+
int team2Size = plugin.K4Players.Count(p => p.Controller.Team == CsTeam.CounterTerrorist);
159+
160+
if (Math.Abs(team1Size - team2Size) > 1)
161+
{
162+
var team1Players = plugin.K4Players.Where(p => p.Controller.Team == CsTeam.Terrorist).ToList();
163+
var team2Players = plugin.K4Players.Where(p => p.Controller.Team == CsTeam.CounterTerrorist).ToList();
164+
165+
var team1RankPoints = team1Players.Select(p => p.rankData?.Points ?? 0).Sum();
166+
var team2RankPoints = team2Players.Select(p => p.rankData?.Points ?? 0).Sum();
167+
168+
while (Math.Abs(team1RankPoints - team2RankPoints) > Config.RankSettings.RankBasedTeamBalanceMaxDifference)
169+
{
170+
if (team1RankPoints > team2RankPoints)
171+
{
172+
var playerToSwitch = team1Players.OrderByDescending(p => p.rankData?.Points ?? 0).First();
173+
team1Players.Remove(playerToSwitch);
174+
team2Players.Add(playerToSwitch);
175+
playerToSwitch.Controller.ChangeTeam(CsTeam.CounterTerrorist);
176+
}
177+
else
178+
{
179+
var playerToSwitch = team2Players.OrderByDescending(p => p.rankData?.Points ?? 0).First();
180+
team2Players.Remove(playerToSwitch);
181+
team1Players.Add(playerToSwitch);
182+
playerToSwitch.Controller.ChangeTeam(CsTeam.Terrorist);
183+
}
184+
185+
team1RankPoints = team1Players.Select(p => p.rankData?.Points ?? 0).Sum();
186+
team2RankPoints = team2Players.Select(p => p.rankData?.Points ?? 0).Sum();
187+
}
188+
189+
Server.PrintToChatAll($" {plugin.Localizer["k4.general.prefix"]} {plugin.Localizer["k4.ranks.rank.teamsbalanced"]}");
190+
}
191+
}
192+
193+
return HookResult.Continue;
194+
}, HookMode.Post);
195+
153196
plugin.RegisterEventHandler((EventBombPlanted @event, GameEventInfo info) =>
154197
{
155198
ModifyPlayerPointsConnector(@event.Userid, Config.PointSettings.BombPlant, "k4.phrases.bombplanted");

K4-System/src/Module/Rank/RankFunctions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public Rank GetPlayerRank(int points)
8585
return rankDictionary.LastOrDefault(kv => points >= kv.Value.Point).Value ?? noneRank!;
8686
}
8787

88-
public void ModifyPlayerPointsConnector(CCSPlayerController player, int amount, string reason, string? extraInfo = null)
88+
public void ModifyPlayerPointsConnector(CCSPlayerController? player, int amount, string reason, string? extraInfo = null)
8989
{
9090
K4Player? k4player = plugin.GetK4Player(player);
9191
if (k4player is null)
@@ -264,6 +264,14 @@ public void SetPlayerClanTag(K4Player k4player)
264264
}
265265
}
266266

267+
if (Config.RankSettings.DisplayToplistPlacement && k4player.rankData != null)
268+
{
269+
if (k4player.rankData.TopPlacement != 0 && k4player.rankData.TopPlacement <= Config.RankSettings.DisplayToplistPlacementMax)
270+
{
271+
tag = $"{Config.RankSettings.DisplayToplistPlacementTag.Replace("{placement}", k4player.rankData.TopPlacement.ToString())} {tag}";
272+
}
273+
}
274+
267275
if (Config.RankSettings.CountryTagEnabled)
268276
{
269277
string countryTag = GetPlayerCountryCode(k4player.Controller);

K4-System/src/Module/Rank/RankGlobals.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class RankData
3030
public required int RoundPoints { get; set; }
3131
public required bool HideAdminTag { get; set; }
3232
public required bool MuteMessages { get; set; }
33+
public int TopPlacement { get; set; }
3334
}
3435

3536
public required Plugin plugin;

0 commit comments

Comments
 (0)