From c701579c693f345d6ea3046d79264949c9f2d57a Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 22 Oct 2021 21:25:13 +0900 Subject: [PATCH] Refactor `BeatmapMetadataContainer` and usages to use interface types --- .../Responses/APIUserMostPlayedBeatmap.cs | 22 +++---- .../Sections/BeatmapMetadataContainer.cs | 11 ++-- .../Historical/DrawableMostPlayedBeatmap.cs | 61 ++++++++++++------- .../PaginatedMostPlayedBeatmapContainer.cs | 4 +- .../Sections/Ranks/DrawableProfileScore.cs | 49 +++++++++------ 5 files changed, 86 insertions(+), 61 deletions(-) diff --git a/osu.Game/Online/API/Requests/Responses/APIUserMostPlayedBeatmap.cs b/osu.Game/Online/API/Requests/Responses/APIUserMostPlayedBeatmap.cs index 10f7ca6fe2..19c581bf95 100644 --- a/osu.Game/Online/API/Requests/Responses/APIUserMostPlayedBeatmap.cs +++ b/osu.Game/Online/API/Requests/Responses/APIUserMostPlayedBeatmap.cs @@ -2,8 +2,6 @@ // See the LICENCE file in the repository root for full licence text. using Newtonsoft.Json; -using osu.Game.Beatmaps; -using osu.Game.Rulesets; namespace osu.Game.Online.API.Requests.Responses { @@ -16,17 +14,19 @@ namespace osu.Game.Online.API.Requests.Responses public int PlayCount { get; set; } [JsonProperty("beatmap")] - private BeatmapInfo beatmapInfo { get; set; } + private APIBeatmap beatmap { get; set; } - [JsonProperty] - private APIBeatmapSet beatmapSet { get; set; } - - public BeatmapInfo GetBeatmapInfo(RulesetStore rulesets) + public APIBeatmap BeatmapInfo { - BeatmapSetInfo setInfo = beatmapSet.ToBeatmapSet(rulesets); - beatmapInfo.BeatmapSet = setInfo; - beatmapInfo.Metadata = setInfo.Metadata; - return beatmapInfo; + get + { + // old osu-web code doesn't nest set. + beatmap.BeatmapSet = BeatmapSet; + return beatmap; + } } + + [JsonProperty("beatmapset")] + public APIBeatmapSet BeatmapSet { get; set; } } } diff --git a/osu.Game/Overlays/Profile/Sections/BeatmapMetadataContainer.cs b/osu.Game/Overlays/Profile/Sections/BeatmapMetadataContainer.cs index 7812a81f30..94ef5e5d86 100644 --- a/osu.Game/Overlays/Profile/Sections/BeatmapMetadataContainer.cs +++ b/osu.Game/Overlays/Profile/Sections/BeatmapMetadataContainer.cs @@ -15,9 +15,9 @@ namespace osu.Game.Overlays.Profile.Sections /// public abstract class BeatmapMetadataContainer : OsuHoverContainer { - private readonly BeatmapInfo beatmapInfo; + private readonly IBeatmapInfo beatmapInfo; - protected BeatmapMetadataContainer(BeatmapInfo beatmapInfo) + protected BeatmapMetadataContainer(IBeatmapInfo beatmapInfo) : base(HoverSampleSet.Submit) { this.beatmapInfo = beatmapInfo; @@ -30,10 +30,7 @@ namespace osu.Game.Overlays.Profile.Sections { Action = () => { - if (beatmapInfo.OnlineBeatmapID != null) - beatmapSetOverlay?.FetchAndShowBeatmap(beatmapInfo.OnlineBeatmapID.Value); - else if (beatmapInfo.BeatmapSet?.OnlineBeatmapSetID != null) - beatmapSetOverlay?.FetchAndShowBeatmapSet(beatmapInfo.BeatmapSet.OnlineBeatmapSetID.Value); + beatmapSetOverlay?.FetchAndShowBeatmap(beatmapInfo.OnlineID); }; Child = new FillFlowContainer @@ -43,6 +40,6 @@ namespace osu.Game.Overlays.Profile.Sections }; } - protected abstract Drawable[] CreateText(BeatmapInfo beatmapInfo); + protected abstract Drawable[] CreateText(IBeatmapInfo beatmapInfo); } } diff --git a/osu.Game/Overlays/Profile/Sections/Historical/DrawableMostPlayedBeatmap.cs b/osu.Game/Overlays/Profile/Sections/Historical/DrawableMostPlayedBeatmap.cs index 32201e36a9..c4c8bfb84f 100644 --- a/osu.Game/Overlays/Profile/Sections/Historical/DrawableMostPlayedBeatmap.cs +++ b/osu.Game/Overlays/Profile/Sections/Historical/DrawableMostPlayedBeatmap.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System.Diagnostics; using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; @@ -13,6 +14,7 @@ using osu.Game.Graphics.Sprites; using osuTK; using osu.Framework.Graphics.Cursor; using osu.Framework.Localisation; +using osu.Game.Online.API.Requests.Responses; using osu.Game.Resources.Localisation.Web; namespace osu.Game.Overlays.Profile.Sections.Historical @@ -22,13 +24,11 @@ namespace osu.Game.Overlays.Profile.Sections.Historical private const int cover_width = 100; private const int corner_radius = 6; - private readonly BeatmapInfo beatmapInfo; - private readonly int playCount; + private readonly APIUserMostPlayedBeatmap mostPlayed; - public DrawableMostPlayedBeatmap(BeatmapInfo beatmapInfo, int playCount) + public DrawableMostPlayedBeatmap(APIUserMostPlayedBeatmap mostPlayed) { - this.beatmapInfo = beatmapInfo; - this.playCount = playCount; + this.mostPlayed = mostPlayed; RelativeSizeAxes = Axes.X; Height = 50; @@ -46,7 +46,7 @@ namespace osu.Game.Overlays.Profile.Sections.Historical { RelativeSizeAxes = Axes.Y, Width = cover_width, - BeatmapSet = beatmapInfo.BeatmapSet, + BeatmapSet = mostPlayed.BeatmapSet, }, new Container { @@ -77,7 +77,7 @@ namespace osu.Game.Overlays.Profile.Sections.Historical Direction = FillDirection.Vertical, Children = new Drawable[] { - new MostPlayedBeatmapMetadataContainer(beatmapInfo), + new MostPlayedBeatmapMetadataContainer(mostPlayed.BeatmapInfo), new LinkFlowContainer(t => { t.Font = OsuFont.GetFont(size: 12, weight: FontWeight.Regular); @@ -89,11 +89,11 @@ namespace osu.Game.Overlays.Profile.Sections.Historical }.With(d => { d.AddText("mapped by "); - d.AddUserLink(beatmapInfo.Metadata.Author); + d.AddUserLink(mostPlayed.BeatmapSet.Author); }), } }, - new PlayCountText(playCount) + new PlayCountText(mostPlayed.PlayCount) { Anchor = Anchor.CentreRight, Origin = Anchor.CentreRight @@ -120,26 +120,41 @@ namespace osu.Game.Overlays.Profile.Sections.Historical private class MostPlayedBeatmapMetadataContainer : BeatmapMetadataContainer { - public MostPlayedBeatmapMetadataContainer(BeatmapInfo beatmapInfo) + public MostPlayedBeatmapMetadataContainer(IBeatmapInfo beatmapInfo) : base(beatmapInfo) { } - protected override Drawable[] CreateText(BeatmapInfo beatmapInfo) => new Drawable[] + protected override Drawable[] CreateText(IBeatmapInfo beatmapInfo) { - new OsuSpriteText + var metadata = beatmapInfo.Metadata; + + Debug.Assert(metadata != null); + + return new Drawable[] { - Text = new RomanisableString( - $"{beatmapInfo.Metadata.TitleUnicode ?? beatmapInfo.Metadata.Title} [{beatmapInfo.Version}] ", - $"{beatmapInfo.Metadata.Title ?? beatmapInfo.Metadata.TitleUnicode} [{beatmapInfo.Version}] "), - Font = OsuFont.GetFont(weight: FontWeight.Bold) - }, - new OsuSpriteText - { - Text = "by " + new RomanisableString(beatmapInfo.Metadata.ArtistUnicode, beatmapInfo.Metadata.Artist), - Font = OsuFont.GetFont(weight: FontWeight.Regular) - }, - }; + new OsuSpriteText + { + Text = new RomanisableString(metadata.TitleUnicode, metadata.Title), + Font = OsuFont.GetFont(weight: FontWeight.Bold) + }, + new OsuSpriteText + { + Text = $" [{beatmapInfo.DifficultyName}]", + Font = OsuFont.GetFont(weight: FontWeight.Bold) + }, + new OsuSpriteText + { + Text = " by ", + Font = OsuFont.GetFont(weight: FontWeight.Regular) + }, + new OsuSpriteText + { + Text = new RomanisableString(metadata.ArtistUnicode, metadata.Artist), + Font = OsuFont.GetFont(weight: FontWeight.Regular) + }, + }; + } } private class PlayCountText : CompositeDrawable, IHasTooltip diff --git a/osu.Game/Overlays/Profile/Sections/Historical/PaginatedMostPlayedBeatmapContainer.cs b/osu.Game/Overlays/Profile/Sections/Historical/PaginatedMostPlayedBeatmapContainer.cs index d0979526da..428d04f985 100644 --- a/osu.Game/Overlays/Profile/Sections/Historical/PaginatedMostPlayedBeatmapContainer.cs +++ b/osu.Game/Overlays/Profile/Sections/Historical/PaginatedMostPlayedBeatmapContainer.cs @@ -33,7 +33,7 @@ namespace osu.Game.Overlays.Profile.Sections.Historical protected override APIRequest> CreateRequest() => new GetUserMostPlayedBeatmapsRequest(User.Value.Id, VisiblePages++, ItemsPerPage); - protected override Drawable CreateDrawableItem(APIUserMostPlayedBeatmap model) => - new DrawableMostPlayedBeatmap(model.GetBeatmapInfo(Rulesets), model.PlayCount); + protected override Drawable CreateDrawableItem(APIUserMostPlayedBeatmap mostPlayed) => + new DrawableMostPlayedBeatmap(mostPlayed); } } diff --git a/osu.Game/Overlays/Profile/Sections/Ranks/DrawableProfileScore.cs b/osu.Game/Overlays/Profile/Sections/Ranks/DrawableProfileScore.cs index 3561e9700e..7bfa2ee51e 100644 --- a/osu.Game/Overlays/Profile/Sections/Ranks/DrawableProfileScore.cs +++ b/osu.Game/Overlays/Profile/Sections/Ranks/DrawableProfileScore.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System.Diagnostics; using System.Linq; using JetBrains.Annotations; using osu.Framework.Allocation; @@ -245,30 +246,42 @@ namespace osu.Game.Overlays.Profile.Sections.Ranks private class ScoreBeatmapMetadataContainer : BeatmapMetadataContainer { - public ScoreBeatmapMetadataContainer(BeatmapInfo beatmapInfo) + public ScoreBeatmapMetadataContainer(IBeatmapInfo beatmapInfo) : base(beatmapInfo) { } - protected override Drawable[] CreateText(BeatmapInfo beatmapInfo) => new Drawable[] + protected override Drawable[] CreateText(IBeatmapInfo beatmapInfo) { - new OsuSpriteText + var metadata = beatmapInfo.Metadata; + + Debug.Assert(metadata != null); + + return new Drawable[] { - Anchor = Anchor.BottomLeft, - Origin = Anchor.BottomLeft, - Text = new RomanisableString( - $"{beatmapInfo.Metadata.TitleUnicode ?? beatmapInfo.Metadata.Title} ", - $"{beatmapInfo.Metadata.Title ?? beatmapInfo.Metadata.TitleUnicode} "), - Font = OsuFont.GetFont(size: 14, weight: FontWeight.SemiBold, italics: true) - }, - new OsuSpriteText - { - Anchor = Anchor.BottomLeft, - Origin = Anchor.BottomLeft, - Text = "by " + new RomanisableString(beatmapInfo.Metadata.ArtistUnicode, beatmapInfo.Metadata.Artist), - Font = OsuFont.GetFont(size: 12, italics: true) - }, - }; + new OsuSpriteText + { + Anchor = Anchor.BottomLeft, + Origin = Anchor.BottomLeft, + Text = new RomanisableString(metadata.TitleUnicode, metadata.Title), + Font = OsuFont.GetFont(size: 14, weight: FontWeight.SemiBold, italics: true) + }, + new OsuSpriteText + { + Anchor = Anchor.BottomLeft, + Origin = Anchor.BottomLeft, + Text = " by ", + Font = OsuFont.GetFont(size: 12, italics: true) + }, + new OsuSpriteText + { + Anchor = Anchor.BottomLeft, + Origin = Anchor.BottomLeft, + Text = new RomanisableString(metadata.ArtistUnicode, metadata.Artist), + Font = OsuFont.GetFont(size: 12, italics: true) + }, + }; + } } } }