2020-08-08 08:53:35 +02:00
|
|
|
|
namespace Giants.Services
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-08-09 01:31:16 +02:00
|
|
|
|
using System.Linq;
|
2020-08-08 08:53:35 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2022-09-05 05:29:59 +02:00
|
|
|
|
using AutoMapper;
|
2020-08-09 01:31:16 +02:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2020-08-09 11:10:33 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-08-08 08:53:35 +02:00
|
|
|
|
|
|
|
|
|
public class ServerRegistryService : IServerRegistryService
|
|
|
|
|
{
|
2022-09-05 05:29:59 +02:00
|
|
|
|
private static readonly string[] SupportedGameNames = new[] { "Giants", "Giants Beta", "Giants Beta Dedicated", "Giants Dedicated" };
|
2020-08-09 11:10:33 +02:00
|
|
|
|
private readonly ILogger<ServerRegistryService> logger;
|
2020-08-08 08:53:35 +02:00
|
|
|
|
private readonly IServerRegistryStore registryStore;
|
2020-08-09 01:31:16 +02:00
|
|
|
|
private readonly IConfiguration configuration;
|
|
|
|
|
private readonly int maxServerCount;
|
2022-09-05 05:29:59 +02:00
|
|
|
|
private readonly int maxServersPerIp;
|
2020-08-08 08:53:35 +02:00
|
|
|
|
|
2020-08-09 01:31:16 +02:00
|
|
|
|
public ServerRegistryService(
|
2020-08-09 11:10:33 +02:00
|
|
|
|
ILogger<ServerRegistryService> logger,
|
2020-08-09 01:31:16 +02:00
|
|
|
|
IServerRegistryStore registryStore,
|
2020-08-12 06:03:02 +02:00
|
|
|
|
IConfiguration configuration)
|
2020-08-08 08:53:35 +02:00
|
|
|
|
{
|
2020-08-09 11:10:33 +02:00
|
|
|
|
this.logger = logger;
|
2020-08-08 08:53:35 +02:00
|
|
|
|
this.registryStore = registryStore;
|
2020-08-09 01:31:16 +02:00
|
|
|
|
this.configuration = configuration;
|
|
|
|
|
this.maxServerCount = Convert.ToInt32(this.configuration["MaxServerCount"]);
|
2022-09-05 05:29:59 +02:00
|
|
|
|
this.maxServersPerIp = Convert.ToInt32(this.configuration["MaxServersPerIp"]);
|
2020-08-08 08:53:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task AddServer(
|
2020-08-09 02:52:26 +02:00
|
|
|
|
ServerInfo serverInfo)
|
2020-08-08 08:53:35 +02:00
|
|
|
|
{
|
2020-08-09 02:52:26 +02:00
|
|
|
|
ArgumentUtility.CheckForNull(serverInfo, nameof(serverInfo));
|
|
|
|
|
ArgumentUtility.CheckStringForNullOrEmpty(serverInfo.HostIpAddress, nameof(serverInfo.HostIpAddress));
|
2020-08-08 08:53:35 +02:00
|
|
|
|
|
2022-09-05 05:29:59 +02:00
|
|
|
|
string gameName = serverInfo.GameName.Replace("Dedicated", string.Empty).Trim();
|
|
|
|
|
serverInfo.GameName = gameName;
|
2020-08-09 11:10:33 +02:00
|
|
|
|
if (!SupportedGameNames.Contains(serverInfo.GameName, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"Unsupported game name {serverInfo.GameName}", nameof(serverInfo));
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-05 05:29:59 +02:00
|
|
|
|
var existingServers = await this.registryStore.GetServerInfos(whereExpression: x => x.HostIpAddress == serverInfo.HostIpAddress);
|
|
|
|
|
if (existingServers.GroupBy(g => new { g.HostIpAddress }).Any(g => g.Count() > this.maxServersPerIp))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Exceeded maximum servers per IP.");
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 02:52:26 +02:00
|
|
|
|
await this.registryStore.UpsertServerInfo(serverInfo);
|
2020-08-08 08:53:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<ServerInfo>> GetAllServers()
|
|
|
|
|
{
|
2020-08-12 06:03:02 +02:00
|
|
|
|
return (await this.registryStore.GetServerInfos())
|
|
|
|
|
.Take(this.maxServerCount);
|
2020-08-09 11:10:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-05 05:29:59 +02:00
|
|
|
|
// Old API, soon to be deprecated
|
2020-08-10 07:11:10 +02:00
|
|
|
|
public async Task DeleteServer(string ipAddress)
|
|
|
|
|
{
|
|
|
|
|
ArgumentUtility.CheckStringForNullOrEmpty(ipAddress, nameof(ipAddress));
|
|
|
|
|
|
2022-09-05 05:29:59 +02:00
|
|
|
|
var serverInfos = await this.registryStore.GetServerInfos(whereExpression: x => x.HostIpAddress == ipAddress);
|
2020-08-10 07:11:10 +02:00
|
|
|
|
|
2022-09-05 05:29:59 +02:00
|
|
|
|
foreach (var serverInfo in serverInfos)
|
2020-08-10 07:11:10 +02:00
|
|
|
|
{
|
|
|
|
|
await this.registryStore.DeleteServer(serverInfo.id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-05 05:29:59 +02:00
|
|
|
|
|
|
|
|
|
public async Task DeleteServer(string ipAddress, string gameName, int port)
|
|
|
|
|
{
|
|
|
|
|
ArgumentUtility.CheckStringForNullOrEmpty(ipAddress, nameof(ipAddress));
|
|
|
|
|
ArgumentUtility.CheckStringForNullOrEmpty(gameName, nameof(gameName));
|
|
|
|
|
ArgumentUtility.CheckForNonnegativeInt(port, nameof(port));
|
|
|
|
|
|
|
|
|
|
var existingServerInfo = (await this.registryStore.GetServerInfos(
|
|
|
|
|
whereExpression:
|
|
|
|
|
x => x.HostIpAddress == ipAddress &&
|
|
|
|
|
x.Port == port &&
|
|
|
|
|
x.GameName.Equals(gameName, StringComparison.OrdinalIgnoreCase)))
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (existingServerInfo != null)
|
|
|
|
|
{
|
|
|
|
|
await this.registryStore.DeleteServer(existingServerInfo.id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-08 08:53:35 +02:00
|
|
|
|
}
|
|
|
|
|
}
|