1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-27 17:53:15 +08:00

Simplify api request logic

This commit is contained in:
Dean Herbert 2020-04-11 17:07:08 +09:00
parent abea7b5299
commit 310cf830d4

View File

@ -15,7 +15,7 @@ using osu.Game.Rulesets;
namespace osu.Game.Screens.Select namespace osu.Game.Screens.Select
{ {
public class DifficultyRecommender : Component public class DifficultyRecommender : Component, IOnlineComponent
{ {
[Resolved] [Resolved]
private IAPIProvider api { get; set; } private IAPIProvider api { get; set; }
@ -28,12 +28,10 @@ namespace osu.Game.Screens.Select
private readonly Dictionary<RulesetInfo, double> recommendedStarDifficulty = new Dictionary<RulesetInfo, double>(); private readonly Dictionary<RulesetInfo, double> recommendedStarDifficulty = new Dictionary<RulesetInfo, double>();
private int pendingAPIRequests;
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
calculateRecommendedDifficulties(); api.Register(this);
} }
/// <summary> /// <summary>
@ -48,7 +46,6 @@ namespace osu.Game.Screens.Select
{ {
if (!recommendedStarDifficulty.ContainsKey(ruleset.Value)) if (!recommendedStarDifficulty.ContainsKey(ruleset.Value))
{ {
calculateRecommendedDifficulties();
return null; return null;
} }
@ -61,11 +58,6 @@ namespace osu.Game.Screens.Select
private void calculateRecommendedDifficulties() private void calculateRecommendedDifficulties()
{ {
if (pendingAPIRequests > 0)
return;
if (api.LocalUser.Value is GuestUser)
return;
rulesets.AvailableRulesets.ForEach(rulesetInfo => rulesets.AvailableRulesets.ForEach(rulesetInfo =>
{ {
var req = new GetUserRequest(api.LocalUser.Value.Id, rulesetInfo); var req = new GetUserRequest(api.LocalUser.Value.Id, rulesetInfo);
@ -74,14 +66,27 @@ namespace osu.Game.Screens.Select
{ {
// algorithm taken from https://github.com/ppy/osu-web/blob/e6e2825516449e3d0f3f5e1852c6bdd3428c3437/app/Models/User.php#L1505 // algorithm taken from https://github.com/ppy/osu-web/blob/e6e2825516449e3d0f3f5e1852c6bdd3428c3437/app/Models/User.php#L1505
recommendedStarDifficulty[rulesetInfo] = Math.Pow((double)(result.Statistics.PP ?? 0), 0.4) * 0.195; recommendedStarDifficulty[rulesetInfo] = Math.Pow((double)(result.Statistics.PP ?? 0), 0.4) * 0.195;
pendingAPIRequests--;
}; };
req.Failure += _ => pendingAPIRequests--;
pendingAPIRequests++;
api.Queue(req); api.Queue(req);
}); });
} }
public void APIStateChanged(IAPIProvider api, APIState state)
{
switch (state)
{
case APIState.Online:
calculateRecommendedDifficulties();
break;
}
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
api.Unregister(this);
}
} }
} }