1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 11:37:58 +08:00
osu-lazer/osu.Game/Tests/Visual/Multiplayer/TestMultiplayerClient.cs

415 lines
16 KiB
C#
Raw Normal View History

// 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.
#nullable enable
using System;
2021-02-01 16:54:56 +08:00
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Beatmaps;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
2020-12-25 12:38:11 +08:00
using osu.Game.Online.Multiplayer;
using osu.Game.Online.Multiplayer.MatchTypes.TeamVersus;
using osu.Game.Online.Multiplayer.Queueing;
using osu.Game.Online.Rooms;
2021-02-01 16:54:56 +08:00
using osu.Game.Rulesets.Mods;
using APIUser = osu.Game.Online.API.Requests.Responses.APIUser;
2020-12-25 12:38:11 +08:00
namespace osu.Game.Tests.Visual.Multiplayer
{
2021-06-25 17:02:53 +08:00
/// <summary>
/// A <see cref="MultiplayerClient"/> for use in multiplayer test scenes. Should generally not be used by itself outside of a <see cref="MultiplayerTestScene"/>.
/// </summary>
2021-05-20 14:39:45 +08:00
public class TestMultiplayerClient : MultiplayerClient
{
public override IBindable<bool> IsConnected => isConnected;
private readonly Bindable<bool> isConnected = new Bindable<bool>(true);
public new Room? APIRoom => base.APIRoom;
2021-05-11 18:01:41 +08:00
2021-04-22 22:22:44 +08:00
public Action<MultiplayerRoom>? RoomSetupAction;
[Resolved]
private IAPIProvider api { get; set; } = null!;
[Resolved]
private BeatmapManager beatmaps { get; set; } = null!;
2021-10-27 15:10:22 +08:00
private readonly TestMultiplayerRoomManager roomManager;
2021-03-03 18:40:19 +08:00
2021-10-27 15:10:22 +08:00
public TestMultiplayerClient(TestMultiplayerRoomManager roomManager)
2021-03-03 18:40:19 +08:00
{
this.roomManager = roomManager;
}
public void Connect() => isConnected.Value = true;
public void Disconnect() => isConnected.Value = false;
public MultiplayerRoomUser AddUser(APIUser user, bool markAsPlaying = false)
{
var roomUser = new MultiplayerRoomUser(user.Id) { User = user };
2021-10-14 23:10:39 +08:00
addUser(roomUser);
if (markAsPlaying)
PlayingUserIds.Add(user.Id);
return roomUser;
}
public void TestAddUnresolvedUser() => addUser(new MultiplayerRoomUser(TestUserLookupCache.UNRESOLVED_USER_ID));
2021-10-14 23:10:39 +08:00
private void addUser(MultiplayerRoomUser user)
{
((IMultiplayerClient)this).UserJoined(user).Wait();
2021-10-14 23:20:45 +08:00
// We want the user to be immediately available for testing, so force a scheduler update to run the update-bound continuation.
2021-10-14 23:10:39 +08:00
Scheduler.Update();
}
public void RemoveUser(APIUser user)
{
Debug.Assert(Room != null);
2020-12-24 04:00:47 +08:00
((IMultiplayerClient)this).UserLeft(new MultiplayerRoomUser(user.Id));
Schedule(() =>
{
if (Room.Users.Any())
TransferHost(Room.Users.First().UserID);
});
}
2021-04-07 15:35:36 +08:00
public void ChangeRoomState(MultiplayerRoomState newState)
{
Debug.Assert(Room != null);
((IMultiplayerClient)this).RoomStateChanged(newState);
}
public void ChangeUserState(int userId, MultiplayerUserState newState)
{
Debug.Assert(Room != null);
((IMultiplayerClient)this).UserStateChanged(userId, newState);
Schedule(() =>
{
switch (Room.State)
{
case MultiplayerRoomState.WaitingForLoad:
if (Room.Users.All(u => u.State != MultiplayerUserState.WaitingForLoad))
{
foreach (var u in Room.Users.Where(u => u.State == MultiplayerUserState.Loaded))
ChangeUserState(u.UserID, MultiplayerUserState.Playing);
((IMultiplayerClient)this).MatchStarted();
ChangeRoomState(MultiplayerRoomState.Playing);
}
break;
case MultiplayerRoomState.Playing:
if (Room.Users.All(u => u.State != MultiplayerUserState.Playing))
{
foreach (var u in Room.Users.Where(u => u.State == MultiplayerUserState.FinishedPlay))
ChangeUserState(u.UserID, MultiplayerUserState.Results);
ChangeRoomState(MultiplayerRoomState.Open);
((IMultiplayerClient)this).ResultsReady();
2021-10-22 21:07:41 +08:00
finishPlaylistItem().ContinueWith(_ => advanceToNextPlaylistItem());
}
break;
}
});
}
public void ChangeUserBeatmapAvailability(int userId, BeatmapAvailability newBeatmapAvailability)
{
Debug.Assert(Room != null);
((IMultiplayerClient)this).UserBeatmapAvailabilityChanged(userId, newBeatmapAvailability);
}
protected override Task<MultiplayerRoom> JoinRoom(long roomId, string? password = null)
{
var apiRoom = roomManager.ServerSideRooms.Single(r => r.RoomID.Value == roomId);
if (password != apiRoom.Password.Value)
throw new InvalidOperationException("Invalid password.");
2021-04-22 22:22:44 +08:00
var localUser = new MultiplayerRoomUser(api.LocalUser.Value.Id)
{
User = api.LocalUser.Value
};
var room = new MultiplayerRoom(roomId)
{
Settings =
{
Name = apiRoom.Name.Value,
MatchType = apiRoom.Type.Value,
2021-10-22 21:07:41 +08:00
PlaylistItemId = apiRoom.Playlist.Single().ID,
2021-10-22 19:14:04 +08:00
Password = password,
QueueMode = apiRoom.QueueMode.Value
},
2021-04-22 22:22:44 +08:00
Users = { localUser },
Host = localUser
};
2021-04-22 22:22:44 +08:00
RoomSetupAction?.Invoke(room);
RoomSetupAction = null;
return Task.FromResult(room);
}
protected override void OnRoomJoined()
{
Debug.Assert(Room != null);
// emulate the server sending this after the join room. scheduler required to make sure the join room event is fired first (in Join).
changeMatchType(Room.Settings.MatchType).Wait();
}
protected override Task LeaveRoomInternal() => Task.CompletedTask;
public override Task TransferHost(int userId) => ((IMultiplayerClient)this).HostChanged(userId);
public override Task KickUser(int userId)
{
Debug.Assert(Room != null);
return ((IMultiplayerClient)this).UserKicked(Room.Users.Single(u => u.UserID == userId));
}
public override async Task ChangeSettings(MultiplayerRoomSettings settings)
{
Debug.Assert(Room != null);
2021-10-22 21:07:41 +08:00
Debug.Assert(APIRoom != null);
switch (Room.Settings.QueueMode, settings.QueueMode)
{
case (QueueModes.HostOnly, QueueModes.HostOnly):
break;
// Host-only is incompatible with other queueing modes, so expire all non-expired items.
case (QueueModes.HostOnly, _):
case (_, QueueModes.HostOnly):
foreach (var playlistItem in APIRoom.Playlist.Where(i => !i.Expired).ToArray())
{
playlistItem.Expired = true;
await ((IMultiplayerClient)this).PlaylistItemChanged(new APIPlaylistItem(playlistItem)).ConfigureAwait(false);
}
break;
}
await ((IMultiplayerClient)this).SettingsChanged(settings).ConfigureAwait(false);
foreach (var user in Room.Users.Where(u => u.State == MultiplayerUserState.Ready))
ChangeUserState(user.UserID, MultiplayerUserState.Idle);
await changeMatchType(settings.MatchType).ConfigureAwait(false);
}
public override Task ChangeState(MultiplayerUserState newState)
{
ChangeUserState(api.LocalUser.Value.Id, newState);
return Task.CompletedTask;
}
public override Task ChangeBeatmapAvailability(BeatmapAvailability newBeatmapAvailability)
{
ChangeUserBeatmapAvailability(api.LocalUser.Value.Id, newBeatmapAvailability);
return Task.CompletedTask;
}
2021-02-01 16:57:32 +08:00
public void ChangeUserMods(int userId, IEnumerable<Mod> newMods)
=> ChangeUserMods(userId, newMods.Select(m => new APIMod(m)).ToList());
2021-02-01 16:54:56 +08:00
2021-02-01 16:57:32 +08:00
public void ChangeUserMods(int userId, IEnumerable<APIMod> newMods)
2021-02-01 16:54:56 +08:00
{
Debug.Assert(Room != null);
2021-02-01 16:57:32 +08:00
((IMultiplayerClient)this).UserModsChanged(userId, newMods.ToList());
2021-02-01 16:54:56 +08:00
}
2021-02-01 16:57:32 +08:00
public override Task ChangeUserMods(IEnumerable<APIMod> newMods)
2021-02-01 16:54:56 +08:00
{
2021-02-01 16:57:32 +08:00
ChangeUserMods(api.LocalUser.Value.Id, newMods);
2021-02-01 16:54:56 +08:00
return Task.CompletedTask;
}
public override async Task SendMatchRequest(MatchUserRequest request)
{
Debug.Assert(Room != null);
Debug.Assert(LocalUser != null);
switch (request)
{
case ChangeTeamRequest changeTeam:
TeamVersusRoomState roomState = (TeamVersusRoomState)Room.MatchState!;
TeamVersusUserState userState = (TeamVersusUserState)LocalUser.MatchState!;
var targetTeam = roomState.Teams.FirstOrDefault(t => t.ID == changeTeam.TeamID);
if (targetTeam != null)
{
userState.TeamID = targetTeam.ID;
await ((IMultiplayerClient)this).MatchUserStateChanged(LocalUser.UserID, userState).ConfigureAwait(false);
}
break;
}
}
2020-12-20 17:24:13 +08:00
public override Task StartMatch()
{
Debug.Assert(Room != null);
2021-04-07 19:46:30 +08:00
ChangeRoomState(MultiplayerRoomState.WaitingForLoad);
foreach (var user in Room.Users.Where(u => u.State == MultiplayerUserState.Ready))
ChangeUserState(user.UserID, MultiplayerUserState.WaitingForLoad);
2020-12-20 17:24:13 +08:00
return ((IMultiplayerClient)this).LoadRequested();
}
public override async Task AddPlaylistItem(APIPlaylistItem item)
{
Debug.Assert(Room != null);
Debug.Assert(APIRoom != null);
2021-10-22 21:07:41 +08:00
if (Room.Settings.QueueMode == QueueModes.HostOnly && APIRoom.Playlist.Count > 0)
{
2021-10-22 21:07:41 +08:00
if (Room.Host?.UserID != LocalUser?.UserID)
throw new InvalidOperationException("Local user is not the room host.");
2021-10-22 21:07:41 +08:00
item.ID = APIRoom.Playlist.Last().ID;
await ((IMultiplayerClient)this).PlaylistItemChanged(item).ConfigureAwait(false);
}
2021-10-22 21:07:41 +08:00
else
await addPlaylistItem(item).ConfigureAwait(false);
}
public override Task RemovePlaylistItem(APIPlaylistItem item)
{
Debug.Assert(Room != null);
if (Room.Host?.UserID != LocalUser?.UserID)
throw new InvalidOperationException("Local user is not the room host.");
return ((IMultiplayerClient)this).PlaylistItemRemoved(item);
}
protected override Task<APIBeatmapSet> GetOnlineBeatmapSet(int beatmapId, CancellationToken cancellationToken = default)
{
Debug.Assert(Room != null);
var apiRoom = roomManager.ServerSideRooms.Single(r => r.RoomID.Value == Room.RoomID);
IBeatmapSetInfo? set = apiRoom.Playlist.FirstOrDefault(p => p.BeatmapID == beatmapId)?.Beatmap.Value.BeatmapSet
?? beatmaps.QueryBeatmap(b => b.OnlineBeatmapID == beatmapId)?.BeatmapSet;
if (set == null)
throw new InvalidOperationException("Beatmap not found.");
var apiSet = new APIBeatmapSet
{
OnlineID = set.OnlineID,
Beatmaps = set.Beatmaps.Select(b => new APIBeatmap { OnlineID = b.OnlineID }).ToArray(),
};
return Task.FromResult(apiSet);
}
private async Task changeMatchType(MatchType type)
{
Debug.Assert(Room != null);
switch (type)
{
case MatchType.HeadToHead:
await ((IMultiplayerClient)this).MatchRoomStateChanged(null).ConfigureAwait(false);
foreach (var user in Room.Users)
await ((IMultiplayerClient)this).MatchUserStateChanged(user.UserID, null).ConfigureAwait(false);
break;
case MatchType.TeamVersus:
await ((IMultiplayerClient)this).MatchRoomStateChanged(TeamVersusRoomState.CreateDefault()).ConfigureAwait(false);
foreach (var user in Room.Users)
await ((IMultiplayerClient)this).MatchUserStateChanged(user.UserID, new TeamVersusUserState()).ConfigureAwait(false);
break;
}
}
2021-10-22 21:07:41 +08:00
private async Task addPlaylistItem(APIPlaylistItem newItem)
{
Debug.Assert(Room != null);
Debug.Assert(APIRoom != null);
newItem.ID = (APIRoom.Playlist.LastOrDefault()?.ID ?? 0) + 1;
await ((IMultiplayerClient)this).PlaylistItemAdded(newItem).ConfigureAwait(false);
// A more valid selection can occur as a result of adding a new playlist item (e.g. if all previous items were expired).
await advanceToNextPlaylistItem().ConfigureAwait(false);
}
private async Task finishPlaylistItem()
{
Debug.Assert(Room != null);
Debug.Assert(APIRoom != null);
var currentItem = APIRoom.Playlist.Single(i => i.ID == Room.Settings.PlaylistItemId);
// Expire the current playlist item.
await ((IMultiplayerClient)this).PlaylistItemChanged(new APIPlaylistItem(currentItem) { Expired = true }).ConfigureAwait(false);
// In host-only mode, a duplicate playlist item will be used for the next round.
if (Room.Settings.QueueMode == QueueModes.HostOnly)
await addPlaylistItem(new APIPlaylistItem(currentItem)).ConfigureAwait(false);
}
private async Task advanceToNextPlaylistItem()
{
Debug.Assert(Room != null);
Debug.Assert(APIRoom != null);
long nextId = 0;
switch (Room.Settings.QueueMode)
{
case QueueModes.HostOnly:
// Pick the single non-expired playlist item.
nextId = APIRoom.Playlist.SingleOrDefault(i => !i.Expired)?.ID ?? 0;
break;
case QueueModes.FairRotate:
// Group playlist items by (user_id -> count_expired), and select the first available playlist item from a user that has available beatmaps where count_expired is the lowest.
throw new NotImplementedException();
case QueueModes.FreeForAll:
// Pick the first available non-expired playlist item, or default to the last.
nextId = APIRoom.Playlist.FirstOrDefault(i => !i.Expired)?.ID
?? APIRoom.Playlist.LastOrDefault()?.ID
?? 0;
break;
}
if (nextId != Room.Settings.PlaylistItemId)
{
Room.Settings.PlaylistItemId = nextId;
await ChangeSettings(Room.Settings).ConfigureAwait(false);
}
}
}
}