1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-15 10:22:56 +08:00

Fix tests following playlist changes

Also more closely follows the server implementation.
This commit is contained in:
Dan Balasescu 2021-11-16 14:37:29 +09:00
parent 9076519710
commit 41e46f158f

View File

@ -41,9 +41,10 @@ namespace osu.Game.Tests.Visual.Multiplayer
[Resolved] [Resolved]
private BeatmapManager beatmaps { get; set; } = null!; private BeatmapManager beatmaps { get; set; } = null!;
private readonly List<MultiplayerPlaylistItem> playlistItems = new List<MultiplayerPlaylistItem>(); private MultiplayerPlaylistItem? currentItem => Room?.Playlist[currentIndex];
private readonly TestMultiplayerRoomManager roomManager; private readonly TestMultiplayerRoomManager roomManager;
private MultiplayerPlaylistItem? currentItem; private int currentIndex;
public TestMultiplayerClient(TestMultiplayerRoomManager roomManager) public TestMultiplayerClient(TestMultiplayerRoomManager roomManager)
{ {
@ -162,30 +163,27 @@ namespace osu.Game.Tests.Visual.Multiplayer
if (password != apiRoom.Password.Value) if (password != apiRoom.Password.Value)
throw new InvalidOperationException("Invalid password."); throw new InvalidOperationException("Invalid password.");
playlistItems.AddRange(apiRoom.Playlist.Select(i => new MultiplayerPlaylistItem(i)));
var localUser = new MultiplayerRoomUser(api.LocalUser.Value.Id) var localUser = new MultiplayerRoomUser(api.LocalUser.Value.Id)
{ {
User = api.LocalUser.Value User = api.LocalUser.Value
}; };
await updateCurrentItem(apiRoom: apiRoom).ConfigureAwait(false);
Debug.Assert(currentItem != null);
var room = new MultiplayerRoom(roomId) var room = new MultiplayerRoom(roomId)
{ {
Settings = Settings =
{ {
Name = apiRoom.Name.Value, Name = apiRoom.Name.Value,
MatchType = apiRoom.Type.Value, MatchType = apiRoom.Type.Value,
PlaylistItemId = currentItem.ID,
Password = password, Password = password,
QueueMode = apiRoom.QueueMode.Value QueueMode = apiRoom.QueueMode.Value
}, },
Playlist = apiRoom.Playlist.Select(item => new MultiplayerPlaylistItem(item)).ToList(),
Users = { localUser }, Users = { localUser },
Host = localUser Host = localUser
}; };
await updateCurrentItem(room, false).ConfigureAwait(false);
RoomSetupAction?.Invoke(room); RoomSetupAction?.Invoke(room);
RoomSetupAction = null; RoomSetupAction = null;
@ -308,28 +306,29 @@ namespace osu.Game.Tests.Visual.Multiplayer
case QueueModes.HostOnly: case QueueModes.HostOnly:
// In host-only mode, the current item is re-used. // In host-only mode, the current item is re-used.
item.ID = currentItem.ID; item.ID = currentItem.ID;
playlistItems[playlistItems.FindIndex(i => i == currentItem)] = item;
Room.Playlist[currentIndex] = item;
await ((IMultiplayerClient)this).PlaylistItemChanged(item).ConfigureAwait(false); await ((IMultiplayerClient)this).PlaylistItemChanged(item).ConfigureAwait(false);
// Note: Unlike the server, this is the easiest way to update the current item at this point. // Note: Unlike the server, this is the easiest way to update the current item at this point.
await updateCurrentItem(false).ConfigureAwait(false); await updateCurrentItem(Room, false).ConfigureAwait(false);
break; break;
default: default:
item.ID = playlistItems.Last().ID + 1; item.ID = Room.Playlist.Last().ID + 1;
playlistItems.Add(item);
Room.Playlist.Add(item);
await ((IMultiplayerClient)this).PlaylistItemAdded(item).ConfigureAwait(false); await ((IMultiplayerClient)this).PlaylistItemAdded(item).ConfigureAwait(false);
await updateCurrentItem().ConfigureAwait(false);
await updateCurrentItem(Room).ConfigureAwait(false);
break; break;
} }
} }
protected override Task<APIBeatmapSet> GetOnlineBeatmapSet(int beatmapId, CancellationToken cancellationToken = default) protected override Task<APIBeatmapSet> GetOnlineBeatmapSet(int beatmapId, CancellationToken cancellationToken = default)
{ {
Debug.Assert(Room != null); IBeatmapSetInfo? set = roomManager.ServerSideRooms.SelectMany(r => r.Playlist)
.FirstOrDefault(p => p.BeatmapID == beatmapId)?.Beatmap.Value.BeatmapSet
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.OnlineID == beatmapId)?.BeatmapSet; ?? beatmaps.QueryBeatmap(b => b.OnlineID == beatmapId)?.BeatmapSet;
if (set == null) if (set == null)
@ -368,14 +367,24 @@ namespace osu.Game.Tests.Visual.Multiplayer
private async Task changeQueueMode(QueueModes newMode) private async Task changeQueueMode(QueueModes newMode)
{ {
Debug.Assert(Room != null);
Debug.Assert(APIRoom != null); Debug.Assert(APIRoom != null);
Debug.Assert(currentItem != null); Debug.Assert(currentItem != null);
if (newMode == QueueModes.HostOnly) if (newMode == QueueModes.HostOnly)
{ {
// Remove all but the current and expired items. The current item may be re-used for host-only mode if it's non-expired. // Remove all but the current and expired items. The current item may be re-used for host-only mode if it's non-expired.
foreach (var playlistItem in playlistItems.Where(i => !i.Expired && i.ID != currentItem.ID).ToArray()) for (int i = 0; i < Room.Playlist.Count; i++)
await ((IMultiplayerClient)this).PlaylistItemRemoved(playlistItem.ID).ConfigureAwait(false); {
var item = Room.Playlist[i];
if (item.Expired || item.ID == Room.Settings.PlaylistItemId)
continue;
Room.Playlist.RemoveAt(i--);
await ((IMultiplayerClient)this).PlaylistItemRemoved(item.ID).ConfigureAwait(false);
}
// Always ensure that at least one non-expired item exists by duplicating the current item if required. // Always ensure that at least one non-expired item exists by duplicating the current item if required.
if (currentItem.Expired) if (currentItem.Expired)
@ -383,7 +392,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
} }
// When changing modes, items could have been added (above) or the queueing order could have changed. // When changing modes, items could have been added (above) or the queueing order could have changed.
await updateCurrentItem().ConfigureAwait(false); await updateCurrentItem(Room).ConfigureAwait(false);
} }
private async Task finishCurrentItem() private async Task finishCurrentItem()
@ -400,7 +409,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
if (Room.Settings.QueueMode == QueueModes.HostOnly) if (Room.Settings.QueueMode == QueueModes.HostOnly)
await duplicateCurrentItem().ConfigureAwait(false); await duplicateCurrentItem().ConfigureAwait(false);
await updateCurrentItem().ConfigureAwait(false); await updateCurrentItem(Room).ConfigureAwait(false);
} }
private async Task duplicateCurrentItem() private async Task duplicateCurrentItem()
@ -411,7 +420,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
var newItem = new MultiplayerPlaylistItem var newItem = new MultiplayerPlaylistItem
{ {
ID = playlistItems.Last().ID + 1, ID = Room.Playlist.Last().ID + 1,
BeatmapID = currentItem.BeatmapID, BeatmapID = currentItem.BeatmapID,
BeatmapChecksum = currentItem.BeatmapChecksum, BeatmapChecksum = currentItem.BeatmapChecksum,
RulesetID = currentItem.RulesetID, RulesetID = currentItem.RulesetID,
@ -419,23 +428,19 @@ namespace osu.Game.Tests.Visual.Multiplayer
AllowedMods = currentItem.AllowedMods AllowedMods = currentItem.AllowedMods
}; };
playlistItems.Add(newItem); Room.Playlist.Add(newItem);
await ((IMultiplayerClient)this).PlaylistItemAdded(newItem).ConfigureAwait(false); await ((IMultiplayerClient)this).PlaylistItemAdded(newItem).ConfigureAwait(false);
} }
private async Task updateCurrentItem(bool notify = true, Room? apiRoom = null) private async Task updateCurrentItem(MultiplayerRoom room, bool notify = true)
{ {
if (apiRoom == null) MultiplayerPlaylistItem newItem;
{
Debug.Assert(APIRoom != null);
apiRoom = APIRoom;
}
switch (apiRoom.QueueMode.Value) switch (room.Settings.QueueMode)
{ {
default: default:
// Pick the single non-expired playlist item. // Pick the single non-expired playlist item.
currentItem = playlistItems.FirstOrDefault(i => !i.Expired) ?? playlistItems.Last(); newItem = room.Playlist.FirstOrDefault(i => !i.Expired) ?? room.Playlist.Last();
break; break;
case QueueModes.FairRotate: case QueueModes.FairRotate:
@ -443,14 +448,13 @@ namespace osu.Game.Tests.Visual.Multiplayer
throw new NotImplementedException(); throw new NotImplementedException();
} }
if (Room != null) currentIndex = room.Playlist.IndexOf(newItem);
{
long lastItem = Room.Settings.PlaylistItemId;
Room.Settings.PlaylistItemId = currentItem.ID;
if (notify && currentItem.ID != lastItem) long lastItem = room.Settings.PlaylistItemId;
await ((IMultiplayerClient)this).SettingsChanged(Room.Settings).ConfigureAwait(false); room.Settings.PlaylistItemId = newItem.ID;
}
if (notify && newItem.ID != lastItem)
await ((IMultiplayerClient)this).SettingsChanged(room.Settings).ConfigureAwait(false);
} }
} }
} }