mirror of
https://github.com/ppy/osu.git
synced 2025-01-13 10:43:04 +08:00
Add ability to handle user join/leave/kick events in MultiplayerComposite
s
This commit is contained in:
parent
6fcbf81995
commit
e1ab3434ed
@ -33,6 +33,12 @@ namespace osu.Game.Online.Multiplayer
|
||||
/// </summary>
|
||||
public event Action? RoomUpdated;
|
||||
|
||||
public event Action<MultiplayerRoomUser>? UserJoined;
|
||||
|
||||
public event Action<MultiplayerRoomUser>? UserLeft;
|
||||
|
||||
public event Action<MultiplayerRoomUser>? UserKicked;
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when the multiplayer server requests the current beatmap to be loaded into play.
|
||||
/// </summary>
|
||||
@ -366,11 +372,26 @@ namespace osu.Game.Online.Multiplayer
|
||||
|
||||
Room.Users.Add(user);
|
||||
|
||||
UserJoined?.Invoke(user);
|
||||
RoomUpdated?.Invoke();
|
||||
}, 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)
|
||||
return Task.CompletedTask;
|
||||
@ -383,24 +404,13 @@ namespace osu.Game.Online.Multiplayer
|
||||
Room.Users.Remove(user);
|
||||
PlayingUserIds.Remove(user.UserID);
|
||||
|
||||
callback?.Invoke(user);
|
||||
RoomUpdated?.Invoke();
|
||||
}, false);
|
||||
|
||||
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)
|
||||
{
|
||||
if (Room == null)
|
||||
|
@ -20,9 +20,38 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer
|
||||
base.LoadComplete();
|
||||
|
||||
Client.RoomUpdated += OnRoomUpdated;
|
||||
|
||||
Client.UserLeft += UserLeft;
|
||||
Client.UserKicked += UserKicked;
|
||||
Client.UserJoined += UserJoined;
|
||||
|
||||
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>
|
||||
/// Invoked when any change occurs to the multiplayer room.
|
||||
/// </summary>
|
||||
@ -33,7 +62,12 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
if (Client != null)
|
||||
{
|
||||
Client.UserLeft -= UserLeft;
|
||||
Client.UserKicked -= UserKicked;
|
||||
Client.UserJoined -= UserJoined;
|
||||
Client.RoomUpdated -= OnRoomUpdated;
|
||||
}
|
||||
|
||||
base.Dispose(isDisposing);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user