diff --git a/Pal.Client/Pal.Client.csproj b/Pal.Client/Pal.Client.csproj index ee709bd..5f366a8 100644 --- a/Pal.Client/Pal.Client.csproj +++ b/Pal.Client/Pal.Client.csproj @@ -3,7 +3,7 @@ net6.0-windows 9.0 - 1.14.0.0 + 1.15.0.0 enable diff --git a/Pal.Client/RemoteApi.cs b/Pal.Client/RemoteApi.cs index bc8a83e..b72fef0 100644 --- a/Pal.Client/RemoteApi.cs +++ b/Pal.Client/RemoteApi.cs @@ -25,10 +25,11 @@ namespace Pal.Client private GrpcChannel? _channel; private LoginReply? _lastLoginReply; + private bool _warnedAboutUpgrade = false; public Guid? AccountId { - get => Service.Configuration.AccountIds[remoteUrl]; + get => Service.Configuration.AccountIds.TryGetValue(remoteUrl, out Guid accountId) ? accountId : null; set { if (value != null) @@ -38,10 +39,10 @@ namespace Pal.Client } } - private async Task Connect(CancellationToken cancellationToken, bool retry = true) + private async Task<(bool Success, string Error)> TryConnect(CancellationToken cancellationToken, bool retry = true) { if (Service.Configuration.Mode != Configuration.EMode.Online) - return false; + return (false, "You are not online."); if (_channel == null || !(_channel.State == ConnectivityState.Ready || _channel.State == ConnectivityState.Idle)) { @@ -67,10 +68,19 @@ namespace Pal.Client AccountId = Guid.Parse(createAccountReply.AccountId); Service.Configuration.Save(); } + else + { + if (createAccountReply.Error == CreateAccountError.UpgradeRequired && !_warnedAboutUpgrade) + { + Service.Chat.PrintError("[Palace Pal] Your version of Palace Pal is outdated, please update the plugin using the Plugin Installer."); + _warnedAboutUpgrade = true; + } + return (false, $"Could not create account ({createAccountReply.Error})."); + } } if (AccountId == null) - return false; + return (false, "No account-id after account was attempted to be created."); if (_lastLoginReply == null || string.IsNullOrEmpty(_lastLoginReply.AuthToken) || _lastLoginReply.ExpiresAt.ToDateTime().ToLocalTime() < DateTime.Now) { @@ -82,24 +92,46 @@ namespace Pal.Client AccountId = null; Service.Configuration.Save(); if (retry) - return await Connect(cancellationToken, retry: false); + return await TryConnect(cancellationToken, retry: false); else - return false; + return (false, "Invalid account id."); } + if (_lastLoginReply.Error == LoginError.UpgradeRequired && !_warnedAboutUpgrade) + { + Service.Chat.PrintError("[Palace Pal] Your version of Palace Pal is outdated, please update the plugin using the Plugin Installer."); + _warnedAboutUpgrade = true; + } + return (false, $"Could not log in ({_lastLoginReply.Error})."); } } - return !string.IsNullOrEmpty(_lastLoginReply?.AuthToken); + if (_lastLoginReply == null) + return (false, "No login information available."); + + bool success = !string.IsNullOrEmpty(_lastLoginReply?.AuthToken); + if (!success) + return (success, "Login reply did not include auth token."); + + return (success, string.Empty); + } + + private async Task Connect(CancellationToken cancellationToken) + { + var result = await TryConnect(cancellationToken); + return result.Success; } public async Task VerifyConnection(CancellationToken cancellationToken = default) { - if (!await Connect(cancellationToken)) - return "Could not connect to server"; + _warnedAboutUpgrade = false; + + var connectionResult = await TryConnect(cancellationToken); + if (!connectionResult.Success) + return $"Could not connect to server: {connectionResult.Error}"; var accountClient = new AccountService.AccountServiceClient(_channel); await accountClient.VerifyAsync(new VerifyRequest(), headers: AuthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken); - return "Connection successful"; + return "Connection successful."; } public async Task<(bool, List)> DownloadRemoteMarkers(ushort territoryId, CancellationToken cancellationToken = default) diff --git a/Pal.Common/Protos/account.proto b/Pal.Common/Protos/account.proto index 5186276..21ea121 100644 --- a/Pal.Common/Protos/account.proto +++ b/Pal.Common/Protos/account.proto @@ -25,6 +25,14 @@ message CreateAccountRequest { message CreateAccountReply { bool success = 1; string accountId = 2; + CreateAccountError error = 3; +} + +enum CreateAccountError { + CREATE_ACCOUNT_ERROR_NONE = 0; + CREATE_ACCOUNT_ERROR_UNKNOWN = 1; + CREATE_ACCOUNT_ERROR_UPGRADE_REQUIRED = 2; + CREATE_ACCOUNT_ERROR_INVALID_HASH = 3; } message LoginRequest { @@ -42,6 +50,7 @@ enum LoginError { LOGIN_ERROR_NONE = 0; LOGIN_ERROR_UNKNOWN = 1; LOGIN_ERROR_INVALID_ACCOUNT_ID = 2; + LOGIN_ERROR_UPGRADE_REQUIRED = 3; } message VerifyRequest {