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

51 lines
1.8 KiB
C#
Raw Normal View History

2023-02-18 20:12:36 +00:00
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
2023-02-18 20:12:36 +00:00
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 MarkRemoteSeen : DbTask<MarkRemoteSeen>
2023-02-18 20:12:36 +00:00
{
2023-03-30 20:01:43 +00:00
private readonly MemoryTerritory _territory;
private readonly IReadOnlyList<PersistentLocation> _locations;
private readonly string _accountId;
public MarkRemoteSeen(IServiceScopeFactory serviceScopeFactory,
MemoryTerritory territory,
IReadOnlyList<PersistentLocation> locations,
string accountId)
: base(serviceScopeFactory)
2023-02-18 20:12:36 +00:00
{
2023-03-30 20:01:43 +00:00
_territory = territory;
_locations = locations;
_accountId = accountId;
}
2023-02-18 20:12:36 +00:00
2023-03-30 20:01:43 +00:00
protected override void Run(PalClientContext dbContext, ILogger<MarkRemoteSeen> logger)
{
lock (_territory.LockObj)
2023-02-18 20:12:36 +00:00
{
2023-03-30 20:01:43 +00:00
logger.LogInformation("Marking {Count} locations as seen remotely on {Account} in territory {Territory}",
_locations.Count, _accountId, _territory.TerritoryType);
2023-02-18 20:12:36 +00:00
2023-03-30 20:01:43 +00:00
List<int> locationIds = _locations.Select(x => x.LocalId).Where(x => x != null).Cast<int>().ToList();
List<ClientLocation> locationsToUpdate =
dbContext.Locations
.Include(x => x.RemoteEncounters)
.Where(x => locationIds.Contains(x.LocalId))
.ToList()
.Where(x => x.RemoteEncounters.All(encounter => encounter.AccountId != _accountId))
.ToList();
foreach (var clientLocation in locationsToUpdate)
2023-02-18 20:12:36 +00:00
{
2023-03-30 20:01:43 +00:00
clientLocation.RemoteEncounters.Add(new RemoteEncounter(clientLocation, _accountId));
2023-02-18 20:12:36 +00:00
}
2023-03-30 20:01:43 +00:00
dbContext.SaveChanges();
2023-02-18 20:12:36 +00:00
}
}
}