From a041f32072ac44dd1d0ae9c6877ce7e753fc161e Mon Sep 17 00:00:00 2001
From: Dean Herbert <pe@ppy.sh>
Date: Thu, 26 Dec 2019 19:05:32 +0900
Subject: [PATCH] Use cleaner solution via cancellation of older schedule

---
 osu.Game/Screens/Play/Player.cs | 36 +++++++++++++++------------------
 1 file changed, 16 insertions(+), 20 deletions(-)

diff --git a/osu.Game/Screens/Play/Player.cs b/osu.Game/Screens/Play/Player.cs
index ab32c55229..6368c30def 100644
--- a/osu.Game/Screens/Play/Player.cs
+++ b/osu.Game/Screens/Play/Player.cs
@@ -350,18 +350,7 @@ namespace osu.Game.Screens.Play
             if (!showResults) return;
 
             using (BeginDelayedSequence(1000))
-            {
-                completionProgressDelegate = Schedule(delegate
-                {
-                    if (!this.IsCurrentScreen()) return;
-
-                    var score = CreateScore();
-                    if (DrawableRuleset.ReplayScore == null)
-                        scoreManager.Import(score).Wait();
-
-                    this.Push(CreateResults(score));
-                });
-            }
+                scheduleGotoRanking();
         }
 
         protected virtual ScoreInfo CreateScore()
@@ -541,14 +530,8 @@ namespace osu.Game.Screens.Play
         {
             if (completionProgressDelegate != null && !completionProgressDelegate.Cancelled && !completionProgressDelegate.Completed)
             {
-                // Proceed to result screen if beatmap already finished playing.
-                // This is scheduled since the player needs to become the current screen before the delegate runs. This happens after the return true.
-                Scheduler.Add(() =>
-                {
-                    if (!completionProgressDelegate.Completed && !completionProgressDelegate.Cancelled)
-                        completionProgressDelegate.RunTask();
-                });
-
+                // proceed to result screen if beatmap already finished playing
+                scheduleGotoRanking();
                 return true;
             }
 
@@ -583,6 +566,19 @@ namespace osu.Game.Screens.Play
             storyboardReplacesBackground.Value = false;
         }
 
+        private void scheduleGotoRanking()
+        {
+            completionProgressDelegate?.Cancel();
+            completionProgressDelegate = Schedule(delegate
+            {
+                var score = CreateScore();
+                if (DrawableRuleset.ReplayScore == null)
+                    scoreManager.Import(score).Wait();
+
+                this.Push(CreateResults(score));
+            });
+        }
+
         #endregion
     }
 }