2019-01-24 16:43:03 +08: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 17:19:50 +08:00
2018-12-17 13:45:06 +08:00
using System ;
2024-11-12 00:38:31 +08:00
using System.Collections.Generic ;
using System.ComponentModel ;
2018-12-03 19:50:40 +08:00
using System.Linq ;
2024-11-12 00:38:31 +08:00
using System.Runtime.CompilerServices ;
2018-12-12 15:06:56 +08:00
using Newtonsoft.Json ;
2019-02-05 18:00:01 +08:00
using osu.Framework.Allocation ;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables ;
2020-12-21 15:56:45 +08:00
using osu.Game.IO.Serialization.Converters ;
2021-11-04 17:02:44 +08:00
using osu.Game.Online.API.Requests.Responses ;
2021-11-19 13:46:53 +08:00
using osu.Game.Online.Multiplayer ;
2020-12-25 12:38:11 +08:00
using osu.Game.Online.Rooms.RoomStatuses ;
2018-04-13 17:19:50 +08:00
2020-12-25 12:38:11 +08:00
namespace osu.Game.Online.Rooms
2017-05-22 11:07:15 +08:00
{
2022-02-23 16:06:40 +08:00
[JsonObject(MemberSerialization.OptIn)]
2024-11-12 00:38:31 +08:00
public partial class Room : IDependencyInjectionCandidate , INotifyPropertyChanged
2017-05-22 11:07:15 +08:00
{
2024-11-12 00:38:31 +08:00
public event PropertyChangedEventHandler ? PropertyChanged ;
2024-11-13 15:28:39 +08:00
/// <summary>
/// The online room ID. Will be <c>null</c> while the room has not yet been created.
/// </summary>
public long? RoomID
{
get = > roomId ;
set = > SetField ( ref roomId , value ) ;
}
2024-11-13 15:55:18 +08:00
/// <summary>
/// The room name.
/// </summary>
public string Name
{
get = > name ;
set = > SetField ( ref name , value ) ;
}
2024-11-13 18:11:05 +08:00
/// <summary>
/// Sets the room password. Will be <c>null</c> after the room is created.
/// </summary>
/// <remarks>
/// To check if the room has a password, use <see cref="HasPassword"/>.
/// </remarks>
public string? Password
{
get = > password ;
set
{
SetField ( ref password , value ) ;
HasPassword = ! string . IsNullOrEmpty ( value ) ;
}
}
/// <summary>
/// Whether the room has a password.
/// </summary>
/// <remarks>
/// To set a password, use <see cref="Password"/>.
/// </remarks>
[JsonProperty("has_password")]
public bool HasPassword
{
get = > hasPassword ;
private set = > SetField ( ref hasPassword , value ) ;
}
2024-11-13 17:27:32 +08:00
/// <summary>
/// The room host. Will be <c>null</c> while the room has not yet been created.
/// </summary>
2024-11-13 16:32:32 +08:00
public APIUser ? Host
{
get = > host ;
set = > SetField ( ref host , value ) ;
}
2024-11-13 17:27:32 +08:00
/// <summary>
/// The room category.
/// </summary>
public RoomCategory Category
{
get = > category ;
set = > SetField ( ref category , value ) ;
}
2024-11-13 19:20:14 +08:00
/// <summary>
/// The duration for which the room will be open. Will be <c>null</c> after the room is created.
/// </summary>
/// <remarks>
/// To check the room end time, use <see cref="EndDate"/>.
/// </remarks>
public TimeSpan ? Duration
{
get = > duration = = null ? null : TimeSpan . FromMinutes ( duration . Value ) ;
set = > SetField ( ref duration , value = = null ? null : ( int ) value . Value . TotalMinutes ) ;
}
/// <summary>
/// The date at which the room was opened. Will be <c>null</c> while the room has not yet been created.
/// </summary>
public DateTimeOffset ? StartDate
{
get = > startDate ;
set = > SetField ( ref startDate , value ) ;
}
/// <summary>
/// The date at which the room will be closed.
/// </summary>
/// <remarks>
/// To set the room duration, use <see cref="Duration"/>.
/// </remarks>
public DateTimeOffset ? EndDate
{
get = > endDate ;
set = > SetField ( ref endDate , value ) ;
}
2024-11-13 18:45:03 +08:00
/// <summary>
/// The maximum number of users allowed in the room.
/// </summary>
public int? MaxParticipants
{
get = > maxParticipants ;
set = > SetField ( ref maxParticipants , value ) ;
}
/// <summary>
/// The current number of users in the room.
/// </summary>
public int ParticipantCount
{
get = > participantCount ;
set = > SetField ( ref participantCount , value ) ;
}
2024-11-13 17:36:47 +08:00
/// <summary>
/// The match type.
/// </summary>
public MatchType Type
{
get = > type ;
set = > SetField ( ref type , value ) ;
}
2024-11-13 19:32:34 +08:00
/// <summary>
/// The maximum number of attempts on the playlist. Only valid for playlist rooms.
/// </summary>
public int? MaxAttempts
{
get = > maxAttempts ;
set = > SetField ( ref maxAttempts , value ) ;
}
2024-11-13 17:57:51 +08:00
/// <summary>
/// The playlist queueing mode. Only valid for multiplayer rooms.
/// </summary>
public QueueMode QueueMode
{
get = > queueMode ;
set = > SetField ( ref queueMode , value ) ;
}
2024-11-13 18:15:29 +08:00
/// <summary>
/// Whether to automatically skip map intros. Only valid for multiplayer rooms.
/// </summary>
public bool AutoSkip
{
get = > autoSkip ;
set = > SetField ( ref autoSkip , value ) ;
}
2024-11-13 18:53:21 +08:00
/// <summary>
/// The amount of time before the match is automatically started. Only valid for multiplayer rooms.
/// </summary>
public TimeSpan AutoStartDuration
{
get = > TimeSpan . FromSeconds ( autoStartDuration ) ;
set = > SetField ( ref autoStartDuration , ( ushort ) value . TotalSeconds ) ;
}
2024-11-12 00:38:31 +08:00
/// <summary>
/// Represents the current item selected within the room.
/// </summary>
/// <remarks>
/// Only valid for room listing requests (i.e. in the lounge screen), and may not be valid while inside the room.
/// </remarks>
public PlaylistItem ? CurrentPlaylistItem
{
get = > currentPlaylistItem ;
set = > SetField ( ref currentPlaylistItem , value ) ;
}
2024-11-13 17:48:13 +08:00
/// <summary>
/// The current room status.
/// </summary>
public RoomStatus Status
{
get = > status ;
set = > SetField ( ref status , value ) ;
}
2024-11-13 17:52:04 +08:00
/// <summary>
/// Describes which players are able to join the room.
/// </summary>
public RoomAvailability Availability
{
get = > availability ;
set = > SetField ( ref availability , value ) ;
}
2024-11-13 15:28:39 +08:00
[JsonProperty("id")]
private long? roomId ;
2024-11-13 15:55:18 +08:00
[JsonProperty("name")]
private string name = string . Empty ;
2024-11-13 18:11:05 +08:00
[JsonProperty("password")]
private string? password ;
// Not serialised (internal use only).
private bool hasPassword ;
2024-11-13 16:32:32 +08:00
[JsonProperty("host")]
private APIUser ? host ;
2024-11-13 17:27:32 +08:00
[JsonProperty("category")]
[JsonConverter(typeof(SnakeCaseStringEnumConverter))]
private RoomCategory category ;
2024-11-13 19:20:14 +08:00
[JsonProperty("duration")]
private int? duration ;
[JsonProperty("starts_at")]
private DateTimeOffset ? startDate ;
[JsonProperty("ends_at")]
private DateTimeOffset ? endDate ;
2024-11-13 18:45:03 +08:00
// Not yet serialised (not implemented).
private int? maxParticipants ;
[JsonProperty("participant_count")]
private int participantCount ;
2024-11-13 19:32:34 +08:00
[JsonProperty("max_attempts", DefaultValueHandling = DefaultValueHandling.Ignore)]
private int? maxAttempts ;
2024-11-13 17:36:47 +08:00
[JsonConverter(typeof(SnakeCaseStringEnumConverter))]
[JsonProperty("type")]
private MatchType type ;
2024-11-13 17:57:51 +08:00
[JsonConverter(typeof(SnakeCaseStringEnumConverter))]
[JsonProperty("queue_mode")]
private QueueMode queueMode ;
2024-11-13 18:15:29 +08:00
[JsonProperty("auto_skip")]
private bool autoSkip ;
2024-11-13 18:53:21 +08:00
[JsonProperty("auto_start_duration")]
private ushort autoStartDuration ;
2024-11-13 14:50:01 +08:00
[JsonProperty("current_playlist_item")]
private PlaylistItem ? currentPlaylistItem ;
2024-11-13 17:48:13 +08:00
// Not serialised (see: GetRoomsRequest).
private RoomStatus status = new RoomStatusOpen ( ) ;
2024-11-13 17:52:04 +08:00
// Not yet serialised (not implemented).
private RoomAvailability availability ;
2019-02-05 18:00:01 +08:00
[Cached]
2018-12-12 15:06:56 +08:00
[JsonProperty("playlist")]
2020-07-10 11:07:17 +08:00
public readonly BindableList < PlaylistItem > Playlist = new BindableList < PlaylistItem > ( ) ;
2018-12-12 15:06:56 +08:00
2019-02-05 18:00:01 +08:00
[Cached]
2018-12-21 13:01:06 +08:00
[JsonProperty("channel_id")]
2020-07-10 11:07:17 +08:00
public readonly Bindable < int > ChannelId = new Bindable < int > ( ) ;
2018-12-21 13:01:06 +08:00
2022-02-21 18:02:21 +08:00
[JsonProperty("playlist_item_stats")]
[Cached]
public readonly Bindable < RoomPlaylistItemStats > PlaylistItemStats = new Bindable < RoomPlaylistItemStats > ( ) ;
2022-02-17 18:15:09 +08:00
[JsonProperty("difficulty_range")]
2022-02-24 15:12:15 +08:00
[Cached]
2022-02-17 18:15:09 +08:00
public readonly Bindable < RoomDifficultyRange > DifficultyRange = new Bindable < RoomDifficultyRange > ( ) ;
2021-02-16 12:32:14 +08:00
[Cached]
[JsonProperty("current_user_score")]
public readonly Bindable < PlaylistAggregateScore > UserScore = new Bindable < PlaylistAggregateScore > ( ) ;
2019-02-05 18:00:01 +08:00
[Cached]
2020-02-27 18:24:13 +08:00
[JsonProperty("recent_participants")]
2021-11-04 17:02:44 +08:00
public readonly BindableList < APIUser > RecentParticipants = new BindableList < APIUser > ( ) ;
2018-12-17 13:45:06 +08:00
2022-06-03 18:17:31 +08: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-16 04:06:16 +08:00
public void CopyFrom ( Room other )
{
2024-11-13 15:28:39 +08:00
RoomID = other . RoomID ;
2024-11-13 15:55:18 +08:00
Name = other . Name ;
2020-12-26 19:13:28 +08:00
2024-11-13 17:27:32 +08:00
Category = other . Category ;
2018-12-25 17:07:50 +08:00
2024-11-13 16:32:32 +08:00
if ( other . Host ! = null & & Host ? . Id ! = other . Host . Id )
Host = other . Host ;
2020-08-16 04:06:16 +08:00
ChannelId . Value = other . ChannelId . Value ;
2024-11-13 17:48:13 +08:00
Status = other . Status ;
2024-11-13 17:52:04 +08:00
Availability = other . Availability ;
2024-11-13 18:11:05 +08:00
HasPassword = other . HasPassword ;
2024-11-13 17:36:47 +08:00
Type = other . Type ;
2024-11-13 18:45:03 +08:00
MaxParticipants = other . MaxParticipants ;
ParticipantCount = other . ParticipantCount ;
2024-11-13 19:20:14 +08:00
EndDate = other . EndDate ;
2021-02-16 12:32:14 +08:00
UserScore . Value = other . UserScore . Value ;
2024-11-13 17:57:51 +08:00
QueueMode = other . QueueMode ;
2024-11-13 18:53:21 +08:00
AutoStartDuration = other . AutoStartDuration ;
2022-02-22 14:47:00 +08:00
DifficultyRange . Value = other . DifficultyRange . Value ;
PlaylistItemStats . Value = other . PlaylistItemStats . Value ;
2024-11-12 00:38:31 +08:00
CurrentPlaylistItem = other . CurrentPlaylistItem ;
2024-11-13 18:15:29 +08:00
AutoSkip = other . AutoSkip ;
2020-08-16 04:06:16 +08:00
2021-09-15 16:03:26 +08:00
other . RemoveExpiredPlaylistItems ( ) ;
2021-02-17 16:33:10 +08:00
2020-02-16 15:23:46 +08:00
if ( ! Playlist . SequenceEqual ( other . Playlist ) )
{
Playlist . Clear ( ) ;
Playlist . AddRange ( other . Playlist ) ;
}
2020-08-16 04:06:16 +08:00
if ( ! RecentParticipants . SequenceEqual ( other . RecentParticipants ) )
{
RecentParticipants . Clear ( ) ;
RecentParticipants . AddRange ( other . RecentParticipants ) ;
}
2018-12-12 15:06:56 +08:00
}
2018-12-17 10:04:38 +08:00
2021-09-15 16:03:26 +08: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.
2024-11-13 17:48:13 +08:00
if ( Status is not RoomStatusEnded )
2021-09-15 16:03:26 +08:00
Playlist . RemoveAll ( i = > i . Expired ) ;
}
2022-02-21 18:02:21 +08:00
[JsonObject(MemberSerialization.OptIn)]
public class RoomPlaylistItemStats
{
[JsonProperty("count_active")]
public int CountActive ;
[JsonProperty("count_total")]
public int CountTotal ;
[JsonProperty("ruleset_ids")]
2024-11-12 00:38:31 +08:00
public int [ ] RulesetIDs = [ ] ;
2022-02-21 18:02:21 +08:00
}
2022-02-17 18:15:09 +08:00
[JsonObject(MemberSerialization.OptIn)]
public class RoomDifficultyRange
{
[JsonProperty("min")]
public double Min ;
[JsonProperty("max")]
public double Max ;
}
2024-11-12 00:38:31 +08:00
protected virtual void OnPropertyChanged ( [ CallerMemberName ] string propertyName = null ! )
= > PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( propertyName ) ) ;
protected bool SetField < T > ( ref T field , T value , [ CallerMemberName ] string propertyName = null ! )
{
if ( EqualityComparer < T > . Default . Equals ( field , value ) )
return false ;
field = value ;
OnPropertyChanged ( propertyName ) ;
return true ;
}
2018-12-12 15:06:56 +08:00
}
2017-05-22 11:07:15 +08:00
}