Skip to content

Commit

Permalink
Merge pull request #233 from smoogipoo/write-object-counts
Browse files Browse the repository at this point in the history
Update hitobject counts in `osu_beatmaps` table
  • Loading branch information
peppy authored Nov 15, 2024
2 parents 18c5d29 + f4036d8 commit 277303d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 40 deletions.
79 changes: 49 additions & 30 deletions osu.Server.DifficultyCalculator/ServerDifficultyCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using osu.Game.Beatmaps.Legacy;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects.Legacy;
using osu.Game.Rulesets.Scoring.Legacy;
using osu.Server.DifficultyCalculator.Commands;

Expand Down Expand Up @@ -67,7 +68,7 @@ public void Process(WorkingBeatmap beatmap, ProcessingMode mode)

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

private void run(WorkingBeatmap beatmap, Action<ProcessableBeatmap, MySqlConnection> callback)
private void run(WorkingBeatmap beatmap, Action<ProcessableItem, MySqlConnection> callback)
{
try
{
Expand All @@ -89,10 +90,10 @@ private void run(WorkingBeatmap beatmap, Action<ProcessableBeatmap, MySqlConnect
if (processConverts && beatmap.BeatmapInfo.Ruleset.OnlineID == 0)
{
foreach (var ruleset in processableRulesets)
callback(new ProcessableBeatmap(beatmap, ruleset, ranked), conn);
callback(new ProcessableItem(beatmap, ruleset, ranked), conn);
}
else if (processableRulesets.Any(r => r.RulesetInfo.OnlineID == beatmap.BeatmapInfo.Ruleset.OnlineID))
callback(new ProcessableBeatmap(beatmap, beatmap.BeatmapInfo.Ruleset.CreateInstance(), ranked), conn);
callback(new ProcessableItem(beatmap, beatmap.BeatmapInfo.Ruleset.CreateInstance(), ranked), conn);
}
}
catch (Exception e)
Expand All @@ -101,37 +102,37 @@ private void run(WorkingBeatmap beatmap, Action<ProcessableBeatmap, MySqlConnect
}
}

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

LegacyMods legacyMods = beatmap.Ruleset.ConvertToLegacyMods(attribute.Mods);
LegacyMods legacyMods = item.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 = beatmap.BeatmapID,
Mode = beatmap.RulesetID,
BeatmapId = item.BeatmapID,
Mode = item.RulesetID,
Mods = (int)legacyMods,
Diff = attribute.StarRating
});

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

foreach (var mapping in attribute.ToDatabaseAttributes())
{
parameters.Add(new
{
BeatmapId = beatmap.BeatmapID,
Mode = beatmap.RulesetID,
BeatmapId = item.BeatmapID,
Mode = item.RulesetID,
Mods = (int)legacyMods,
Attribute = mapping.attributeId,
Value = Convert.ToSingle(mapping.value)
Expand All @@ -145,49 +146,67 @@ private void processDifficulty(ProcessableBeatmap beatmap, MySqlConnection conn)
parameters.ToArray());
}

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

int countCircle = 0;
int countSlider = 0;
int countSpinner = 0;

foreach (var obj in item.WorkingBeatmap.Beatmap.HitObjects.OfType<IHasLegacyHitObjectType>())
{
if ((obj.LegacyType & LegacyHitObjectType.Circle) > 0)
countCircle++;
if ((obj.LegacyType & LegacyHitObjectType.Slider) > 0)
countSlider++;
if ((obj.LegacyType & LegacyHitObjectType.Spinner) > 0)
countSpinner++;
}

object param = new
{
BeatmapId = beatmap.BeatmapID,
BeatmapId = item.BeatmapID,
Diff = attribute.StarRating,
AR = beatmap.Beatmap.BeatmapInfo.Difficulty.ApproachRate,
OD = beatmap.Beatmap.BeatmapInfo.Difficulty.OverallDifficulty,
HP = beatmap.Beatmap.BeatmapInfo.Difficulty.DrainRate,
CS = beatmap.Beatmap.BeatmapInfo.Difficulty.CircleSize,
AR = item.WorkingBeatmap.BeatmapInfo.Difficulty.ApproachRate,
OD = item.WorkingBeatmap.BeatmapInfo.Difficulty.OverallDifficulty,
HP = item.WorkingBeatmap.BeatmapInfo.Difficulty.DrainRate,
CS = item.WorkingBeatmap.BeatmapInfo.Difficulty.CircleSize,
BPM = Math.Round(bpm, 2),
MaxCombo = attribute.MaxCombo,
CountCircle = countCircle,
CountSlider = countSlider,
CountSpinner = countSpinner,
CountTotal = countCircle + countSlider + countSpinner
};

if (AppSettings.INSERT_BEATMAPS)
{
conn.Execute(
"INSERT INTO `osu_beatmaps` (`beatmap_id`, `difficultyrating`, `diff_approach`, `diff_overall`, `diff_drain`, `diff_size`, `bpm`, `max_combo`) "
+ "VALUES (@BeatmapId, @Diff, @AR, @OD, @HP, @CS, @BPM, @MaxCombo) "
+ "ON DUPLICATE KEY UPDATE `difficultyrating` = @Diff, `diff_approach` = @AR, `diff_overall` = @OD, `diff_drain` = @HP, `diff_size` = @CS, `bpm` = @BPM, `max_combo` = @MaxCombo",
"INSERT INTO `osu_beatmaps` (`beatmap_id`, `difficultyrating`, `diff_approach`, `diff_overall`, `diff_drain`, `diff_size`, `bpm`, `max_combo`, `countNormal`, `countSlider`, `countSpinner`, `countTotal`) "
+ "VALUES (@BeatmapId, @Diff, @AR, @OD, @HP, @CS, @BPM, @MaxCombo, @CountCircle, @CountSlider, @CountSpinner, @CountTotal) "
+ "ON DUPLICATE KEY UPDATE `difficultyrating` = @Diff, `diff_approach` = @AR, `diff_overall` = @OD, `diff_drain` = @HP, `diff_size` = @CS, `bpm` = @BPM, `max_combo` = @MaxCombo, `countNormal` = @CountCircle, `countSlider` = @CountSlider, `countSpinner` = @CountSpinner, `countTotal` = @CountTotal",
param);
}
else
{
conn.Execute(
"UPDATE `osu_beatmaps` SET `difficultyrating` = @Diff, `diff_approach` = @AR, `diff_overall` = @OD, `diff_drain` = @HP, `diff_size` = @CS, `bpm` = @BPM , `max_combo` = @MaxCombo "
"UPDATE `osu_beatmaps` SET `difficultyrating` = @Diff, `diff_approach` = @AR, `diff_overall` = @OD, `diff_drain` = @HP, `diff_size` = @CS, `bpm` = @BPM , `max_combo` = @MaxCombo, `countNormal` = @CountCircle, `countSlider` = @CountSlider, `countSpinner` = @CountSpinner, `countTotal` = @CountTotal "
+ "WHERE `beatmap_id` = @BeatmapId",
param);
}
}
}
}

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

