GiantsTools/Giants.Services/Services/ServerRegistryService.cs

62 lines
2.2 KiB
C#
Raw Normal View History

namespace Giants.Services
{
using System;
using System.Collections.Generic;
2020-08-09 01:31:16 +02:00
using System.Linq;
using System.Threading.Tasks;
2020-08-09 01:31:16 +02:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
public class ServerRegistryService : IServerRegistryService
{
private static readonly string[] SupportedGameNames = new[] { "Giants" };
private readonly ILogger<ServerRegistryService> logger;
private readonly IServerRegistryStore registryStore;
2020-08-09 01:31:16 +02:00
private readonly IConfiguration configuration;
private readonly int maxServerCount;
2020-08-09 01:31:16 +02:00
public ServerRegistryService(
ILogger<ServerRegistryService> logger,
2020-08-09 01:31:16 +02:00
IServerRegistryStore registryStore,
IConfiguration configuration)
{
this.logger = logger;
this.registryStore = registryStore;
2020-08-09 01:31:16 +02:00
this.configuration = configuration;
this.maxServerCount = Convert.ToInt32(this.configuration["MaxServerCount"]);
}
public async Task AddServer(
2020-08-09 02:52:26 +02:00
ServerInfo serverInfo)
{
2020-08-09 02:52:26 +02:00
ArgumentUtility.CheckForNull(serverInfo, nameof(serverInfo));
ArgumentUtility.CheckStringForNullOrEmpty(serverInfo.HostIpAddress, nameof(serverInfo.HostIpAddress));
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);
}
public async Task<IEnumerable<ServerInfo>> GetAllServers()
{
return (await this.registryStore.GetServerInfos())
.Take(this.maxServerCount);
}
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);
}
}
}
}