PalacePal/Pal.Client/Scheduled/QueuedSyncResponse.cs

151 lines
6.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2023-02-18 20:12:36 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Pal.Client.Configuration;
using Pal.Client.DependencyInjection;
using Pal.Client.Extensions;
2023-02-18 20:12:36 +00:00
using Pal.Client.Floors;
using Pal.Client.Floors.Tasks;
using Pal.Client.Net;
namespace Pal.Client.Scheduled
{
2023-02-15 22:17:19 +00:00
internal sealed class QueuedSyncResponse : IQueueOnFrameworkThread
{
2023-02-11 20:10:45 +00:00
public required SyncType Type { get; init; }
public required ushort TerritoryType { get; init; }
public required bool Success { get; init; }
2023-02-18 20:12:36 +00:00
public required IReadOnlyList<PersistentLocation> Locations { get; init; }
internal sealed class Handler : IQueueOnFrameworkThread.Handler<QueuedSyncResponse>
{
2023-02-18 20:12:36 +00:00
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly IPalacePalConfiguration _configuration;
private readonly FloorService _floorService;
private readonly TerritoryState _territoryState;
private readonly DebugState _debugState;
public Handler(
ILogger<Handler> logger,
2023-02-18 20:12:36 +00:00
IServiceScopeFactory serviceScopeFactory,
IPalacePalConfiguration configuration,
FloorService floorService,
TerritoryState territoryState,
DebugState debugState)
: base(logger)
{
2023-02-18 20:12:36 +00:00
_serviceScopeFactory = serviceScopeFactory;
_configuration = configuration;
_floorService = floorService;
_territoryState = territoryState;
_debugState = debugState;
}
2023-02-18 20:12:36 +00:00
protected override void Run(QueuedSyncResponse queued, ref bool recreateLayout)
{
recreateLayout = true;
try
{
2023-02-18 20:12:36 +00:00
var remoteMarkers = queued.Locations;
var memoryTerritory = _floorService.GetTerritoryIfReady(queued.TerritoryType);
if (memoryTerritory != null && _configuration.Mode == EMode.Online && queued.Success &&
remoteMarkers.Count > 0)
{
switch (queued.Type)
{
case SyncType.Download:
case SyncType.Upload:
2023-02-18 20:12:36 +00:00
List<PersistentLocation> newLocations = new();
foreach (var remoteMarker in remoteMarkers)
{
// Both uploads and downloads return the network id to be set, but only the downloaded marker is new as in to-be-saved.
2023-02-18 20:12:36 +00:00
PersistentLocation? localLocation =
memoryTerritory.Locations.SingleOrDefault(x => x == remoteMarker);
if (localLocation != null)
{
2023-02-18 20:12:36 +00:00
localLocation.NetworkId = remoteMarker.NetworkId;
continue;
}
if (queued.Type == SyncType.Download)
2023-02-18 20:12:36 +00:00
{
memoryTerritory.Locations.Add(remoteMarker);
newLocations.Add(remoteMarker);
}
}
2023-02-18 20:12:36 +00:00
if (newLocations.Count > 0)
new SaveNewLocations(_serviceScopeFactory, memoryTerritory, newLocations).Start();
break;
case SyncType.MarkSeen:
var partialAccountId =
_configuration.FindAccount(RemoteApi.RemoteUrl)?.AccountId.ToPartialId();
if (partialAccountId == null)
break;
2023-02-18 20:12:36 +00:00
List<PersistentLocation> locationsToUpdate = new();
foreach (var remoteMarker in remoteMarkers)
{
2023-02-18 20:12:36 +00:00
PersistentLocation? localLocation =
memoryTerritory.Locations.SingleOrDefault(x => x == remoteMarker);
if (localLocation != null)
{
localLocation.RemoteSeenOn.Add(partialAccountId);
locationsToUpdate.Add(localLocation);
}
}
if (locationsToUpdate.Count > 0)
{
new MarkRemoteSeen(_serviceScopeFactory, memoryTerritory, locationsToUpdate,
partialAccountId).Start();
}
break;
}
}
// don't modify state for outdated floors
if (_territoryState.LastTerritory != queued.TerritoryType)
return;
if (queued.Type == SyncType.Download)
{
if (queued.Success)
2023-02-18 20:12:36 +00:00
_territoryState.TerritorySyncState = ESyncState.Complete;
else
2023-02-18 20:12:36 +00:00
_territoryState.TerritorySyncState = ESyncState.Failed;
}
}
catch (Exception e)
{
_debugState.SetFromException(e);
if (queued.Type == SyncType.Download)
2023-02-18 20:12:36 +00:00
_territoryState.TerritorySyncState = ESyncState.Failed;
}
}
}
}
2023-02-18 20:12:36 +00:00
public enum ESyncState
{
NotAttempted,
NotNeeded,
Started,
Complete,
Failed,
}
public enum SyncType
{
Upload,
Download,
MarkSeen,
}
}