mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
code style
This commit is contained in:
@@ -200,6 +200,13 @@ internal static partial class EnumerableExtension
|
||||
return list;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
||||
public static List<TSource> SortBy<TSource, TKey>(this List<TSource> list, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
list.Sort((left, right) => comparer.Compare(keySelector(left), keySelector(right)));
|
||||
return list;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
||||
public static List<TSource> SortByDescending<TSource, TKey>(this List<TSource> list, Func<TSource, TKey> keySelector)
|
||||
where TKey : IComparable
|
||||
@@ -207,4 +214,11 @@ internal static partial class EnumerableExtension
|
||||
list.Sort((left, right) => keySelector(right).CompareTo(keySelector(left)));
|
||||
return list;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
|
||||
public static List<TSource> SortByDescending<TSource, TKey>(this List<TSource> list, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
|
||||
{
|
||||
list.Sort((left, right) => comparer.Compare(keySelector(right), keySelector(left)));
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -126,10 +126,7 @@ internal sealed partial class CultivationService : ICultivationService
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
return resultItems
|
||||
.OrderByDescending(i => i.TotalCount)
|
||||
.ThenByDescending(i => i.Count)
|
||||
.ToObservableCollection();
|
||||
return resultItems.SortBy(item => item.Inner.Id, MaterialIdComparer.Shared).ToObservableCollection();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Snap.Hutao.Model.Primitive;
|
||||
using Snap.Hutao.ViewModel.Cultivation;
|
||||
|
||||
namespace Snap.Hutao.Service.Cultivation;
|
||||
|
||||
internal sealed class MaterialIdComparer : IComparer<MaterialId>
|
||||
{
|
||||
private static readonly Lazy<MaterialIdComparer> LazyShared = new(() => new());
|
||||
|
||||
public static MaterialIdComparer Shared { get => LazyShared.Value; }
|
||||
|
||||
public int Compare(MaterialId x, MaterialId y)
|
||||
{
|
||||
return Transform(x).CompareTo(Transform(y));
|
||||
}
|
||||
|
||||
private static uint Transform(MaterialId value)
|
||||
{
|
||||
return value.Value switch
|
||||
{
|
||||
// 摩拉
|
||||
202U => 0U,
|
||||
|
||||
// 经验
|
||||
104001U => 1U,
|
||||
104002U => 2U,
|
||||
104003U => 3U,
|
||||
|
||||
// 魔矿
|
||||
104011U => 4U,
|
||||
104012U => 5U,
|
||||
104013U => 6U,
|
||||
|
||||
_ => value,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using Snap.Hutao.Model.Metadata.Avatar;
|
||||
using Snap.Hutao.Model.Metadata.Item;
|
||||
using Snap.Hutao.Model.Metadata.Weapon;
|
||||
using Snap.Hutao.Model.Primitive;
|
||||
using Snap.Hutao.Service.Cultivation;
|
||||
|
||||
namespace Snap.Hutao.Service.Metadata.ContextAbstraction;
|
||||
|
||||
@@ -67,7 +68,7 @@ internal static class MetadataServiceContextExtension
|
||||
#pragma warning disable SH002
|
||||
public static IEnumerable<Material> EnumerateInventoryMaterial(this IMetadataListMaterialSource context)
|
||||
{
|
||||
return context.Materials.Where(m => m.IsInventoryItem()).OrderBy(m => m.Id.Value);
|
||||
return context.Materials.Where(m => m.IsInventoryItem()).OrderBy(m => m.Id, MaterialIdComparer.Shared);
|
||||
}
|
||||
|
||||
public static Avatar GetAvatar(this IMetadataDictionaryIdAvatarSource context, AvatarId id)
|
||||
|
||||
@@ -181,10 +181,6 @@ internal sealed partial class CultivationViewModel : Abstraction.ViewModel
|
||||
{
|
||||
CultivationMetadataContext context = await metadataService.GetContextAsync<CultivationMetadataContext>().ConfigureAwait(false);
|
||||
statistics = await cultivationService.GetStatisticsCultivateItemCollectionAsync(SelectedProject, context, token).ConfigureAwait(false);
|
||||
if (statistics is not null)
|
||||
{
|
||||
statistics = this.SortStatistics(statistics);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
@@ -196,35 +192,6 @@ internal sealed partial class CultivationViewModel : Abstraction.ViewModel
|
||||
}
|
||||
}
|
||||
|
||||
private ObservableCollection<StatisticsCultivateItem> SortStatistics(ObservableCollection<StatisticsCultivateItem> statistics)
|
||||
{
|
||||
return statistics.Order(new StatisticsCaltivateItemComparer()).ToObservableCollection();
|
||||
}
|
||||
|
||||
private class StatisticsCaltivateItemComparer: IComparer<StatisticsCultivateItem>
|
||||
{
|
||||
public int Compare(StatisticsCultivateItem? x, StatisticsCultivateItem? y)
|
||||
{
|
||||
// TODO: 理论上的最优解:先通过观测枢获取所有背包物品,然后根据filter字段依次分类,先按这个类别做排序,然后再按品质等进行排序
|
||||
// 不仅如此,以后想按照材料类型分类的话,这也是必做的。
|
||||
|
||||
// 对null做判定,防止IDE警告
|
||||
if (x is null) { return -1; }
|
||||
if (y is null) { return -1; }
|
||||
|
||||
// 摩拉、矿、经验书全局只出现一次,放在最前面
|
||||
if (x.Inner.Id.Value == 202U) { return -1; } // 摩拉
|
||||
if (y.Inner.Id.Value == 202U) { return 1; }
|
||||
if (x.Inner.Id.Value == 104013U) { return -1; } // 精锻用魔矿
|
||||
if (y.Inner.Id.Value == 104013U) { return 1; }
|
||||
if (x.Inner.Id.Value == 104003U) { return -1; } // 大英雄的经验
|
||||
if (y.Inner.Id.Value == 104003U) { return 1; }
|
||||
|
||||
// 剩下的物品暂时按照id排序,更细致的排序策略以后再说
|
||||
return (int)x.Inner.Id.Value - (int)y.Inner.Id.Value;
|
||||
}
|
||||
}
|
||||
|
||||
[Command("NavigateToPageCommand")]
|
||||
private void NavigateToPage(string? typeString)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user