2021-10-28 05:29:49 +00: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.
2022-06-17 07:37:17 +00:00
#nullable disable
2021-10-28 05:29:49 +00:00
using osu.Framework.Allocation ;
using osu.Framework.Graphics ;
using osu.Framework.Screens ;
2022-02-15 12:08:27 +00:00
using osu.Game.Database ;
2022-02-15 13:02:33 +00:00
using osu.Game.Beatmaps ;
2021-10-28 05:29:49 +00:00
using osu.Game.Online.API ;
using osu.Game.Online.Multiplayer ;
using osu.Game.Screens ;
using osu.Game.Screens.OnlinePlay.Components ;
using osu.Game.Tests.Visual.Multiplayer ;
using osu.Game.Tests.Visual.OnlinePlay ;
namespace osu.Game.Tests.Visual
{
/// <summary>
/// An <see cref="OsuScreen"/> loadable into <see cref="ScreenTestScene"/>s via <see cref="ScreenTestScene.LoadScreen"/>,
/// which provides dependencies for and loads an isolated <see cref="Screens.OnlinePlay.Multiplayer.Multiplayer"/> screen.
/// <p>
/// This screen:
/// <list type="bullet">
/// <item>Provides a <see cref="TestMultiplayerClient"/> to be resolved as a dependency in the <see cref="Screens.OnlinePlay.Multiplayer.Multiplayer"/> screen,
/// which is typically a part of <see cref="OsuGameBase"/>.</item>
/// <item>Rebinds the <see cref="DummyAPIAccess"/> to handle requests via a <see cref="TestRoomRequestsHandler"/>.</item>
/// <item>Provides a <see cref="TestMultiplayerRoomManager"/> for the <see cref="Screens.OnlinePlay.Multiplayer.Multiplayer"/> screen.</item>
/// </list>
/// </p>
/// </summary>
2021-12-20 09:24:59 +00:00
public class TestMultiplayerComponents : OsuScreen
2021-10-28 05:29:49 +00:00
{
public Screens . OnlinePlay . Multiplayer . Multiplayer MultiplayerScreen = > multiplayerScreen ;
public TestMultiplayerRoomManager RoomManager = > multiplayerScreen . RoomManager ;
public IScreen CurrentScreen = > screenStack . CurrentScreen ;
public new bool IsLoaded = > base . IsLoaded & & MultiplayerScreen . IsLoaded ;
[Cached(typeof(MultiplayerClient))]
2022-02-16 00:43:28 +00:00
public readonly TestMultiplayerClient MultiplayerClient ;
2021-10-28 05:29:49 +00:00
2022-02-15 12:08:27 +00:00
[Cached(typeof(UserLookupCache))]
private readonly UserLookupCache userLookupCache = new TestUserLookupCache ( ) ;
[Cached]
private readonly BeatmapLookupCache beatmapLookupCache = new BeatmapLookupCache ( ) ;
2022-02-15 13:02:33 +00:00
[Resolved]
private BeatmapManager beatmapManager { get ; set ; }
2021-10-28 05:29:49 +00:00
private readonly OsuScreenStack screenStack ;
private readonly TestMultiplayer multiplayerScreen ;
2021-12-20 09:24:59 +00:00
public TestMultiplayerComponents ( )
2021-10-28 05:29:49 +00:00
{
multiplayerScreen = new TestMultiplayer ( ) ;
InternalChildren = new Drawable [ ]
{
2022-02-15 12:08:27 +00:00
userLookupCache ,
beatmapLookupCache ,
2022-02-16 00:43:28 +00:00
MultiplayerClient = new TestMultiplayerClient ( RoomManager ) ,
2021-12-20 09:24:14 +00:00
screenStack = new OsuScreenStack
{
2021-12-20 09:24:59 +00:00
Name = nameof ( TestMultiplayerComponents ) ,
2021-12-20 09:24:14 +00:00
RelativeSizeAxes = Axes . Both
}
2021-10-28 05:29:49 +00:00
} ;
screenStack . Push ( multiplayerScreen ) ;
}
[BackgroundDependencyLoader]
2022-02-15 13:02:33 +00:00
private void load ( IAPIProvider api )
2021-10-28 05:29:49 +00:00
{
2022-02-15 13:02:33 +00:00
( ( DummyAPIAccess ) api ) . HandleRequest = request = > multiplayerScreen . RequestsHandler . HandleRequest ( request , api . LocalUser . Value , beatmapManager ) ;
2021-10-28 05:29:49 +00:00
}
2021-12-15 08:37:39 +00:00
public override bool OnBackButton ( ) = > ( screenStack . CurrentScreen as OsuScreen ) ? . OnBackButton ( ) ? ? base . OnBackButton ( ) ;
2021-10-28 05:29:49 +00:00
2022-04-21 15:52:44 +00:00
public override bool OnExiting ( ScreenExitEvent e )
2021-10-28 05:29:49 +00:00
{
if ( screenStack . CurrentScreen = = null )
2022-04-21 15:52:44 +00:00
return base . OnExiting ( e ) ;
2021-10-28 05:29:49 +00:00
screenStack . Exit ( ) ;
return true ;
}
private class TestMultiplayer : Screens . OnlinePlay . Multiplayer . Multiplayer
{
public new TestMultiplayerRoomManager RoomManager { get ; private set ; }
public TestRoomRequestsHandler RequestsHandler { get ; private set ; }
protected override RoomManager CreateRoomManager ( ) = > RoomManager = new TestMultiplayerRoomManager ( RequestsHandler = new TestRoomRequestsHandler ( ) ) ;
}
}
}