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;
|
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
|
|
|
|
|
{
|
2020-08-09 11:10:33 +02:00
|
|
|
|
private static readonly string[] SupportedGameNames = new[] { "Giants" };
|
|
|
|
|
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;
|
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"]);
|
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
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-08-10 07:11:10 +02:00
|
|
|
|
public async Task DeleteServer(string ipAddress)
|
|
|
|
|
{
|
|
|
|
|
ArgumentUtility.CheckStringForNullOrEmpty(ipAddress, nameof(ipAddress));
|
|
|
|
|
|
|
|
|
|
ServerInfo serverInfo = await this.registryStore.GetServerInfo(ipAddress);
|
|
|
|
|
|
|
|
|
|
if (serverInfo != null)
|
|
|
|
|
{
|
|
|
|
|
await this.registryStore.DeleteServer(serverInfo.id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-08 08:53:35 +02:00
|
|
|
|
}
|
|
|
|
|
}
|