2019-01-24 17:43:03 +09: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 18:19:50 +09:00
2022-06-17 16:37:17 +09:00
#nullable disable
2018-12-17 14:45:06 +09:00
using System ;
2018-12-03 20:50:40 +09:00
using System.Linq ;
2018-12-12 16:06:56 +09:00
using Newtonsoft.Json ;
2019-02-05 19:00:01 +09:00
using osu.Framework.Allocation ;
2019-02-21 19:04:31 +09:00
using osu.Framework.Bindables ;
2020-12-21 16:56:45 +09:00
using osu.Game.IO.Serialization.Converters ;
2021-11-04 18:02:44 +09:00
using osu.Game.Online.API.Requests.Responses ;
2021-11-19 14:46:53 +09:00
using osu.Game.Online.Multiplayer ;
2020-12-25 13:38:11 +09:00
using osu.Game.Online.Rooms.RoomStatuses ;
2018-04-13 18:19:50 +09:00
2020-12-25 13:38:11 +09:00
namespace osu.Game.Online.Rooms
2017-05-22 00:07:15 -03:00
{
2022-02-23 17:06:40 +09:00
[JsonObject(MemberSerialization.OptIn)]
2022-11-29 14:45:26 +09:00
public partial class Room : IDependencyInjectionCandidate
2017-05-22 00:07:15 -03:00
{
2019-02-05 19:00:01 +09:00
[Cached]
2018-12-12 19:04:11 +09:00
[JsonProperty("id")]
2021-02-16 19:29:40 +09:00
public readonly Bindable < long? > RoomID = new Bindable < long? > ( ) ;
2018-12-12 16:06:56 +09:00
2019-02-05 19:00:01 +09:00
[Cached]
2018-12-12 16:06:56 +09:00
[JsonProperty("name")]
2020-07-10 12:07:17 +09:00
public readonly Bindable < string > Name = new Bindable < string > ( ) ;
2018-12-12 16:06:56 +09:00
2019-02-05 19:00:01 +09:00
[Cached]
2018-12-12 16:06:56 +09:00
[JsonProperty("host")]
2021-11-04 18:02:44 +09:00
public readonly Bindable < APIUser > Host = new Bindable < APIUser > ( ) ;
2018-12-12 19:04:11 +09:00
2019-02-05 19:00:01 +09:00
[Cached]
2018-12-12 16:06:56 +09:00
[JsonProperty("playlist")]
2020-07-10 12:07:17 +09:00
public readonly BindableList < PlaylistItem > Playlist = new BindableList < PlaylistItem > ( ) ;
2018-12-12 16:06:56 +09:00
2019-02-05 19:00:01 +09:00
[Cached]
2018-12-21 14:01:06 +09:00
[JsonProperty("channel_id")]
2020-07-10 12:07:17 +09:00
public readonly Bindable < int > ChannelId = new Bindable < int > ( ) ;
2018-12-21 14:01:06 +09:00
2022-02-21 19:02:21 +09:00
[JsonProperty("current_playlist_item")]
[Cached]
public readonly Bindable < PlaylistItem > CurrentPlaylistItem = new Bindable < PlaylistItem > ( ) ;
[JsonProperty("playlist_item_stats")]
[Cached]
public readonly Bindable < RoomPlaylistItemStats > PlaylistItemStats = new Bindable < RoomPlaylistItemStats > ( ) ;
2022-02-17 19:15:09 +09:00
[JsonProperty("difficulty_range")]
2022-02-24 16:12:15 +09:00
[Cached]
2022-02-17 19:15:09 +09:00
public readonly Bindable < RoomDifficultyRange > DifficultyRange = new Bindable < RoomDifficultyRange > ( ) ;
2020-07-10 19:37:27 +09:00
[Cached]
public readonly Bindable < RoomCategory > Category = new Bindable < RoomCategory > ( ) ;
2020-12-21 16:42:21 +09:00
// Todo: osu-framework bug (https://github.com/ppy/osu-framework/issues/4106)
[JsonProperty("category")]
2020-12-21 16:56:45 +09:00
[JsonConverter(typeof(SnakeCaseStringEnumConverter))]
private RoomCategory category
2020-12-21 16:42:21 +09:00
{
2020-12-21 16:56:45 +09:00
get = > Category . Value ;
set = > Category . Value = value ;
2020-12-21 16:42:21 +09:00
}
2019-02-05 19:00:01 +09:00
[Cached]
2020-07-10 12:07:17 +09:00
public readonly Bindable < int? > MaxAttempts = new Bindable < int? > ( ) ;
2018-12-13 16:06:30 +09:00
2019-02-05 19:00:01 +09:00
[Cached]
2020-07-10 12:07:17 +09:00
public readonly Bindable < RoomStatus > Status = new Bindable < RoomStatus > ( new RoomStatusOpen ( ) ) ;
2018-12-17 14:45:06 +09:00
2019-02-05 19:00:01 +09:00
[Cached]
2020-07-10 12:07:17 +09:00
public readonly Bindable < RoomAvailability > Availability = new Bindable < RoomAvailability > ( ) ;
2018-12-17 14:45:06 +09:00
2019-02-05 19:00:01 +09:00
[Cached]
2021-08-03 14:46:31 +09:00
public readonly Bindable < MatchType > Type = new Bindable < MatchType > ( ) ;
2018-12-17 14:45:06 +09:00
2021-08-03 23:08:11 +09:00
// Todo: osu-framework bug (https://github.com/ppy/osu-framework/issues/4106)
[JsonConverter(typeof(SnakeCaseStringEnumConverter))]
[JsonProperty("type")]
private MatchType type
{
get = > Type . Value ;
set = > Type . Value = value ;
}
2021-10-20 14:51:59 +09:00
[Cached]
2021-11-16 14:53:10 +09:00
public readonly Bindable < QueueMode > QueueMode = new Bindable < QueueMode > ( ) ;
2021-10-20 14:51:59 +09:00
[JsonConverter(typeof(SnakeCaseStringEnumConverter))]
[JsonProperty("queue_mode")]
2021-11-16 14:53:10 +09:00
private QueueMode queueMode
2021-10-20 14:51:59 +09:00
{
get = > QueueMode . Value ;
set = > QueueMode . Value = value ;
}
2022-03-18 22:00:39 +09:00
[Cached]
public readonly Bindable < TimeSpan > AutoStartDuration = new Bindable < TimeSpan > ( ) ;
2022-03-25 15:34:33 +09:00
[JsonProperty("auto_start_duration")]
2022-03-18 22:00:39 +09:00
private ushort autoStartDuration
{
get = > ( ushort ) AutoStartDuration . Value . TotalSeconds ;
set = > AutoStartDuration . Value = TimeSpan . FromSeconds ( value ) ;
}
2019-02-05 19:00:01 +09:00
[Cached]
2020-07-10 12:07:17 +09:00
public readonly Bindable < int? > MaxParticipants = new Bindable < int? > ( ) ;
2018-12-17 14:45:06 +09:00
2021-02-16 13:32:14 +09:00
[Cached]
[JsonProperty("current_user_score")]
public readonly Bindable < PlaylistAggregateScore > UserScore = new Bindable < PlaylistAggregateScore > ( ) ;
2021-07-07 18:53:13 +09:00
[JsonProperty("has_password")]
public readonly BindableBool HasPassword = new BindableBool ( ) ;
2019-02-05 19:00:01 +09:00
[Cached]
2020-02-27 19:24:13 +09:00
[JsonProperty("recent_participants")]
2021-11-04 18:02:44 +09:00
public readonly BindableList < APIUser > RecentParticipants = new BindableList < APIUser > ( ) ;
2018-12-17 14:45:06 +09:00
2019-02-05 19:00:01 +09:00
[Cached]
2018-12-26 22:25:15 +09:00
[JsonProperty("participant_count")]
2020-12-21 16:35:19 +09:00
public readonly Bindable < int > ParticipantCount = new Bindable < int > ( ) ;
2018-12-26 22:25:15 +09:00
2021-07-07 18:53:13 +09:00
#region Properties only used for room creation request
2021-07-19 20:02:14 +09:00
[Cached(Name = nameof(Password))]
2021-07-07 18:53:13 +09:00
[JsonProperty("password")]
public readonly Bindable < string > Password = new Bindable < string > ( ) ;
[Cached]
public readonly Bindable < TimeSpan ? > Duration = new Bindable < TimeSpan ? > ( ) ;
2018-12-17 14:44:54 +09:00
[JsonProperty("duration")]
2020-12-21 16:18:39 +09:00
private int? duration
2018-12-17 14:44:54 +09:00
{
2020-12-21 16:18:39 +09:00
get = > ( int? ) Duration . Value ? . TotalMinutes ;
set
{
if ( value = = null )
Duration . Value = null ;
else
Duration . Value = TimeSpan . FromMinutes ( value . Value ) ;
}
2018-12-17 14:44:54 +09:00
}
2018-12-17 14:45:06 +09:00
2021-07-07 18:53:13 +09:00
#endregion
2018-12-19 10:52:15 +09:00
// Only supports retrieval for now
2019-02-05 19:00:01 +09:00
[Cached]
2018-12-19 10:52:15 +09:00
[JsonProperty("ends_at")]
2020-12-21 16:18:39 +09:00
public readonly Bindable < DateTimeOffset ? > EndDate = new Bindable < DateTimeOffset ? > ( ) ;
2018-12-19 10:52:15 +09:00
2018-12-13 16:06:30 +09: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 18:56:34 +09:00
get = > MaxAttempts . Value ;
2018-12-13 16:06:30 +09:00
set = > MaxAttempts . Value = value ;
}
2018-12-12 19:04:11 +09:00
2022-08-31 19:49:04 +09:00
[Cached]
[JsonProperty("auto_skip")]
2022-09-01 14:14:22 +09:00
public readonly Bindable < bool > AutoSkip = new Bindable < bool > ( ) ;
2022-08-31 19:49:04 +09:00
2021-07-10 14:14:22 +09:00
public Room ( )
{
Password . BindValueChanged ( p = > HasPassword . Value = ! string . IsNullOrEmpty ( p . NewValue ) ) ;
}
2022-06-03 19:17:31 +09:00
/// <summary>
/// Copies values from another <see cref="Room"/> into this one.
/// </summary>
/// <remarks>
/// **Beware**: This will store references between <see cref="Room"/>s.
/// </remarks>
/// <param name="other">The <see cref="Room"/> to copy values from.</param>
2020-08-15 13:06:16 -07:00
public void CopyFrom ( Room other )
{
RoomID . Value = other . RoomID . Value ;
2020-08-09 16:16:01 -07:00
Name . Value = other . Name . Value ;
2020-12-26 20:13:28 +09:00
2022-02-21 17:45:57 +09:00
Category . Value = other . Category . Value ;
2018-12-25 18:07:50 +09:00
2020-08-15 13:06:16 -07: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 18:56:34 +09:00
Availability . Value = other . Availability . Value ;
2021-07-12 18:54:07 +09:00
HasPassword . Value = other . HasPassword . Value ;
2019-02-21 18:56:34 +09:00
Type . Value = other . Type . Value ;
MaxParticipants . Value = other . MaxParticipants . Value ;
2020-08-15 13:06:16 -07:00
ParticipantCount . Value = other . ParticipantCount . Value ;
EndDate . Value = other . EndDate . Value ;
2021-02-16 13:32:14 +09:00
UserScore . Value = other . UserScore . Value ;
2021-10-22 20:14:04 +09:00
QueueMode . Value = other . QueueMode . Value ;
2022-03-18 22:00:39 +09:00
AutoStartDuration . Value = other . AutoStartDuration . Value ;
2022-02-22 15:47:00 +09:00
DifficultyRange . Value = other . DifficultyRange . Value ;
PlaylistItemStats . Value = other . PlaylistItemStats . Value ;
2022-02-24 16:12:15 +09:00
CurrentPlaylistItem . Value = other . CurrentPlaylistItem . Value ;
2022-08-31 19:49:04 +09:00
AutoSkip . Value = other . AutoSkip . Value ;
2020-08-15 13:06:16 -07:00
2020-12-21 16:23:42 +09:00
if ( EndDate . Value ! = null & & DateTimeOffset . Now > = EndDate . Value )
2020-08-15 13:06:16 -07:00
Status . Value = new RoomStatusEnded ( ) ;
2018-12-27 13:30:36 +09:00
2021-09-15 17:03:26 +09:00
other . RemoveExpiredPlaylistItems ( ) ;
2021-02-17 17:33:10 +09:00
2020-02-16 16:23:46 +09:00
if ( ! Playlist . SequenceEqual ( other . Playlist ) )
{
Playlist . Clear ( ) ;
Playlist . AddRange ( other . Playlist ) ;
}
2020-08-15 13:06:16 -07:00
if ( ! RecentParticipants . SequenceEqual ( other . RecentParticipants ) )
{
RecentParticipants . Clear ( ) ;
RecentParticipants . AddRange ( other . RecentParticipants ) ;
}
2018-12-12 16:06:56 +09:00
}
2018-12-17 11:04:38 +09:00
2021-09-15 17:03:26 +09:00
public void RemoveExpiredPlaylistItems ( )
{
// 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,
// 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.
if ( ! ( Status . Value is RoomStatusEnded ) )
Playlist . RemoveAll ( i = > i . Expired ) ;
}
2022-02-21 19:02:21 +09:00
[JsonObject(MemberSerialization.OptIn)]
public class RoomPlaylistItemStats
{
[JsonProperty("count_active")]
public int CountActive ;
[JsonProperty("count_total")]
public int CountTotal ;
[JsonProperty("ruleset_ids")]
public int [ ] RulesetIDs ;
}
2022-02-17 19:15:09 +09:00
[JsonObject(MemberSerialization.OptIn)]
public class RoomDifficultyRange
{
[JsonProperty("min")]
public double Min ;
[JsonProperty("max")]
public double Max ;
}
2018-12-12 16:06:56 +09:00
}
2017-05-22 00:07:15 -03:00
}