private async Task LeaveGame(string gameId) { for (int i = 0; i < 5; i++) { try { await SaliensApi.LeaveGameAsync(this.Token, gameId); if (this.HasActivePlanet && this.ActivePlanet.Id == gameId) { this.Logger?.LogMessage($"{{action}}Leaving planet {{planet}}{gameId} ({this.ActivePlanet.State.Name}){{action}}..."); // States this.ActivePlanet = null; this.PlayerInfo.ActivePlanet = null; this.PlayerInfo.TimeOnPlanet = TimeSpan.FromSeconds(0); this.State = BotState.Idle; } else if (this.HasActiveZone && this.ActiveZone.GameId == gameId) { this.Logger?.LogMessage($"{{action}}Leaving zone {{zone}}{this.ActiveZone.ZonePosition} ({gameId}){{action}}..."); // States this.ActiveZone = null; this.PlayerInfo.ActiveBossGame = null; this.PlayerInfo.ActiveZoneGame = null; this.PlayerInfo.ActiveZonePosition = null; this.PlayerInfo.TimeInZone = TimeSpan.FromSeconds(0); this.State = BotState.OnPlanet; } this.PresenceUpdateTrigger.SetSaliensPlayerState(this.PlayerInfo); return; } catch (SaliensApiException ex) { switch (ex.EResult) { case EResult.Fail: case EResult.Busy: case EResult.RateLimitExceeded: this.Logger?.LogMessage($"{{warn}}Failed to leave game: {ex.Message} - Giving it a few seconds ({i + 1}/5)..."); await Task.Delay(2000); continue; case EResult.Expired: case EResult.NoMatch: default: this.Logger?.LogMessage($"{{warn}}Failed to leave game: {ex.Message}"); ResetState(); throw; } } catch (WebException ex) { this.Logger?.LogMessage($"{{warn}}Failed to leave game: {ex.Message} - Giving it a few seconds ({i + 1}/5)..."); await Task.Delay(2000); continue; } } // States, only set when failed ResetState(); void ResetState() { this.State = BotState.Invalid; // Just reset } }