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-04-13 09:19:50 +00:00
2018-12-17 05:45:06 +00:00
using System ;
2018-12-03 11:50:40 +00:00
using System.Linq ;
2018-12-12 07:06:56 +00:00
using Newtonsoft.Json ;
2019-02-05 10:00:01 +00:00
using osu.Framework.Allocation ;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables ;
2020-12-21 07:56:45 +00:00
using osu.Game.IO.Serialization.Converters ;
2020-12-25 04:38:11 +00:00
using osu.Game.Online.Rooms.GameTypes ;
using osu.Game.Online.Rooms.RoomStatuses ;
2018-04-13 09:19:50 +00:00
using osu.Game.Users ;
2020-12-25 04:38:11 +00:00
namespace osu.Game.Online.Rooms
2018-04-13 09:19:50 +00:00
{
public class Room
{
2019-02-05 10:00:01 +00:00
[Cached]
2018-12-12 10:04:11 +00:00
[JsonProperty("id")]
2021-02-16 10:29:40 +00:00
public readonly Bindable < long? > RoomID = new Bindable < long? > ( ) ;
2018-12-12 07:06:56 +00:00
2019-02-05 10:00:01 +00:00
[Cached]
2018-12-12 07:06:56 +00:00
[JsonProperty("name")]
2020-07-10 03:07:17 +00:00
public readonly Bindable < string > Name = new Bindable < string > ( ) ;
2018-12-12 07:06:56 +00:00
2019-02-05 10:00:01 +00:00
[Cached]
2018-12-12 07:06:56 +00:00
[JsonProperty("host")]
2020-07-10 03:07:17 +00:00
public readonly Bindable < User > Host = new Bindable < User > ( ) ;
2018-12-12 10:04:11 +00:00
2019-02-05 10:00:01 +00:00
[Cached]
2018-12-12 07:06:56 +00:00
[JsonProperty("playlist")]
2020-07-10 03:07:17 +00:00
public readonly BindableList < PlaylistItem > Playlist = new BindableList < PlaylistItem > ( ) ;
2018-12-12 07:06:56 +00:00
2019-02-05 10:00:01 +00:00
[Cached]
2018-12-21 05:01:06 +00:00
[JsonProperty("channel_id")]
2020-07-10 03:07:17 +00:00
public readonly Bindable < int > ChannelId = new Bindable < int > ( ) ;
2018-12-21 05:01:06 +00:00
2020-07-10 10:37:27 +00:00
[Cached]
2020-12-21 07:42:21 +00:00
[JsonIgnore]
2020-07-10 10:37:27 +00:00
public readonly Bindable < RoomCategory > Category = new Bindable < RoomCategory > ( ) ;
2020-12-21 07:42:21 +00:00
// Todo: osu-framework bug (https://github.com/ppy/osu-framework/issues/4106)
[JsonProperty("category")]
2020-12-21 07:56:45 +00:00
[JsonConverter(typeof(SnakeCaseStringEnumConverter))]
private RoomCategory category
2020-12-21 07:42:21 +00:00
{
2020-12-21 07:56:45 +00:00
get = > Category . Value ;
set = > Category . Value = value ;
2020-12-21 07:42:21 +00:00
}
2019-02-05 10:00:01 +00:00
[Cached]
2018-12-13 07:06:30 +00:00
[JsonIgnore]
2020-07-10 03:07:17 +00:00
public readonly Bindable < int? > MaxAttempts = new Bindable < int? > ( ) ;
2018-12-13 07:06:30 +00:00
2019-02-05 10:00:01 +00:00
[Cached]
2018-12-17 05:45:06 +00:00
[JsonIgnore]
2020-07-10 03:07:17 +00:00
public readonly Bindable < RoomStatus > Status = new Bindable < RoomStatus > ( new RoomStatusOpen ( ) ) ;
2018-12-17 05:45:06 +00:00
2019-02-05 10:00:01 +00:00
[Cached]
2018-12-17 05:45:06 +00:00
[JsonIgnore]
2020-07-10 03:07:17 +00:00
public readonly Bindable < RoomAvailability > Availability = new Bindable < RoomAvailability > ( ) ;
2018-12-17 05:45:06 +00:00
2019-02-05 10:00:01 +00:00
[Cached]
2018-12-17 05:45:06 +00:00
[JsonIgnore]
2020-12-25 04:11:21 +00:00
public readonly Bindable < GameType > Type = new Bindable < GameType > ( new GameTypePlaylists ( ) ) ;
2018-12-17 05:45:06 +00:00
2019-02-05 10:00:01 +00:00
[Cached]
2018-12-17 05:45:06 +00:00
[JsonIgnore]
2020-07-10 03:07:17 +00:00
public readonly Bindable < int? > MaxParticipants = new Bindable < int? > ( ) ;
2018-12-17 05:45:06 +00:00
2021-02-16 04:32:14 +00:00
[Cached]
[JsonProperty("current_user_score")]
public readonly Bindable < PlaylistAggregateScore > UserScore = new Bindable < PlaylistAggregateScore > ( ) ;
2021-07-07 09:53:13 +00:00
[JsonProperty("has_password")]
public readonly BindableBool HasPassword = new BindableBool ( ) ;
2019-02-05 10:00:01 +00:00
[Cached]
2020-02-27 10:24:13 +00:00
[JsonProperty("recent_participants")]
2020-07-10 03:07:17 +00:00
public readonly BindableList < User > RecentParticipants = new BindableList < User > ( ) ;
2018-12-17 05:45:06 +00:00
2019-02-05 10:00:01 +00:00
[Cached]
2018-12-26 13:25:15 +00:00
[JsonProperty("participant_count")]
2020-12-21 07:35:19 +00:00
public readonly Bindable < int > ParticipantCount = new Bindable < int > ( ) ;
2018-12-26 13:25:15 +00:00
2021-07-07 09:53:13 +00:00
#region Properties only used for room creation request
[JsonProperty("password")]
public readonly Bindable < string > Password = new Bindable < string > ( ) ;
[Cached]
[JsonIgnore]
public readonly Bindable < TimeSpan ? > Duration = new Bindable < TimeSpan ? > ( ) ;
2018-12-17 05:44:54 +00:00
[JsonProperty("duration")]
2020-12-21 07:18:39 +00:00
private int? duration
2018-12-17 05:44:54 +00:00
{
2020-12-21 07:18:39 +00:00
get = > ( int? ) Duration . Value ? . TotalMinutes ;
set
{
if ( value = = null )
Duration . Value = null ;
else
Duration . Value = TimeSpan . FromMinutes ( value . Value ) ;
}
2018-12-17 05:44:54 +00:00
}
2018-12-17 05:45:06 +00:00
2021-07-07 09:53:13 +00:00
#endregion
2018-12-19 01:52:15 +00:00
// Only supports retrieval for now
2019-02-05 10:00:01 +00:00
[Cached]
2018-12-19 01:52:15 +00:00
[JsonProperty("ends_at")]
2020-12-21 07:18:39 +00:00
public readonly Bindable < DateTimeOffset ? > EndDate = new Bindable < DateTimeOffset ? > ( ) ;
2018-12-19 01:52:15 +00:00
2018-12-13 07:06:30 +00:00
// Todo: Find a better way to do this (https://github.com/ppy/osu-framework/issues/1930)
[JsonProperty("max_attempts", DefaultValueHandling = DefaultValueHandling.Ignore)]
private int? maxAttempts
{
2019-02-21 09:56:34 +00:00
get = > MaxAttempts . Value ;
2018-12-13 07:06:30 +00:00
set = > MaxAttempts . Value = value ;
}
2018-12-12 10:04:11 +00:00
2018-12-27 16:45:19 +00:00
/// <summary>
/// The position of this <see cref="Room"/> in the list. This is not read from or written to the API.
/// </summary>
[JsonIgnore]
2020-07-10 03:07:17 +00:00
public readonly Bindable < int > Position = new Bindable < int > ( - 1 ) ;
2018-12-27 16:45:19 +00:00
2021-07-10 05:14:22 +00:00
public Room ( )
{
Password . BindValueChanged ( p = > HasPassword . Value = ! string . IsNullOrEmpty ( p . NewValue ) ) ;
}
2020-08-09 23:16:01 +00:00
/// <summary>
2020-10-19 08:15:13 +00:00
/// Create a copy of this room without online information.
/// Should be used to create a local copy of a room for submitting in the future.
2020-08-09 23:16:01 +00:00
/// </summary>
2020-08-15 20:06:16 +00:00
public Room CreateCopy ( )
2018-12-12 07:06:56 +00:00
{
2020-10-19 08:15:13 +00:00
var copy = new Room ( ) ;
2020-08-09 23:16:01 +00:00
2020-10-19 08:15:13 +00:00
copy . CopyFrom ( this ) ;
copy . RoomID . Value = null ;
2020-08-09 23:16:01 +00:00
2020-10-19 08:15:13 +00:00
return copy ;
2020-08-15 20:06:16 +00:00
}
2020-08-09 23:16:01 +00:00
2020-08-15 20:06:16 +00:00
public void CopyFrom ( Room other )
{
RoomID . Value = other . RoomID . Value ;
2020-08-09 23:16:01 +00:00
Name . Value = other . Name . Value ;
2020-12-26 11:13:28 +00:00
if ( other . Category . Value ! = RoomCategory . Spotlight )
Category . Value = other . Category . Value ;
2018-12-25 09:07:50 +00:00
2020-08-15 20:06:16 +00:00
if ( other . Host . Value ! = null & & Host . Value ? . Id ! = other . Host . Value . Id )
Host . Value = other . Host . Value ;
ChannelId . Value = other . ChannelId . Value ;
Status . Value = other . Status . Value ;
2019-02-21 09:56:34 +00:00
Availability . Value = other . Availability . Value ;
2021-07-12 09:54:07 +00:00
Password . Value = other . Password . Value ;
HasPassword . Value = other . HasPassword . Value ;
2019-02-21 09:56:34 +00:00
Type . Value = other . Type . Value ;
MaxParticipants . Value = other . MaxParticipants . Value ;
2020-08-15 20:06:16 +00:00
ParticipantCount . Value = other . ParticipantCount . Value ;
EndDate . Value = other . EndDate . Value ;
2021-02-16 04:32:14 +00:00
UserScore . Value = other . UserScore . Value ;
2020-08-15 20:06:16 +00:00
2020-12-21 07:23:42 +00:00
if ( EndDate . Value ! = null & & DateTimeOffset . Now > = EndDate . Value )
2020-08-15 20:06:16 +00:00
Status . Value = new RoomStatusEnded ( ) ;
2018-12-27 04:30:36 +00:00
2021-02-17 08:33:10 +00:00
// Todo: This is not the best way/place to do this, but the intention is to display all playlist items when the room has ended,
2021-02-17 08:58:24 +00:00
// and display only the non-expired playlist items while the room is still active. In order to achieve this, all expired items are removed from the source Room.
// More refactoring is required before this can be done locally instead - DrawableRoomPlaylist is currently directly bound to the playlist to display items in the room.
2021-02-17 08:33:10 +00:00
if ( ! ( Status . Value is RoomStatusEnded ) )
other . Playlist . RemoveAll ( i = > i . Expired ) ;
2020-02-16 07:23:46 +00:00
if ( ! Playlist . SequenceEqual ( other . Playlist ) )
{
Playlist . Clear ( ) ;
Playlist . AddRange ( other . Playlist ) ;
}
2020-08-15 20:06:16 +00:00
if ( ! RecentParticipants . SequenceEqual ( other . RecentParticipants ) )
{
RecentParticipants . Clear ( ) ;
RecentParticipants . AddRange ( other . RecentParticipants ) ;
}
Position . Value = other . Position . Value ;
2018-12-12 07:06:56 +00:00
}
2018-12-17 02:04:38 +00:00
2018-12-17 05:45:06 +00:00
public bool ShouldSerializeRoomID ( ) = > false ;
public bool ShouldSerializeHost ( ) = > false ;
2018-12-19 01:52:15 +00:00
public bool ShouldSerializeEndDate ( ) = > false ;
2018-12-12 07:06:56 +00:00
}
2018-04-13 09:19:50 +00:00
}