2020-12-28 19:01:17 +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.Linq;
|
|
|
|
using Humanizer;
|
|
|
|
using NUnit.Framework;
|
2021-05-14 21:51:06 +00:00
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
2020-12-28 19:01:17 +00:00
|
|
|
using osu.Framework.Testing;
|
2021-11-04 09:02:44 +00:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2020-12-28 19:01:17 +00:00
|
|
|
using osu.Game.Online.Multiplayer;
|
2021-06-25 06:00:10 +00:00
|
|
|
using osu.Game.Online.Rooms;
|
2020-12-28 19:01:17 +00:00
|
|
|
using osu.Game.Tests.Visual.Multiplayer;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.NonVisual.Multiplayer
|
|
|
|
{
|
|
|
|
[HeadlessTest]
|
|
|
|
public class StatefulMultiplayerClientTest : MultiplayerTestScene
|
|
|
|
{
|
|
|
|
[Test]
|
|
|
|
public void TestPlayingUserTracking()
|
|
|
|
{
|
|
|
|
int id = 2000;
|
|
|
|
|
2021-11-04 09:02:44 +00:00
|
|
|
AddRepeatStep("add some users", () => Client.AddUser(new APIUser { Id = id++ }), 5);
|
2020-12-28 19:01:17 +00:00
|
|
|
checkPlayingUserCount(0);
|
|
|
|
|
2021-08-10 07:50:39 +00:00
|
|
|
AddAssert("playlist item is available", () => Client.CurrentMatchPlayingItem.Value != null);
|
|
|
|
|
2020-12-28 19:01:17 +00:00
|
|
|
changeState(3, MultiplayerUserState.WaitingForLoad);
|
|
|
|
checkPlayingUserCount(3);
|
|
|
|
|
|
|
|
changeState(3, MultiplayerUserState.Playing);
|
|
|
|
checkPlayingUserCount(3);
|
|
|
|
|
|
|
|
changeState(3, MultiplayerUserState.Results);
|
|
|
|
checkPlayingUserCount(0);
|
|
|
|
|
|
|
|
changeState(6, MultiplayerUserState.WaitingForLoad);
|
|
|
|
checkPlayingUserCount(6);
|
|
|
|
|
2021-05-14 21:51:06 +00:00
|
|
|
AddStep("another user left", () => Client.RemoveUser((Client.Room?.Users.Last().User).AsNonNull()));
|
2020-12-28 19:01:17 +00:00
|
|
|
checkPlayingUserCount(5);
|
|
|
|
|
|
|
|
AddStep("leave room", () => Client.LeaveRoom());
|
|
|
|
checkPlayingUserCount(0);
|
2021-08-10 07:50:39 +00:00
|
|
|
|
|
|
|
AddAssert("playlist item is null", () => Client.CurrentMatchPlayingItem.Value == null);
|
2020-12-28 19:01:17 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 14:22:44 +00:00
|
|
|
[Test]
|
|
|
|
public void TestPlayingUsersUpdatedOnJoin()
|
|
|
|
{
|
|
|
|
AddStep("leave room", () => Client.LeaveRoom());
|
|
|
|
AddUntilStep("wait for room part", () => Client.Room == null);
|
|
|
|
|
|
|
|
AddStep("create room initially in gameplay", () =>
|
|
|
|
{
|
2021-06-25 06:00:10 +00:00
|
|
|
var newRoom = new Room();
|
|
|
|
newRoom.CopyFrom(SelectedRoom.Value);
|
|
|
|
|
|
|
|
newRoom.RoomID.Value = null;
|
2021-04-22 14:22:44 +00:00
|
|
|
Client.RoomSetupAction = room =>
|
|
|
|
{
|
|
|
|
room.State = MultiplayerRoomState.Playing;
|
2021-04-26 08:22:16 +00:00
|
|
|
room.Users.Add(new MultiplayerRoomUser(PLAYER_1_ID)
|
2021-04-22 14:22:44 +00:00
|
|
|
{
|
2021-11-04 09:02:44 +00:00
|
|
|
User = new APIUser { Id = PLAYER_1_ID },
|
2021-04-22 14:22:44 +00:00
|
|
|
State = MultiplayerUserState.Playing
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-06-25 06:00:10 +00:00
|
|
|
RoomManager.CreateRoom(newRoom);
|
2021-04-22 14:22:44 +00:00
|
|
|
});
|
|
|
|
|
2021-11-23 07:04:11 +00:00
|
|
|
AddUntilStep("wait for room join", () => RoomJoined);
|
2021-04-22 14:22:44 +00:00
|
|
|
checkPlayingUserCount(1);
|
|
|
|
}
|
|
|
|
|
2020-12-28 19:01:17 +00:00
|
|
|
private void checkPlayingUserCount(int expectedCount)
|
2020-12-29 05:27:33 +00:00
|
|
|
=> AddAssert($"{"user".ToQuantity(expectedCount)} playing", () => Client.CurrentMatchPlayingUserIds.Count == expectedCount);
|
2020-12-28 19:01:17 +00:00
|
|
|
|
|
|
|
private void changeState(int userCount, MultiplayerUserState state)
|
|
|
|
=> AddStep($"{"user".ToQuantity(userCount)} in {state}", () =>
|
|
|
|
{
|
|
|
|
for (int i = 0; i < userCount; ++i)
|
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
int userId = Client.Room?.Users[i].UserID ?? throw new AssertionException("Room cannot be null!");
|
2020-12-28 19:01:17 +00:00
|
|
|
Client.ChangeUserState(userId, state);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|