// 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.Collections.Generic; namespace osu.Game.Online.RealtimeMultiplayer { [Serializable] public class MultiplayerRoom { /// /// The ID of the room, used for database persistence. /// public long RoomID { get; set; } /// /// The current state of the room (ie. whether it is in progress or otherwise). /// public MultiplayerRoomState State { get; set; } /// /// All currently enforced game settings for this room. /// public MultiplayerRoomSettings Settings { get; set; } private List users = new List(); private object writeLock = new object(); /// /// All users which are currently in this room, in any state. /// public IReadOnlyList Users { get { lock (writeLock) return users.ToArray(); } } /// /// Join a new user to this room. /// public MultiplayerRoomUser Join(int userId) { var user = new MultiplayerRoomUser(userId); PerformUpdate(_ => users.Add(user)); return user; } /// /// Remove a user from this room. /// public MultiplayerRoomUser Leave(int userId) { MultiplayerRoomUser user = null; PerformUpdate(_ => { user = users.Find(u => u.UserID == userId); if (user != null) users.Remove(user); }); return user; } /// /// Perform an update on this room in a thread-safe manner. /// /// The action to perform. public void PerformUpdate(Action action) { lock (writeLock) action(this); } } }