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