// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; using Newtonsoft.Json; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Development; using osu.Framework.Graphics; using osu.Game.Database; using osu.Game.Online.API; using osu.Game.Online.API.Requests.Responses; using osu.Game.Online.Multiplayer.Countdown; using osu.Game.Online.Rooms; using osu.Game.Online.Rooms.RoomStatuses; using osu.Game.Overlays.Notifications; using osu.Game.Rulesets; using osu.Game.Rulesets.Mods; using osu.Game.Utils; using osu.Game.Localisation; namespace osu.Game.Online.Multiplayer { public abstract partial class MultiplayerClient : Component, IMultiplayerClient, IMultiplayerRoomServer { public Action? PostNotification { protected get; set; } public Action? PresentMatch { protected get; set; } /// /// Invoked when any change occurs to the multiplayer room. /// public virtual event Action? RoomUpdated; /// /// Invoked when a new user joins the room. /// public event Action? UserJoined; /// /// Invoked when a user leaves the room of their own accord. /// public virtual event Action? UserLeft; /// /// Invoked when a user was kicked from the room forcefully. /// public event Action? UserKicked; /// /// Invoked when a new item is added to the playlist. /// public event Action? ItemAdded; /// /// Invoked when a playlist item is removed from the playlist. The provided long is the playlist's item ID. /// public event Action? ItemRemoved; /// /// Invoked when a playlist item's details change. /// public event Action? ItemChanged; /// /// Invoked when the multiplayer server requests the current beatmap to be loaded into play. /// public virtual event Action? LoadRequested; /// /// Invoked when the multiplayer server requests gameplay to be aborted. /// public event Action? GameplayAborted; /// /// Invoked when the multiplayer server requests gameplay to be started. /// public event Action? GameplayStarted; /// /// Invoked when the multiplayer server has finished collating results. /// public event Action? ResultsReady; /// /// Invoked just prior to disconnection requested by the server via . /// public event Action? Disconnecting; /// /// Whether the is currently connected. /// This is NOT thread safe and usage should be scheduled. /// public abstract IBindable IsConnected { get; } /// /// The joined . /// public virtual MultiplayerRoom? Room // virtual for moq { get { Debug.Assert(ThreadSafety.IsUpdateThread); return room; } private set { Debug.Assert(ThreadSafety.IsUpdateThread); room = value; } } private MultiplayerRoom? room; /// /// The users in the joined which are participating in the current gameplay loop. /// public virtual IBindableList CurrentMatchPlayingUserIds => PlayingUserIds; protected readonly BindableList PlayingUserIds = new BindableList(); /// /// The corresponding to the local player, if available. /// public virtual MultiplayerRoomUser? LocalUser => Room?.Users.SingleOrDefault(u => u.User?.Id == API.LocalUser.Value.Id); /// /// Whether the is the host in . /// public virtual bool IsHost { get { var localUser = LocalUser; return localUser != null && Room?.Host != null && localUser.Equals(Room.Host); } } [Resolved] protected IAPIProvider API { get; private set; } = null!; [Resolved] protected IRulesetStore Rulesets { get; private set; } = null!; [Resolved] private UserLookupCache userLookupCache { get; set; } = null!; protected Room? APIRoom { get; private set; } [BackgroundDependencyLoader] private void load() { IsConnected.BindValueChanged(connected => Scheduler.Add(() => { // clean up local room state on server disconnect. if (!connected.NewValue && Room != null) LeaveRoom(); })); } private readonly TaskChain joinOrLeaveTaskChain = new TaskChain(); private CancellationTokenSource? joinCancellationSource; /// /// Joins the for a given API . /// /// The API . /// An optional password to use for the join operation. public async Task JoinRoom(Room room, string? password = null) { if (Room != null) throw new InvalidOperationException("Cannot join a multiplayer room while already in one."); var cancellationSource = joinCancellationSource = new CancellationTokenSource(); await joinOrLeaveTaskChain.Add(async () => { Debug.Assert(room.RoomID.Value != null); // Join the server-side room. var joinedRoom = await JoinRoom(room.RoomID.Value.Value, password ?? room.Password.Value).ConfigureAwait(false); Debug.Assert(joinedRoom != null); // Populate users. Debug.Assert(joinedRoom.Users != null); await Task.WhenAll(joinedRoom.Users.Select(PopulateUser)).ConfigureAwait(false); // Update the stored room (must be done on update thread for thread-safety). await runOnUpdateThreadAsync(() => { Debug.Assert(Room == null); Room = joinedRoom; APIRoom = room; Debug.Assert(joinedRoom.Playlist.Count > 0); APIRoom.Playlist.Clear(); APIRoom.Playlist.AddRange(joinedRoom.Playlist.Select(createPlaylistItem)); APIRoom.CurrentPlaylistItem.Value = APIRoom.Playlist.Single(item => item.ID == joinedRoom.Settings.PlaylistItemId); // The server will null out the end date upon the host joining the room, but the null value is never communicated to the client. APIRoom.EndDate.Value = null; Debug.Assert(LocalUser != null); addUserToAPIRoom(LocalUser); foreach (var user in joinedRoom.Users) updateUserPlayingState(user.UserID, user.State); updateLocalRoomSettings(joinedRoom.Settings); postServerShuttingDownNotification(); OnRoomJoined(); }, cancellationSource.Token).ConfigureAwait(false); }, cancellationSource.Token).ConfigureAwait(false); } /// /// Fired when the room join sequence is complete /// protected virtual void OnRoomJoined() { } /// /// Joins the with a given ID. /// /// The room ID. /// An optional password to use when joining the room. /// The joined . protected abstract Task JoinRoom(long roomId, string? password = null); public Task LeaveRoom() { // The join may have not completed yet, so certain tasks that either update the room or reference the room should be cancelled. // This includes the setting of Room itself along with the initial update of the room settings on join. joinCancellationSource?.Cancel(); // Leaving rooms is expected to occur instantaneously whilst the operation is finalised in the background. // However a few members need to be reset immediately to prevent other components from entering invalid states whilst the operation hasn't yet completed. // For example, if a room was left and the user immediately pressed the "create room" button, then the user could be taken into the lobby if the value of Room is not reset in time. var scheduledReset = runOnUpdateThreadAsync(() => { APIRoom = null; Room = null; PlayingUserIds.Clear(); RoomUpdated?.Invoke(); }); return joinOrLeaveTaskChain.Add(async () => { await scheduledReset.ConfigureAwait(false); await LeaveRoomInternal().ConfigureAwait(false); }); } protected abstract Task LeaveRoomInternal(); public abstract Task InvitePlayer(int userId); /// /// Change the current settings. /// /// /// A room must be joined for this to have any effect. /// /// The new room name, if any. /// The new password, if any. /// The type of the match, if any. /// The new queue mode, if any. /// The new auto-start countdown duration, if any. /// The new auto-skip setting. public Task ChangeSettings(Optional name = default, Optional password = default, Optional matchType = default, Optional queueMode = default, Optional autoStartDuration = default, Optional autoSkip = default) { if (Room == null) throw new InvalidOperationException("Must be joined to a match to change settings."); return ChangeSettings(new MultiplayerRoomSettings { Name = name.GetOr(Room.Settings.Name), Password = password.GetOr(Room.Settings.Password), MatchType = matchType.GetOr(Room.Settings.MatchType), QueueMode = queueMode.GetOr(Room.Settings.QueueMode), AutoStartDuration = autoStartDuration.GetOr(Room.Settings.AutoStartDuration), AutoSkip = autoSkip.GetOr(Room.Settings.AutoSkip) }); } /// /// Toggles the 's ready state. /// /// If a toggle of ready state is not valid at this time. public async Task ToggleReady() { var localUser = LocalUser; if (localUser == null) return; switch (localUser.State) { case MultiplayerUserState.Idle: await ChangeState(MultiplayerUserState.Ready).ConfigureAwait(false); return; case MultiplayerUserState.Ready: await ChangeState(MultiplayerUserState.Idle).ConfigureAwait(false); return; default: throw new InvalidOperationException($"Cannot toggle ready when in {localUser.State}"); } } /// /// Toggles the 's spectating state. /// /// If a toggle of the spectating state is not valid at this time. public async Task ToggleSpectate() { var localUser = LocalUser; if (localUser == null) return; switch (localUser.State) { case MultiplayerUserState.Idle: case MultiplayerUserState.Ready: await ChangeState(MultiplayerUserState.Spectating).ConfigureAwait(false); return; case MultiplayerUserState.Spectating: await ChangeState(MultiplayerUserState.Idle).ConfigureAwait(false); return; default: throw new InvalidOperationException($"Cannot toggle spectate when in {localUser.State}"); } } public abstract Task TransferHost(int userId); public abstract Task KickUser(int userId); public abstract Task ChangeSettings(MultiplayerRoomSettings settings); public abstract Task ChangeState(MultiplayerUserState newState); public abstract Task ChangeBeatmapAvailability(BeatmapAvailability newBeatmapAvailability); public abstract Task DisconnectInternal(); /// /// Change the local user's mods in the currently joined room. /// /// The proposed new mods, excluding any required by the room itself. public Task ChangeUserMods(IEnumerable newMods) => ChangeUserMods(newMods.Select(m => new APIMod(m)).ToList()); public abstract Task ChangeUserMods(IEnumerable newMods); public abstract Task SendMatchRequest(MatchUserRequest request); public abstract Task StartMatch(); public abstract Task AbortGameplay(); public abstract Task AbortMatch(); public abstract Task AddPlaylistItem(MultiplayerPlaylistItem item); public abstract Task EditPlaylistItem(MultiplayerPlaylistItem item); public abstract Task RemovePlaylistItem(long playlistItemId); Task IMultiplayerClient.RoomStateChanged(MultiplayerRoomState state) { Scheduler.Add(() => { if (Room == null) return; Debug.Assert(APIRoom != null); Room.State = state; switch (state) { case MultiplayerRoomState.Open: APIRoom.Status.Value = new RoomStatusOpen(); break; case MultiplayerRoomState.Playing: APIRoom.Status.Value = new RoomStatusPlaying(); break; case MultiplayerRoomState.Closed: APIRoom.Status.Value = new RoomStatusEnded(); break; } RoomUpdated?.Invoke(); }, false); return Task.CompletedTask; } async Task IMultiplayerClient.UserJoined(MultiplayerRoomUser user) { await PopulateUser(user).ConfigureAwait(false); Scheduler.Add(() => { if (Room == null) return; // for sanity, ensure that there can be no duplicate users in the room user list. if (Room.Users.Any(existing => existing.UserID == user.UserID)) return; Room.Users.Add(user); addUserToAPIRoom(user); UserJoined?.Invoke(user); RoomUpdated?.Invoke(); }, false); } 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); } async Task IMultiplayerClient.Invited(int invitedBy, long roomID, string password) { APIUser? apiUser = await userLookupCache.GetUserAsync(invitedBy).ConfigureAwait(false); Room? apiRoom = await getRoomAsync(roomID).ConfigureAwait(false); if (apiUser == null || apiRoom == null) return; PostNotification?.Invoke( new UserAvatarNotification(apiUser, NotificationsStrings.InvitedYouToTheMultiplayer(apiUser.Username, apiRoom.Name.Value)) { Activated = () => { PresentMatch?.Invoke(apiRoom, password); return true; } } ); Task getRoomAsync(long id) { TaskCompletionSource taskCompletionSource = new TaskCompletionSource(); var request = new GetRoomRequest(id); request.Success += room => taskCompletionSource.TrySetResult(room); request.Failure += _ => taskCompletionSource.TrySetResult(null); API.Queue(request); return taskCompletionSource.Task; } } private void addUserToAPIRoom(MultiplayerRoomUser user) { Debug.Assert(APIRoom != null); APIRoom.RecentParticipants.Add(user.User ?? new APIUser { Id = user.UserID, Username = "[Unresolved]" }); APIRoom.ParticipantCount.Value++; } private Task handleUserLeft(MultiplayerRoomUser user, Action? callback) { Scheduler.Add(() => { if (Room == null) return; Room.Users.Remove(user); PlayingUserIds.Remove(user.UserID); Debug.Assert(APIRoom != null); APIRoom.RecentParticipants.RemoveAll(u => u.Id == user.UserID); APIRoom.ParticipantCount.Value--; callback?.Invoke(user); RoomUpdated?.Invoke(); }, false); return Task.CompletedTask; } Task IMultiplayerClient.HostChanged(int userId) { Scheduler.Add(() => { if (Room == null) return; Debug.Assert(APIRoom != null); var user = Room.Users.FirstOrDefault(u => u.UserID == userId); Room.Host = user; APIRoom.Host.Value = user?.User; RoomUpdated?.Invoke(); }, false); return Task.CompletedTask; } Task IMultiplayerClient.SettingsChanged(MultiplayerRoomSettings newSettings) { Scheduler.Add(() => updateLocalRoomSettings(newSettings)); return Task.CompletedTask; } Task IMultiplayerClient.UserStateChanged(int userId, MultiplayerUserState state) { Scheduler.Add(() => { var user = Room?.Users.SingleOrDefault(u => u.UserID == userId); // TODO: user should NEVER be null here, see https://github.com/ppy/osu/issues/17713. if (user == null) return; user.State = state; updateUserPlayingState(userId, state); RoomUpdated?.Invoke(); }, false); return Task.CompletedTask; } Task IMultiplayerClient.MatchUserStateChanged(int userId, MatchUserState state) { Scheduler.Add(() => { var user = Room?.Users.SingleOrDefault(u => u.UserID == userId); // TODO: user should NEVER be null here, see https://github.com/ppy/osu/issues/17713. if (user == null) return; user.MatchState = state; RoomUpdated?.Invoke(); }, false); return Task.CompletedTask; } Task IMultiplayerClient.MatchRoomStateChanged(MatchRoomState state) { Scheduler.Add(() => { if (Room == null) return; Room.MatchState = state; RoomUpdated?.Invoke(); }, false); return Task.CompletedTask; } public Task MatchEvent(MatchServerEvent e) { Scheduler.Add(() => { if (Room == null) return; switch (e) { case CountdownStartedEvent countdownStartedEvent: Room.ActiveCountdowns.Add(countdownStartedEvent.Countdown); switch (countdownStartedEvent.Countdown) { case ServerShuttingDownCountdown: postServerShuttingDownNotification(); break; } break; case CountdownStoppedEvent countdownStoppedEvent: MultiplayerCountdown? countdown = Room.ActiveCountdowns.FirstOrDefault(countdown => countdown.ID == countdownStoppedEvent.ID); if (countdown != null) Room.ActiveCountdowns.Remove(countdown); break; } RoomUpdated?.Invoke(); }, false); return Task.CompletedTask; } private void postServerShuttingDownNotification() { ServerShuttingDownCountdown? countdown = room?.ActiveCountdowns.OfType().FirstOrDefault(); if (countdown == null) return; PostNotification?.Invoke(new ServerShutdownNotification(countdown.TimeRemaining)); } Task IMultiplayerClient.UserBeatmapAvailabilityChanged(int userId, BeatmapAvailability beatmapAvailability) { Scheduler.Add(() => { var user = Room?.Users.SingleOrDefault(u => u.UserID == userId); // errors here are not critical - beatmap availability state is mostly for display. if (user == null) return; user.BeatmapAvailability = beatmapAvailability; RoomUpdated?.Invoke(); }, false); return Task.CompletedTask; } public Task UserModsChanged(int userId, IEnumerable mods) { Scheduler.Add(() => { var user = Room?.Users.SingleOrDefault(u => u.UserID == userId); // errors here are not critical - user mods are mostly for display. if (user == null) return; user.Mods = mods; RoomUpdated?.Invoke(); }, false); return Task.CompletedTask; } Task IMultiplayerClient.LoadRequested() { Scheduler.Add(() => { if (Room == null) return; LoadRequested?.Invoke(); }, false); return Task.CompletedTask; } Task IMultiplayerClient.GameplayAborted(GameplayAbortReason reason) { Scheduler.Add(() => { if (Room == null) return; GameplayAborted?.Invoke(reason); }, false); return Task.CompletedTask; } Task IMultiplayerClient.GameplayStarted() { Scheduler.Add(() => { if (Room == null) return; GameplayStarted?.Invoke(); }, false); return Task.CompletedTask; } Task IMultiplayerClient.ResultsReady() { Scheduler.Add(() => { if (Room == null) return; ResultsReady?.Invoke(); }, false); return Task.CompletedTask; } public Task PlaylistItemAdded(MultiplayerPlaylistItem item) { Scheduler.Add(() => { if (Room == null) return; Debug.Assert(APIRoom != null); Room.Playlist.Add(item); APIRoom.Playlist.Add(createPlaylistItem(item)); ItemAdded?.Invoke(item); RoomUpdated?.Invoke(); }); return Task.CompletedTask; } public Task PlaylistItemRemoved(long playlistItemId) { Scheduler.Add(() => { if (Room == null) return; Debug.Assert(APIRoom != null); Room.Playlist.Remove(Room.Playlist.Single(existing => existing.ID == playlistItemId)); APIRoom.Playlist.RemoveAll(existing => existing.ID == playlistItemId); Debug.Assert(Room.Playlist.Count > 0); ItemRemoved?.Invoke(playlistItemId); RoomUpdated?.Invoke(); }); return Task.CompletedTask; } public Task PlaylistItemChanged(MultiplayerPlaylistItem item) { Scheduler.Add(() => { if (Room == null) return; try { Debug.Assert(APIRoom != null); Room.Playlist[Room.Playlist.IndexOf(Room.Playlist.Single(existing => existing.ID == item.ID))] = item; int existingIndex = APIRoom.Playlist.IndexOf(APIRoom.Playlist.Single(existing => existing.ID == item.ID)); APIRoom.Playlist.RemoveAt(existingIndex); APIRoom.Playlist.Insert(existingIndex, createPlaylistItem(item)); } catch (Exception ex) { throw new AggregateException($"Item: {JsonConvert.SerializeObject(createPlaylistItem(item))}\n\nRoom:{JsonConvert.SerializeObject(APIRoom)}", ex); } ItemChanged?.Invoke(item); RoomUpdated?.Invoke(); }); return Task.CompletedTask; } /// /// Populates the for a given . /// /// The to populate. protected async Task PopulateUser(MultiplayerRoomUser multiplayerUser) => multiplayerUser.User ??= await userLookupCache.GetUserAsync(multiplayerUser.UserID).ConfigureAwait(false); /// /// Updates the local room settings with the given . /// /// /// This updates both the joined and the respective API . /// /// The new to update from. private void updateLocalRoomSettings(MultiplayerRoomSettings settings) { if (Room == null) return; Debug.Assert(APIRoom != null); // Update a few properties of the room instantaneously. Room.Settings = settings; APIRoom.Name.Value = Room.Settings.Name; APIRoom.Password.Value = Room.Settings.Password; APIRoom.Type.Value = Room.Settings.MatchType; APIRoom.QueueMode.Value = Room.Settings.QueueMode; APIRoom.AutoStartDuration.Value = Room.Settings.AutoStartDuration; APIRoom.CurrentPlaylistItem.Value = APIRoom.Playlist.Single(item => item.ID == settings.PlaylistItemId); APIRoom.AutoSkip.Value = Room.Settings.AutoSkip; RoomUpdated?.Invoke(); } private PlaylistItem createPlaylistItem(MultiplayerPlaylistItem item) => new PlaylistItem(new APIBeatmap { OnlineID = item.BeatmapID, StarRating = item.StarRating }) { ID = item.ID, OwnerID = item.OwnerID, RulesetID = item.RulesetID, Expired = item.Expired, PlaylistOrder = item.PlaylistOrder, PlayedAt = item.PlayedAt, RequiredMods = item.RequiredMods.ToArray(), AllowedMods = item.AllowedMods.ToArray() }; /// /// For the provided user ID, update whether the user is included in . /// /// The user's ID. /// The new state of the user. private void updateUserPlayingState(int userId, MultiplayerUserState state) { bool wasPlaying = PlayingUserIds.Contains(userId); bool isPlaying = state >= MultiplayerUserState.WaitingForLoad && state <= MultiplayerUserState.FinishedPlay; if (isPlaying == wasPlaying) return; if (isPlaying) PlayingUserIds.Add(userId); else PlayingUserIds.Remove(userId); } private Task runOnUpdateThreadAsync(Action action, CancellationToken cancellationToken = default) { var tcs = new TaskCompletionSource(); Scheduler.Add(() => { if (cancellationToken.IsCancellationRequested) { tcs.SetCanceled(cancellationToken); return; } try { action(); tcs.SetResult(true); } catch (Exception ex) { tcs.SetException(ex); } }); return tcs.Task; } Task IStatefulUserHubClient.DisconnectRequested() { Schedule(() => { Disconnecting?.Invoke(); DisconnectInternal(); }); return Task.CompletedTask; } } }