2020-12-20 22:26:31 +08: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.
#nullable enable
2021-05-20 14:39:45 +08:00
using System ;
2021-02-01 16:57:32 +08:00
using System.Collections.Generic ;
2021-05-20 14:39:45 +08:00
using System.Diagnostics ;
using System.Linq ;
2020-12-20 22:26:31 +08:00
using System.Threading ;
using System.Threading.Tasks ;
2021-02-09 13:02:32 +08:00
using osu.Framework.Allocation ;
2020-12-20 22:26:31 +08:00
using osu.Framework.Bindables ;
2022-04-08 13:42:54 +08:00
using osu.Framework.Development ;
2021-05-20 14:39:45 +08:00
using osu.Framework.Graphics ;
using osu.Framework.Logging ;
using osu.Game.Database ;
2020-12-20 22:26:31 +08:00
using osu.Game.Online.API ;
2021-11-04 11:30:19 +08:00
using osu.Game.Online.API.Requests.Responses ;
2022-03-17 18:26:42 +08:00
using osu.Game.Online.Multiplayer.Countdown ;
2021-01-03 09:32:50 +08:00
using osu.Game.Online.Rooms ;
2021-05-20 14:39:45 +08:00
using osu.Game.Online.Rooms.RoomStatuses ;
using osu.Game.Rulesets ;
using osu.Game.Rulesets.Mods ;
using osu.Game.Utils ;
2021-11-04 17:02:44 +08:00
using APIUser = osu . Game . Online . API . Requests . Responses . APIUser ;
2020-12-20 22:26:31 +08:00
2020-12-25 12:38:11 +08:00
namespace osu.Game.Online.Multiplayer
2020-12-20 22:26:31 +08:00
{
2021-05-20 14:39:45 +08:00
public abstract class MultiplayerClient : Component , IMultiplayerClient , IMultiplayerRoomServer
2020-12-20 22:26:31 +08:00
{
2021-05-20 14:39:45 +08:00
/// <summary>
/// Invoked when any change occurs to the multiplayer room.
/// </summary>
2022-04-12 18:26:02 +08:00
public virtual event Action ? RoomUpdated ;
2021-05-20 14:39:45 +08:00
2021-12-02 15:45:26 +08:00
/// <summary>
/// Invoked when a new user joins the room.
/// </summary>
2021-08-25 13:25:57 +08:00
public event Action < MultiplayerRoomUser > ? UserJoined ;
2021-12-02 15:45:26 +08:00
/// <summary>
/// Invoked when a user leaves the room of their own accord.
/// </summary>
2022-04-11 17:51:37 +08:00
public virtual event Action < MultiplayerRoomUser > ? UserLeft ;
2021-08-25 13:25:57 +08:00
2021-12-02 15:45:26 +08:00
/// <summary>
/// Invoked when a user was kicked from the room forcefully.
/// </summary>
2021-08-25 13:25:57 +08:00
public event Action < MultiplayerRoomUser > ? UserKicked ;
2021-12-02 15:45:26 +08:00
/// <summary>
/// Invoked when a new item is added to the playlist.
/// </summary>
2021-11-22 17:39:50 +08:00
public event Action < MultiplayerPlaylistItem > ? ItemAdded ;
2021-12-02 15:45:26 +08:00
/// <summary>
/// Invoked when a playlist item is removed from the playlist. The provided <c>long</c> is the playlist's item ID.
/// </summary>
2021-11-22 17:39:50 +08:00
public event Action < long > ? ItemRemoved ;
2021-12-02 15:45:26 +08:00
/// <summary>
/// Invoked when a playlist item's details change.
/// </summary>
2021-11-22 17:39:50 +08:00
public event Action < MultiplayerPlaylistItem > ? ItemChanged ;
2021-05-20 14:39:45 +08:00
/// <summary>
/// Invoked when the multiplayer server requests the current beatmap to be loaded into play.
/// </summary>
2022-04-13 19:57:40 +08:00
public virtual event Action ? LoadRequested ;
2021-05-20 14:39:45 +08:00
2022-04-21 21:38:58 +08:00
/// <summary>
/// Invoked when the multiplayer server requests loading of play to be aborted.
/// </summary>
public event Action ? LoadAborted ;
2021-05-20 14:39:45 +08:00
/// <summary>
/// Invoked when the multiplayer server requests gameplay to be started.
/// </summary>
2022-04-21 21:37:26 +08:00
public event Action ? GameplayStarted ;
2021-05-20 14:39:45 +08:00
/// <summary>
/// Invoked when the multiplayer server has finished collating results.
/// </summary>
public event Action ? ResultsReady ;
/// <summary>
/// Whether the <see cref="MultiplayerClient"/> is currently connected.
/// This is NOT thread safe and usage should be scheduled.
/// </summary>
public abstract IBindable < bool > IsConnected { get ; }
/// <summary>
/// The joined <see cref="MultiplayerRoom"/>.
/// </summary>
2022-04-12 18:26:02 +08:00
public virtual MultiplayerRoom ? Room
2022-04-08 13:42:54 +08:00
{
get
{
Debug . Assert ( ThreadSafety . IsUpdateThread ) ;
return room ;
}
private set
{
Debug . Assert ( ThreadSafety . IsUpdateThread ) ;
room = value ;
}
}
private MultiplayerRoom ? room ;
2021-05-20 14:39:45 +08:00
/// <summary>
/// The users in the joined <see cref="Room"/> which are participating in the current gameplay loop.
/// </summary>
2022-04-11 17:51:37 +08:00
public virtual IBindableList < int > CurrentMatchPlayingUserIds = > PlayingUserIds ;
2021-08-09 18:18:13 +08:00
protected readonly BindableList < int > PlayingUserIds = new BindableList < int > ( ) ;
2021-05-20 14:39:45 +08:00
/// <summary>
/// The <see cref="MultiplayerRoomUser"/> corresponding to the local player, if available.
/// </summary>
2022-04-13 16:02:33 +08:00
public virtual MultiplayerRoomUser ? LocalUser = > Room ? . Users . SingleOrDefault ( u = > u . User ? . Id = = API . LocalUser . Value . Id ) ;
2021-05-20 14:39:45 +08:00
/// <summary>
/// Whether the <see cref="LocalUser"/> is the host in <see cref="Room"/>.
/// </summary>
2022-04-13 16:02:33 +08:00
public virtual bool IsHost
2021-05-20 14:39:45 +08:00
{
get
{
var localUser = LocalUser ;
return localUser ! = null & & Room ? . Host ! = null & & localUser . Equals ( Room . Host ) ;
}
}
[Resolved]
protected IAPIProvider API { get ; private set ; } = null ! ;
2021-02-15 15:31:00 +08:00
2021-05-20 14:39:45 +08:00
[Resolved]
2021-12-03 17:14:44 +08:00
protected IRulesetStore Rulesets { get ; private set ; } = null ! ;
2020-12-20 22:26:31 +08:00
2021-05-20 14:39:45 +08:00
[Resolved]
private UserLookupCache userLookupCache { get ; set ; } = null ! ;
2020-12-20 22:26:31 +08:00
2021-07-19 19:20:08 +08:00
protected Room ? APIRoom { get ; private set ; }
2020-12-24 16:58:38 +08:00
2021-05-20 14:39:45 +08:00
[BackgroundDependencyLoader]
private void load ( )
2020-12-24 16:58:38 +08:00
{
2022-04-08 16:41:03 +08:00
IsConnected . BindValueChanged ( connected = > Scheduler . Add ( ( ) = >
2021-05-20 14:39:45 +08:00
{
// clean up local room state on server disconnect.
if ( ! connected . NewValue & & Room ! = null )
{
Logger . Log ( "Connection to multiplayer server was lost." , LoggingTarget . Runtime , LogLevel . Important ) ;
LeaveRoom ( ) ;
}
2022-04-08 16:41:03 +08:00
} ) ) ;
2021-02-09 13:02:32 +08:00
}
2021-05-20 14:39:45 +08:00
private readonly TaskChain joinOrLeaveTaskChain = new TaskChain ( ) ;
private CancellationTokenSource ? joinCancellationSource ;
/// <summary>
/// Joins the <see cref="MultiplayerRoom"/> for a given API <see cref="Room"/>.
/// </summary>
/// <param name="room">The API <see cref="Room"/>.</param>
2021-07-10 15:08:12 +08:00
/// <param name="password">An optional password to use for the join operation.</param>
public async Task JoinRoom ( Room room , string? password = null )
2021-02-09 13:02:32 +08:00
{
2022-04-08 13:52:56 +08:00
if ( Room ! = null )
throw new InvalidOperationException ( "Cannot join a multiplayer room while already in one." ) ;
2021-05-20 14:39:45 +08:00
var cancellationSource = joinCancellationSource = new CancellationTokenSource ( ) ;
2021-02-15 15:31:00 +08:00
2021-05-20 14:39:45 +08:00
await joinOrLeaveTaskChain . Add ( async ( ) = >
2020-12-20 22:26:31 +08:00
{
2021-05-20 14:39:45 +08:00
Debug . Assert ( room . RoomID . Value ! = null ) ;
// Join the server-side room.
2021-07-10 15:08:12 +08:00
var joinedRoom = await JoinRoom ( room . RoomID . Value . Value , password ? ? room . Password . Value ) . ConfigureAwait ( false ) ;
2021-05-20 14:39:45 +08:00
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).
2022-04-08 13:43:53 +08:00
await runOnUpdateThreadAsync ( ( ) = >
2020-12-20 22:26:31 +08:00
{
2022-04-08 13:52:56 +08:00
Debug . Assert ( Room = = null ) ;
2021-05-20 14:39:45 +08:00
Room = joinedRoom ;
2021-07-19 19:20:08 +08:00
APIRoom = room ;
2021-11-02 15:36:28 +08:00
2022-03-24 16:47:58 +08:00
Debug . Assert ( joinedRoom . Playlist . Count > 0 ) ;
2021-11-16 12:49:06 +08:00
APIRoom . Playlist . Clear ( ) ;
2021-12-10 19:08:59 +08:00
APIRoom . Playlist . AddRange ( joinedRoom . Playlist . Select ( createPlaylistItem ) ) ;
2022-06-03 20:12:28 +08:00
APIRoom . CurrentPlaylistItem . Value = APIRoom . Playlist . Single ( item = > item . ID = = joinedRoom . Settings . PlaylistItemId ) ;
2021-11-16 12:49:06 +08:00
2021-11-02 15:36:28 +08:00
Debug . Assert ( LocalUser ! = null ) ;
addUserToAPIRoom ( LocalUser ) ;
2021-05-20 14:39:45 +08:00
foreach ( var user in joinedRoom . Users )
updateUserPlayingState ( user . UserID , user . State ) ;
2021-08-03 16:08:19 +08:00
2021-11-23 14:07:17 +08:00
updateLocalRoomSettings ( joinedRoom . Settings ) ;
2021-08-03 16:08:19 +08:00
OnRoomJoined ( ) ;
2021-05-20 14:39:45 +08:00
} , cancellationSource . Token ) . ConfigureAwait ( false ) ;
} , cancellationSource . Token ) . ConfigureAwait ( false ) ;
}
2021-08-03 16:08:19 +08:00
/// <summary>
/// Fired when the room join sequence is complete
/// </summary>
protected virtual void OnRoomJoined ( )
{
}
2021-05-20 14:39:45 +08:00
/// <summary>
/// Joins the <see cref="MultiplayerRoom"/> with a given ID.
/// </summary>
/// <param name="roomId">The room ID.</param>
2021-07-09 15:01:45 +08:00
/// <param name="password">An optional password to use when joining the room.</param>
2021-05-20 14:39:45 +08:00
/// <returns>The joined <see cref="MultiplayerRoom"/>.</returns>
2021-07-09 15:01:45 +08:00
protected abstract Task < MultiplayerRoom > JoinRoom ( long roomId , string? password = null ) ;
2021-05-20 14:39:45 +08:00
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.
2022-04-08 13:43:53 +08:00
var scheduledReset = runOnUpdateThreadAsync ( ( ) = >
2021-05-20 14:39:45 +08:00
{
2021-07-19 19:20:08 +08:00
APIRoom = null ;
2021-05-20 14:39:45 +08:00
Room = null ;
2021-08-09 18:18:13 +08:00
PlayingUserIds . Clear ( ) ;
2021-05-20 14:39:45 +08:00
RoomUpdated ? . Invoke ( ) ;
} ) ;
return joinOrLeaveTaskChain . Add ( async ( ) = >
{
await scheduledReset . ConfigureAwait ( false ) ;
2021-08-14 23:24:14 +08:00
await LeaveRoomInternal ( ) . ConfigureAwait ( false ) ;
2021-05-20 14:39:45 +08:00
} ) ;
}
protected abstract Task LeaveRoomInternal ( ) ;
/// <summary>
/// Change the current <see cref="MultiplayerRoom"/> settings.
/// </summary>
/// <remarks>
/// A room must be joined for this to have any effect.
/// </remarks>
/// <param name="name">The new room name, if any.</param>
2021-07-10 12:50:52 +08:00
/// <param name="password">The new password, if any.</param>
2021-07-28 14:30:57 +08:00
/// <param name="matchType">The type of the match, if any.</param>
2021-10-20 19:25:06 +08:00
/// <param name="queueMode">The new queue mode, if any.</param>
2022-03-18 21:00:39 +08:00
/// <param name="autoStartDuration">The new auto-start countdown duration, if any.</param>
public Task ChangeSettings ( Optional < string > name = default , Optional < string > password = default , Optional < MatchType > matchType = default , Optional < QueueMode > queueMode = default ,
Optional < TimeSpan > autoStartDuration = default )
2021-05-20 14:39:45 +08:00
{
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 ) ,
2021-07-10 12:50:52 +08:00
Password = password . GetOr ( Room . Settings . Password ) ,
2021-07-28 14:30:57 +08:00
MatchType = matchType . GetOr ( Room . Settings . MatchType ) ,
2021-10-20 19:25:06 +08:00
QueueMode = queueMode . GetOr ( Room . Settings . QueueMode ) ,
2022-03-18 21:00:39 +08:00
AutoStartDuration = autoStartDuration . GetOr ( Room . Settings . AutoStartDuration ) ,
2021-05-20 14:39:45 +08:00
} ) ;
}
/// <summary>
/// Toggles the <see cref="LocalUser"/>'s ready state.
/// </summary>
/// <exception cref="InvalidOperationException">If a toggle of ready state is not valid at this time.</exception>
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}" ) ;
}
}
/// <summary>
/// Toggles the <see cref="LocalUser"/>'s spectating state.
/// </summary>
/// <exception cref="InvalidOperationException">If a toggle of the spectating state is not valid at this time.</exception>
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}" ) ;
2021-02-15 15:31:00 +08:00
}
2020-12-20 22:26:31 +08:00
}
2021-05-20 14:39:45 +08:00
public abstract Task TransferHost ( int userId ) ;
2021-08-11 16:20:41 +08:00
public abstract Task KickUser ( int userId ) ;
2021-05-20 14:39:45 +08:00
public abstract Task ChangeSettings ( MultiplayerRoomSettings settings ) ;
public abstract Task ChangeState ( MultiplayerUserState newState ) ;
public abstract Task ChangeBeatmapAvailability ( BeatmapAvailability newBeatmapAvailability ) ;
/// <summary>
/// Change the local user's mods in the currently joined room.
/// </summary>
/// <param name="newMods">The proposed new mods, excluding any required by the room itself.</param>
public Task ChangeUserMods ( IEnumerable < Mod > newMods ) = > ChangeUserMods ( newMods . Select ( m = > new APIMod ( m ) ) . ToList ( ) ) ;
public abstract Task ChangeUserMods ( IEnumerable < APIMod > newMods ) ;
2021-08-03 14:43:04 +08:00
public abstract Task SendMatchRequest ( MatchUserRequest request ) ;
2021-07-21 18:13:56 +08:00
2021-05-20 14:39:45 +08:00
public abstract Task StartMatch ( ) ;
2021-12-14 15:52:57 +08:00
public abstract Task AbortGameplay ( ) ;
2021-12-14 10:30:42 +08:00
2021-11-15 22:14:27 +08:00
public abstract Task AddPlaylistItem ( MultiplayerPlaylistItem item ) ;
2021-10-22 15:48:28 +08:00
2021-12-10 13:44:35 +08:00
public abstract Task EditPlaylistItem ( MultiplayerPlaylistItem item ) ;
2021-12-09 01:12:33 +08:00
public abstract Task RemovePlaylistItem ( long playlistItemId ) ;
2021-05-20 14:39:45 +08:00
Task IMultiplayerClient . RoomStateChanged ( MultiplayerRoomState state )
{
Scheduler . Add ( ( ) = >
{
if ( Room = = null )
return ;
2021-07-19 19:20:08 +08:00
Debug . Assert ( APIRoom ! = null ) ;
2021-05-20 14:39:45 +08:00
Room . State = state ;
switch ( state )
{
case MultiplayerRoomState . Open :
2021-07-19 19:20:08 +08:00
APIRoom . Status . Value = new RoomStatusOpen ( ) ;
2021-05-20 14:39:45 +08:00
break ;
case MultiplayerRoomState . Playing :
2021-07-19 19:20:08 +08:00
APIRoom . Status . Value = new RoomStatusPlaying ( ) ;
2021-05-20 14:39:45 +08:00
break ;
case MultiplayerRoomState . Closed :
2021-07-19 19:20:08 +08:00
APIRoom . Status . Value = new RoomStatusEnded ( ) ;
2021-05-20 14:39:45 +08:00
break ;
}
RoomUpdated ? . Invoke ( ) ;
} , false ) ;
return Task . CompletedTask ;
}
async Task IMultiplayerClient . UserJoined ( MultiplayerRoomUser user )
{
2021-10-15 12:27:54 +08:00
await PopulateUser ( user ) . ConfigureAwait ( false ) ;
2021-05-20 14:39:45 +08:00
2021-10-15 12:27:54 +08:00
Scheduler . Add ( ( ) = >
{
if ( Room = = null )
return ;
2021-05-20 14:39:45 +08:00
2021-10-15 12:27:54 +08:00
// 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 ;
2021-05-20 14:39:45 +08:00
2021-10-15 12:27:54 +08:00
Room . Users . Add ( user ) ;
2021-10-14 23:10:39 +08:00
2021-11-02 15:36:28 +08:00
addUserToAPIRoom ( user ) ;
2021-10-15 12:27:54 +08:00
UserJoined ? . Invoke ( user ) ;
RoomUpdated ? . Invoke ( ) ;
} ) ;
2021-05-20 14:39:45 +08:00
}
2021-08-25 13:25:57 +08:00
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 ) ;
}
2021-11-02 15:36:28 +08:00
private void addUserToAPIRoom ( MultiplayerRoomUser user )
{
Debug . Assert ( APIRoom ! = null ) ;
2021-11-04 17:02:44 +08:00
APIRoom . RecentParticipants . Add ( user . User ? ? new APIUser
2021-11-02 15:51:42 +08:00
{
Id = user . UserID ,
Username = "[Unresolved]"
} ) ;
2021-11-02 15:36:28 +08:00
APIRoom . ParticipantCount . Value + + ;
}
2021-08-25 13:25:57 +08:00
private Task handleUserLeft ( MultiplayerRoomUser user , Action < MultiplayerRoomUser > ? callback )
2020-12-20 22:26:31 +08:00
{
2021-05-20 14:39:45 +08:00
Scheduler . Add ( ( ) = >
{
if ( Room = = null )
return ;
Room . Users . Remove ( user ) ;
2021-08-09 18:18:13 +08:00
PlayingUserIds . Remove ( user . UserID ) ;
2020-12-20 22:26:31 +08:00
2021-11-02 15:36:28 +08:00
Debug . Assert ( APIRoom ! = null ) ;
APIRoom . RecentParticipants . RemoveAll ( u = > u . Id = = user . UserID ) ;
APIRoom . ParticipantCount . Value - - ;
2021-08-25 13:25:57 +08:00
callback ? . Invoke ( user ) ;
2021-05-20 14:39:45 +08:00
RoomUpdated ? . Invoke ( ) ;
} , false ) ;
return Task . CompletedTask ;
2020-12-20 22:26:31 +08:00
}
2021-05-20 14:39:45 +08:00
Task IMultiplayerClient . HostChanged ( int userId )
2020-12-20 22:26:31 +08:00
{
2021-05-20 14:39:45 +08:00
Scheduler . Add ( ( ) = >
{
if ( Room = = null )
return ;
2020-12-20 22:26:31 +08:00
2021-07-19 19:20:08 +08:00
Debug . Assert ( APIRoom ! = null ) ;
2021-05-20 14:39:45 +08:00
var user = Room . Users . FirstOrDefault ( u = > u . UserID = = userId ) ;
Room . Host = user ;
2021-07-19 19:20:08 +08:00
APIRoom . Host . Value = user ? . User ;
2021-05-20 14:39:45 +08:00
RoomUpdated ? . Invoke ( ) ;
} , false ) ;
return Task . CompletedTask ;
2020-12-20 22:26:31 +08:00
}
2021-12-01 21:43:03 +08:00
Task IMultiplayerClient . SettingsChanged ( MultiplayerRoomSettings newSettings )
2021-11-13 02:34:45 +08:00
{
2021-12-10 19:08:59 +08:00
Scheduler . Add ( ( ) = > updateLocalRoomSettings ( newSettings ) ) ;
2021-12-01 21:43:03 +08:00
return Task . CompletedTask ;
2021-11-13 02:34:45 +08:00
}
2021-05-20 14:39:45 +08:00
Task IMultiplayerClient . UserStateChanged ( int userId , MultiplayerUserState state )
{
Scheduler . Add ( ( ) = >
{
2022-04-08 16:55:43 +08:00
var user = Room ? . Users . SingleOrDefault ( u = > u . UserID = = userId ) ;
2021-05-20 14:39:45 +08:00
2022-04-08 16:55:43 +08:00
// TODO: user should NEVER be null here, see https://github.com/ppy/osu/issues/17713.
if ( user = = null )
return ;
2021-05-20 14:39:45 +08:00
2022-04-08 16:55:43 +08:00
user . State = state ;
2021-05-20 14:39:45 +08:00
updateUserPlayingState ( userId , state ) ;
RoomUpdated ? . Invoke ( ) ;
} , false ) ;
return Task . CompletedTask ;
2020-12-20 22:26:31 +08:00
}
2021-08-03 14:43:04 +08:00
Task IMultiplayerClient . MatchUserStateChanged ( int userId , MatchUserState state )
2021-07-21 17:59:02 +08:00
{
2021-07-30 17:10:38 +08:00
Scheduler . Add ( ( ) = >
{
2022-04-08 16:55:43 +08:00
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 )
2021-07-30 17:10:38 +08:00
return ;
2022-04-08 16:55:43 +08:00
user . MatchState = state ;
2021-07-30 17:10:38 +08:00
RoomUpdated ? . Invoke ( ) ;
} , false ) ;
return Task . CompletedTask ;
2021-07-21 17:59:02 +08:00
}
2021-08-03 14:43:04 +08:00
Task IMultiplayerClient . MatchRoomStateChanged ( MatchRoomState state )
2021-07-21 17:59:02 +08:00
{
2021-07-30 17:10:38 +08:00
Scheduler . Add ( ( ) = >
{
if ( Room = = null )
return ;
2021-08-03 14:43:04 +08:00
Room . MatchState = state ;
2021-07-30 17:10:38 +08:00
RoomUpdated ? . Invoke ( ) ;
2021-08-03 13:53:04 +08:00
} , false ) ;
2021-07-30 17:10:38 +08:00
return Task . CompletedTask ;
2021-07-21 17:59:02 +08:00
}
2021-08-03 14:43:04 +08:00
public Task MatchEvent ( MatchServerEvent e )
2021-07-26 16:38:08 +08:00
{
2022-03-17 18:26:42 +08:00
Scheduler . Add ( ( ) = >
{
if ( Room = = null )
return ;
switch ( e )
{
case CountdownChangedEvent countdownChangedEvent :
Room . Countdown = countdownChangedEvent . Countdown ;
break ;
}
RoomUpdated ? . Invoke ( ) ;
} , false ) ;
2021-07-30 17:10:38 +08:00
return Task . CompletedTask ;
2021-07-26 16:38:08 +08:00
}
2021-05-20 14:39:45 +08:00
Task IMultiplayerClient . UserBeatmapAvailabilityChanged ( int userId , BeatmapAvailability beatmapAvailability )
2020-12-20 22:26:31 +08:00
{
2021-05-20 14:39:45 +08:00
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 ;
2020-12-20 22:26:31 +08:00
}
2021-05-20 14:39:45 +08:00
public Task UserModsChanged ( int userId , IEnumerable < APIMod > mods )
2020-12-20 22:26:31 +08:00
{
2021-05-20 14:39:45 +08:00
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 ;
2020-12-20 22:26:31 +08:00
}
2021-05-20 14:39:45 +08:00
Task IMultiplayerClient . LoadRequested ( )
2021-01-03 09:32:50 +08:00
{
2021-05-20 14:39:45 +08:00
Scheduler . Add ( ( ) = >
{
if ( Room = = null )
return ;
LoadRequested ? . Invoke ( ) ;
} , false ) ;
return Task . CompletedTask ;
2021-01-03 09:32:50 +08:00
}
2022-04-21 21:38:58 +08:00
Task IMultiplayerClient . LoadAborted ( )
{
Scheduler . Add ( ( ) = >
{
if ( Room = = null )
return ;
LoadAborted ? . Invoke ( ) ;
} , false ) ;
return Task . CompletedTask ;
}
2022-04-21 21:37:26 +08:00
Task IMultiplayerClient . GameplayStarted ( )
2021-02-01 16:57:32 +08:00
{
2021-05-20 14:39:45 +08:00
Scheduler . Add ( ( ) = >
{
if ( Room = = null )
return ;
2022-04-21 21:37:26 +08:00
GameplayStarted ? . Invoke ( ) ;
2021-05-20 14:39:45 +08:00
} , false ) ;
return Task . CompletedTask ;
2021-02-01 16:57:32 +08:00
}
2021-05-20 14:39:45 +08:00
Task IMultiplayerClient . ResultsReady ( )
2020-12-20 22:26:31 +08:00
{
2021-05-20 14:39:45 +08:00
Scheduler . Add ( ( ) = >
{
if ( Room = = null )
return ;
ResultsReady ? . Invoke ( ) ;
} , false ) ;
return Task . CompletedTask ;
2020-12-20 22:26:31 +08:00
}
2021-02-09 13:02:32 +08:00
2021-12-10 19:08:59 +08:00
public Task PlaylistItemAdded ( MultiplayerPlaylistItem item )
2021-10-22 15:48:28 +08:00
{
2021-11-13 02:34:45 +08:00
Scheduler . Add ( ( ) = >
2021-10-22 15:48:28 +08:00
{
if ( Room = = null )
return ;
Debug . Assert ( APIRoom ! = null ) ;
2021-11-19 15:35:42 +08:00
Room . Playlist . Add ( item ) ;
2021-12-10 19:08:59 +08:00
APIRoom . Playlist . Add ( createPlaylistItem ( item ) ) ;
2021-11-19 15:35:42 +08:00
2021-11-22 17:39:50 +08:00
ItemAdded ? . Invoke ( item ) ;
2021-10-22 15:48:28 +08:00
RoomUpdated ? . Invoke ( ) ;
2021-11-13 02:34:45 +08:00
} ) ;
2021-12-10 19:08:59 +08:00
return Task . CompletedTask ;
2021-10-22 15:48:28 +08:00
}
2021-11-10 18:58:25 +08:00
public Task PlaylistItemRemoved ( long playlistItemId )
2021-10-22 15:48:28 +08:00
{
2021-11-13 02:34:45 +08:00
Scheduler . Add ( ( ) = >
2021-10-22 15:48:28 +08:00
{
if ( Room = = null )
return ;
Debug . Assert ( APIRoom ! = null ) ;
2021-11-19 15:35:42 +08:00
Room . Playlist . Remove ( Room . Playlist . Single ( existing = > existing . ID = = playlistItemId ) ) ;
2021-11-16 15:06:30 +08:00
APIRoom . Playlist . RemoveAll ( existing = > existing . ID = = playlistItemId ) ;
2021-11-19 15:35:42 +08:00
2022-03-24 16:47:58 +08:00
Debug . Assert ( Room . Playlist . Count > 0 ) ;
2021-11-22 17:39:50 +08:00
ItemRemoved ? . Invoke ( playlistItemId ) ;
2021-10-22 15:48:28 +08:00
RoomUpdated ? . Invoke ( ) ;
} ) ;
2021-11-13 02:34:45 +08:00
return Task . CompletedTask ;
2021-10-22 15:48:28 +08:00
}
2021-12-10 19:08:59 +08:00
public Task PlaylistItemChanged ( MultiplayerPlaylistItem item )
2021-10-22 19:53:45 +08:00
{
2021-11-13 02:34:45 +08:00
Scheduler . Add ( ( ) = >
2021-10-22 19:53:45 +08:00
{
if ( Room = = null )
return ;
Debug . Assert ( APIRoom ! = null ) ;
2021-11-19 15:36:32 +08:00
Room . Playlist [ Room . Playlist . IndexOf ( Room . Playlist . Single ( existing = > existing . ID = = item . ID ) ) ] = item ;
2021-10-29 15:44:39 +08:00
2021-11-19 15:35:42 +08:00
int existingIndex = APIRoom . Playlist . IndexOf ( APIRoom . Playlist . Single ( existing = > existing . ID = = item . ID ) ) ;
2021-11-16 13:44:47 +08:00
APIRoom . Playlist . RemoveAt ( existingIndex ) ;
2021-12-10 19:08:59 +08:00
APIRoom . Playlist . Insert ( existingIndex , createPlaylistItem ( item ) ) ;
2021-11-10 20:23:18 +08:00
2021-11-22 17:39:50 +08:00
ItemChanged ? . Invoke ( item ) ;
2021-11-10 20:23:18 +08:00
RoomUpdated ? . Invoke ( ) ;
2021-11-13 02:34:45 +08:00
} ) ;
2021-12-10 19:08:59 +08:00
return Task . CompletedTask ;
2021-10-22 19:53:45 +08:00
}
2021-05-20 14:39:45 +08:00
/// <summary>
2021-11-04 17:02:44 +08:00
/// Populates the <see cref="APIUser"/> for a given <see cref="MultiplayerRoomUser"/>.
2021-05-20 14:39:45 +08:00
/// </summary>
/// <param name="multiplayerUser">The <see cref="MultiplayerRoomUser"/> to populate.</param>
protected async Task PopulateUser ( MultiplayerRoomUser multiplayerUser ) = > multiplayerUser . User ? ? = await userLookupCache . GetUserAsync ( multiplayerUser . UserID ) . ConfigureAwait ( false ) ;
/// <summary>
/// Updates the local room settings with the given <see cref="MultiplayerRoomSettings"/>.
/// </summary>
/// <remarks>
/// This updates both the joined <see cref="MultiplayerRoom"/> and the respective API <see cref="Room"/>.
/// </remarks>
/// <param name="settings">The new <see cref="MultiplayerRoomSettings"/> to update from.</param>
2021-11-23 14:07:17 +08:00
private void updateLocalRoomSettings ( MultiplayerRoomSettings settings )
2021-03-01 16:24:54 +08:00
{
2021-05-20 14:39:45 +08:00
if ( Room = = null )
return ;
2021-07-19 19:20:08 +08:00
Debug . Assert ( APIRoom ! = null ) ;
2021-03-01 16:24:54 +08:00
2021-05-20 14:39:45 +08:00
// Update a few properties of the room instantaneously.
Room . Settings = settings ;
2021-07-19 19:20:08 +08:00
APIRoom . Name . Value = Room . Settings . Name ;
APIRoom . Password . Value = Room . Settings . Password ;
2021-11-30 16:09:40 +08:00
APIRoom . Type . Value = Room . Settings . MatchType ;
2021-11-22 12:46:01 +08:00
APIRoom . QueueMode . Value = Room . Settings . QueueMode ;
2022-03-24 19:23:58 +08:00
APIRoom . AutoStartDuration . Value = Room . Settings . AutoStartDuration ;
2022-06-03 20:12:28 +08:00
APIRoom . CurrentPlaylistItem . Value = APIRoom . Playlist . Single ( item = > item . ID = = settings . PlaylistItemId ) ;
2021-05-20 14:39:45 +08:00
2021-12-10 19:08:59 +08:00
RoomUpdated ? . Invoke ( ) ;
2021-11-23 14:07:17 +08:00
}
2021-05-20 14:39:45 +08:00
2022-02-15 22:33:26 +08:00
private PlaylistItem createPlaylistItem ( MultiplayerPlaylistItem item ) = > new PlaylistItem ( new APIBeatmap { OnlineID = item . BeatmapID } )
2021-10-22 19:53:45 +08:00
{
2022-02-15 15:01:14 +08:00
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 ( )
} ;
2021-10-22 19:53:45 +08:00
2021-05-20 14:39:45 +08:00
/// <summary>
/// For the provided user ID, update whether the user is included in <see cref="CurrentMatchPlayingUserIds"/>.
/// </summary>
/// <param name="userId">The user's ID.</param>
/// <param name="state">The new state of the user.</param>
private void updateUserPlayingState ( int userId , MultiplayerUserState state )
2021-02-09 13:02:32 +08:00
{
2021-08-09 18:18:13 +08:00
bool wasPlaying = PlayingUserIds . Contains ( userId ) ;
2021-05-20 14:39:45 +08:00
bool isPlaying = state > = MultiplayerUserState . WaitingForLoad & & state < = MultiplayerUserState . FinishedPlay ;
if ( isPlaying = = wasPlaying )
return ;
if ( isPlaying )
2021-08-09 18:18:13 +08:00
PlayingUserIds . Add ( userId ) ;
2021-05-20 14:39:45 +08:00
else
2021-08-09 18:18:13 +08:00
PlayingUserIds . Remove ( userId ) ;
2021-05-20 14:39:45 +08:00
}
2022-04-08 13:43:53 +08:00
private Task runOnUpdateThreadAsync ( Action action , CancellationToken cancellationToken = default )
2021-05-20 14:39:45 +08:00
{
var tcs = new TaskCompletionSource < bool > ( ) ;
Scheduler . Add ( ( ) = >
{
if ( cancellationToken . IsCancellationRequested )
{
tcs . SetCanceled ( ) ;
return ;
}
try
{
action ( ) ;
tcs . SetResult ( true ) ;
}
catch ( Exception ex )
{
tcs . SetException ( ex ) ;
}
} ) ;
return tcs . Task ;
2021-02-09 13:02:32 +08:00
}
2020-12-20 22:26:31 +08:00
}
}