// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Game.Online.Multiplayer; using osu.Game.Online.Rooms; namespace osu.Game.Screens.OnlinePlay.Multiplayer { public abstract class MultiplayerRoomComposite : OnlinePlayComposite { [CanBeNull] protected MultiplayerRoom Room => Client.Room; [Resolved] protected MultiplayerClient Client { get; private set; } protected override void LoadComplete() { base.LoadComplete(); Client.RoomUpdated += invokeOnRoomUpdated; Client.UserLeft += invokeUserLeft; Client.UserKicked += invokeUserKicked; Client.UserJoined += invokeUserJoined; Client.ItemAdded += invokeItemAdded; Client.ItemRemoved += invokeItemRemoved; Client.ItemChanged += invokeItemChanged; OnRoomUpdated(); } private void invokeOnRoomUpdated() => Scheduler.AddOnce(OnRoomUpdated); private void invokeUserJoined(MultiplayerRoomUser user) => Scheduler.AddOnce(UserJoined, user); private void invokeUserKicked(MultiplayerRoomUser user) => Scheduler.AddOnce(UserKicked, user); private void invokeUserLeft(MultiplayerRoomUser user) => Scheduler.AddOnce(UserLeft, user); private void invokeItemAdded(MultiplayerPlaylistItem item) => Scheduler.AddOnce(PlaylistItemAdded, item); private void invokeItemRemoved(long item) => Scheduler.AddOnce(PlaylistItemRemoved, item); private void invokeItemChanged(MultiplayerPlaylistItem item) => Scheduler.AddOnce(PlaylistItemChanged, item); /// /// Invoked when a user has joined the room. /// /// The user. protected virtual void UserJoined(MultiplayerRoomUser user) { } /// /// Invoked when a user has been kicked from the room (including the local user). /// /// The user. protected virtual void UserKicked(MultiplayerRoomUser user) { } /// /// Invoked when a user has left the room. /// /// The user. protected virtual void UserLeft(MultiplayerRoomUser user) { } /// /// Invoked when a playlist item is added to the room. /// /// The added playlist item. protected virtual void PlaylistItemAdded(MultiplayerPlaylistItem item) { } /// /// Invoked when a playlist item is removed from the room. /// /// The ID of the removed playlist item. protected virtual void PlaylistItemRemoved(long item) { } /// /// Invoked when a playlist item is changed in the room. /// /// The new playlist item, with an existing item's ID. protected virtual void PlaylistItemChanged(MultiplayerPlaylistItem item) { } /// /// Invoked when any change occurs to the multiplayer room. /// protected virtual void OnRoomUpdated() { } protected override void Dispose(bool isDisposing) { if (Client != null) { Client.RoomUpdated -= invokeOnRoomUpdated; Client.UserLeft -= invokeUserLeft; Client.UserKicked -= invokeUserKicked; Client.UserJoined -= invokeUserJoined; Client.ItemAdded -= invokeItemAdded; Client.ItemRemoved -= invokeItemRemoved; Client.ItemChanged -= invokeItemChanged; } base.Dispose(isDisposing); } } }