2020-01-23 21:03:54 +01:00
|
|
|
|
using Common;
|
|
|
|
|
using Model;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2020-01-24 14:34:10 +01:00
|
|
|
|
using System.IO;
|
2020-01-23 21:03:54 +01:00
|
|
|
|
using System.Linq;
|
2020-01-24 12:23:40 +01:00
|
|
|
|
using System.Runtime.InteropServices;
|
2020-01-23 21:03:54 +01:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Supervisor
|
|
|
|
|
{
|
|
|
|
|
class PublicServerManager
|
|
|
|
|
{
|
2020-01-24 12:23:40 +01:00
|
|
|
|
public static void Start()
|
2020-01-23 21:03:54 +01:00
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(TimeSpan.FromSeconds(2));
|
|
|
|
|
KillOrphanedServers();
|
|
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2020-01-24 12:23:40 +01:00
|
|
|
|
LaunchNewServersWhenNeeded();
|
2020-01-23 21:03:54 +01:00
|
|
|
|
Thread.Sleep(TimeSpan.FromSeconds(1));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void KillOrphanedServers()
|
|
|
|
|
{
|
|
|
|
|
foreach (var process in Process.GetProcessesByName("nblood_server"))
|
|
|
|
|
{
|
|
|
|
|
if (!Program.State.Servers.Values.Any(s => s.ProcessId == process.Id))
|
|
|
|
|
{
|
|
|
|
|
process.Kill();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 12:23:40 +01:00
|
|
|
|
private static void LaunchNewServersWhenNeeded()
|
2020-01-23 21:03:54 +01:00
|
|
|
|
{
|
|
|
|
|
const int maxPlayers = 8;
|
|
|
|
|
|
|
|
|
|
for (int i = 3; i <= maxPlayers; i++)
|
|
|
|
|
{
|
|
|
|
|
if (IsNewServerNeeded(i))
|
|
|
|
|
{
|
|
|
|
|
int port = PortUtils.GetPort();
|
2020-01-24 14:34:10 +01:00
|
|
|
|
var process = Process.Start(NBloodServerStartInfo.Get(i, port));
|
2020-01-23 21:03:54 +01:00
|
|
|
|
Program.State.Servers.AddOrUpdate(port, new Server()
|
|
|
|
|
{
|
|
|
|
|
Port = port,
|
|
|
|
|
ProcessId = process.Id,
|
|
|
|
|
MaximumPlayers = i,
|
|
|
|
|
CurrentPlayers = 1,
|
|
|
|
|
},
|
|
|
|
|
(prt, server) =>
|
|
|
|
|
{
|
|
|
|
|
server.ProcessId = process.Id;
|
|
|
|
|
return server;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(TimeSpan.FromSeconds(2));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool IsNewServerNeeded(int i)
|
|
|
|
|
{
|
|
|
|
|
return !Program.State.Servers.Values.Any(s =>
|
|
|
|
|
!s.IsPrivate && s.MaximumPlayers == i && s.CurrentPlayers < s.MaximumPlayers);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|