// 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 System.Linq; using Newtonsoft.Json; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Game.Online.Multiplayer.GameTypes; using osu.Game.Online.Multiplayer.RoomStatuses; using osu.Game.Users; namespace osu.Game.Online.Multiplayer { public class Room { [Cached] [JsonProperty("id")] public readonly Bindable RoomID = new Bindable(); [Cached] [JsonProperty("name")] public readonly Bindable Name = new Bindable(); [Cached] [JsonProperty("host")] public readonly Bindable Host = new Bindable(); [Cached] [JsonProperty("playlist")] public readonly BindableList Playlist = new BindableList(); [Cached] [JsonProperty("channel_id")] public readonly Bindable ChannelId = new Bindable(); [Cached] [JsonProperty("category")] public readonly Bindable Category = new Bindable(); [Cached] [JsonIgnore] public readonly Bindable Duration = new Bindable(TimeSpan.FromMinutes(30)); [Cached] [JsonIgnore] public readonly Bindable MaxAttempts = new Bindable(); [Cached] [JsonIgnore] public readonly Bindable Status = new Bindable(new RoomStatusOpen()); [Cached] [JsonIgnore] public readonly Bindable Availability = new Bindable(); [Cached] [JsonIgnore] public readonly Bindable Type = new Bindable(new GameTypeTimeshift()); [Cached] [JsonIgnore] public readonly Bindable MaxParticipants = new Bindable(); [Cached] [JsonProperty("recent_participants")] public readonly BindableList RecentParticipants = new BindableList(); [Cached] public readonly Bindable ParticipantCount = new Bindable(); // todo: TEMPORARY [JsonProperty("participant_count")] private int? participantCount { get => ParticipantCount.Value; set => ParticipantCount.Value = value ?? 0; } [JsonProperty("duration")] private int duration { get => (int)Duration.Value.TotalMinutes; set => Duration.Value = TimeSpan.FromMinutes(value); } // Only supports retrieval for now [Cached] [JsonProperty("ends_at")] public readonly Bindable EndDate = new Bindable(); // 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 { get => MaxAttempts.Value; set => MaxAttempts.Value = value; } /// /// The position of this in the list. This is not read from or written to the API. /// [JsonIgnore] public readonly Bindable Position = new Bindable(-1); /// /// Copies the properties from another to this room. /// /// The room to copy /// Whether the copy should exclude information unique to a specific room (i.e. when duplicating a room) public void CopyFrom(Room other, bool duplicate = false) { if (!duplicate) { RoomID.Value = other.RoomID.Value; 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; ParticipantCount.Value = other.ParticipantCount.Value; EndDate.Value = other.EndDate.Value; if (DateTimeOffset.Now >= EndDate.Value) Status.Value = new RoomStatusEnded(); if (!RecentParticipants.SequenceEqual(other.RecentParticipants)) { RecentParticipants.Clear(); RecentParticipants.AddRange(other.RecentParticipants); } Position.Value = other.Position.Value; } Name.Value = other.Name.Value; Availability.Value = other.Availability.Value; Type.Value = other.Type.Value; MaxParticipants.Value = other.MaxParticipants.Value; if (!Playlist.SequenceEqual(other.Playlist)) { Playlist.Clear(); Playlist.AddRange(other.Playlist); } } public bool ShouldSerializeRoomID() => false; public bool ShouldSerializeHost() => false; public bool ShouldSerializeEndDate() => false; } }