PalacePal/Pal.Client/Floors/Tasks/LoadTerritory.cs

79 lines
2.6 KiB
C#
Raw Normal View History

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;
using Microsoft.Extensions.Logging;
2023-02-18 20:12:36 +00:00
using Pal.Client.Database;
2023-03-30 20:01:43 +00:00
namespace Pal.Client.Floors.Tasks;
internal sealed class LoadTerritory : DbTask<LoadTerritory>
2023-02-18 20:12:36 +00:00
{
2023-03-30 20:01:43 +00:00
private readonly Cleanup _cleanup;
private readonly MemoryTerritory _territory;
2023-02-18 20:12:36 +00:00
2023-03-30 20:01:43 +00:00
public LoadTerritory(IServiceScopeFactory serviceScopeFactory,
Cleanup cleanup,
MemoryTerritory territory)
: base(serviceScopeFactory)
{
_cleanup = cleanup;
_territory = territory;
}
2023-02-18 20:12:36 +00:00
2023-03-30 20:01:43 +00:00
protected override void Run(PalClientContext dbContext, ILogger<LoadTerritory> logger)
{
lock (_territory.LockObj)
2023-02-18 20:12:36 +00:00
{
2023-03-30 20:01:43 +00:00
if (_territory.ReadyState != MemoryTerritory.EReadyState.Loading)
2023-02-18 20:12:36 +00:00
{
2023-03-30 20:01:43 +00:00
logger.LogInformation("Territory {Territory} is in state {State}", _territory.TerritoryType,
_territory.ReadyState);
return;
}
2023-02-18 20:12:36 +00:00
2023-03-30 20:01:43 +00:00
logger.LogInformation("Loading territory {Territory}", _territory.TerritoryType);
2023-02-22 19:29:58 +00:00
2023-03-30 20:01:43 +00:00
// purge outdated locations
_cleanup.Purge(dbContext, _territory.TerritoryType);
2023-02-22 19:29:58 +00:00
2023-03-30 20:01:43 +00:00
// load good locations
List<ClientLocation> locations = dbContext.Locations
.Where(o => o.TerritoryType == (ushort)_territory.TerritoryType)
.Include(o => o.ImportedBy)
.Include(o => o.RemoteEncounters)
.AsSplitQuery()
.ToList();
_territory.Initialize(locations.Select(ToMemoryLocation));
2023-03-30 20:01:43 +00:00
logger.LogInformation("Loaded {Count} locations for territory {Territory}", locations.Count,
_territory.TerritoryType);
2023-02-18 20:12:36 +00:00
}
2023-03-30 20:01:43 +00:00
}
2023-02-18 20:12:36 +00:00
2023-03-30 20:01:43 +00:00
public static PersistentLocation ToMemoryLocation(ClientLocation location)
{
return new PersistentLocation
2023-02-18 20:12:36 +00:00
{
2023-03-30 20:01:43 +00:00
LocalId = location.LocalId,
Type = ToMemoryLocationType(location.Type),
Position = new Vector3(location.X, location.Y, location.Z),
Seen = location.Seen,
Source = location.Source,
RemoteSeenOn = location.RemoteEncounters.Select(o => o.AccountId).ToList(),
};
}
2023-02-18 20:12:36 +00:00
2023-03-30 20:01:43 +00:00
private static MemoryLocation.EType ToMemoryLocationType(ClientLocation.EType type)
{
return type switch
2023-02-18 20:12:36 +00:00
{
2023-03-30 20:01:43 +00:00
ClientLocation.EType.Trap => MemoryLocation.EType.Trap,
ClientLocation.EType.Hoard => MemoryLocation.EType.Hoard,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
2023-02-18 20:12:36 +00:00
}
}