1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-07 03:17:18 +08:00

182 lines
6.3 KiB
C#
Raw Normal View History

// 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
2018-12-17 14:45:06 +09:00
using System;
2018-12-03 20:50:40 +09:00
using System.Linq;
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;
2020-12-25 13:38:11 +09:00
using osu.Game.Online.Rooms.GameTypes;
using osu.Game.Online.Rooms.RoomStatuses;
2018-04-13 18:19:50 +09:00
using osu.Game.Users;
2020-12-25 13:38:11 +09:00
namespace osu.Game.Online.Rooms
2018-04-13 18:19:50 +09:00
{
public class Room
{
2019-02-05 19:00:01 +09:00
[Cached]
2018-12-12 19:04:11 +09:00
[JsonProperty("id")]
public readonly Bindable<long?> RoomID = new Bindable<long?>();
2019-02-05 19:00:01 +09:00
[Cached]
[JsonProperty("name")]
2020-07-10 12:07:17 +09:00
public readonly Bindable<string> Name = new Bindable<string>();
2019-02-05 19:00:01 +09:00
[Cached]
[JsonProperty("host")]
2020-07-10 12:07:17 +09:00
public readonly Bindable<User> Host = new Bindable<User>();
2018-12-12 19:04:11 +09:00
2019-02-05 19:00:01 +09:00
[Cached]
[JsonProperty("playlist")]
2020-07-10 12:07:17 +09:00
public readonly BindableList<PlaylistItem> Playlist = new BindableList<PlaylistItem>();
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
[Cached]
[JsonIgnore]
public readonly Bindable<RoomCategory> Category = new Bindable<RoomCategory>();
// 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:56:45 +09:00
get => Category.Value;
set => Category.Value = value;
}
2019-02-05 19:00:01 +09:00
[Cached]
2018-12-17 14:44:54 +09:00
[JsonIgnore]
2020-12-21 16:18:39 +09:00
public readonly Bindable<TimeSpan?> Duration = new Bindable<TimeSpan?>();
2018-12-12 19:04:11 +09:00
2019-02-05 19:00:01 +09:00
[Cached]
2018-12-13 16:06:30 +09:00
[JsonIgnore]
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]
2018-12-17 14:45:06 +09:00
[JsonIgnore]
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]
2018-12-17 14:45:06 +09:00
[JsonIgnore]
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]
2018-12-17 14:45:06 +09:00
[JsonIgnore]
2020-12-25 13:11:21 +09:00
public readonly Bindable<GameType> Type = new Bindable<GameType>(new GameTypePlaylists());
2018-12-17 14:45:06 +09:00
2019-02-05 19:00:01 +09:00
[Cached]
2018-12-17 14:45:06 +09:00
[JsonIgnore]
2020-07-10 12:07:17 +09:00
public readonly Bindable<int?> MaxParticipants = new Bindable<int?>();
2018-12-17 14:45:06 +09:00
[Cached]
[JsonProperty("current_user_score")]
public readonly Bindable<PlaylistAggregateScore> UserScore = new Bindable<PlaylistAggregateScore>();
2019-02-05 19:00:01 +09:00
[Cached]
[JsonProperty("recent_participants")]
2020-07-10 12:07:17 +09:00
public readonly BindableList<User> RecentParticipants = new BindableList<User>();
2018-12-17 14:45:06 +09:00
2019-02-05 19:00:01 +09:00
[Cached]
[JsonProperty("participant_count")]
2020-12-21 16:35:19 +09:00
public readonly Bindable<int> ParticipantCount = new Bindable<int>();
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
// Only supports retrieval for now
2019-02-05 19:00:01 +09:00
[Cached]
[JsonProperty("ends_at")]
2020-12-21 16:18:39 +09:00
public readonly Bindable<DateTimeOffset?> EndDate = new Bindable<DateTimeOffset?>();
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
2018-12-28 01:45:19 +09: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 12:07:17 +09:00
public readonly Bindable<int> Position = new Bindable<int>(-1);
2018-12-28 01:45:19 +09:00
/// <summary>
/// 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.
/// </summary>
public Room CreateCopy()
{
var copy = new Room();
copy.CopyFrom(this);
copy.RoomID.Value = null;
return copy;
}
public void CopyFrom(Room other)
{
RoomID.Value = other.RoomID.Value;
Name.Value = other.Name.Value;
2020-12-26 20:13:28 +09:00
if (other.Category.Value != RoomCategory.Spotlight)
Category.Value = other.Category.Value;
2018-12-25 18:07:50 +09: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;
Type.Value = other.Type.Value;
MaxParticipants.Value = other.MaxParticipants.Value;
ParticipantCount.Value = other.ParticipantCount.Value;
EndDate.Value = other.EndDate.Value;
UserScore.Value = other.UserScore.Value;
2020-12-21 16:23:42 +09:00
if (EndDate.Value != null && DateTimeOffset.Now >= EndDate.Value)
Status.Value = new RoomStatusEnded();
2018-12-27 13:30:36 +09:00
2021-02-17 17:33:10 +09: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 17:58:24 +09: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 17:33:10 +09:00
if (!(Status.Value is RoomStatusEnded))
other.Playlist.RemoveAll(i => i.Expired);
if (!Playlist.SequenceEqual(other.Playlist))
{
Playlist.Clear();
Playlist.AddRange(other.Playlist);
}
if (!RecentParticipants.SequenceEqual(other.RecentParticipants))
{
RecentParticipants.Clear();
RecentParticipants.AddRange(other.RecentParticipants);
}
Position.Value = other.Position.Value;
}
2018-12-17 14:45:06 +09:00
public bool ShouldSerializeRoomID() => false;
public bool ShouldSerializeHost() => false;
public bool ShouldSerializeEndDate() => false;
}
2018-04-13 18:19:50 +09:00
}