forked from ppy/osu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoom.cs
More file actions
414 lines (369 loc) · 12.8 KB
/
Copy pathRoom.cs
File metadata and controls
414 lines (369 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// 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.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
using osu.Game.IO.Serialization.Converters;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Multiplayer;
namespace osu.Game.Online.Rooms
{
[JsonObject(MemberSerialization.OptIn)]
public partial class Room : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// The online room ID. Will be <c>null</c> while the room has not yet been created.
/// </summary>
[field: JsonProperty("id")]
public long? RoomID
{
get;
set => SetField(ref field, value);
}
/// <summary>
/// The room name.
/// </summary>
[field: JsonProperty("name")]
public string Name
{
get;
set => SetField(ref field, value);
} = string.Empty;
/// <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>
[field: JsonProperty("password")]
public string? Password
{
get;
set
{
SetField(ref field, 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;
private set => SetField(ref field, value);
}
/// <summary>
/// The room host. Will be <c>null</c> while the room has not yet been created.
/// </summary>
[field: JsonProperty("host")]
public APIUser? Host
{
get;
set => SetField(ref field, value);
}
/// <summary>
/// The room category.
/// </summary>
[field: JsonProperty("category")]
[field: JsonConverter(typeof(SnakeCaseStringEnumConverter))]
public RoomCategory Category
{
get;
set => SetField(ref field, value);
}
/// <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>
[field: JsonProperty("starts_at")]
public DateTimeOffset? StartDate
{
get;
set => SetField(ref field, value);
}
/// <summary>
/// The date at which the room will be closed.
/// </summary>
/// <remarks>
/// To set the room duration, use <see cref="Duration"/>.
/// </remarks>
[field: JsonProperty("ends_at")]
public DateTimeOffset? EndDate
{
get;
set => SetField(ref field, value);
}
/// <summary>
/// The maximum number of users allowed in the room.
/// </summary>
public byte? MaxParticipants
{
get;
set => SetField(ref field, value);
}
/// <summary>
/// The current number of users in the room.
/// </summary>
[field: JsonProperty("participant_count")]
public int ParticipantCount
{
get;
set => SetField(ref field, value);
}
/// <summary>
/// The set of most recent participants in the room.
/// </summary>
[field: JsonProperty("recent_participants")]
public IReadOnlyList<APIUser> RecentParticipants
{
get;
set => SetList(ref field, value);
} = [];
/// <summary>
/// The match type.
/// </summary>
[field: JsonConverter(typeof(SnakeCaseStringEnumConverter))]
[field: JsonProperty("type")]
public MatchType Type
{
get;
set => SetField(ref field, value);
}
/// <summary>
/// The maximum number of attempts on the playlist. Only valid for playlist rooms.
/// </summary>
[field: JsonProperty("max_attempts", DefaultValueHandling = DefaultValueHandling.Ignore)]
public int? MaxAttempts
{
get;
set => SetField(ref field, value);
}
/// <summary>
/// The room playlist.
/// </summary>
[field: JsonProperty("playlist")]
public IReadOnlyList<PlaylistItem> Playlist
{
get;
set => SetList(ref field, value);
} = [];
/// <summary>
/// Describes the items in the playlist.
/// </summary>
[field: JsonProperty("playlist_item_stats")]
public RoomPlaylistItemStats? PlaylistItemStats
{
get;
set => SetField(ref field, value);
}
/// <summary>
/// Describes the range of difficulty of the room.
/// </summary>
[field: JsonProperty("difficulty_range")]
public RoomDifficultyRange? DifficultyRange
{
get;
set => SetField(ref field, value);
}
/// <summary>
/// The playlist queueing mode. Only valid for multiplayer rooms.
/// </summary>
[field: JsonConverter(typeof(SnakeCaseStringEnumConverter))]
[field: JsonProperty("queue_mode")]
public QueueMode QueueMode
{
get;
set => SetField(ref field, value);
}
/// <summary>
/// Whether to automatically skip map intros. Only valid for multiplayer rooms.
/// </summary>
[field: JsonProperty("auto_skip")]
public bool AutoSkip
{
get;
set => SetField(ref field, value);
}
/// <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);
}
/// <summary>
/// Provides some extra scoring statistics for the local user in the room.
/// </summary>
[field: JsonProperty("current_user_score")]
public PlaylistAggregateScore? UserScore
{
get;
set => SetField(ref field, value);
}
/// <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>
[field: JsonProperty("current_playlist_item")]
public PlaylistItem? CurrentPlaylistItem
{
get;
set => SetField(ref field, value);
}
/// <summary>
/// The chat channel id for the room. Will be <c>0</c> while the room has not yet been created.
/// </summary>
[field: JsonProperty("channel_id")]
public int ChannelId
{
get;
set => SetField(ref field, value);
}
/// <summary>
/// The current status of the room.
/// </summary>
[field: JsonProperty("status")]
[field: JsonConverter(typeof(SnakeCaseStringEnumConverter))]
public RoomStatus Status
{
get;
set => SetField(ref field, value);
}
/// <summary>
/// Describes which players are able to join the room.
/// </summary>
public RoomAvailability Availability
{
get;
set => SetField(ref field, value);
}
[field: JsonProperty("pinned")]
public bool Pinned
{
get;
set => SetField(ref field, value);
}
// Not serialised (internal use only).
[JsonProperty("duration")]
private int? duration;
// Not yet serialised (not implemented).
[JsonProperty("auto_start_duration")]
private ushort autoStartDuration;
// Not yet serialised (not implemented).
public Room()
{
}
public Room(MultiplayerRoom room)
{
RoomID = room.RoomID;
ChannelId = room.ChannelID;
Name = room.Settings.Name;
Password = room.Settings.Password;
Type = room.Settings.MatchType;
QueueMode = room.Settings.QueueMode;
AutoStartDuration = room.Settings.AutoStartDuration;
AutoSkip = room.Settings.AutoSkip;
MaxParticipants = room.Settings.MaxParticipants;
Host = room.Host != null ? new APIUser { Id = room.Host.UserID } : null;
Playlist = room.Playlist.Select(p => new PlaylistItem(p)).ToArray();
}
/// <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>
public void CopyFrom(Room other)
{
RoomID = other.RoomID;
Name = other.Name;
Category = other.Category;
Host = other.Host;
ChannelId = other.ChannelId;
Status = other.Status;
Availability = other.Availability;
HasPassword = other.HasPassword;
Type = other.Type;
MaxParticipants = other.MaxParticipants;
ParticipantCount = other.ParticipantCount;
StartDate = other.StartDate;
EndDate = other.EndDate;
UserScore = other.UserScore;
QueueMode = other.QueueMode;
AutoStartDuration = other.AutoStartDuration;
DifficultyRange = other.DifficultyRange;
PlaylistItemStats = other.PlaylistItemStats;
CurrentPlaylistItem = other.CurrentPlaylistItem;
AutoSkip = other.AutoSkip;
Playlist = other.Playlist;
RecentParticipants = other.RecentParticipants;
}
/// <summary>
/// Whether the room is no longer available.
/// </summary>
/// <remarks>
/// This property does not update in real-time and needs to be queried periodically.
/// Subscribe to <see cref="EndDate"/> to be notified of any immediate changes.
/// </remarks>
public bool HasEnded => DateTimeOffset.Now >= EndDate;
[JsonObject(MemberSerialization.OptIn)]
public class RoomPlaylistItemStats
{
[JsonProperty("count_active")]
public int CountActive;
[JsonProperty("count_total")]
public int CountTotal;
[JsonProperty("ruleset_ids")]
public int[] RulesetIDs = [];
}
[JsonObject(MemberSerialization.OptIn)]
public class RoomDifficultyRange
{
[JsonProperty("min")]
public double Min;
[JsonProperty("max")]
public double Max;
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null!)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
protected bool SetList<T>(ref IReadOnlyList<T> list, IReadOnlyList<T> value, [CallerMemberName] string propertyName = null!)
{
if (list.SequenceEqual(value))
return false;
list = value;
OnPropertyChanged(propertyName);
return true;
}
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;
}
}
}