2019-01-24 08:43:03 +00:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-12-17 05:45:06 +00:00
2020-02-13 09:48:28 +00:00
using System ;
2018-12-17 05:45:06 +00:00
using System.Linq ;
2021-12-28 07:37:16 +00:00
using JetBrains.Annotations ;
2018-12-17 05:45:06 +00:00
using Newtonsoft.Json ;
2020-02-13 09:12:47 +00:00
using osu.Framework.Bindables ;
2018-12-17 05:45:06 +00:00
using osu.Game.Beatmaps ;
2020-01-17 04:27:47 +00:00
using osu.Game.Online.API ;
2021-10-21 07:43:46 +00:00
using osu.Game.Online.API.Requests.Responses ;
2022-04-01 08:06:37 +00:00
using osu.Game.Utils ;
2018-12-17 05:45:06 +00:00
2020-12-25 04:38:11 +00:00
namespace osu.Game.Online.Rooms
2018-12-17 05:45:06 +00:00
{
2022-02-15 14:33:26 +00:00
[JsonObject(MemberSerialization.OptIn)]
2020-02-13 09:48:28 +00:00
public class PlaylistItem : IEquatable < PlaylistItem >
2018-12-17 05:45:06 +00:00
{
[JsonProperty("id")]
2021-02-16 10:29:40 +00:00
public long ID { get ; set ; }
2018-12-17 05:45:06 +00:00
2021-11-25 12:41:03 +00:00
[JsonProperty("owner_id")]
public int OwnerID { get ; set ; }
2018-12-17 05:45:06 +00:00
[JsonProperty("ruleset_id")]
public int RulesetID { get ; set ; }
2021-02-17 07:44:39 +00:00
/// <summary>
/// Whether this <see cref="PlaylistItem"/> is still a valid selection for the <see cref="Room"/>.
/// </summary>
[JsonProperty("expired")]
public bool Expired { get ; set ; }
2021-12-03 06:45:13 +00:00
[JsonProperty("playlist_order")]
public ushort? PlaylistOrder { get ; set ; }
2021-12-02 10:15:27 +00:00
2021-12-03 11:05:25 +00:00
[JsonProperty("played_at")]
public DateTimeOffset ? PlayedAt { get ; set ; }
2018-12-17 05:45:06 +00:00
[JsonProperty("allowed_mods")]
2022-02-15 07:01:14 +00:00
public APIMod [ ] AllowedMods { get ; set ; } = Array . Empty < APIMod > ( ) ;
2019-12-11 05:10:35 +00:00
2018-12-17 05:45:06 +00:00
[JsonProperty("required_mods")]
2022-02-15 07:01:14 +00:00
public APIMod [ ] RequiredMods { get ; set ; } = Array . Empty < APIMod > ( ) ;
2018-12-17 05:45:06 +00:00
2022-02-15 14:33:26 +00:00
/// <summary>
/// Used for deserialising from the API.
/// </summary>
[JsonProperty("beatmap")]
private APIBeatmap apiBeatmap
2020-02-13 09:12:47 +00:00
{
2022-02-15 14:33:26 +00:00
// This getter is required/used internally by JSON.NET during deserialisation to do default-value comparisons. It is never used during serialisation (see: ShouldSerializeapiBeatmap()).
// It will always return a null value on deserialisation, which JSON.NET will handle gracefully.
get = > ( APIBeatmap ) Beatmap ;
set = > Beatmap = value ;
2020-02-13 09:12:47 +00:00
}
2018-12-17 05:45:06 +00:00
2022-02-15 14:33:26 +00:00
/// <summary>
/// Used for serialising to the API.
/// </summary>
[JsonProperty("beatmap_id")]
2022-06-03 11:36:11 +00:00
private int onlineBeatmapId
{
get = > Beatmap . OnlineID ;
// This setter is only required for client-side serialise-then-deserialise operations.
// Serialisation is supposed to emit only a `beatmap_id`, but a (non-null) `beatmap` is required on deserialise.
set = > Beatmap = new APIBeatmap { OnlineID = value } ;
}
2022-02-15 14:33:26 +00:00
2022-02-22 06:31:08 +00:00
/// <summary>
/// A beatmap representing this playlist item.
/// In many cases, this will *not* contain any usable information apart from OnlineID.
/// </summary>
2022-02-15 14:33:26 +00:00
[JsonIgnore]
2022-06-30 05:24:49 +00:00
public IBeatmapInfo Beatmap { get ; private set ; }
2022-02-15 14:33:26 +00:00
[JsonIgnore]
public IBindable < bool > Valid = > valid ;
2021-11-16 08:01:24 +00:00
2022-02-15 14:33:26 +00:00
private readonly Bindable < bool > valid = new BindableBool ( true ) ;
[JsonConstructor]
private PlaylistItem ( )
2022-06-30 05:24:49 +00:00
: this ( new APIBeatmap ( ) )
2022-02-15 14:33:26 +00:00
{
}
public PlaylistItem ( IBeatmapInfo beatmap )
2018-12-17 05:45:06 +00:00
{
2022-02-15 14:33:26 +00:00
Beatmap = beatmap ;
2018-12-17 05:45:06 +00:00
}
2022-04-01 09:31:17 +00:00
public PlaylistItem ( MultiplayerPlaylistItem item )
: this ( new APIBeatmap { OnlineID = item . BeatmapID } )
{
ID = item . ID ;
OwnerID = item . OwnerID ;
RulesetID = item . RulesetID ;
Expired = item . Expired ;
PlaylistOrder = item . PlaylistOrder ;
PlayedAt = item . PlayedAt ;
RequiredMods = item . RequiredMods . ToArray ( ) ;
AllowedMods = item . AllowedMods . ToArray ( ) ;
}
2022-02-15 14:33:26 +00:00
public void MarkInvalid ( ) = > valid . Value = false ;
2021-12-28 07:37:16 +00:00
#region Newtonsoft . Json implicit ShouldSerialize ( ) methods
// The properties in this region are used implicitly by Newtonsoft.Json to not serialise certain fields in some cases.
// They rely on being named exactly the same as the corresponding fields (casing included) and as such should NOT be renamed
// unless the fields are also renamed.
[UsedImplicitly]
2018-12-17 05:45:06 +00:00
public bool ShouldSerializeID ( ) = > false ;
2021-12-28 07:37:16 +00:00
// ReSharper disable once IdentifierTypo
[UsedImplicitly]
2021-12-28 07:31:27 +00:00
public bool ShouldSerializeapiBeatmap ( ) = > false ;
2020-02-13 09:48:28 +00:00
2021-12-28 07:37:16 +00:00
#endregion
2022-04-01 08:06:37 +00:00
public PlaylistItem With ( Optional < IBeatmapInfo > beatmap = default , Optional < ushort? > playlistOrder = default ) = > new PlaylistItem ( beatmap . GetOr ( Beatmap ) )
2022-02-15 14:33:26 +00:00
{
ID = ID ,
OwnerID = OwnerID ,
RulesetID = RulesetID ,
Expired = Expired ,
2022-04-01 08:06:37 +00:00
PlaylistOrder = playlistOrder . GetOr ( PlaylistOrder ) ,
2022-02-15 14:33:26 +00:00
PlayedAt = PlayedAt ,
AllowedMods = AllowedMods ,
RequiredMods = RequiredMods ,
valid = { Value = Valid . Value } ,
} ;
public bool Equals ( PlaylistItem ? other )
2021-10-29 07:23:10 +00:00
= > ID = = other ? . ID
2022-02-15 14:33:26 +00:00
& & Beatmap . OnlineID = = other . Beatmap . OnlineID
2021-10-29 07:23:10 +00:00
& & RulesetID = = other . RulesetID
& & Expired = = other . Expired
2022-04-01 08:06:37 +00:00
& & PlaylistOrder = = other . PlaylistOrder
2022-02-15 07:01:14 +00:00
& & AllowedMods . SequenceEqual ( other . AllowedMods )
& & RequiredMods . SequenceEqual ( other . RequiredMods ) ;
2018-12-17 05:45:06 +00:00
}
}