Skip to content

Commit

Permalink
Merge pull request #204 from bdach/fix-broken-online-difficulty-attri…
Browse files Browse the repository at this point in the history
…bute-lookup

Fix online attribute flag in score performance command not retrieving full difficulty attributes
  • Loading branch information
smoogipoo authored Apr 30, 2024
2 parents c3cbe41 + d6cf8dc commit 25ca400
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions PerformanceCalculator/Performance/ScorePerformanceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,41 @@ private DifficultyAttributes queryApiAttributes(int beatmapId, int rulesetId, Le
{ "mods", ((int)mods).ToString(CultureInfo.InvariantCulture) }
};

var beatmap = GetJsonFromApi<APIBeatmap>($"beatmaps/{beatmapId}");

switch (rulesetId)
{
case 0:
return GetJsonFromApi<AttributesResponse<OsuDifficultyAttributes>>($"beatmaps/{beatmapId}/attributes", HttpMethod.Post, parameters).Attributes;
return getMergedAttributes<OsuDifficultyAttributes>(beatmap);

case 1:
return GetJsonFromApi<AttributesResponse<TaikoDifficultyAttributes>>($"beatmaps/{beatmapId}/attributes", HttpMethod.Post, parameters).Attributes;
return getMergedAttributes<TaikoDifficultyAttributes>(beatmap);

case 2:
return GetJsonFromApi<AttributesResponse<CatchDifficultyAttributes>>($"beatmaps/{beatmapId}/attributes", HttpMethod.Post, parameters).Attributes;
return getMergedAttributes<CatchDifficultyAttributes>(beatmap);

case 3:
return GetJsonFromApi<AttributesResponse<ManiaDifficultyAttributes>>($"beatmaps/{beatmapId}/attributes", HttpMethod.Post, parameters).Attributes;
return getMergedAttributes<ManiaDifficultyAttributes>(beatmap);

default:
throw new ArgumentOutOfRangeException(nameof(rulesetId));
}

DifficultyAttributes getMergedAttributes<TAttributes>(APIBeatmap apiBeatmap)
where TAttributes : DifficultyAttributes, new()
{
// the osu-web endpoint queries osu-beatmap-difficulty-cache, which in turn does not return the full set of attributes -
// it skips ones that are already present on `APIBeatmap`
// (https://github.com/ppy/osu-beatmap-difficulty-lookup-cache/blob/db2203368221109803f2031788da31deb94e0f11/BeatmapDifficultyLookupCache/DifficultyCache.cs#L125-L128).
// to circumvent this, do some manual grafting on our side to produce a fully populated set of attributes.
var databasedAttributes = GetJsonFromApi<AttributesResponse<TAttributes>>($"beatmaps/{beatmapId}/attributes", HttpMethod.Post, parameters).Attributes;
var fullAttributes = new TAttributes();
fullAttributes.FromDatabaseAttributes(databasedAttributes.ToDatabaseAttributes().ToDictionary(
pair => pair.attributeId,
pair => Convert.ToDouble(pair.value, CultureInfo.InvariantCulture)
), apiBeatmap);
return fullAttributes;
}
}

[JsonObject(MemberSerialization.OptIn)]
Expand Down

0 comments on commit 25ca400

Please sign in to comment.