2020-12-04 06:34:31 +00:00
|
|
|
// 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;
|
2020-12-04 06:34:31 +00:00
|
|
|
|
|
|
|
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>
|
2020-12-04 06:34:31 +00:00
|
|
|
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>
|
2020-12-04 06:34:31 +00:00
|
|
|
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>
|
2020-12-08 05:33:38 +00:00
|
|
|
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>
|
2020-12-07 16:24:38 +00:00
|
|
|
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:24:38 +00:00
|
|
|
{
|
2020-12-07 16:35:29 +00:00
|
|
|
var user = new MultiplayerRoomUser(userId);
|
2020-12-08 06:46:11 +00:00
|
|
|
PerformUpdate(_ => users.Add(user));
|
2020-12-07 16:35:29 +00:00
|
|
|
return user;
|
2020-12-07 16:24:38 +00:00
|
|
|
}
|
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)
|
2020-12-07 16:24:38 +00:00
|
|
|
{
|
2020-12-08 06:46:11 +00:00
|
|
|
MultiplayerRoomUser user = null;
|
|
|
|
|
|
|
|
PerformUpdate(_ =>
|
2020-12-07 16:35:29 +00:00
|
|
|
{
|
2020-12-08 06:46:11 +00:00
|
|
|
user = users.Find(u => u.UserID == userId);
|
2020-12-07 16:35:29 +00:00
|
|
|
|
2020-12-08 06:46:11 +00:00
|
|
|
if (user != null)
|
|
|
|
users.Remove(user);
|
|
|
|
});
|
2020-12-07 16:35:29 +00:00
|
|
|
|
2020-12-08 06:46:11 +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);
|
2020-12-07 16:24:38 +00:00
|
|
|
}
|
2020-12-04 06:34:31 +00:00
|
|
|
}
|
|
|
|
}
|