Ejemplo n.º 1
0
        private Task TryGameLoginAsync(StarSonataApi ssApi, DiscordApi discordApi, Settings settings)
        {
            return(Task.Run(
                       async() =>
            {
                var connected = false;
                var semaphoreEntered = false;
                try
                {
                    semaphoreEntered = await this.loginSemaphore.WaitAsync(0).ConfigureAwait(false);
                    if (semaphoreEntered)
                    {
                        if (this.AppState == EAppState.ManuallyStopped)
                        {
                            return;
                        }

                        this.AppState = EAppState.WaitingForLogin;
                        ssApi.TryLoginAsync(settings.GameUsername, settings.GamePassword).Wait();


                        if (!ssApi.IsConnected)
                        {
                            lock (this)
                            {
                                this.lastError = DateTime.Now;
                            }
                        }
                        else
                        {
                            connected = true;
                            if (this.AppState == EAppState.ManuallyStopped)
                            {
                                this.PreStoppedState = EAppState.Ready;
                            }
                            else
                            {
                                this.AppState = EAppState.Ready;
                            }
                        }


                        if (!connected)
                        {
                            lock (this)
                            {
                                this.lastError = DateTime.Now;
                            }

                            Environment.Exit(-1);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Environment.Exit(-1);
                }
            }));
        }
Ejemplo n.º 2
0
        private Task TryGameLoginAsync(StarSonataApi ssApi, DiscordApi discordApi, Settings settings)
        {
            return(Task.Run(
                       async() =>
            {
                var connected = false;
                var semaphoreEntered = false;
                try
                {
                    semaphoreEntered = await this.loginSemaphore.WaitAsync(0).ConfigureAwait(false);
                    if (semaphoreEntered)
                    {
                        for (var i = 0; i < 10; ++i)
                        {
                            if (this.AppState == EAppState.ManuallyStopped)
                            {
                                return;
                            }

                            this.AppState = EAppState.WaitingForLogin;

                            try
                            {
                                ssApi.TryLoginAsync(settings.GameUsername, settings.GamePassword).Wait();
                            }
                            catch (Exception)
                            {
                                // ignored
                            }

                            if (!ssApi.IsConnected)
                            {
                                lock (this)
                                {
                                    this.lastError = DateTime.Now;
                                }

                                discordApi.SendMessageAsync(
                                    settings.ErrorChannel,
                                    $"Error: Login attempt {i + 1} of 10 failed. Trying again in 30 seconds.")
                                .Forget();
                                Task.Delay(TimeSpan.FromSeconds(1)).Wait();
                            }
                            else
                            {
                                connected = true;
                                if (this.AppState == EAppState.ManuallyStopped)
                                {
                                    this.PreStoppedState = EAppState.Ready;
                                }
                                else
                                {
                                    this.AppState = EAppState.Ready;
                                }

                                break;
                            }
                        }

                        if (!connected)
                        {
                            lock (this)
                            {
                                this.lastError = DateTime.Now;
                            }

                            await discordApi.SendMessageAsync(
                                settings.ErrorChannel,
                                "Error: Was unable to establish a connection. Will retry in 5 minutes")
                            .ConfigureAwait(false);
                            Task.Delay(TimeSpan.FromMinutes(0.1)).ContinueWith(
                                t => { this.TryGameLoginAsync(ssApi, discordApi, settings).Forget(); })
                            .Forget();
                        }
                    }
                }
                finally
                {
                    if (semaphoreEntered)
                    {
                        this.loginSemaphore.Release();
                    }
                }
            }));
        }