osu/osu.Game/Online/RealtimeMultiplayer/MultiplayerRoom.cs

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