2020-12-18 16:17:24 +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.
using System ;
using System.Collections.Generic ;
2020-12-18 16:22:52 +00:00
using System.Linq ;
2020-12-18 16:17:24 +00:00
using osu.Framework.Allocation ;
2020-12-18 16:57:40 +00:00
using osu.Framework.Bindables ;
2020-12-18 16:17:24 +00:00
using osu.Game.Online.API ;
using osu.Game.Online.API.Requests ;
2020-12-25 04:38:11 +00:00
using osu.Game.Online.Rooms ;
2020-12-18 16:17:24 +00:00
using osu.Game.Rulesets.Scoring ;
using osu.Game.Scoring ;
2021-06-25 09:02:53 +00:00
using osu.Game.Screens.OnlinePlay.Components ;
2020-12-25 15:50:00 +00:00
using osu.Game.Screens.OnlinePlay.Lounge.Components ;
using osu.Game.Screens.OnlinePlay.Multiplayer ;
2020-12-18 16:17:24 +00:00
2020-12-25 04:38:11 +00:00
namespace osu.Game.Tests.Visual.Multiplayer
2020-12-18 16:17:24 +00:00
{
2021-06-25 09:02:53 +00:00
/// <summary>
/// A <see cref="RoomManager"/> for use in multiplayer test scenes. Should generally not be used by itself outside of a <see cref="MultiplayerTestScene"/>.
/// </summary>
2020-12-25 04:38:11 +00:00
public class TestMultiplayerRoomManager : MultiplayerRoomManager
2020-12-18 16:17:24 +00:00
{
[Resolved]
private IAPIProvider api { get ; set ; }
[Resolved]
private OsuGameBase game { get ; set ; }
2020-12-18 16:57:40 +00:00
[Cached]
public readonly Bindable < FilterCriteria > Filter = new Bindable < FilterCriteria > ( new FilterCriteria ( ) ) ;
2021-03-03 10:40:19 +00:00
public new readonly List < Room > Rooms = new List < Room > ( ) ;
2020-12-18 16:17:24 +00:00
2021-06-25 06:00:10 +00:00
[BackgroundDependencyLoader]
private void load ( )
2020-12-18 16:17:24 +00:00
{
int currentScoreId = 0 ;
2020-12-18 16:57:40 +00:00
int currentRoomId = 0 ;
2021-03-01 08:24:05 +00:00
int currentPlaylistItemId = 0 ;
2020-12-18 16:17:24 +00:00
( ( DummyAPIAccess ) api ) . HandleRequest = req = >
{
switch ( req )
{
case CreateRoomRequest createRoomRequest :
var createdRoom = new APICreatedRoom ( ) ;
createdRoom . CopyFrom ( createRoomRequest . Room ) ;
2020-12-18 16:57:40 +00:00
createdRoom . RoomID . Value ? ? = currentRoomId + + ;
2020-12-18 16:17:24 +00:00
2021-03-01 08:24:05 +00:00
for ( int i = 0 ; i < createdRoom . Playlist . Count ; i + + )
createdRoom . Playlist [ i ] . ID = currentPlaylistItemId + + ;
2021-03-03 10:40:19 +00:00
Rooms . Add ( createdRoom ) ;
2020-12-18 16:17:24 +00:00
createRoomRequest . TriggerSuccess ( createdRoom ) ;
2021-03-23 09:08:32 +00:00
return true ;
2020-12-18 16:17:24 +00:00
case JoinRoomRequest joinRoomRequest :
joinRoomRequest . TriggerSuccess ( ) ;
2021-03-23 09:08:32 +00:00
return true ;
2020-12-18 16:17:24 +00:00
case PartRoomRequest partRoomRequest :
partRoomRequest . TriggerSuccess ( ) ;
2021-03-23 09:08:32 +00:00
return true ;
2020-12-18 16:17:24 +00:00
case GetRoomsRequest getRoomsRequest :
2020-12-18 16:22:52 +00:00
var roomsWithoutParticipants = new List < Room > ( ) ;
2021-03-03 10:40:19 +00:00
foreach ( var r in Rooms )
2020-12-18 16:22:52 +00:00
{
var newRoom = new Room ( ) ;
newRoom . CopyFrom ( r ) ;
newRoom . RecentParticipants . Clear ( ) ;
roomsWithoutParticipants . Add ( newRoom ) ;
}
getRoomsRequest . TriggerSuccess ( roomsWithoutParticipants ) ;
2021-03-23 09:08:32 +00:00
return true ;
2020-12-18 16:22:52 +00:00
case GetRoomRequest getRoomRequest :
2021-03-03 10:40:19 +00:00
getRoomRequest . TriggerSuccess ( Rooms . Single ( r = > r . RoomID . Value = = getRoomRequest . RoomId ) ) ;
2021-03-23 09:08:32 +00:00
return true ;
2020-12-18 16:17:24 +00:00
case GetBeatmapSetRequest getBeatmapSetRequest :
var onlineReq = new GetBeatmapSetRequest ( getBeatmapSetRequest . ID , getBeatmapSetRequest . Type ) ;
onlineReq . Success + = res = > getBeatmapSetRequest . TriggerSuccess ( res ) ;
onlineReq . Failure + = e = > getBeatmapSetRequest . TriggerFailure ( e ) ;
// Get the online API from the game's dependencies.
game . Dependencies . Get < IAPIProvider > ( ) . Queue ( onlineReq ) ;
2021-03-23 09:08:32 +00:00
return true ;
2020-12-18 16:17:24 +00:00
case CreateRoomScoreRequest createRoomScoreRequest :
createRoomScoreRequest . TriggerSuccess ( new APIScoreToken { ID = 1 } ) ;
2021-03-23 09:08:32 +00:00
return true ;
2020-12-18 16:17:24 +00:00
case SubmitRoomScoreRequest submitRoomScoreRequest :
submitRoomScoreRequest . TriggerSuccess ( new MultiplayerScore
{
ID = currentScoreId + + ,
Accuracy = 1 ,
EndedAt = DateTimeOffset . Now ,
Passed = true ,
Rank = ScoreRank . S ,
MaxCombo = 1000 ,
TotalScore = 1000000 ,
User = api . LocalUser . Value ,
Statistics = new Dictionary < HitResult , int > ( )
} ) ;
2021-03-23 09:08:32 +00:00
return true ;
2020-12-18 16:17:24 +00:00
}
2021-03-23 09:08:32 +00:00
return false ;
2020-12-18 16:17:24 +00:00
} ;
}
2020-12-18 16:57:40 +00:00
public new void ClearRooms ( ) = > base . ClearRooms ( ) ;
2020-12-18 16:17:24 +00:00
public new void Schedule ( Action action ) = > base . Schedule ( action ) ;
}
}