コード例 #1
0
ファイル: SaliensBot.cs プロジェクト: Archomeda/AutoSaliens
        private async Task JoinPlanet(string planetId)
        {
            for (int i = 0; i < 5; i++)
            {
                try
                {
                    var planet = await SaliensApi.GetPlanetAsync(planetId);

                    this.Logger?.LogMessage($"{{action}}Joining planet {{planet}}{planetId} ({planet.State.Name}){{action}}...");
                    await SaliensApi.JoinPlanetAsync(this.Token, planetId);

                    // States
                    this.ActivePlanet            = planet;
                    this.PlayerInfo.ActivePlanet = planetId;
                    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 join planet: {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 join planet: {ex.Message}");
                        ResetState();
                        throw;
                    }
                }
                catch (WebException ex)
                {
                    this.Logger?.LogMessage($"{{warn}}Failed to join planet: {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.Idle;
            }
        }