NBloodServerSupervisor/Supervisor/StatisticsManager.cs
2020-02-12 16:19:15 +00:00

43 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Supervisor
{
static class StatisticsManager
{
public static void Start()
{
Task.Factory.StartNew(() =>
{
while (true)
{
Thread.Sleep(TimeSpan.FromSeconds(5));
UpdatePlaytime();
}
});
}
private static void UpdatePlaytime()
{
TimeSpan timeToAdd = new TimeSpan(0);
var now = DateTime.UtcNow;
foreach (var server in Program.State.Servers.Values.Where(s => s.IsStarted))
{
if (server.LastCollectionUtc.HasValue)
{
var elapsedTime = now - server.LastCollectionUtc.Value;
timeToAdd += elapsedTime * (server.CurrentPlayers - 1);
}
server.LastCollectionUtc = now;
}
Program.State.IncreasePlaytime(timeToAdd);
}
}
}