2020-10-05 04:40:38 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BirthdayBot.BackgroundServices
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Keeps track of the connection status, assigning a score based on either the connection's
|
|
|
|
|
/// longevity or the amount of time it has remained persistently disconnected.
|
|
|
|
|
/// </summary>
|
|
|
|
|
class ConnectionStatus : BackgroundService
|
|
|
|
|
{
|
|
|
|
|
// About 5 minutes
|
2020-10-25 05:43:20 +00:00
|
|
|
|
public const int StableScore = 300 / ShardBackgroundWorker.Interval;
|
2020-10-05 04:40:38 +00:00
|
|
|
|
|
|
|
|
|
public bool Stable { get { return Score >= StableScore; } }
|
|
|
|
|
public int Score { get; private set; }
|
|
|
|
|
|
|
|
|
|
public ConnectionStatus(ShardInstance instance) : base(instance) { }
|
|
|
|
|
|
|
|
|
|
public override Task OnTick(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
switch (ShardInstance.DiscordClient.ConnectionState)
|
|
|
|
|
{
|
|
|
|
|
case Discord.ConnectionState.Connected:
|
|
|
|
|
if (Score < 0) Score = 0;
|
|
|
|
|
Score++;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
if (Score > 0) Score = 0;
|
|
|
|
|
Score--;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// In response to a disconnection event, will immediately reset a positive score to zero.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Disconnected()
|
|
|
|
|
{
|
|
|
|
|
if (Score > 0) Score = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|