Skip to content

Commit

Permalink
Migrate MusicController to Live<T> to fix broken migrations instead
Browse files Browse the repository at this point in the history
  • Loading branch information
bdach committed Oct 9, 2024
1 parent 081d87f commit baa64d1
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions osu.Game/Overlays/MusicController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public partial class MusicController : CompositeDrawable
private AudioFilter audioDuckFilter = null!;

private readonly Bindable<RandomSelectAlgorithm> randomSelectAlgorithm = new Bindable<RandomSelectAlgorithm>();
private readonly List<BeatmapSetInfo> previousRandomSets = new List<BeatmapSetInfo>();
private readonly List<Live<BeatmapSetInfo>> previousRandomSets = new List<Live<BeatmapSetInfo>>();
private int randomHistoryDirection;

[BackgroundDependencyLoader]
Expand Down Expand Up @@ -249,19 +249,19 @@ private PreviousTrackResult prev(bool allowProtectedTracks)

queuedDirection = TrackChangeDirection.Prev;

BeatmapSetInfo? playableSet;
Live<BeatmapSetInfo>? playableSet;

if (Shuffle.Value)
playableSet = getNextRandom(-1, allowProtectedTracks);
else
{
playableSet = getBeatmapSets().AsEnumerable().TakeWhile(i => !i.Equals(current?.BeatmapSetInfo)).LastOrDefault(s => !s.Protected || allowProtectedTracks)
?? getBeatmapSets().AsEnumerable().LastOrDefault(s => !s.Protected || allowProtectedTracks);
playableSet = getBeatmapSets().TakeWhile(i => !i.Value.Equals(current?.BeatmapSetInfo)).LastOrDefault(s => !s.Value.Protected || allowProtectedTracks)
?? getBeatmapSets().LastOrDefault(s => !s.Value.Protected || allowProtectedTracks);
}

if (playableSet != null)
{
changeBeatmap(beatmaps.GetWorkingBeatmap(playableSet.Beatmaps.First()));
changeBeatmap(beatmaps.GetWorkingBeatmap(playableSet.Value.Beatmaps.First()));
restartTrack();
return PreviousTrackResult.Previous;
}
Expand Down Expand Up @@ -345,19 +345,19 @@ private bool next(bool allowProtectedTracks)

queuedDirection = TrackChangeDirection.Next;

BeatmapSetInfo? playableSet;
Live<BeatmapSetInfo>? playableSet;

if (Shuffle.Value)
playableSet = getNextRandom(1, allowProtectedTracks);
else
{
playableSet = getBeatmapSets().AsEnumerable().SkipWhile(i => !i.Equals(current?.BeatmapSetInfo))
.Where(i => !i.Protected || allowProtectedTracks)
playableSet = getBeatmapSets().SkipWhile(i => !i.Value.Equals(current?.BeatmapSetInfo))
.Where(i => !i.Value.Protected || allowProtectedTracks)
.ElementAtOrDefault(1)
?? getBeatmapSets().AsEnumerable().FirstOrDefault(i => !i.Protected || allowProtectedTracks);
?? getBeatmapSets().FirstOrDefault(i => !i.Value.Protected || allowProtectedTracks);
}

var playableBeatmap = playableSet?.Beatmaps.FirstOrDefault();
var playableBeatmap = playableSet?.Value.Beatmaps.FirstOrDefault();

if (playableBeatmap != null)
{
Expand All @@ -369,11 +369,11 @@ private bool next(bool allowProtectedTracks)
return false;
}

private BeatmapSetInfo? getNextRandom(int direction, bool allowProtectedTracks)
private Live<BeatmapSetInfo>? getNextRandom(int direction, bool allowProtectedTracks)
{
BeatmapSetInfo result;
Live<BeatmapSetInfo> result;

var possibleSets = getBeatmapSets().AsEnumerable().Where(s => !s.Protected || allowProtectedTracks).ToArray();
var possibleSets = getBeatmapSets().Where(s => !s.Value.Protected || allowProtectedTracks).ToArray();

if (possibleSets.Length == 0)
return null;
Expand Down Expand Up @@ -432,7 +432,9 @@ private void restartTrack()

private TrackChangeDirection? queuedDirection;

private IQueryable<BeatmapSetInfo> getBeatmapSets() => realm.Realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending);
private IEnumerable<Live<BeatmapSetInfo>> getBeatmapSets() => realm.Realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending)
.AsEnumerable()
.Select(s => new RealmLive<BeatmapSetInfo>(s, realm));

private void changeBeatmap(WorkingBeatmap newWorking)
{
Expand All @@ -459,8 +461,8 @@ private void changeBeatmap(WorkingBeatmap newWorking)
else
{
// figure out the best direction based on order in playlist.
int last = getBeatmapSets().AsEnumerable().TakeWhile(b => !b.Equals(current.BeatmapSetInfo)).Count();
int next = getBeatmapSets().AsEnumerable().TakeWhile(b => !b.Equals(newWorking.BeatmapSetInfo)).Count();
int last = getBeatmapSets().TakeWhile(b => !b.Value.Equals(current.BeatmapSetInfo)).Count();
int next = getBeatmapSets().TakeWhile(b => !b.Value.Equals(newWorking.BeatmapSetInfo)).Count();

direction = last > next ? TrackChangeDirection.Prev : TrackChangeDirection.Next;
}
Expand Down

0 comments on commit baa64d1

Please sign in to comment.