2025-02-06 22:25:45 +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.
|
|
|
|
|
|
2025-02-10 09:45:13 +08:00
|
|
|
|
using System;
|
2025-02-14 13:30:33 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-02-06 22:25:45 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Framework.Allocation;
|
2025-02-14 12:53:42 +08:00
|
|
|
|
using osu.Framework.Graphics.Cursor;
|
|
|
|
|
using osu.Framework.Localisation;
|
2025-02-07 18:13:51 +08:00
|
|
|
|
using osu.Game.Beatmaps;
|
2025-02-06 22:25:45 +08:00
|
|
|
|
using osu.Game.Collections;
|
|
|
|
|
using osu.Game.Database;
|
|
|
|
|
using osu.Game.Graphics.UserInterfaceV2;
|
|
|
|
|
using osu.Game.Online.Rooms;
|
|
|
|
|
using osu.Game.Overlays;
|
|
|
|
|
using osu.Game.Overlays.Notifications;
|
2025-02-07 18:13:51 +08:00
|
|
|
|
using Realms;
|
2025-02-06 22:25:45 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.OnlinePlay.Playlists
|
|
|
|
|
{
|
2025-02-14 12:53:42 +08:00
|
|
|
|
public partial class AddPlaylistToCollectionButton : RoundedButton, IHasTooltip
|
2025-02-06 22:25:45 +08:00
|
|
|
|
{
|
|
|
|
|
private readonly Room room;
|
2025-02-14 13:30:33 +08:00
|
|
|
|
|
2025-02-10 09:45:13 +08:00
|
|
|
|
private IDisposable? beatmapSubscription;
|
|
|
|
|
private IDisposable? collectionSubscription;
|
2025-02-06 22:25:45 +08:00
|
|
|
|
|
2025-02-14 13:30:33 +08:00
|
|
|
|
private Live<BeatmapCollection>? collection;
|
|
|
|
|
private HashSet<string> localBeatmapHashes = new HashSet<string>();
|
|
|
|
|
|
2025-02-06 22:25:45 +08:00
|
|
|
|
[Resolved]
|
2025-02-14 13:30:33 +08:00
|
|
|
|
private RealmAccess realm { get; set; } = null!;
|
2025-02-06 22:25:45 +08:00
|
|
|
|
|
|
|
|
|
[Resolved(canBeNull: true)]
|
|
|
|
|
private INotificationOverlay? notifications { get; set; }
|
|
|
|
|
|
|
|
|
|
public AddPlaylistToCollectionButton(Room room)
|
|
|
|
|
{
|
|
|
|
|
this.room = room;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2025-02-07 18:13:51 +08:00
|
|
|
|
private void load()
|
2025-02-06 22:25:45 +08:00
|
|
|
|
{
|
|
|
|
|
Action = () =>
|
|
|
|
|
{
|
2025-02-07 19:04:29 +08:00
|
|
|
|
if (room.Playlist.Count == 0)
|
2025-02-06 22:25:45 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2025-02-14 13:30:33 +08:00
|
|
|
|
var beatmaps = getBeatmapsForPlaylist(realm.Realm).ToArray();
|
2025-02-06 22:25:45 +08:00
|
|
|
|
|
2025-02-14 13:30:33 +08:00
|
|
|
|
int countBefore = 0;
|
|
|
|
|
int countAfter = 0;
|
2025-02-06 22:25:45 +08:00
|
|
|
|
|
2025-02-14 13:30:33 +08:00
|
|
|
|
collection ??= realm.Realm.Write(() => realm.Realm.Add(new BeatmapCollection(room.Name)).ToLive(realm));
|
|
|
|
|
collection.PerformWrite(c =>
|
2025-02-07 18:13:51 +08:00
|
|
|
|
{
|
2025-02-14 13:30:33 +08:00
|
|
|
|
countBefore = c.BeatmapMD5Hashes.Count;
|
2025-02-14 12:53:42 +08:00
|
|
|
|
|
|
|
|
|
foreach (var item in beatmaps)
|
2025-02-06 22:25:45 +08:00
|
|
|
|
{
|
2025-02-14 12:53:42 +08:00
|
|
|
|
if (!c.BeatmapMD5Hashes.Contains(item.MD5Hash))
|
2025-02-07 18:13:51 +08:00
|
|
|
|
c.BeatmapMD5Hashes.Add(item.MD5Hash);
|
2025-02-14 12:53:42 +08:00
|
|
|
|
}
|
2025-02-14 13:30:33 +08:00
|
|
|
|
|
|
|
|
|
countAfter = c.BeatmapMD5Hashes.Count;
|
2025-02-14 12:53:42 +08:00
|
|
|
|
});
|
2025-02-14 13:30:33 +08:00
|
|
|
|
|
|
|
|
|
if (countBefore == 0)
|
|
|
|
|
notifications?.Post(new SimpleNotification { Text = $"Created new collection \"{room.Name}\" with {countAfter} beatmaps." });
|
|
|
|
|
else
|
|
|
|
|
notifications?.Post(new SimpleNotification { Text = $"Added {countAfter - countBefore} beatmaps to collection \"{room.Name}\"." });
|
2025-02-06 22:25:45 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
2025-02-10 09:45:13 +08:00
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
2025-02-14 13:30:33 +08:00
|
|
|
|
Enabled.Value = false;
|
|
|
|
|
|
|
|
|
|
if (room.Playlist.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
beatmapSubscription = realm.RegisterForNotifications(getBeatmapsForPlaylist, (sender, _) =>
|
2025-02-14 12:53:42 +08:00
|
|
|
|
{
|
2025-02-14 13:30:33 +08:00
|
|
|
|
localBeatmapHashes = sender.Select(b => b.MD5Hash).ToHashSet();
|
|
|
|
|
Schedule(updateButtonState);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
collectionSubscription = realm.RegisterForNotifications(r => r.All<BeatmapCollection>().Where(c => c.Name == room.Name), (sender, _) =>
|
|
|
|
|
{
|
|
|
|
|
collection = sender.FirstOrDefault()?.ToLive(realm);
|
|
|
|
|
Schedule(updateButtonState);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateButtonState()
|
|
|
|
|
{
|
|
|
|
|
int countToAdd = getCountToBeAdded();
|
2025-02-10 09:45:13 +08:00
|
|
|
|
|
2025-02-14 13:30:33 +08:00
|
|
|
|
if (collection == null)
|
|
|
|
|
Text = $"Create new collection with {countToAdd} beatmaps";
|
|
|
|
|
else
|
|
|
|
|
Text = $"Update collection with {countToAdd} beatmaps";
|
2025-02-10 09:45:13 +08:00
|
|
|
|
|
2025-02-14 13:30:33 +08:00
|
|
|
|
Enabled.Value = countToAdd > 0;
|
2025-02-14 12:53:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-14 13:30:33 +08:00
|
|
|
|
private int getCountToBeAdded()
|
2025-02-14 12:53:42 +08:00
|
|
|
|
{
|
2025-02-14 13:30:33 +08:00
|
|
|
|
if (collection == null)
|
|
|
|
|
return localBeatmapHashes.Count;
|
|
|
|
|
|
|
|
|
|
return collection.PerformRead(c =>
|
|
|
|
|
{
|
|
|
|
|
int count = localBeatmapHashes.Count;
|
|
|
|
|
|
|
|
|
|
foreach (string hash in localBeatmapHashes)
|
|
|
|
|
{
|
|
|
|
|
if (c.BeatmapMD5Hashes.Contains(hash))
|
|
|
|
|
count--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
});
|
2025-02-14 12:53:42 +08:00
|
|
|
|
}
|
2025-02-10 09:45:13 +08:00
|
|
|
|
|
2025-02-14 13:30:33 +08:00
|
|
|
|
private IQueryable<BeatmapInfo> getBeatmapsForPlaylist(Realm r)
|
2025-02-14 12:53:42 +08:00
|
|
|
|
{
|
2025-02-14 13:30:33 +08:00
|
|
|
|
return r.All<BeatmapInfo>().Filter(string.Join(" OR ", room.Playlist.Select(item => $"(OnlineID == {item.Beatmap.OnlineID})").Distinct()));
|
2025-02-10 09:45:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
|
|
beatmapSubscription?.Dispose();
|
|
|
|
|
collectionSubscription?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-14 13:30:33 +08:00
|
|
|
|
public LocalisableString TooltipText
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (Enabled.Value)
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
|
|
|
int currentCollectionCount = collection?.PerformRead(c => c.BeatmapMD5Hashes.Count) ?? 0;
|
|
|
|
|
if (room.Playlist.DistinctBy(i => i.Beatmap.OnlineID).Count() == currentCollectionCount)
|
|
|
|
|
return "All beatmaps have been added!";
|
|
|
|
|
|
|
|
|
|
return "Download some beatmaps first.";
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-06 22:25:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|