2023-02-18 20:12:36 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-02-22 16:21:48 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-02-18 20:12:36 +00:00
|
|
|
|
using Pal.Client.Database;
|
|
|
|
|
using Pal.Common;
|
|
|
|
|
|
|
|
|
|
namespace Pal.Client.Floors.Tasks
|
|
|
|
|
{
|
2023-02-22 16:21:48 +00:00
|
|
|
|
internal sealed class SaveNewLocations : DbTask<SaveNewLocations>
|
2023-02-18 20:12:36 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly MemoryTerritory _territory;
|
|
|
|
|
private readonly List<PersistentLocation> _newLocations;
|
|
|
|
|
|
|
|
|
|
public SaveNewLocations(IServiceScopeFactory serviceScopeFactory, MemoryTerritory territory,
|
|
|
|
|
List<PersistentLocation> newLocations)
|
|
|
|
|
: base(serviceScopeFactory)
|
|
|
|
|
{
|
|
|
|
|
_territory = territory;
|
|
|
|
|
_newLocations = newLocations;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 16:21:48 +00:00
|
|
|
|
protected override void Run(PalClientContext dbContext, ILogger<SaveNewLocations> logger)
|
2023-02-18 20:12:36 +00:00
|
|
|
|
{
|
2023-02-22 16:21:48 +00:00
|
|
|
|
Run(_territory, dbContext, logger, _newLocations);
|
2023-02-18 20:12:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 16:21:48 +00:00
|
|
|
|
public static void Run<T>(
|
|
|
|
|
MemoryTerritory territory,
|
|
|
|
|
PalClientContext dbContext,
|
|
|
|
|
ILogger<T> logger,
|
2023-02-18 20:12:36 +00:00
|
|
|
|
List<PersistentLocation> locations)
|
|
|
|
|
{
|
|
|
|
|
lock (territory.LockObj)
|
|
|
|
|
{
|
2023-02-22 16:21:48 +00:00
|
|
|
|
logger.LogInformation("Saving {Count} new locations for territory {Territory}", locations.Count,
|
|
|
|
|
territory.TerritoryType);
|
|
|
|
|
|
2023-02-18 20:12:36 +00:00
|
|
|
|
Dictionary<PersistentLocation, ClientLocation> mapping =
|
|
|
|
|
locations.ToDictionary(x => x, x => ToDatabaseLocation(x, territory.TerritoryType));
|
|
|
|
|
dbContext.Locations.AddRange(mapping.Values);
|
|
|
|
|
dbContext.SaveChanges();
|
|
|
|
|
|
|
|
|
|
foreach ((PersistentLocation persistentLocation, ClientLocation clientLocation) in mapping)
|
|
|
|
|
{
|
|
|
|
|
persistentLocation.LocalId = clientLocation.LocalId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ClientLocation ToDatabaseLocation(PersistentLocation location, ETerritoryType territoryType)
|
|
|
|
|
{
|
|
|
|
|
return new ClientLocation
|
|
|
|
|
{
|
|
|
|
|
TerritoryType = (ushort)territoryType,
|
|
|
|
|
Type = ToDatabaseType(location.Type),
|
|
|
|
|
X = location.Position.X,
|
|
|
|
|
Y = location.Position.Y,
|
|
|
|
|
Z = location.Position.Z,
|
|
|
|
|
Seen = location.Seen,
|
2023-02-22 19:29:58 +00:00
|
|
|
|
Source = location.Source,
|
2023-02-18 20:12:36 +00:00
|
|
|
|
SinceVersion = typeof(Plugin).Assembly.GetName().Version!.ToString(2),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ClientLocation.EType ToDatabaseType(MemoryLocation.EType type)
|
|
|
|
|
{
|
|
|
|
|
return type switch
|
|
|
|
|
{
|
|
|
|
|
MemoryLocation.EType.Trap => ClientLocation.EType.Trap,
|
|
|
|
|
MemoryLocation.EType.Hoard => ClientLocation.EType.Hoard,
|
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|