Check notice on line 206 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 206

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

if (dryRun)
return;
Expand All @@ -198,8 +217,8 @@ private void processLegacyAttributes(ProcessableBeatmap beatmap, MySqlConnection
+ "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 = beatmap.BeatmapID,
Mode = beatmap.RulesetID,
BeatmapId = item.BeatmapID,
Mode = item.RulesetID,
AccuracyScore = attributes.AccuracyScore,
ComboScore = attributes.ComboScore,
BonusScoreRatio = attributes.BonusScoreRatio,
Expand Down Expand Up @@ -231,9 +250,9 @@ private static List<Ruleset> getRulesets()
return rulesetsToProcess;
}

private readonly record struct ProcessableBeatmap(WorkingBeatmap Beatmap, Ruleset Ruleset, bool Ranked)
private readonly record struct ProcessableItem(WorkingBeatmap WorkingBeatmap, Ruleset Ruleset, bool Ranked)
{
public int BeatmapID => Beatmap.BeatmapInfo.OnlineID;
public int BeatmapID => WorkingBeatmap.BeatmapInfo.OnlineID;
public int RulesetID => Ruleset.RulesetInfo.OnlineID;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
<PackageReference Include="Dapper" Version="2.1.44" />
<PackageReference Include="Dapper.Contrib" Version="2.0.78" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="MySqlConnector" Version="2.3.7" />
<PackageReference Include="ppy.osu.Game" Version="2024.1104.0" />
<PackageReference Include="ppy.osu.Game.Rulesets.Osu" Version="2024.1104.0" />
<PackageReference Include="ppy.osu.Game.Rulesets.Taiko" Version="2024.1104.0" />
<PackageReference Include="ppy.osu.Game.Rulesets.Catch" Version="2024.1104.0" />
<PackageReference Include="ppy.osu.Game.Rulesets.Mania" Version="2024.1104.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
<PackageReference Include="MySqlConnector" Version="2.4.0" />
<PackageReference Include="ppy.osu.Game" Version="2024.1115.1" />
<PackageReference Include="ppy.osu.Game.Rulesets.Osu" Version="2024.1115.1" />
<PackageReference Include="ppy.osu.Game.Rulesets.Taiko" Version="2024.1115.1" />
<PackageReference Include="ppy.osu.Game.Rulesets.Catch" Version="2024.1115.1" />
<PackageReference Include="ppy.osu.Game.Rulesets.Mania" Version="2024.1115.1" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.*.json">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.44" />
<PackageReference Include="ppy.osu.Server.OsuQueueProcessor" Version="2024.507.0" />
<PackageReference Include="ppy.osu.Server.OsuQueueProcessor" Version="2024.1111.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 277303d

Please sign in to comment.