66using System . Linq ;
77using System . Threading ;
88using System . Threading . Tasks ;
9- using Microsoft . EntityFrameworkCore ;
109using Microsoft . Extensions . Configuration ;
1110using Microsoft . Extensions . Logging ;
1211using Npgsql ;
@@ -18,52 +17,50 @@ namespace OoLunar.CookieClicker
1817{
1918 public sealed class CookieTracker : IAsyncDisposable
2019 {
21- private readonly Dictionary < Ulid , CachedCookie > UnbakedCookies = new ( ) ;
22- private readonly CookieDatabaseContext DatabaseContext ;
20+ private readonly Dictionary < Ulid , CachedCookie > _unbakedCookies = new ( ) ;
21+ private readonly NpgsqlConnection _databaseConnection ;
2322 private readonly ILogger < CookieTracker > _logger ;
24- private readonly SemaphoreSlim Semaphore = new ( 1 , 1 ) ;
25- private readonly PeriodicTimer Timer ;
26- private readonly Task BakingTask ;
27- private readonly FrozenDictionary < DatabaseOperation , NpgsqlCommand > DatabaseCommands ;
23+ private readonly SemaphoreSlim _semaphore = new ( 1 , 1 ) ;
24+ private readonly PeriodicTimer _timer ;
25+ private readonly Task _bakingTask ;
26+ private readonly FrozenDictionary < DatabaseOperation , NpgsqlCommand > _databaseCommands ;
2827
29- public CookieTracker ( CookieDatabaseContext databaseContext , IConfiguration configuration , ILogger < CookieTracker > logger )
28+ public CookieTracker ( IConfiguration configuration , ILogger < CookieTracker > logger )
3029 {
31- ArgumentNullException . ThrowIfNull ( databaseContext , nameof ( databaseContext ) ) ;
3230 ArgumentNullException . ThrowIfNull ( configuration , nameof ( configuration ) ) ;
3331 ArgumentNullException . ThrowIfNull ( logger , nameof ( logger ) ) ;
3432
3533 _logger = logger ;
36- Timer = new PeriodicTimer ( TimeSpan . FromSeconds ( configuration . GetValue ( "CookieTracker:Period" , 30 ) ) ) ;
34+ _timer = new PeriodicTimer ( TimeSpan . FromSeconds ( configuration . GetValue ( "CookieTracker:Period" , 30 ) ) ) ;
3735
38- DatabaseContext = databaseContext ;
39- NpgsqlConnection connection = ( NpgsqlConnection ) DatabaseContext . Database . GetDbConnection ( ) ;
40- connection . Open ( ) ;
41- DatabaseCommands = new Dictionary < DatabaseOperation , NpgsqlCommand >
36+ _databaseConnection = NpgsqlDataSource . Create ( CookieDatabaseContext . GetConnectionString ( configuration ) ) . CreateConnection ( ) ;
37+ _databaseConnection . Open ( ) ;
38+ _databaseCommands = new Dictionary < DatabaseOperation , NpgsqlCommand >
4239 {
43- [ DatabaseOperation . Create ] = GetInsertCommand ( connection ) ,
44- [ DatabaseOperation . Read ] = GetSelectCommand ( connection ) ,
45- [ DatabaseOperation . Update ] = GetUpdateCommand ( connection ) ,
46- [ DatabaseOperation . Delete ] = GetDeleteCommand ( connection )
40+ [ DatabaseOperation . Create ] = GetInsertCommand ( _databaseConnection ) ,
41+ [ DatabaseOperation . Read ] = GetSelectCommand ( _databaseConnection ) ,
42+ [ DatabaseOperation . Update ] = GetUpdateCommand ( _databaseConnection ) ,
43+ [ DatabaseOperation . Delete ] = GetDeleteCommand ( _databaseConnection )
4744 } . ToFrozenDictionary ( ) ;
4845
49- BakingTask = StartBakingAsync ( ) ;
46+ _bakingTask = StartBakingAsync ( ) ;
5047 }
5148
5249 public void CreateCookie ( Cookie cookie )
5350 {
5451 ArgumentNullException . ThrowIfNull ( cookie , nameof ( cookie ) ) ;
55- UnbakedCookies . Add ( cookie . Id , new ( cookie , false ) ) ;
52+ _unbakedCookies . Add ( cookie . Id , new ( cookie , false ) ) ;
5653 }
5754
5855 public ulong Click ( Ulid cookieId )
5956 {
6057 // Check if the cookie is in the cache. If it isn't, pull it from the database.
61- if ( ! UnbakedCookies . TryGetValue ( cookieId , out CachedCookie ? unbakedCookie ) )
58+ if ( ! _unbakedCookies . TryGetValue ( cookieId , out CachedCookie ? unbakedCookie ) )
6259 {
63- Semaphore . Wait ( ) ;
60+ _semaphore . Wait ( ) ;
6461 try
6562 {
66- DbCommand command = DatabaseCommands [ DatabaseOperation . Read ] ;
63+ DbCommand command = _databaseCommands [ DatabaseOperation . Read ] ;
6764 command . Parameters [ 0 ] . Value = cookieId . ToGuid ( ) ;
6865 using DbDataReader reader = command . ExecuteReader ( CommandBehavior . SingleRow ) ;
6966 if ( ! reader . Read ( ) )
@@ -76,11 +73,11 @@ public ulong Click(Ulid cookieId)
7673 Id = new Ulid ( reader . GetFieldValue < Guid > ( 0 ) ) ,
7774 Clicks = ( ulong ) reader . GetFieldValue < decimal > ( 1 )
7875 } , true ) ;
79- UnbakedCookies . Add ( cookieId , unbakedCookie ) ;
76+ _unbakedCookies . Add ( cookieId , unbakedCookie ) ;
8077 }
8178 finally
8279 {
83- Semaphore . Release ( ) ;
80+ _semaphore . Release ( ) ;
8481 }
8582 }
8683
@@ -90,18 +87,18 @@ public ulong Click(Ulid cookieId)
9087
9188 private async Task StartBakingAsync ( )
9289 {
93- DbCommand createCommand = DatabaseCommands [ DatabaseOperation . Create ] ;
94- DbCommand updateCommand = DatabaseCommands [ DatabaseOperation . Update ] ;
95- foreach ( DbCommand command in DatabaseCommands . Values )
90+ DbCommand createCommand = _databaseCommands [ DatabaseOperation . Create ] ;
91+ DbCommand updateCommand = _databaseCommands [ DatabaseOperation . Update ] ;
92+ foreach ( DbCommand command in _databaseCommands . Values )
9693 {
9794 await command . PrepareAsync ( ) ;
9895 }
9996
100- while ( await Timer . WaitForNextTickAsync ( ) )
97+ while ( await _timer . WaitForNextTickAsync ( ) )
10198 {
102- await Semaphore . WaitAsync ( ) ;
103- CachedCookie [ ] unbakedCookies = UnbakedCookies . Values . ToArray ( ) ;
104- Semaphore . Release ( ) ;
99+ await _semaphore . WaitAsync ( ) ;
100+ CachedCookie [ ] unbakedCookies = _unbakedCookies . Values . ToArray ( ) ;
101+ _semaphore . Release ( ) ;
105102
106103 List < Guid > updatedCookieIds = new ( ) ;
107104 List < decimal > updatedCookieCount = new ( ) ;
@@ -120,7 +117,7 @@ private async Task StartBakingAsync()
120117 }
121118
122119 // Remove the cookie from the unbaked cookies dictionary. Prefer the returned result over the current result.
123- if ( ! UnbakedCookies . Remove ( cookie . Cookie . Id , out CachedCookie ? unbakedCookie ) )
120+ if ( ! _unbakedCookies . Remove ( cookie . Cookie . Id , out CachedCookie ? unbakedCookie ) )
124121 {
125122 unbakedCookie = cookie ;
126123 }
@@ -143,7 +140,7 @@ private async Task StartBakingAsync()
143140 continue ;
144141 }
145142
146- await Semaphore . WaitAsync ( ) ;
143+ await _semaphore . WaitAsync ( ) ;
147144 try
148145 {
149146 if ( newCookieIds . Count != 0 )
@@ -162,17 +159,17 @@ private async Task StartBakingAsync()
162159 }
163160 finally
164161 {
165- Semaphore . Release ( ) ;
162+ _semaphore . Release ( ) ;
166163 }
167164 }
168165 }
169166
170167 public async ValueTask DisposeAsync ( )
171168 {
172- Timer . Dispose ( ) ;
173- await BakingTask ;
174- await DatabaseContext . DisposeAsync ( ) ;
175- Semaphore . Dispose ( ) ;
169+ _timer . Dispose ( ) ;
170+ await _bakingTask ;
171+ await _databaseConnection . DisposeAsync ( ) ;
172+ _semaphore . Dispose ( ) ;
176173 }
177174
178175 private static NpgsqlCommand GetSelectCommand ( NpgsqlConnection connection )
0 commit comments