-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiffComparer.cs
178 lines (148 loc) · 6.27 KB
/
DiffComparer.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System.Diagnostics.CodeAnalysis;
using System.Text;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
using osu.Game.IO;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using Spectre.Console;
namespace diffcalc_comparer;
public class DiffComparer
{
private readonly DiffComparerOptions options;
public DiffComparer(DiffComparerOptions options)
{
this.options = options;
}
public void Compare()
{
var beatmaps = loadBeatmaps().ToArray();
var rows = new List<BeatmapCalculationResult>();
var table = new Table();
table.AddColumn("Title");
table.AddColumn(new TableColumn("SR (A)").Centered());
table.AddColumn(new TableColumn("SR (B)").Centered());
table.AddColumn(new TableColumn("DIFF").Centered());
table.AddColumn(new TableColumn("AP (A)").Centered());
table.AddColumn(new TableColumn("AP (B)").Centered());
table.AddColumn(new TableColumn("RX (A)").Centered());
table.AddColumn(new TableColumn("RX (B)").Centered());
AnsiConsole.Progress()
.Start(ctx =>
{
var sraTask = ctx.AddTask("Calculate star ratings (Variant A)", true, beatmaps.Length);
var srbTask = ctx.AddTask("Calculate star ratings (Variant B)", true, beatmaps.Length);
foreach (var beatmap in beatmaps)
{
var srA = getStarRatings(beatmap, Env.RulesetA);
sraTask.Increment(1);
var srB = getStarRatings(beatmap, Env.RulesetB);
srbTask.Increment(1);
rows.Add(new BeatmapCalculationResult
{
Beatmap = beatmap,
SRA = srA.sr,
SRB = srB.sr,
Diff = srB.sr - srA.sr,
APA = srA.ap,
APB = srB.ap,
RXA = srA.rx,
RXB = srB.rx
});
}
});
var orderedRows = rows.OrderByDescending(r => r.Diff).ToArray();
foreach (var row in orderedRows)
table.AddRow(row.Beatmap.ToString().EscapeMarkup(),
row.SRA.ToString("N"), row.SRB.ToString("N"),
row.Diff < 0 ? row.Diff.ToString("N") : "+" + row.Diff.ToString("N"),
row.APA.HasValue ? row.APA.Value.ToString("N") : "-", row.APB.HasValue ? row.APB.Value.ToString("N") : "-",
row.RXA.HasValue ? row.RXA.Value.ToString("N") : "-", row.RXB.HasValue ? row.RXB.Value.ToString("N") : "-");
AnsiConsole.Write(table);
if (string.IsNullOrEmpty(options.ExportPath))
return;
var sb = new StringBuilder();
sb.AppendLine("| Title | SR (A) | SR (B) | DIFF | AP (A) | AP (B) | RX (A) | RX (B) |");
sb.AppendLine("|-------|:------:|:------:|:----:|:------:|:------:|:------:|:------:|");
foreach (var row in orderedRows)
{
var title = options.IncludeUrl.HasValue && options.IncludeUrl.Value
? $"[{row.Beatmap}](https://osu.ppy.sh/b/{row.Beatmap.BeatmapInfo.OnlineID})"
: row.Beatmap.ToString();
sb.AppendLine(
$"| {title} " +
$"| {row.SRA.ToString("N")} " +
$"| {row.SRB.ToString("N")} " +
$"| {(row.Diff < 0 ? row.Diff.ToString("N") : "+" + row.Diff.ToString("N"))} " +
$"| {(row.APA.HasValue ? row.APA.Value.ToString("N") : "-")} " +
$"| {(row.APB.HasValue ? row.APB.Value.ToString("N") : "-")} " +
$"| {(row.RXA.HasValue ? row.RXA.Value.ToString("N") : "-")} " +
$"| {(row.RXB.HasValue ? row.RXB.Value.ToString("N") : "-")} |");
}
File.WriteAllText(options.ExportPath, sb.ToString());
}
private static (double sr, double? ap, double? rx) getStarRatings(Beatmap beatmap, Ruleset ruleset)
{
var convert = ruleset.CreateBeatmapConverter(beatmap).Convert();
var calculator = ruleset.CreateDifficultyCalculator(new CalcWorkingBeatmap(ruleset, convert));
var sr = calculator.Calculate().StarRating;
double? ap = null!;
double? rx = null!;
try
{
var autoplayMod = ruleset.AllMods.First(m => m.Name == "Autopilot") as Mod;
ap = calculator.Calculate(new[] { autoplayMod }).StarRating;
}
catch
{
// ignore
}
try
{
var relaxMod = ruleset.AllMods.First(m => m.GetType().IsSubclassOf(typeof(ModRelax))) as Mod;
rx = calculator.Calculate(new[] { relaxMod }).StarRating;
}
catch
{
// ignore
}
return (sr, ap, rx);
}
private static IEnumerable<Beatmap> loadBeatmaps()
{
var mapsPath = Path.Combine(Directory.GetParent(Environment.CurrentDirectory)!.Parent!.Parent!.FullName, "maps");
var beatmaps = new List<Beatmap>();
AnsiConsole.Progress()
.Start(ctx =>
{
var length = Directory.GetFiles(mapsPath).Length;
var task = ctx.AddTask("Parse osu beatmaps", true, length);
foreach (var file in Directory.GetFiles(mapsPath))
{
if (file == ".gitignore" || !file.EndsWith(".osu"))
continue;
var legacyDecoder = new LegacyBeatmapDecoder();
beatmaps.Add(legacyDecoder.Decode(new LineBufferedReader(File.Open(Path.Combine(mapsPath, file), FileMode.Open))));
task.Increment(1);
}
});
return beatmaps;
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
private class BeatmapCalculationResult
{
public Beatmap Beatmap { get; init; } = null!;
public double SRA { get; init; }
public double SRB { get; init; }
public double Diff { get; init; }
public double? APA { get; init; }
public double? APB { get; init; }
public double? RXA { get; init; }
public double? RXB { get; init; }
}
}
public struct DiffComparerOptions
{
public string? ExportPath;
public bool? IncludeUrl;
}