1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 16:27:26 +08:00

Add ability to handle user join/leave/kick events in MultiplayerComposites

This commit is contained in:
Dean Herbert 2021-08-25 14:25:57 +09:00
parent 6fcbf81995
commit e1ab3434ed
2 changed files with 57 additions and 13 deletions

View File

@ -33,6 +33,12 @@ namespace osu.Game.Online.Multiplayer
/// </summary> /// </summary>
public event Action? RoomUpdated; public event Action? RoomUpdated;
public event Action<MultiplayerRoomUser>? UserJoined;
public event Action<MultiplayerRoomUser>? UserLeft;
public event Action<MultiplayerRoomUser>? UserKicked;
/// <summary> /// <summary>
/// Invoked when the multiplayer server requests the current beatmap to be loaded into play. /// Invoked when the multiplayer server requests the current beatmap to be loaded into play.
/// </summary> /// </summary>
@ -366,11 +372,26 @@ namespace osu.Game.Online.Multiplayer
Room.Users.Add(user); Room.Users.Add(user);
UserJoined?.Invoke(user);
RoomUpdated?.Invoke(); RoomUpdated?.Invoke();
}, false); }, false);
} }
Task IMultiplayerClient.UserLeft(MultiplayerRoomUser user) Task IMultiplayerClient.UserLeft(MultiplayerRoomUser user) =>
handleUserLeft(user, UserLeft);
Task IMultiplayerClient.UserKicked(MultiplayerRoomUser user)
{
if (LocalUser == null)
return Task.CompletedTask;
if (user.Equals(LocalUser))
LeaveRoom();
return handleUserLeft(user, UserKicked);
}
private Task handleUserLeft(MultiplayerRoomUser user, Action<MultiplayerRoomUser>? callback)
{ {
if (Room == null) if (Room == null)
return Task.CompletedTask; return Task.CompletedTask;
@ -383,24 +404,13 @@ namespace osu.Game.Online.Multiplayer
Room.Users.Remove(user); Room.Users.Remove(user);
PlayingUserIds.Remove(user.UserID); PlayingUserIds.Remove(user.UserID);
callback?.Invoke(user);
RoomUpdated?.Invoke(); RoomUpdated?.Invoke();
}, false); }, false);
return Task.CompletedTask; return Task.CompletedTask;
} }
Task IMultiplayerClient.UserKicked(MultiplayerRoomUser user)
{
if (LocalUser == null)
return Task.CompletedTask;
if (user.Equals(LocalUser))
LeaveRoom();
// TODO: also inform users of the kick operation.
return ((IMultiplayerClient)this).UserLeft(user);
}
Task IMultiplayerClient.HostChanged(int userId) Task IMultiplayerClient.HostChanged(int userId)
{ {
if (Room == null) if (Room == null)

View File

@ -20,9 +20,38 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer
base.LoadComplete(); base.LoadComplete();
Client.RoomUpdated += OnRoomUpdated; Client.RoomUpdated += OnRoomUpdated;
Client.UserLeft += UserLeft;
Client.UserKicked += UserKicked;
Client.UserJoined += UserJoined;
OnRoomUpdated(); OnRoomUpdated();
} }
/// <summary>
/// Invoked when a user has joined the room.
/// </summary>
/// <param name="user">The user.</param>
protected virtual void UserJoined(MultiplayerRoomUser user)
{
}
/// <summary>
/// Invoked when a user has been kicked from the room (including the local user).
/// </summary>
/// <param name="user">The user.</param>
protected virtual void UserKicked(MultiplayerRoomUser user)
{
}
/// <summary>
/// Invoked when a user has left the room.
/// </summary>
/// <param name="user">The user.</param>
protected virtual void UserLeft(MultiplayerRoomUser user)
{
}
/// <summary> /// <summary>
/// Invoked when any change occurs to the multiplayer room. /// Invoked when any change occurs to the multiplayer room.
/// </summary> /// </summary>
@ -33,7 +62,12 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {
if (Client != null) if (Client != null)
{
Client.UserLeft -= UserLeft;
Client.UserKicked -= UserKicked;
Client.UserJoined -= UserJoined;
Client.RoomUpdated -= OnRoomUpdated; Client.RoomUpdated -= OnRoomUpdated;
}
base.Dispose(isDisposing); base.Dispose(isDisposing);
} }