Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现对材料统计页面中的材料做初步排序。 #1510

Merged
merged 4 commits into from
Mar 28, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ private async ValueTask UpdateStatisticsItemsAsync()
{
CultivationMetadataContext context = await metadataService.GetContextAsync<CultivationMetadataContext>().ConfigureAwait(false);
statistics = await cultivationService.GetStatisticsCultivateItemCollectionAsync(SelectedProject, context, token).ConfigureAwait(false);
if (statistics is not null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既然在获取statistics后立即进行了排序,为什么没有考虑直接在Service里面排序完了再赋值呢

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不敢动,怕出现隐藏bug;此外,后续个人希望按照观测枢的数据分类再排序,这一次排序做的事就有点多了,考虑一个函数只做一件事,我就没有直接改service本身的实现,保证service的返回值是绝对原始的返回值

不过你说没问题的话,就可以改

{
statistics = this.SortStatistics(statistics);
}
}
catch (OperationCanceledException)
{
Expand All @@ -192,6 +196,35 @@ private async ValueTask UpdateStatisticsItemsAsync()
}
}

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)
{
Expand Down
Loading