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

Skip writing unranked beatmaps to osu_beatmap_difficulty_attribs #232

Merged
merged 7 commits into from
May 30, 2024
Merged
64 changes: 36 additions & 28 deletions osu.Server.DifficultyCalculator/ServerDifficultyCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

private readonly bool processConverts;
private readonly bool dryRun;
private readonly List<Ruleset> processableRulesets = new List<Ruleset>();

Check notice on line 26 in osu.Server.DifficultyCalculator/ServerDifficultyCalculator.cs

View workflow job for this annotation

GitHub Actions / Code Quality

Redundant type specification in osu.Server.DifficultyCalculator\ServerDifficultyCalculator.cs on line 26

public ServerDifficultyCalculator(int[]? rulesetIds = null, bool processConverts = true, bool dryRun = false)
{
Expand Down Expand Up @@ -67,69 +67,71 @@

public void ProcessLegacyAttributes(WorkingBeatmap beatmap) => run(beatmap, processLegacyAttributes);

private void run(WorkingBeatmap beatmap, Action<int, WorkingBeatmap, Ruleset, MySqlConnection> callback)
private void run(WorkingBeatmap beatmap, Action<ProcessableBeatmap, MySqlConnection> callback)
{
int beatmapId = beatmap.BeatmapInfo.OnlineID;

try
{
if (beatmap.Beatmap.HitObjects.Count == 0)
bool ranked;

using (var conn = Database.GetSlaveConnection())
{
using (var conn = Database.GetSlaveConnection())
ranked = conn.QuerySingleOrDefault<int>("SELECT `approved` FROM `osu_beatmaps` WHERE `beatmap_id` = @BeatmapId", new
{
if (conn.QuerySingleOrDefault<int>("SELECT `approved` FROM `osu_beatmaps` WHERE `beatmap_id` = @BeatmapId", new { BeatmapId = beatmapId }) > 0)
throw new ArgumentException($"Ranked beatmap {beatmapId} has 0 hitobjects!");
}
BeatmapId = beatmap.BeatmapInfo.OnlineID
}) > 0;

if (ranked && beatmap.Beatmap.HitObjects.Count == 0)
throw new ArgumentException($"Ranked beatmap {beatmap.BeatmapInfo.OnlineInfo} has 0 hitobjects!");
}

using (var conn = Database.GetConnection())
{
if (processConverts && beatmap.BeatmapInfo.Ruleset.OnlineID == 0)
{
foreach (var ruleset in processableRulesets)
callback(beatmapId, beatmap, ruleset, conn);
callback(new ProcessableBeatmap(beatmap, ruleset, ranked), conn);
}
else if (processableRulesets.Any(r => r.RulesetInfo.OnlineID == beatmap.BeatmapInfo.Ruleset.OnlineID))
callback(beatmapId, beatmap, beatmap.BeatmapInfo.Ruleset.CreateInstance(), conn);
callback(new ProcessableBeatmap(beatmap, beatmap.BeatmapInfo.Ruleset.CreateInstance(), ranked), conn);
}
}
catch (Exception e)
{
throw new Exception($"{beatmapId} failed with: {e.Message}");
throw new Exception($"{beatmap.BeatmapInfo.OnlineID} failed with: {e.Message}");
}
}

