1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 22:07:25 +08:00

Move api request outside the scores container

This commit is contained in:
Andrei Zavatski 2019-07-09 08:05:34 +03:00
parent ebff35de66
commit 5d81445454
3 changed files with 184 additions and 171 deletions

View File

@ -50,120 +50,123 @@ namespace osu.Game.Tests.Visual.Online
}
};
var scores = new List<ScoreInfo>
var allScores = new APILegacyScores
{
new ScoreInfo
Scores = new List<APILegacyScoreInfo>
{
User = new User
new APILegacyScoreInfo
{
Id = 6602580,
Username = @"waaiiru",
Country = new Country
User = new User
{
FullName = @"Spain",
FlagName = @"ES",
Id = 6602580,
Username = @"waaiiru",
Country = new Country
{
FullName = @"Spain",
FlagName = @"ES",
},
},
},
Mods = new Mod[]
{
new OsuModDoubleTime(),
new OsuModHidden(),
new OsuModFlashlight(),
new OsuModHardRock(),
},
Rank = ScoreRank.XH,
PP = 200,
MaxCombo = 1234,
TotalScore = 1234567890,
Accuracy = 1,
},
new ScoreInfo
{
User = new User
{
Id = 4608074,
Username = @"Skycries",
Country = new Country
Mods = new Mod[]
{
FullName = @"Brazil",
FlagName = @"BR",
new OsuModDoubleTime(),
new OsuModHidden(),
new OsuModFlashlight(),
new OsuModHardRock(),
},
Rank = ScoreRank.XH,
PP = 200,
MaxCombo = 1234,
TotalScore = 1234567890,
Accuracy = 1,
},
Mods = new Mod[]
new APILegacyScoreInfo
{
new OsuModDoubleTime(),
new OsuModHidden(),
new OsuModFlashlight(),
},
Rank = ScoreRank.S,
PP = 190,
MaxCombo = 1234,
TotalScore = 1234789,
Accuracy = 0.9997,
},
new ScoreInfo
{
User = new User
{
Id = 1014222,
Username = @"eLy",
Country = new Country
User = new User
{
FullName = @"Japan",
FlagName = @"JP",
Id = 4608074,
Username = @"Skycries",
Country = new Country
{
FullName = @"Brazil",
FlagName = @"BR",
},
},
},
Mods = new Mod[]
{
new OsuModDoubleTime(),
new OsuModHidden(),
},
Rank = ScoreRank.B,
PP = 180,
MaxCombo = 1234,
TotalScore = 12345678,
Accuracy = 0.9854,
},
new ScoreInfo
{
User = new User
{
Id = 1541390,
Username = @"Toukai",
Country = new Country
Mods = new Mod[]
{
FullName = @"Canada",
FlagName = @"CA",
new OsuModDoubleTime(),
new OsuModHidden(),
new OsuModFlashlight(),
},
Rank = ScoreRank.S,
PP = 190,
MaxCombo = 1234,
TotalScore = 1234789,
Accuracy = 0.9997,
},
Mods = new Mod[]
new APILegacyScoreInfo
{
new OsuModDoubleTime(),
},
Rank = ScoreRank.C,
PP = 170,
MaxCombo = 1234,
TotalScore = 1234567,
Accuracy = 0.8765,
},
new ScoreInfo
{
User = new User
{
Id = 7151382,
Username = @"Mayuri Hana",
Country = new Country
User = new User
{
FullName = @"Thailand",
FlagName = @"TH",
Id = 1014222,
Username = @"eLy",
Country = new Country
{
FullName = @"Japan",
FlagName = @"JP",
},
},
Mods = new Mod[]
{
new OsuModDoubleTime(),
new OsuModHidden(),
},
Rank = ScoreRank.B,
PP = 180,
MaxCombo = 1234,
TotalScore = 12345678,
Accuracy = 0.9854,
},
Rank = ScoreRank.D,
PP = 160,
MaxCombo = 1234,
TotalScore = 123456,
Accuracy = 0.6543,
},
new APILegacyScoreInfo
{
User = new User
{
Id = 1541390,
Username = @"Toukai",
Country = new Country
{
FullName = @"Canada",
FlagName = @"CA",
},
},
Mods = new Mod[]
{
new OsuModDoubleTime(),
},
Rank = ScoreRank.C,
PP = 170,
MaxCombo = 1234,
TotalScore = 1234567,
Accuracy = 0.8765,
},
new APILegacyScoreInfo
{
User = new User
{
Id = 7151382,
Username = @"Mayuri Hana",
Country = new Country
{
FullName = @"Thailand",
FlagName = @"TH",
},
},
Rank = ScoreRank.D,
PP = 160,
MaxCombo = 1234,
TotalScore = 123456,
Accuracy = 0.6543,
},
}
};
var myBestScore = new APILegacyUserTopScoreInfo
@ -189,7 +192,39 @@ namespace osu.Game.Tests.Visual.Online
Position = 1337,
};
foreach (var s in scores)
var oneScore = new APILegacyScores
{
Scores = new List<APILegacyScoreInfo>
{
new APILegacyScoreInfo
{
User = new User
{
Id = 6602580,
Username = @"waaiiru",
Country = new Country
{
FullName = @"Spain",
FlagName = @"ES",
},
},
Mods = new Mod[]
{
new OsuModDoubleTime(),
new OsuModHidden(),
new OsuModFlashlight(),
new OsuModHardRock(),
},
Rank = ScoreRank.XH,
PP = 200,
MaxCombo = 1234,
TotalScore = 1234567890,
Accuracy = 1,
}
}
};
foreach (var s in allScores.Scores)
{
s.Statistics.Add(HitResult.Great, RNG.Next(2000));
s.Statistics.Add(HitResult.Good, RNG.Next(2000));
@ -199,16 +234,16 @@ namespace osu.Game.Tests.Visual.Online
AddStep("Load all scores", () =>
{
scoresContainer.Scores = scores;
scoresContainer.UserScore = myBestScore;
allScores.UserScore = null;
scoresContainer.Scores = allScores;
});
AddStep("Load null scores", () =>
AddStep("Load null scores", () => scoresContainer.Scores = null);
AddStep("Load only one score", () => scoresContainer.Scores = oneScore);
AddStep("Load scores with my best", () =>
{
scoresContainer.Scores = null;
scoresContainer.UserScore = null;
allScores.UserScore = myBestScore;
scoresContainer.Scores = allScores;
});
AddStep("Load only one score", () => scoresContainer.Scores = new[] { scores.First() });
AddStep("Add my best score", () => scoresContainer.UserScore = myBestScore);
}
[BackgroundDependencyLoader]

View File

@ -5,15 +5,11 @@ using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osuTK;
using System.Collections.Generic;
using System.Linq;
using osu.Game.Scoring;
using osu.Game.Online.API.Requests.Responses;
namespace osu.Game.Overlays.BeatmapSet.Scores
@ -29,9 +25,6 @@ namespace osu.Game.Overlays.BeatmapSet.Scores
private readonly FillFlowContainer topScoresContainer;
private readonly LoadingAnimation loadingAnimation;
[Resolved]
private IAPIProvider api { get; set; }
public ScoresContainer()
{
RelativeSizeAxes = Axes.X;
@ -72,7 +65,7 @@ namespace osu.Game.Overlays.BeatmapSet.Scores
loadingAnimation = new LoadingAnimation
{
Alpha = 0,
Margin = new MarginPadding(20)
Margin = new MarginPadding(20),
},
};
}
@ -84,87 +77,46 @@ namespace osu.Game.Overlays.BeatmapSet.Scores
updateDisplay();
}
private bool loading
public bool Loading
{
set => loadingAnimation.FadeTo(value ? 1 : 0, fade_duration);
set
{
loadingAnimation.FadeTo(value ? 1 : 0, fade_duration);
if (value)
Scores = null;
}
}
private GetScoresRequest getScoresRequest;
private IReadOnlyList<ScoreInfo> scores;
private APILegacyScores scores;
public IReadOnlyList<ScoreInfo> Scores
public APILegacyScores Scores
{
get => scores;
set
{
getScoresRequest?.Cancel();
scores = value;
updateDisplay();
}
}
private APILegacyUserTopScoreInfo userScore;
public APILegacyUserTopScoreInfo UserScore
{
get => userScore;
set
{
getScoresRequest?.Cancel();
userScore = value;
updateDisplay();
}
}
private BeatmapInfo beatmap;
public BeatmapInfo Beatmap
{
get => beatmap;
set
{
beatmap = value;
Scores = null;
if (beatmap?.OnlineBeatmapID.HasValue != true)
return;
loading = true;
getScoresRequest = new GetScoresRequest(beatmap, beatmap.Ruleset);
getScoresRequest.Success += r => Schedule(() =>
{
scores = r.Scores;
userScore = r.UserScore;
updateDisplay();
});
api.Queue(getScoresRequest);
}
}
private void updateDisplay()
{
loading = false;
topScoresContainer.Clear();
scoreTable.Scores = scores?.Count > 1 ? scores : new List<ScoreInfo>();
scoreTable.FadeTo(scores?.Count > 1 ? 1 : 0);
scoreTable.Scores = scores?.Scores.Count > 1 ? scores.Scores : new List<APILegacyScoreInfo>();
scoreTable.FadeTo(scores?.Scores.Count > 1 ? 1 : 0);
if (scores?.Any() == true)
if (scores?.Scores.Any() == true)
{
topScoresContainer.Add(new DrawableTopScore(scores.FirstOrDefault()));
topScoresContainer.Add(new DrawableTopScore(scores.Scores.FirstOrDefault()));
var userScore = scores.UserScore;
if (userScore != null && userScore.Position != 1)
topScoresContainer.Add(new DrawableTopScore(userScore.Score, userScore.Position));
}
}
protected override void Dispose(bool isDisposing)
{
getScoresRequest?.Cancel();
}
}
}

