1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 12:47:25 +08:00

Add ability to delete individual scores (#7252)

Add ability to delete individual scores

Co-authored-by: Dan Balasescu <smoogipoo@smgi.me>
Co-authored-by: Dean Herbert <pe@ppy.sh>
This commit is contained in:
Dean Herbert 2020-01-10 01:58:35 +08:00 committed by GitHub
commit f745d74666
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 297 additions and 10 deletions

View File

@ -3,10 +3,12 @@
using System;
using System.Collections.Generic;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Game.Beatmaps;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Leaderboards;
using osu.Game.Overlays;
using osu.Game.Online.Placeholders;
using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Scoring;
@ -29,8 +31,16 @@ namespace osu.Game.Tests.Visual.SongSelect
private readonly FailableLeaderboard leaderboard;
[Cached]
private readonly DialogOverlay dialogOverlay;
public TestSceneBeatmapLeaderboard()
{
Add(dialogOverlay = new DialogOverlay
{
Depth = -1
});
Add(leaderboard = new FailableLeaderboard
{
Origin = Anchor.Centre,

View File

@ -1,11 +1,13 @@
// 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.
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osuTK.Graphics;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays;
using osu.Game.Scoring;
using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Screens.Select.Leaderboards;
@ -15,10 +17,18 @@ namespace osu.Game.Tests.Visual.SongSelect
{
public class TestSceneUserTopScoreContainer : OsuTestScene
{
[Cached]
private readonly DialogOverlay dialogOverlay;
public TestSceneUserTopScoreContainer()
{
UserTopScoreContainer topScoreContainer;
Add(dialogOverlay = new DialogOverlay
{
Depth = -1
});
Add(new Container
{
Origin = Anchor.BottomCentre,

View File

@ -0,0 +1,181 @@
// 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.
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Platform;
using osu.Framework.Testing;
using osu.Framework.Utils;
using osu.Game.Beatmaps;
using osu.Game.Graphics.Cursor;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.Leaderboards;
using osu.Game.Online.Placeholders;
using osu.Game.Overlays;
using osu.Game.Rulesets;
using osu.Game.Scoring;
using osu.Game.Screens.Select.Leaderboards;
using osu.Game.Tests.Resources;
using osu.Game.Users;
using osuTK;
using osuTK.Input;
namespace osu.Game.Tests.Visual.UserInterface
{
public class TestSceneDeleteLocalScore : ManualInputManagerTestScene
{
public override IReadOnlyList<Type> RequiredTypes => new[]
{
typeof(Placeholder),
typeof(MessagePlaceholder),
typeof(RetrievalFailurePlaceholder),
typeof(UserTopScoreContainer),
typeof(Leaderboard<BeatmapLeaderboardScope, ScoreInfo>),
typeof(LeaderboardScore),
};
private readonly ContextMenuContainer contextMenuContainer;
private readonly BeatmapLeaderboard leaderboard;
private RulesetStore rulesetStore;
private BeatmapManager beatmapManager;
private ScoreManager scoreManager;
private readonly List<ScoreInfo> scores = new List<ScoreInfo>();
private BeatmapInfo beatmap;
[Cached]
private readonly DialogOverlay dialogOverlay;
public TestSceneDeleteLocalScore()
{
Children = new Drawable[]
{
contextMenuContainer = new OsuContextMenuContainer
{
RelativeSizeAxes = Axes.Both,
Child = leaderboard = new BeatmapLeaderboard
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Size = new Vector2(550f, 450f),
Scope = BeatmapLeaderboardScope.Local,
Beatmap = new BeatmapInfo
{
ID = 1,
Metadata = new BeatmapMetadata
{
ID = 1,
Title = "TestSong",
Artist = "TestArtist",
Author = new User
{
Username = "TestAuthor"
},
},
Version = "Insane"
},
}
},
dialogOverlay = new DialogOverlay()
};
}
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
dependencies.Cache(rulesetStore = new RulesetStore(ContextFactory));
dependencies.Cache(beatmapManager = new BeatmapManager(LocalStorage, ContextFactory, rulesetStore, null, Audio, dependencies.Get<GameHost>(), Beatmap.Default));
dependencies.Cache(scoreManager = new ScoreManager(rulesetStore, () => beatmapManager, LocalStorage, null, ContextFactory));
beatmap = beatmapManager.Import(TestResources.GetTestBeatmapForImport()).Result.Beatmaps[0];
for (int i = 0; i < 50; i++)
{
var score = new ScoreInfo
{
OnlineScoreID = i,
Beatmap = beatmap,
BeatmapInfoID = beatmap.ID,
Accuracy = RNG.NextDouble(),
TotalScore = RNG.Next(1, 1000000),
MaxCombo = RNG.Next(1, 1000),
Rank = ScoreRank.XH,
User = new User { Username = "TestUser" },
};
scores.Add(scoreManager.Import(score).Result);
}
scores.Sort(Comparer<ScoreInfo>.Create((s1, s2) => s2.TotalScore.CompareTo(s1.TotalScore)));
return dependencies;
}
[SetUp]
public void Setup() => Schedule(() =>
{
// Due to soft deletions, we can re-use deleted scores between test runs
scoreManager.Undelete(scoreManager.QueryScores(s => s.DeletePending).ToList());
leaderboard.Scores = null;
leaderboard.FinishTransforms(true); // After setting scores, we may be waiting for transforms to expire drawables
leaderboard.Beatmap = beatmap;
leaderboard.RefreshScores(); // Required in the case that the beatmap hasn't changed
});
[SetUpSteps]
public void SetupSteps()
{
// Ensure the leaderboard has finished async-loading drawables
AddUntilStep("wait for drawables", () => leaderboard.ChildrenOfType<LeaderboardScore>().Any());
// Ensure the leaderboard items have finished showing up
AddStep("finish transforms", () => leaderboard.FinishTransforms(true));
}
[Test]
public void TestDeleteViaRightClick()
{
AddStep("open menu for top score", () =>
{
InputManager.MoveMouseTo(leaderboard.ChildrenOfType<LeaderboardScore>().First());
InputManager.Click(MouseButton.Right);
});
// Ensure the context menu has finished showing
AddStep("finish transforms", () => contextMenuContainer.FinishTransforms(true));
AddStep("click delete option", () =>
{
InputManager.MoveMouseTo(contextMenuContainer.ChildrenOfType<DrawableOsuMenuItem>().First(i => i.Item.Text.Value.ToLowerInvariant() == "delete"));
InputManager.Click(MouseButton.Left);
});
// Ensure the dialog has finished showing
AddStep("finish transforms", () => dialogOverlay.FinishTransforms(true));
AddStep("click delete button", () =>
{
InputManager.MoveMouseTo(dialogOverlay.ChildrenOfType<DialogButton>().First());
InputManager.Click(MouseButton.Left);
});
AddUntilStep("score removed from leaderboard", () => leaderboard.Scores.All(s => s.OnlineScoreID != scores[0].OnlineScoreID));
}
[Test]
public void TestDeleteViaDatabase()
{
AddStep("delete top score", () => scoreManager.Delete(scores[0]));
AddUntilStep("score removed from leaderboard", () => leaderboard.Scores.All(s => s.OnlineScoreID != scores[0].OnlineScoreID));
}
}
}

View File

@ -12,6 +12,7 @@ using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Threading;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Cursor;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.Placeholders;
@ -181,10 +182,14 @@ namespace osu.Game.Online.Leaderboards
{
new Drawable[]
{
scrollContainer = new OsuScrollContainer
new OsuContextMenuContainer
{
RelativeSizeAxes = Axes.Both,
ScrollbarVisible = false,
Child = scrollContainer = new OsuScrollContainer
{
RelativeSizeAxes = Axes.Both,
ScrollbarVisible = false,
}
}
},
new Drawable[]

View File

@ -11,11 +11,15 @@ using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays;
using osu.Game.Rulesets.UI;
using osu.Game.Screens.Select;
using osu.Game.Scoring;
using osu.Game.Users.Drawables;
using osuTK;
@ -25,7 +29,7 @@ using osu.Game.Online.API;
namespace osu.Game.Online.Leaderboards
{
public class LeaderboardScore : OsuClickableContainer
public class LeaderboardScore : OsuClickableContainer, IHasContextMenu
{
public const float HEIGHT = 60;
@ -51,6 +55,9 @@ namespace osu.Game.Online.Leaderboards
private List<ScoreComponentLabel> statisticsLabels;
[Resolved]
private DialogOverlay dialogOverlay { get; set; }
public LeaderboardScore(ScoreInfo score, int rank, bool allowHighlight = true)
{
this.score = score;
@ -359,5 +366,18 @@ namespace osu.Game.Online.Leaderboards
Value = value;
}
}
public MenuItem[] ContextMenuItems
{
get
{
List<MenuItem> items = new List<MenuItem>();
if (score.ID != 0)
items.Add(new OsuMenuItem("Delete", MenuItemType.Destructive, () => dialogOverlay?.Push(new LocalScoreDeleteDialog(score))));
return items.ToArray();
}
}
}
}

View File

@ -14,7 +14,8 @@ namespace osu.Game.Screens.Select
{
public class BeatmapClearScoresDialog : PopupDialog
{
private ScoreManager scoreManager;
[Resolved]
private ScoreManager scoreManager { get; set; }
public BeatmapClearScoresDialog(BeatmapInfo beatmap, Action onCompletion)
{
@ -38,11 +39,5 @@ namespace osu.Game.Screens.Select
},
};
}
[BackgroundDependencyLoader]
private void load(ScoreManager scoreManager)
{
this.scoreManager = scoreManager;
}
}
}

View File

@ -103,6 +103,8 @@ namespace osu.Game.Screens.Select.Leaderboards
{
ScoreSelected = s => ScoreSelected?.Invoke(s)
});
scoreManager.ItemRemoved += onScoreRemoved;
}
protected override void Reset()
@ -111,6 +113,8 @@ namespace osu.Game.Screens.Select.Leaderboards
TopScore = null;
}
private void onScoreRemoved(ScoreInfo score) => Schedule(RefreshScores);
protected override bool IsOnlineScope => Scope != BeatmapLeaderboardScope.Local;
protected override APIRequest FetchScores(Action<IEnumerable<ScoreInfo>> scoresCallback)
@ -186,5 +190,13 @@ namespace osu.Game.Screens.Select.Leaderboards
{
Action = () => ScoreSelected?.Invoke(model)
};
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (scoreManager != null)
scoreManager.ItemRemoved -= onScoreRemoved;
}
}
}

