@@ -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}
0 commit comments