PalacePal/Pal.Client/DependencyInjection/ImportService.cs

111 lines
3.8 KiB
C#
Raw Normal View History

2023-02-16 18:51:54 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2023-02-18 20:12:36 +00:00
using Account;
using Microsoft.EntityFrameworkCore;
2023-02-16 18:51:54 +00:00
using Microsoft.Extensions.DependencyInjection;
using Pal.Client.Database;
2023-02-18 20:12:36 +00:00
using Pal.Client.Floors;
using Pal.Client.Floors.Tasks;
using Pal.Common;
2023-02-16 18:51:54 +00:00
namespace Pal.Client.DependencyInjection
{
internal sealed class ImportService
{
private readonly IServiceProvider _serviceProvider;
2023-02-18 20:12:36 +00:00
private readonly FloorService _floorService;
2023-02-16 18:51:54 +00:00
2023-02-18 20:12:36 +00:00
public ImportService(IServiceProvider serviceProvider, FloorService floorService)
2023-02-16 18:51:54 +00:00
{
_serviceProvider = serviceProvider;
2023-02-18 20:12:36 +00:00
_floorService = floorService;
2023-02-16 18:51:54 +00:00
}
2023-02-18 20:12:36 +00:00
/*
2023-02-16 18:51:54 +00:00
public void Add(ImportHistory history)
{
using var scope = _serviceProvider.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<PalClientContext>();
dbContext.Imports.Add(history);
dbContext.SaveChanges();
}
2023-02-18 20:12:36 +00:00
*/
2023-02-16 18:51:54 +00:00
public async Task<ImportHistory?> FindLast(CancellationToken token = default)
2023-02-16 18:51:54 +00:00
{
await using var scope = _serviceProvider.CreateAsyncScope();
await using var dbContext = scope.ServiceProvider.GetRequiredService<PalClientContext>();
2023-02-16 18:51:54 +00:00
return await dbContext.Imports.OrderByDescending(x => x.ImportedAt).ThenBy(x => x.Id).FirstOrDefaultAsync(cancellationToken: token);
2023-02-16 18:51:54 +00:00
}
2023-02-18 20:12:36 +00:00
/*
2023-02-16 18:51:54 +00:00
public List<ImportHistory> FindForServer(string server)
{
if (string.IsNullOrEmpty(server))
return new();
using var scope = _serviceProvider.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<PalClientContext>();
return dbContext.Imports.Where(x => x.RemoteUrl == server).ToList();
2023-02-18 20:12:36 +00:00
}*/
2023-02-16 18:51:54 +00:00
2023-02-18 20:12:36 +00:00
public (int traps, int hoard) Import(ExportRoot import)
2023-02-16 18:51:54 +00:00
{
using var scope = _serviceProvider.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<PalClientContext>();
2023-02-18 20:12:36 +00:00
dbContext.Imports.RemoveRange(dbContext.Imports.Where(x => x.RemoteUrl == import.ServerUrl).ToList());
ImportHistory importHistory = new ImportHistory
{
Id = Guid.Parse(import.ExportId),
RemoteUrl = import.ServerUrl,
ExportedAt = import.CreatedAt.ToDateTime(),
ImportedAt = DateTime.UtcNow,
};
dbContext.Imports.Add(importHistory);
int traps = 0;
int hoard = 0;
foreach (var floor in import.Floors)
{
ETerritoryType territoryType = (ETerritoryType)floor.TerritoryType;
List<PersistentLocation> existingLocations = dbContext.Locations
.Where(loc => loc.TerritoryType == floor.TerritoryType)
.ToList()
.Select(LoadTerritory.ToMemoryLocation)
.ToList();
foreach (var newLocation in floor.Objects)
{
throw new NotImplementedException();
}
}
// TODO filter here, update territories
2023-02-16 18:51:54 +00:00
dbContext.SaveChanges();
2023-02-18 20:12:36 +00:00
_floorService.ResetAll();
return (traps, hoard);
2023-02-16 18:51:54 +00:00
}
public void RemoveById(Guid id)
2023-02-18 20:12:36 +00:00
{
using var scope = _serviceProvider.CreateScope();
using var dbContext = scope.ServiceProvider.GetRequiredService<PalClientContext>();
dbContext.RemoveRange(dbContext.Imports.Where(x => x.Id == id));
// TODO filter here, update territories
dbContext.SaveChanges();
_floorService.ResetAll();
}
2023-02-16 18:51:54 +00:00
}
}