2023-02-18 20:12:36 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Numerics;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
namespace Pal.Client.Floors.Tasks
|
|
|
|
|
{
|
2023-02-22 16:21:48 +00:00
|
|
|
|
internal sealed class LoadTerritory : DbTask<LoadTerritory>
|
2023-02-18 20:12:36 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly MemoryTerritory _territory;
|
|
|
|
|
|
|
|
|
|
public LoadTerritory(IServiceScopeFactory serviceScopeFactory, MemoryTerritory territory)
|
|
|
|
|
: base(serviceScopeFactory)
|
|
|
|
|
{
|
|
|
|
|
_territory = territory;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 16:21:48 +00:00
|
|
|
|
protected override void Run(PalClientContext dbContext, ILogger<LoadTerritory> logger)
|
2023-02-18 20:12:36 +00:00
|
|
|
|
{
|
|
|
|
|
lock (_territory.LockObj)
|
|
|
|
|
{
|
|
|
|
|
if (_territory.IsReady)
|
2023-02-22 16:21:48 +00:00
|
|
|
|
{
|
|
|
|
|
logger.LogInformation("Territory {Territory} is already loaded", _territory.TerritoryType);
|
2023-02-18 20:12:36 +00:00
|
|
|
|
return;
|
2023-02-22 16:21:48 +00:00
|
|
|
|
}
|
2023-02-18 20:12:36 +00:00
|
|
|
|
|
2023-02-22 16:21:48 +00:00
|
|
|
|
logger.LogInformation("Loading territory {Territory}", _territory.TerritoryType);
|
2023-02-18 20:12:36 +00:00
|
|
|
|
List<ClientLocation> locations = dbContext.Locations
|
|
|
|
|
.Where(o => o.TerritoryType == (ushort)_territory.TerritoryType)
|
|
|
|
|
.Include(o => o.ImportedBy)
|
|
|
|
|
.Include(o => o.RemoteEncounters)
|
2023-02-22 16:21:48 +00:00
|
|
|
|
.AsSplitQuery()
|
2023-02-18 20:12:36 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
_territory.Initialize(locations.Select(ToMemoryLocation));
|
2023-02-22 16:21:48 +00:00
|
|
|
|
|
|
|
|
|
logger.LogInformation("Loaded {Count} locations for territory {Territory}", locations.Count,
|
|
|
|
|
_territory.TerritoryType);
|
2023-02-18 20:12:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static PersistentLocation ToMemoryLocation(ClientLocation location)
|
|
|
|
|
{
|
|
|
|
|
return new PersistentLocation
|
|
|
|
|
{
|
|
|
|
|
LocalId = location.LocalId,
|
|
|
|
|
Type = ToMemoryLocationType(location.Type),
|
|
|
|
|
Position = new Vector3(location.X, location.Y, location.Z),
|
|
|
|
|
Seen = location.Seen,
|
|
|
|
|
RemoteSeenOn = location.RemoteEncounters.Select(o => o.AccountId).ToList(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static MemoryLocation.EType ToMemoryLocationType(ClientLocation.EType type)
|
|
|
|
|
{
|
|
|
|
|
return type switch
|
|
|
|
|
{
|
|
|
|
|
ClientLocation.EType.Trap => MemoryLocation.EType.Trap,
|
|
|
|
|
ClientLocation.EType.Hoard => MemoryLocation.EType.Hoard,
|
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|