mirror of
https://github.com/CommonLoon102/NBloodServerSupervisor.git
synced 2024-12-23 03:02:51 +01:00
3af4c9dfc7
* add option to play custom maps on private servers * don't show every exception to the end-user * hash the IPs * add description about the purpose of the public custom map list * add punctuation to error messages
73 lines
1.9 KiB
C#
73 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using Common;
|
|
|
|
namespace Supervisor
|
|
{
|
|
static class PrivateServerManager
|
|
{
|
|
public static void Start()
|
|
{
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
while (true)
|
|
{
|
|
TempMapFolderCleanup();
|
|
Thread.Sleep(TimeSpan.FromSeconds(10));
|
|
KillUnusedServers();
|
|
}
|
|
});
|
|
}
|
|
|
|
private static void KillUnusedServers()
|
|
{
|
|
try
|
|
{
|
|
var killables = Program.State.Servers.Values.Where(s =>
|
|
s.IsPrivate
|
|
&& !s.IsStarted
|
|
&& s.CurrentPlayers < 2
|
|
&& (DateTime.UtcNow - s.SpawnedAtUtc) > TimeSpan.FromMinutes(10));
|
|
|
|
foreach (var server in killables)
|
|
{
|
|
Process.GetProcessById(server.ProcessId).Kill();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Log...
|
|
}
|
|
}
|
|
|
|
private static void TempMapFolderCleanup()
|
|
{
|
|
try
|
|
{
|
|
if (!Directory.Exists(CommandLineUtils.TempMapDir))
|
|
{
|
|
Directory.CreateDirectory(CommandLineUtils.TempMapDir);
|
|
}
|
|
|
|
foreach (var dir in Directory.GetDirectories(CommandLineUtils.TempMapDir))
|
|
{
|
|
if (DateTime.UtcNow - File.GetCreationTimeUtc(dir) > TimeSpan.FromDays(1))
|
|
{
|
|
Directory.Delete(dir, recursive: true);
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Log...
|
|
}
|
|
}
|
|
}
|
|
}
|