NBloodServerSupervisor/Supervisor/StatisticsManager.cs

43 lines
1.1 KiB
C#
Raw Permalink Normal View History

2020-01-27 14:48:53 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Supervisor
{
static class StatisticsManager
2020-01-27 14:48:53 +01:00
{
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);
}
}
}