From 51d474099fcb2328a0b44e9fe1455a936b9459c2 Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Mon, 6 Feb 2023 22:00:38 +0100 Subject: [PATCH] Update RemoteSeenOn logic --- Pal.Client/Configuration.cs | 10 +++++++++- Pal.Client/LocalState.cs | 8 +++++++- Pal.Client/Marker.cs | 2 +- Pal.Client/Net/RemoteApi.AccountService.cs | 8 +++++--- Pal.Client/Net/RemoteApi.cs | 5 +++-- Pal.Client/Pal.Client.csproj | 2 +- Pal.Client/Plugin.cs | 8 ++++---- Pal.Client/Scheduled/QueuedSyncResponse.cs | 6 +++--- 8 files changed, 33 insertions(+), 16 deletions(-) diff --git a/Pal.Client/Configuration.cs b/Pal.Client/Configuration.cs index 5937914..ec0b97e 100644 --- a/Pal.Client/Configuration.cs +++ b/Pal.Client/Configuration.cs @@ -17,7 +17,7 @@ namespace Pal.Client { private static readonly byte[] _entropy = { 0x22, 0x4b, 0xe7, 0x21, 0x44, 0x83, 0x69, 0x55, 0x80, 0x38 }; - public int Version { get; set; } = 5; + public int Version { get; set; } = 6; #region Saved configuration values public bool FirstUse { get; set; } = true; @@ -129,6 +129,14 @@ namespace Pal.Client Version = 5; Save(); } + + if (Version == 5) + { + LocalState.UpdateAll(); + + Version = 6; + Save(); + } } #pragma warning restore CS0612 // Type or member is obsolete diff --git a/Pal.Client/LocalState.cs b/Pal.Client/LocalState.cs index 9ac55b8..87e4858 100644 --- a/Pal.Client/LocalState.cs +++ b/Pal.Client/LocalState.cs @@ -14,7 +14,7 @@ namespace Pal.Client internal class LocalState { private static readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions { IncludeFields = true }; - private static readonly int _currentVersion = 3; + private static readonly int _currentVersion = 4; public uint TerritoryType { get; set; } public ConcurrentBag Markers { get; set; } = new(); @@ -69,6 +69,12 @@ namespace Pal.Client localState.ApplyFilters(); + if (version <= 3) + { + foreach (var marker in localState.Markers) + marker.RemoteSeenOn = marker.RemoteSeenOn.Select(x => x.PadRight(14).Substring(0, 13)).ToList(); + } + if (version < _currentVersion) localState.Save(); diff --git a/Pal.Client/Marker.cs b/Pal.Client/Marker.cs index 9748ab3..a212973 100644 --- a/Pal.Client/Marker.cs +++ b/Pal.Client/Marker.cs @@ -33,7 +33,7 @@ namespace Pal.Client /// Which account ids this marker was seen. This is a list merely to support different remote endpoints /// (where each server would assign you a different id). /// - public List RemoteSeenOn { get; set; } = new List(); + public List RemoteSeenOn { get; set; } = new List(); /// /// Whether this marker was requested to be seen, to avoid duplicate requests. diff --git a/Pal.Client/Net/RemoteApi.AccountService.cs b/Pal.Client/Net/RemoteApi.AccountService.cs index 17021ea..3f4a33a 100644 --- a/Pal.Client/Net/RemoteApi.AccountService.cs +++ b/Pal.Client/Net/RemoteApi.AccountService.cs @@ -52,7 +52,7 @@ namespace Pal.Client.Net { Id = Guid.Parse(createAccountReply.AccountId), }; - PluginLog.Information($"TryConnect: Account created with id {PartialAccountId}"); + PluginLog.Information($"TryConnect: Account created with id {FormattedPartialAccountId}"); Service.Configuration.Save(); } @@ -76,11 +76,11 @@ namespace Pal.Client.Net if (!_loginInfo.IsValid) { - PluginLog.Information($"TryConnect: Logging in with account id {PartialAccountId}"); + PluginLog.Information($"TryConnect: Logging in with account id {FormattedPartialAccountId}"); LoginReply loginReply = await accountClient.LoginAsync(new LoginRequest { AccountId = AccountId?.ToString() }, headers: UnauthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken); if (loginReply.Success) { - PluginLog.Information($"TryConnect: Login successful with account id: {PartialAccountId}"); + PluginLog.Information($"TryConnect: Login successful with account id: {FormattedPartialAccountId}"); _loginInfo = new LoginInfo(loginReply.AuthToken); var account = Account; @@ -141,6 +141,8 @@ namespace Pal.Client.Net PluginLog.Information("VerifyConnection: Connection established, trying to verify auth token"); var accountClient = new AccountService.AccountServiceClient(_channel); await accountClient.VerifyAsync(new VerifyRequest(), headers: AuthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken); + + PluginLog.Information("VerifyConnection: Verification returned no errors."); return "Connection successful."; } diff --git a/Pal.Client/Net/RemoteApi.cs b/Pal.Client/Net/RemoteApi.cs index b436989..f19e757 100644 --- a/Pal.Client/Net/RemoteApi.cs +++ b/Pal.Client/Net/RemoteApi.cs @@ -34,8 +34,9 @@ namespace Pal.Client.Net public Guid? AccountId => Account?.Id; - private string PartialAccountId => - Account?.Id?.ToString()?.PadRight(14).Substring(0, 13) ?? "[no account id]"; + public string? PartialAccountId => Account?.Id?.ToString()?.PadRight(14).Substring(0, 13); + + private string FormattedPartialAccountId => PartialAccountId ?? "[no account id]"; public void Dispose() { diff --git a/Pal.Client/Pal.Client.csproj b/Pal.Client/Pal.Client.csproj index 42d4d29..d8c6a8d 100644 --- a/Pal.Client/Pal.Client.csproj +++ b/Pal.Client/Pal.Client.csproj @@ -3,7 +3,7 @@ net7.0-windows 11.0 - 2.4 + 2.5 enable diff --git a/Pal.Client/Plugin.cs b/Pal.Client/Plugin.cs index 8064d35..4d8031a 100644 --- a/Pal.Client/Plugin.cs +++ b/Pal.Client/Plugin.cs @@ -327,7 +327,7 @@ namespace Pal.Client var currentFloorMarkers = currentFloor.Markers; bool updateSeenMarkers = false; - var accountId = Service.RemoteApi.AccountId; + var partialAccountId = Service.RemoteApi.PartialAccountId; foreach (var visibleMarker in visibleMarkers) { Marker? knownMarker = currentFloorMarkers.SingleOrDefault(x => x == visibleMarker); @@ -341,7 +341,7 @@ namespace Pal.Client // This requires you to have seen a trap/hoard marker once per floor to synchronize this for older local states, // markers discovered afterwards are automatically marked seen. - if (accountId != null && knownMarker.NetworkId != null && !knownMarker.RemoteSeenRequested && !knownMarker.RemoteSeenOn.Contains(accountId.Value)) + if (partialAccountId != null && knownMarker.NetworkId != null && !knownMarker.RemoteSeenRequested && !knownMarker.RemoteSeenOn.Contains(partialAccountId)) updateSeenMarkers = true; continue; @@ -377,9 +377,9 @@ namespace Pal.Client } } - if (updateSeenMarkers && accountId != null) + if (updateSeenMarkers && partialAccountId != null) { - var markersToUpdate = currentFloorMarkers.Where(x => x.Seen && x.NetworkId != null && !x.RemoteSeenRequested && !x.RemoteSeenOn.Contains(accountId.Value)).ToList(); + var markersToUpdate = currentFloorMarkers.Where(x => x.Seen && x.NetworkId != null && !x.RemoteSeenRequested && !x.RemoteSeenOn.Contains(partialAccountId)).ToList(); foreach (var marker in markersToUpdate) marker.RemoteSeenRequested = true; Task.Run(async () => await SyncSeenMarkersForTerritory(LastTerritory, markersToUpdate)); diff --git a/Pal.Client/Scheduled/QueuedSyncResponse.cs b/Pal.Client/Scheduled/QueuedSyncResponse.cs index dc272b7..a2539a7 100644 --- a/Pal.Client/Scheduled/QueuedSyncResponse.cs +++ b/Pal.Client/Scheduled/QueuedSyncResponse.cs @@ -45,14 +45,14 @@ namespace Pal.Client.Scheduled break; case SyncType.MarkSeen: - var accountId = Service.RemoteApi.AccountId; - if (accountId == null) + var partialAccountId = Service.RemoteApi.PartialAccountId; + if (partialAccountId == null) break; foreach (var remoteMarker in remoteMarkers) { Marker? localMarker = currentFloor.Markers.SingleOrDefault(x => x == remoteMarker); if (localMarker != null) - localMarker.RemoteSeenOn.Add(accountId.Value); + localMarker.RemoteSeenOn.Add(partialAccountId); } break; }