@@ -23,23 +23,17 @@ public abstract class ASortOrderVariety<TKey, TSortableItem, TItemLoadoutData, T
2323 where TItemLoadoutData : ISortItemLoadoutData < TKey >
2424 where TSortedEntry : ISortItemData < TKey >
2525{
26- private readonly ILogger _logger ;
26+ private readonly ILogger < ASortOrderVariety < TKey , TSortableItem , TItemLoadoutData , TSortedEntry > > _logger ;
2727
2828 /// <summary>
2929 /// Database connection
3030 /// </summary>
3131 protected readonly IConnection Connection ;
32-
33- /// <summary>
34- /// The game sort order manager that this variety belongs to.
35- /// </summary>
36- protected readonly ISortOrderManager Manager ;
37-
38- protected ASortOrderVariety ( IServiceProvider serviceProvider , ISortOrderManager manager )
32+
33+ protected ASortOrderVariety ( IServiceProvider serviceProvider )
3934 {
4035 Connection = serviceProvider . GetRequiredService < IConnection > ( ) ;
41- _logger = serviceProvider . GetRequiredService < ILogger > ( ) ;
42- Manager = manager ;
36+ _logger = serviceProvider . GetRequiredService < ILogger < ASortOrderVariety < TKey , TSortableItem , TItemLoadoutData , TSortedEntry > > > ( ) ;
4337 }
4438
4539#region public members
@@ -195,19 +189,20 @@ public async ValueTask MoveItemDelta(SortOrderId sortOrderId, TKey sourceItem, i
195189 }
196190
197191 /// <inheritdoc />
198- public virtual async ValueTask ReconcileSortOrder ( SortOrderId sortOrderId , IDb ? db = null , CancellationToken token = default )
192+ public virtual async ValueTask ReconcileSortOrder ( SortOrderId sortOrderId , IDb ? referenceDb = null , CancellationToken token = default )
199193 {
200194 // If this is passed a specific database, don't retry the reconciliation using the most recent database.
201- var noRetry = db is not null ;
195+ var noRetry = referenceDb is not null ;
202196 var retryCount = 0 ;
203- var dbToUse = db ?? Connection . Db ;
204197
205198 while ( retryCount <= 3 )
206199 {
207- var reconciledItems = ReconcileSortOrderCore ( sortOrderId , dbToUse ) ;
200+ var refDb = referenceDb ?? Connection . Db ;
208201
209- var succeded = await TryPersistSortOrder ( sortOrderId , reconciledItems . Select ( tuple => tuple . SortedEntry ) . ToArray ( ) , dbToUse , token ) ;
210- if ( succeded ) return ;
202+ var reconciledItems = ReconcileSortOrderCore ( sortOrderId , refDb ) ;
203+
204+ var succeeded = await TryPersistSortOrder ( sortOrderId , reconciledItems . Select ( tuple => tuple . SortedEntry ) . ToArray ( ) , refDb , token ) ;
205+ if ( succeeded ) return ;
211206
212207 if ( noRetry )
213208 {
@@ -222,6 +217,30 @@ public virtual async ValueTask ReconcileSortOrder(SortOrderId sortOrderId, IDb?
222217 _logger . LogError ( "Reconciliation of sort order {SortOrderId} failed after 4 attempts" , sortOrderId ) ;
223218 }
224219
220+ /// <inheritdoc />
221+ public virtual async ValueTask DeleteSortOrder ( SortOrderId sortOrderId , CancellationToken token = default )
222+ {
223+ using var tx = Connection . BeginTransaction ( ) ;
224+
225+ // Delete the items
226+ foreach ( var item in Connection . Db . Datoms ( SortOrderItem . ParentSortOrder , sortOrderId ) )
227+ {
228+ tx . Delete ( item . E , recursive : false ) ;
229+ }
230+
231+ // Delete the sort order
232+ tx . Delete ( sortOrderId , recursive : false ) ;
233+
234+ try
235+ {
236+ await tx . Commit ( ) ;
237+ }
238+ catch ( Exception ex )
239+ {
240+ _logger . LogError ( ex , "Failed to delete sort order {SortOrderId}" , sortOrderId ) ;
241+ }
242+ }
243+
225244
226245#endregion public members
227246
@@ -285,17 +304,20 @@ protected abstract void PersistSortOrderCore(
285304 CancellationToken token = default ) ;
286305
287306
288- protected virtual IReadOnlyList < ( TSortedEntry SortedEntry , TItemLoadoutData ItemLoadoutData ) > ReconcileSortOrderCore ( SortOrderId sortOrderId , IDb dbToUse )
307+ protected virtual IReadOnlyList < ( TSortedEntry SortedEntry , TItemLoadoutData ItemLoadoutData ) > ReconcileSortOrderCore ( SortOrderId sortOrderId , IDb loadoutRevisionDb )
289308 {
290- var sortOrder = SortOrder . Load ( dbToUse , sortOrderId ) ;
309+ // Get the most recent sort order data
310+ var sortOrder = SortOrder . Load ( loadoutRevisionDb . Connection . Db , sortOrderId ) ;
291311
292312 var collectionGroupId = sortOrder . ParentEntity . IsT1 ?
293313 sortOrder . ParentEntity . AsT1 :
294314 Optional < CollectionGroupId > . None ;
295315
296- var loadoutData = RetrieveLoadoutData ( sortOrder . LoadoutId , collectionGroupId , dbToUse ) ;
316+ // Get the loadout data from the revision db
317+ var loadoutData = RetrieveLoadoutData ( sortOrder . LoadoutId , collectionGroupId , loadoutRevisionDb ) ;
297318
298- var currentSortOrder = RetrieveSortOrder ( sortOrderId , dbToUse ) ;
319+ // Get the most recent sort order data
320+ var currentSortOrder = RetrieveSortOrder ( sortOrderId , loadoutRevisionDb . Connection . Db ) ;
299321
300322 var reconciledItems = Reconcile ( currentSortOrder , loadoutData ) ;
301323 return reconciledItems ;
@@ -304,6 +326,7 @@ protected abstract void PersistSortOrderCore(
304326 /// <summary>
305327 /// Returns a collection of loadout-specific TItemLoadoutData for each relevant item found in the provided loadout/collection.
306328 /// These loadout data items are unsorted and do not have a sort index.
329+ /// This already filters out duplicates, currently selecting the most recently added enabled item for each key.
307330 /// </summary>
308331 protected abstract IReadOnlyList < TItemLoadoutData > RetrieveLoadoutData (
309332 LoadoutId loadoutId ,
0 commit comments