1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-13 10:43:04 +08:00

Separate editing and adding playlist items

This commit is contained in:
Dan Balasescu 2021-12-10 14:44:35 +09:00
parent 88670c3b01
commit de0f37b08d
5 changed files with 54 additions and 31 deletions

View File

@ -83,6 +83,12 @@ namespace osu.Game.Online.Multiplayer
/// <param name="item">The item to add.</param> /// <param name="item">The item to add.</param>
Task AddPlaylistItem(MultiplayerPlaylistItem item); Task AddPlaylistItem(MultiplayerPlaylistItem item);
/// <summary>
/// Edits an existing playlist item with new values.
/// </summary>
/// <param name="item">The item to edit, containing new properties. Must have an ID.</param>
Task EditPlaylistItem(MultiplayerPlaylistItem item);
/// <summary> /// <summary>
/// Removes an item from the playlist. /// Removes an item from the playlist.
/// </summary> /// </summary>

View File

@ -335,6 +335,8 @@ namespace osu.Game.Online.Multiplayer
public abstract Task AddPlaylistItem(MultiplayerPlaylistItem item); public abstract Task AddPlaylistItem(MultiplayerPlaylistItem item);
public abstract Task EditPlaylistItem(MultiplayerPlaylistItem item);
public abstract Task RemovePlaylistItem(long playlistItemId); public abstract Task RemovePlaylistItem(long playlistItemId);
Task IMultiplayerClient.RoomStateChanged(MultiplayerRoomState state) Task IMultiplayerClient.RoomStateChanged(MultiplayerRoomState state)

View File

@ -162,6 +162,14 @@ namespace osu.Game.Online.Multiplayer
return connection.InvokeAsync(nameof(IMultiplayerServer.AddPlaylistItem), item); return connection.InvokeAsync(nameof(IMultiplayerServer.AddPlaylistItem), item);
} }
public override Task EditPlaylistItem(MultiplayerPlaylistItem item)
{
if (!IsConnected.Value)
return Task.CompletedTask;
return connection.InvokeAsync(nameof(IMultiplayerServer.EditPlaylistItem), item);
}
public override Task RemovePlaylistItem(long playlistItemId) public override Task RemovePlaylistItem(long playlistItemId)
{ {
if (!IsConnected.Value) if (!IsConnected.Value)

View File

@ -4,6 +4,7 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Logging; using osu.Framework.Logging;
@ -64,7 +65,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer
{ {
loadingLayer.Show(); loadingLayer.Show();
client.AddPlaylistItem(new MultiplayerPlaylistItem var multiplayerItem = new MultiplayerPlaylistItem
{ {
ID = itemToEdit?.ID ?? 0, ID = itemToEdit?.ID ?? 0,
BeatmapID = item.BeatmapID, BeatmapID = item.BeatmapID,
@ -72,7 +73,11 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer
RulesetID = item.RulesetID, RulesetID = item.RulesetID,
RequiredMods = item.RequiredMods.Select(m => new APIMod(m)).ToArray(), RequiredMods = item.RequiredMods.Select(m => new APIMod(m)).ToArray(),
AllowedMods = item.AllowedMods.Select(m => new APIMod(m)).ToArray() AllowedMods = item.AllowedMods.Select(m => new APIMod(m)).ToArray()
}).ContinueWith(t => };
Task task = itemToEdit != null ? client.EditPlaylistItem(multiplayerItem) : client.AddPlaylistItem(multiplayerItem);
task.ContinueWith(t =>
{ {
Schedule(() => Schedule(() =>
{ {

View File

@ -309,49 +309,51 @@ namespace osu.Game.Tests.Visual.Multiplayer
Debug.Assert(APIRoom != null); Debug.Assert(APIRoom != null);
Debug.Assert(currentItem != null); Debug.Assert(currentItem != null);
bool isNewAddition = item.ID == 0;
if (Room.Settings.QueueMode == QueueMode.HostOnly && Room.Host?.UserID != LocalUser?.UserID) if (Room.Settings.QueueMode == QueueMode.HostOnly && Room.Host?.UserID != LocalUser?.UserID)
throw new InvalidOperationException("Local user is not the room host."); throw new InvalidOperationException("Local user is not the room host.");
item.OwnerID = userId; item.OwnerID = userId;
if (isNewAddition) await addItem(item).ConfigureAwait(false);
{ await updateCurrentItem(Room).ConfigureAwait(false);
await addItem(item).ConfigureAwait(false);
await updateCurrentItem(Room).ConfigureAwait(false);
}
else
{
var existingItem = serverSidePlaylist.SingleOrDefault(i => i.ID == item.ID);
if (existingItem == null)
throw new InvalidOperationException("Attempted to change an item that doesn't exist.");
if (existingItem.OwnerID != userId && Room.Host?.UserID != LocalUser?.UserID)
throw new InvalidOperationException("Attempted to change an item which is not owned by the user.");
if (existingItem.Expired)
throw new InvalidOperationException("Attempted to change an item which has already been played.");
// Ensure the playlist order doesn't change.
item.PlaylistOrder = existingItem.PlaylistOrder;
serverSidePlaylist[serverSidePlaylist.IndexOf(existingItem)] = item;
await ((IMultiplayerClient)this).PlaylistItemChanged(item).ConfigureAwait(false);
}
} }
public override Task AddPlaylistItem(MultiplayerPlaylistItem item) => AddUserPlaylistItem(api.LocalUser.Value.OnlineID, item); public override Task AddPlaylistItem(MultiplayerPlaylistItem item) => AddUserPlaylistItem(api.LocalUser.Value.OnlineID, item);
public async Task EditUserPlaylistItem(int userId, MultiplayerPlaylistItem item)
{
Debug.Assert(Room != null);
Debug.Assert(APIRoom != null);
Debug.Assert(currentItem != null);
item.OwnerID = userId;
var existingItem = serverSidePlaylist.SingleOrDefault(i => i.ID == item.ID);
if (existingItem == null)
throw new InvalidOperationException("Attempted to change an item that doesn't exist.");
if (existingItem.OwnerID != userId && Room.Host?.UserID != LocalUser?.UserID)
throw new InvalidOperationException("Attempted to change an item which is not owned by the user.");
if (existingItem.Expired)
throw new InvalidOperationException("Attempted to change an item which has already been played.");
// Ensure the playlist order doesn't change.
item.PlaylistOrder = existingItem.PlaylistOrder;
serverSidePlaylist[serverSidePlaylist.IndexOf(existingItem)] = item;
await ((IMultiplayerClient)this).PlaylistItemChanged(item).ConfigureAwait(false);
}
public override Task EditPlaylistItem(MultiplayerPlaylistItem item) => EditUserPlaylistItem(api.LocalUser.Value.OnlineID, item);
public async Task RemoveUserPlaylistItem(int userId, long playlistItemId) public async Task RemoveUserPlaylistItem(int userId, long playlistItemId)
{ {
Debug.Assert(Room != null); Debug.Assert(Room != null);
Debug.Assert(APIRoom != null); Debug.Assert(APIRoom != null);
if (Room.Settings.QueueMode == QueueMode.HostOnly)
throw new InvalidOperationException("Items cannot be removed in host-only mode.");
var item = serverSidePlaylist.Find(i => i.ID == playlistItemId); var item = serverSidePlaylist.Find(i => i.ID == playlistItemId);
if (item == null) if (item == null)