View File

@ -11,6 +11,7 @@ using osu.Framework.Input.Events;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Overlays.BeatmapSet;
using osu.Game.Overlays.BeatmapSet.Scores;
@ -30,9 +31,14 @@ namespace osu.Game.Overlays
protected readonly Header Header;
private RulesetStore rulesets;
private ScoresContainer scores;
private GetScoresRequest getScoresRequest;
private readonly Bindable<BeatmapSetInfo> beatmapSet = new Bindable<BeatmapSetInfo>();
[Resolved]
private IAPIProvider api { get; set; }
// receive input outside our bounds so we can trigger a close event on ourselves.
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
@ -40,7 +46,6 @@ namespace osu.Game.Overlays
{
OsuScrollContainer scroll;
Info info;
ScoresContainer scores;
Children = new Drawable[]
{
@ -74,12 +79,33 @@ namespace osu.Game.Overlays
Header.Picker.Beatmap.ValueChanged += b =>
{
info.Beatmap = b.NewValue;
scores.Beatmap = b.NewValue;
getScores(b.NewValue);
scroll.ScrollToStart();
};
}
private void getScores(BeatmapInfo b)
{
getScoresRequest?.Cancel();
if (b?.OnlineBeatmapID.HasValue != true)
{
scores.Scores = null;
return;
}
scores.Loading = true;
getScoresRequest = new GetScoresRequest(b, b.Ruleset);
getScoresRequest.Success += r => Schedule(() =>
{
scores.Scores = r;
scores.Loading = false;
});
api.Queue(getScoresRequest);
}
[BackgroundDependencyLoader]
private void load(RulesetStore rulesets)
{