@@ -48,6 +48,28 @@ public override async Task OnActivateAsync(CancellationToken ct)
4848 _raffleFinished = _series . IsRaffleFinished ;
4949 }
5050
51+ public override async Task OnDeactivateAsync ( DeactivationReason reason , CancellationToken ct )
52+ {
53+ _raffleTimer ? . Dispose ( ) ;
54+ _raffleTimer = null ;
55+
56+ if ( _currentBatchEntries . Count > 0 )
57+ {
58+ try
59+ {
60+ await ExecuteRaffleAsync ( ) ;
61+ }
62+ catch ( Exception ex )
63+ {
64+ logger . LogError (
65+ ex ,
66+ "Failed to execute raffle during deactivation for series {SeriesId}" ,
67+ this . GetPrimaryKeyLong ( )
68+ ) ;
69+ }
70+ }
71+ }
72+
5173 public async Task < LtdRaffleEntryResult > EnterRaffleAsync ( int playerId , CancellationToken ct )
5274 {
5375 if ( _series is not { IsAvailable : true } )
@@ -164,6 +186,7 @@ private async Task ExecuteRaffleAsync()
164186 _currentBatchEntries . Clear ( ) ;
165187 _isInBufferPeriod = false ;
166188 _raffleFinished = true ;
189+ _raffleTimer ? . Dispose ( ) ;
167190 _raffleTimer = null ;
168191
169192 await PersistFinishedAsync ( ) ;
@@ -174,12 +197,31 @@ private async Task ExecuteRaffleAsync()
174197 ? [ .. entries . OrderBy ( _ => Random . Shared . Next ( ) ) . Take ( winnersCount ) . Select ( e => e . Key ) ]
175198 : SelectWeighted ( entries , winnersCount ) ;
176199
200+ var loserIds = new List < int > ( ) ;
201+
177202 foreach ( var entry in entries )
178203 {
179204 if ( winners . Contains ( entry . Key ) )
180205 await TryFinalizeWinnerAsync ( entry . Key , batchId , true ) ;
181206 else
207+ {
208+ loserIds . Add ( entry . Key ) ;
182209 await NotifyLoserAsync ( entry . Key , LtdRaffleResultCode . Lost ) ;
210+ }
211+ }
212+
213+ if ( loserIds . Count > 0 )
214+ {
215+ await using var db = await dbCtxFactory . CreateDbContextAsync ( CancellationToken . None ) ;
216+
217+ await db
218+ . LtdRaffleEntries . Where ( e =>
219+ e . BatchId == batchId && loserIds . Contains ( e . PlayerEntityId )
220+ )
221+ . ExecuteUpdateAsync ( u =>
222+ u . SetProperty ( e => e . Result , "lost" )
223+ . SetProperty ( e => e . ProcessedAt , DateTime . UtcNow )
224+ ) ;
183225 }
184226
185227 await ReloadSeriesAsync ( CancellationToken . None ) ;
0 commit comments