BirthdayBot/BackgroundServices/ConnectionStatus.cs
Noi 2f0fe8641a Implement own sharding system
The BirthdayBot class has been split up into ShardInstance and
ShardManager. Several other things have been reorganized so that shards
may act independently.

The overall goal of these changes made is to limit failures to sections
that can easily be discarded and replaced.
2020-10-04 21:40:38 -07:00

44 lines
1.4 KiB
C#

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
private const int StableScore = 300 / ShardBackgroundWorker.Interval;
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;
}
}
}