1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 04:13:00 +08:00
osu-lazer/osu.Game/Online/Multiplayer/ServerShutdownNotification.cs

63 lines
2.2 KiB
C#
Raw Normal View History

2022-09-20 14:51:56 +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.
using System;
using Humanizer.Localisation;
using osu.Framework.Allocation;
2022-09-20 18:53:39 +08:00
using osu.Framework.Threading;
2022-09-20 14:51:56 +08:00
using osu.Game.Overlays.Notifications;
using osu.Game.Utils;
namespace osu.Game.Online.Multiplayer
{
2022-11-24 13:32:20 +08:00
public partial class ServerShutdownNotification : SimpleNotification
2022-09-20 14:51:56 +08:00
{
private readonly DateTimeOffset endDate;
2022-09-20 18:53:39 +08:00
private ScheduledDelegate? updateDelegate;
2022-09-20 14:51:56 +08:00
public ServerShutdownNotification(TimeSpan duration)
{
endDate = DateTimeOffset.UtcNow + duration;
}
[BackgroundDependencyLoader]
private void load()
{
updateTime();
}
protected override void LoadComplete()
{
base.LoadComplete();
2022-09-20 18:53:39 +08:00
updateDelegate = Scheduler.Add(updateTimeWithReschedule);
2022-09-20 14:51:56 +08:00
}
private void updateTimeWithReschedule()
{
updateTime();
// The remaining time on a countdown may be at a fractional portion between two seconds.
// We want to align certain audio/visual cues to the point at which integer seconds change.
// To do so, we schedule to the next whole second. Note that scheduler invocation isn't
// guaranteed to be accurate, so this may still occur slightly late, but even in such a case
// the next invocation will be roughly correct.
double timeToNextSecond = endDate.Subtract(DateTimeOffset.UtcNow).TotalMilliseconds % 1000;
2022-09-20 18:53:39 +08:00
updateDelegate = Scheduler.AddDelayed(updateTimeWithReschedule, timeToNextSecond);
2022-09-20 14:51:56 +08:00
}
2022-09-20 18:53:39 +08:00
private void updateTime()
{
TimeSpan remaining = endDate.Subtract(DateTimeOffset.Now);
if (remaining.TotalSeconds <= 5)
{
updateDelegate?.Cancel();
Text = "The multiplayer server will be right back...";
}
else
Text = $"The multiplayer server is restarting in {HumanizerUtils.Humanize(remaining, precision: 3, minUnit: TimeUnit.Second)}.";
}
2022-09-20 14:51:56 +08:00
}
}