private void processDifficulty(int beatmapId, WorkingBeatmap beatmap, Ruleset ruleset, MySqlConnection conn)
private void processDifficulty(ProcessableBeatmap beatmap, MySqlConnection conn)
{
foreach (var attribute in ruleset.CreateDifficultyCalculator(beatmap).CalculateAllLegacyCombinations())
foreach (var attribute in beatmap.Ruleset.CreateDifficultyCalculator(beatmap.Beatmap).CalculateAllLegacyCombinations())
{
if (dryRun)
continue;

LegacyMods legacyMods = ruleset.ConvertToLegacyMods(attribute.Mods);
LegacyMods legacyMods = beatmap.Ruleset.ConvertToLegacyMods(attribute.Mods);

conn.Execute(
"INSERT INTO `osu_beatmap_difficulty` (`beatmap_id`, `mode`, `mods`, `diff_unified`) "
+ "VALUES (@BeatmapId, @Mode, @Mods, @Diff) "
+ "ON DUPLICATE KEY UPDATE `diff_unified` = @Diff",
new
{
BeatmapId = beatmapId,
Mode = ruleset.RulesetInfo.OnlineID,
BeatmapId = beatmap.BeatmapID,
Mode = beatmap.RulesetID,
Mods = (int)legacyMods,
Diff = attribute.StarRating
});

if (!AppSettings.SKIP_INSERT_ATTRIBUTES)
if (beatmap.Ranked && !AppSettings.SKIP_INSERT_ATTRIBUTES)
{
var parameters = new List<object>();

foreach (var mapping in attribute.ToDatabaseAttributes())
{
parameters.Add(new
{
BeatmapId = beatmapId,
Mode = ruleset.RulesetInfo.OnlineID,
BeatmapId = beatmap.BeatmapID,
Mode = beatmap.RulesetID,
Mods = (int)legacyMods,
Attribute = mapping.attributeId,
Value = Convert.ToSingle(mapping.value)
Expand All @@ -143,14 +145,14 @@
parameters.ToArray());
}

if (legacyMods == LegacyMods.None && ruleset.RulesetInfo.Equals(beatmap.BeatmapInfo.Ruleset))
if (legacyMods == LegacyMods.None && beatmap.Ruleset.RulesetInfo.Equals(beatmap.Beatmap.BeatmapInfo.Ruleset))
{
double beatLength = beatmap.Beatmap.GetMostCommonBeatLength();
double beatLength = beatmap.Beatmap.Beatmap.GetMostCommonBeatLength();
double bpm = beatLength > 0 ? 60000 / beatLength : 0;

object param = new
{
BeatmapId = beatmapId,
BeatmapId = beatmap.BeatmapID,
Diff = attribute.StarRating,
AR = beatmap.Beatmap.BeatmapInfo.Difficulty.ApproachRate,
OD = beatmap.Beatmap.BeatmapInfo.Difficulty.OverallDifficulty,
Expand Down Expand Up @@ -179,13 +181,13 @@
}
}

private void processLegacyAttributes(int beatmapId, WorkingBeatmap beatmap, Ruleset ruleset, MySqlConnection conn)
private void processLegacyAttributes(ProcessableBeatmap beatmap, MySqlConnection conn)
{
Mod? classicMod = ruleset.CreateMod<ModClassic>();
Mod? classicMod = beatmap.Ruleset.CreateMod<ModClassic>();
Mod[] mods = classicMod != null ? new[] { classicMod } : Array.Empty<Mod>();

Check notice on line 187 in osu.Server.DifficultyCalculator/ServerDifficultyCalculator.cs

View workflow job for this annotation

GitHub Actions / Code Quality

Use collection expression in osu.Server.DifficultyCalculator\ServerDifficultyCalculator.cs on line 187

ILegacyScoreSimulator simulator = ((ILegacyRuleset)ruleset).CreateLegacyScoreSimulator();
LegacyScoreAttributes attributes = simulator.Simulate(beatmap, beatmap.GetPlayableBeatmap(ruleset.RulesetInfo, mods));
ILegacyScoreSimulator simulator = ((ILegacyRuleset)beatmap.Ruleset).CreateLegacyScoreSimulator();
LegacyScoreAttributes attributes = simulator.Simulate(beatmap.Beatmap, beatmap.Beatmap.GetPlayableBeatmap(beatmap.Ruleset.RulesetInfo, mods));

if (dryRun)
return;
Expand All @@ -196,8 +198,8 @@
+ "ON DUPLICATE KEY UPDATE `legacy_accuracy_score` = @AccuracyScore, `legacy_combo_score` = @ComboScore, `legacy_bonus_score_ratio` = @BonusScoreRatio, `legacy_bonus_score` = @BonusScore, `max_combo` = @MaxCombo",
new
{
BeatmapId = beatmapId,
Mode = ruleset.RulesetInfo.OnlineID,
BeatmapId = beatmap.BeatmapID,
Mode = beatmap.RulesetID,
AccuracyScore = attributes.AccuracyScore,
ComboScore = attributes.ComboScore,
BonusScoreRatio = attributes.BonusScoreRatio,
Expand Down Expand Up @@ -228,5 +230,11 @@

return rulesetsToProcess;
}

private readonly record struct ProcessableBeatmap(WorkingBeatmap Beatmap, Ruleset Ruleset, bool Ranked)
{
public int BeatmapID => Beatmap.BeatmapInfo.OnlineID;
public int RulesetID => Ruleset.RulesetInfo.OnlineID;
}
}
}
Loading