-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
SortMethods.cs
120 lines (93 loc) · 3.33 KB
/
SortMethods.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using BetterSongList.Interfaces;
using BetterSongList.SortModels;
using BetterSongList.Util;
using SongDetailsCache.Structs;
using System;
using System.Collections.Generic;
namespace BetterSongList {
public static class SortMethods {
public static readonly ISorter alphabeticalSongname = new ComparableFunctionSorterWithLegend(
(songa, songb) => string.Compare(songa.songName, songb.songName),
song => song.songName.Length > 0 ? song.songName.Substring(0, 1) : null
);
public static readonly ISorter bpm = new PrimitiveFunctionSorterWithLegend(
song => song.beatsPerMinute,
song => Math.Round(song.beatsPerMinute).ToString()
);
public static readonly ISorter alphabeticalMapper = new ComparableFunctionSorterWithLegend(
(songa, songb) => string.Compare(songa.levelAuthorName, songb.levelAuthorName),
song => song.levelAuthorName.Length > 0 ? song.levelAuthorName.Substring(0, 1) : null
);
public static readonly ISorter downloadTime = new FolderDateSorter();
internal static float? StarsProcessor(object xx) {
var x = (Song)xx;
if(x.rankedStatus != RankedStatus.Ranked)
return null;
float ret = 0;
for(var i = (int)x.diffOffset; i < x.diffOffset + x.diffCount; i++) {
var diff = SongDetailsUtil.songDetails.instance.difficulties[i];
if(diff.stars == 0)
continue;
if(ret == 0) {
ret = diff.stars;
continue;
}
if(Config.Instance.SortAsc) {
if(ret < diff.stars)
continue;
} else if(ret > diff.stars) {
continue;
}
ret = diff.stars;
}
return ret == 0 ? (float?)null : ret;
}
public static readonly ISorter stars = new BasicSongDetailsSorterWithLegend(StarsProcessor, x => {
var y = StarsProcessor((Song)x);
if(y == null)
return null;
return ((float)y).ToString("0.0");
});
const float funnyOptim = 1 / 60f;
public static readonly ISorter songLength = new PrimitiveFunctionSorterWithLegend(
song => song.songDuration,
song => (song.songDuration < 60 ? "<1" : Math.Round(song.songDuration * funnyOptim).ToString()) + " min"
);
static int GetQuarter(DateTime date) {
if(date.Month > 9)
return 4;
else if(date.Month > 6)
return 3;
else if(date.Month > 3)
return 2;
return 1;
}
public static readonly ISorter beatSaverDate = new BasicSongDetailsSorterWithLegend(
x => ((Song)x).uploadTimeUnix,
x => {
var d = ((Song)x).uploadTime;
return d.ToString($"Q{GetQuarter(d)} yy");
});
internal static Dictionary<string, ISorter> methods = new Dictionary<string, ISorter>() {
{ "Song Name", alphabeticalSongname },
{ "Download Date", downloadTime },
{ "Ranked Stars", stars },
{ "Song Length", songLength },
{ "BPM", bpm },
{ "BeatSaver Date", beatSaverDate },
{ "Default", null }
};
static bool Register(ITransformerPlugin sorter) {
var name = sorter.name;
if(name.Length > 20)
throw new ArgumentException("The name of the Transformer cannot exceed 20 Characters");
if(!Config.Instance.AllowPluginSortsAndFilters)
return false;
name = $"🔌{name}";
methods.Add(name, sorter);
return true;
}
public static bool RegisterPrimitiveSorter<T>(T sorter) where T : ISorterPrimitive, ITransformerPlugin => Register(sorter);
public static bool RegisterCustomSorter<T>(T sorter) where T : ISorterCustom, ITransformerPlugin => Register(sorter);
}
}