2021-04-09 09:16:10 +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.Collections.Generic ;
using System.Linq ;
using NUnit.Framework ;
using osu.Framework.Testing ;
using osu.Framework.Timing ;
2021-08-10 09:39:20 +00:00
using osu.Game.Online.Multiplayer ;
2021-04-09 09:16:10 +00:00
using osu.Game.Rulesets.Osu.Scoring ;
using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate ;
using osu.Game.Screens.Play.HUD ;
2021-08-09 10:05:23 +00:00
using osu.Game.Users ;
2021-04-09 09:16:10 +00:00
namespace osu.Game.Tests.Visual.Multiplayer
{
2021-04-26 07:48:32 +00:00
public class TestSceneMultiSpectatorLeaderboard : MultiplayerTestScene
2021-04-09 09:16:10 +00:00
{
2021-06-25 06:00:10 +00:00
private Dictionary < int , ManualClock > clocks ;
2021-06-24 12:02:04 +00:00
private MultiSpectatorLeaderboard leaderboard ;
2021-04-09 09:16:10 +00:00
[SetUpSteps]
public new void SetUpSteps ( )
{
AddStep ( "reset" , ( ) = >
{
Clear ( ) ;
2021-06-24 12:02:04 +00:00
clocks = new Dictionary < int , ManualClock >
2021-04-09 09:16:10 +00:00
{
2021-06-25 06:00:10 +00:00
{ PLAYER_1_ID , new ManualClock ( ) } ,
{ PLAYER_2_ID , new ManualClock ( ) }
2021-06-24 12:02:04 +00:00
} ;
foreach ( var ( userId , _ ) in clocks )
2021-08-09 10:05:23 +00:00
{
2021-06-25 06:00:10 +00:00
SpectatorClient . StartPlay ( userId , 0 ) ;
2021-08-09 10:05:23 +00:00
OnlinePlayDependencies . Client . AddUser ( new User { Id = userId } ) ;
}
2021-04-09 09:16:10 +00:00
} ) ;
AddStep ( "create leaderboard" , ( ) = >
{
Beatmap . Value = CreateWorkingBeatmap ( Ruleset . Value ) ;
var playable = Beatmap . Value . GetPlayableBeatmap ( Ruleset . Value ) ;
var scoreProcessor = new OsuScoreProcessor ( ) ;
scoreProcessor . ApplyBeatmap ( playable ) ;
2021-08-10 09:39:20 +00:00
LoadComponentAsync ( leaderboard = new MultiSpectatorLeaderboard ( scoreProcessor , clocks . Keys . Select ( id = > new MultiplayerRoomUser ( id ) ) . ToArray ( ) ) { Expanded = { Value = true } } , Add ) ;
2021-04-09 09:16:10 +00:00
} ) ;
AddUntilStep ( "wait for load" , ( ) = > leaderboard . IsLoaded ) ;
AddStep ( "add clock sources" , ( ) = >
{
foreach ( var ( userId , clock ) in clocks )
leaderboard . AddClock ( userId , clock ) ;
} ) ;
}
[Test]
public void TestLeaderboardTracksCurrentTime ( )
{
AddStep ( "send frames" , ( ) = >
{
2021-04-26 08:22:16 +00:00
// For player 1, send frames in sets of 1.
// For player 2, send frames in sets of 10.
2021-04-09 09:16:10 +00:00
for ( int i = 0 ; i < 100 ; i + + )
{
2021-06-29 08:06:57 +00:00
SpectatorClient . SendFrames ( PLAYER_1_ID , 1 ) ;
2021-04-09 09:16:10 +00:00
if ( i % 10 = = 0 )
2021-06-29 08:06:57 +00:00
SpectatorClient . SendFrames ( PLAYER_2_ID , 10 ) ;
2021-04-09 09:16:10 +00:00
}
} ) ;
2021-04-26 08:22:16 +00:00
assertCombo ( PLAYER_1_ID , 1 ) ;
assertCombo ( PLAYER_2_ID , 10 ) ;
2021-04-09 09:16:10 +00:00
2021-04-26 08:22:16 +00:00
// Advance to a point where only user player 1's frame changes.
2021-04-09 09:16:10 +00:00
setTime ( 500 ) ;
2021-04-26 08:22:16 +00:00
assertCombo ( PLAYER_1_ID , 5 ) ;
assertCombo ( PLAYER_2_ID , 10 ) ;
2021-04-09 09:16:10 +00:00
2021-04-09 09:23:41 +00:00
// Advance to a point where both user's frame changes.
2021-04-09 09:16:10 +00:00
setTime ( 1100 ) ;
2021-04-26 08:22:16 +00:00
assertCombo ( PLAYER_1_ID , 11 ) ;
assertCombo ( PLAYER_2_ID , 20 ) ;
2021-04-09 09:23:41 +00:00
2021-04-26 08:22:16 +00:00
// Advance user player 2 only to a point where its frame changes.
setTime ( PLAYER_2_ID , 2100 ) ;
assertCombo ( PLAYER_1_ID , 11 ) ;
assertCombo ( PLAYER_2_ID , 30 ) ;
2021-04-09 09:23:41 +00:00
// Advance both users beyond their last frame
setTime ( 101 * 100 ) ;
2021-04-26 08:22:16 +00:00
assertCombo ( PLAYER_1_ID , 100 ) ;
assertCombo ( PLAYER_2_ID , 100 ) ;
2021-04-09 09:16:10 +00:00
}
2021-04-09 09:18:23 +00:00
[Test]
public void TestNoFrames ( )
{
2021-04-26 08:22:16 +00:00
assertCombo ( PLAYER_1_ID , 0 ) ;
assertCombo ( PLAYER_2_ID , 0 ) ;
2021-04-09 09:18:23 +00:00
}
2021-04-09 09:16:10 +00:00
private void setTime ( double time ) = > AddStep ( $"set time {time}" , ( ) = >
{
foreach ( var ( _ , clock ) in clocks )
clock . CurrentTime = time ;
} ) ;
2021-04-09 09:23:41 +00:00
private void setTime ( int userId , double time )
= > AddStep ( $"set user {userId} time {time}" , ( ) = > clocks [ userId ] . CurrentTime = time ) ;
2021-04-09 09:16:10 +00:00
private void assertCombo ( int userId , int expectedCombo )
= > AddUntilStep ( $"player {userId} has {expectedCombo} combo" , ( ) = > this . ChildrenOfType < GameplayLeaderboardScore > ( ) . Single ( s = > s . User ? . Id = = userId ) . Combo . Value = = expectedCombo ) ;
}
}