View File

@ -0,0 +1,54 @@
// 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.
using osu.Framework.Allocation;
using osu.Game.Overlays.Dialog;
using osu.Game.Scoring;
using System.Diagnostics;
using osu.Framework.Graphics.Sprites;
using osu.Game.Beatmaps;
namespace osu.Game.Screens.Select
{
public class LocalScoreDeleteDialog : PopupDialog
{
private readonly ScoreInfo score;
[Resolved]
private ScoreManager scoreManager { get; set; }
[Resolved]
private BeatmapManager beatmapManager { get; set; }
public LocalScoreDeleteDialog(ScoreInfo score)
{
this.score = score;
Debug.Assert(score != null);
}
[BackgroundDependencyLoader]
private void load()
{
BeatmapInfo beatmap = beatmapManager.QueryBeatmap(b => b.ID == score.BeatmapInfoID);
Debug.Assert(beatmap != null);
string accuracy = string.Format(score.Accuracy == 1 ? "{0:P0}" : "{0:P2}", score.Accuracy);
BodyText = $"{score.User} ({accuracy}, {score.Rank})";
Icon = FontAwesome.Regular.TrashAlt;
HeaderText = "Confirm deletion of local score";
Buttons = new PopupDialogButton[]
{
new PopupDialogOkButton
{
Text = "Yes. Please.",
Action = () => scoreManager?.Delete(score)
},
new PopupDialogCancelButton
{
Text = "No, I'm still attached.",
},
};
}
}
}