From a67e156883fb6f80aa10e3af6701ae2417181c48 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 12 Nov 2021 18:09:45 +0900 Subject: [PATCH] Add `IBeatmapInfo` equality support --- osu.Game/Beatmaps/BeatmapInfo.cs | 11 +++++++++++ osu.Game/Beatmaps/IBeatmapInfo.cs | 3 ++- osu.Game/Models/RealmBeatmap.cs | 15 +++++++++++++++ .../Online/API/Requests/Responses/APIBeatmap.cs | 12 ++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/osu.Game/Beatmaps/BeatmapInfo.cs b/osu.Game/Beatmaps/BeatmapInfo.cs index 5bbd48f26d..ea2346f060 100644 --- a/osu.Game/Beatmaps/BeatmapInfo.cs +++ b/osu.Game/Beatmaps/BeatmapInfo.cs @@ -151,6 +151,17 @@ namespace osu.Game.Beatmaps public override string ToString() => this.GetDisplayTitle(); + public bool Equals(IBeatmapInfo other) + { + if (other is BeatmapInfo b) + return Equals(b); + + if (OnlineID > 0 && other?.OnlineID > 0) + return other.OnlineID == OnlineID; + + return false; + } + public bool Equals(BeatmapInfo other) { if (ID == 0 || other?.ID == 0) diff --git a/osu.Game/Beatmaps/IBeatmapInfo.cs b/osu.Game/Beatmaps/IBeatmapInfo.cs index 84ea6d3019..ab096b8897 100644 --- a/osu.Game/Beatmaps/IBeatmapInfo.cs +++ b/osu.Game/Beatmaps/IBeatmapInfo.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; using osu.Game.Database; using osu.Game.Rulesets; @@ -11,7 +12,7 @@ namespace osu.Game.Beatmaps /// /// A single beatmap difficulty. /// - public interface IBeatmapInfo : IHasOnlineID + public interface IBeatmapInfo : IHasOnlineID, IEquatable { /// /// The user-specified name given to this beatmap. diff --git a/osu.Game/Models/RealmBeatmap.cs b/osu.Game/Models/RealmBeatmap.cs index 9311425cb7..d1907d3ffd 100644 --- a/osu.Game/Models/RealmBeatmap.cs +++ b/osu.Game/Models/RealmBeatmap.cs @@ -98,6 +98,21 @@ namespace osu.Game.Models #endregion + #region Implementation of IEquatable + + public bool Equals(IBeatmapInfo? other) + { + if (other is RealmBeatmap b) + return b.ID == ID; + + if (OnlineID > 0 && other?.OnlineID > 0) + return other.OnlineID == OnlineID; + + return false; + } + + #endregion + public bool AudioEquals(RealmBeatmap? other) => other != null && BeatmapSet != null && other.BeatmapSet != null diff --git a/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs b/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs index 2560502173..33098a2919 100644 --- a/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs +++ b/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs @@ -103,5 +103,17 @@ namespace osu.Game.Online.API.Requests.Responses public string Hash => throw new NotImplementedException(); #endregion + + #region Implementation of IEquatable + + public bool Equals(IBeatmapInfo? other) + { + if (OnlineID > 0 && other?.OnlineID > 0) + return other.OnlineID == OnlineID; + + return false; + } + + #endregion } }