Esempio n. 1
0
        public override async Task RunAsync(string parameters, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(Program.Settings.Token))
            {
                this.Logger?.LogCommandOutput("{{warn}}No token has been set.");
                return;
            }

            var playerInfo = await SaliensApi.GetPlayerInfoAsync(Program.Settings.Token);

            if (string.IsNullOrWhiteSpace(playerInfo?.ActivePlanet))
            {
                this.Logger?.LogCommandOutput("No planet has been joined.");
                return;
            }
            if (string.IsNullOrWhiteSpace(playerInfo?.ActiveZoneGame))
            {
                this.Logger?.LogCommandOutput("No zone has been joined.");
                return;
            }

            if (!int.TryParse(playerInfo.ActiveZonePosition, out int zonePos))
            {
                this.Logger?.LogCommandOutput("Invalid zone.");
                return;
            }
            var planet = await SaliensApi.GetPlanetAsync(playerInfo.ActivePlanet);

            this.Logger?.LogCommandOutput(planet.Zones[zonePos].ToConsoleBlock());
        }
Esempio n. 2
0
        private async Task UpdatePlayerInfo(TimeSpan?forceCacheExpiryTime = null)
        {
            if (string.IsNullOrEmpty(this.Token))
            {
                return;
            }

            this.PlayerInfo = await SaliensApi.GetPlayerInfoAsync(this.Token, forceCacheExpiryTime);

            this.State = BotState.Idle;

            if (!string.IsNullOrWhiteSpace(this.PlayerInfo.ActivePlanet))
            {
                this.ActivePlanet = await SaliensApi.GetPlanetAsync(this.PlayerInfo.ActivePlanet, forceCacheExpiryTime);

                this.State = BotState.OnPlanet;

                if (int.TryParse(this.PlayerInfo.ActiveZonePosition, out int zonePosition))
                {
                    this.ActiveZone          = this.ActivePlanet.Zones[zonePosition];
                    this.ActiveZoneStartDate = DateTime.Now - this.PlayerInfo.TimeInZone;
                    this.State = BotState.InZone;
                }

                if (!string.IsNullOrWhiteSpace(this.PlayerInfo.ActiveBossGame))
                {
                    this.ActiveZone          = this.ActivePlanet.Zones.FirstOrDefault(z => z.GameId == this.PlayerInfo.ActiveBossGame);
                    this.ActiveZoneStartDate = DateTime.Now - this.PlayerInfo.TimeInZone;
                    this.State = BotState.InBossZone;
                }
            }

            this.PresenceUpdateTrigger.SetSaliensPlayerState(this.PlayerInfo);
        }
Esempio n. 3
0
        public override async Task RunAsync(string parameters, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(Program.Settings.Token))
            {
                this.Logger?.LogCommandOutput("{warn}No token has been set.");
                return;
            }

            var info = await SaliensApi.GetPlayerInfoAsync(Program.Settings.Token);

            this.Logger?.LogCommandOutput($"Level: {{level}}{info.Level}{{reset}}{Environment.NewLine}" +
                                          $"XP: {{xp}}{long.Parse(info.Score).ToString("#,##0")}{{reset}}" +
                                          (!string.IsNullOrWhiteSpace(info.NextLevelScore) ? $"(required for next level: {{reqxp}}{long.Parse(info.NextLevelScore).ToString("#,##0")}{{reset}})" : "") +
                                          $"{Environment.NewLine}" +
                                          $"Clan: {info.ClanInfo.Name}{Environment.NewLine}" +
                                          $"Active planet: {{planet}}{info.ActivePlanet} {{reset}}{Environment.NewLine}" +
                                          $"Time spent on planet: {info.TimeOnPlanet.ToString()}{Environment.NewLine}" +
                                          $"Active zone: {{zone}}{info.ActiveZonePosition} ({info.ActiveZoneGame}){{reset}}{Environment.NewLine}" +
                                          $"Time spent in zone: {info.TimeInZone.TotalSeconds} seconds");
        }
Esempio n. 4
0
        public override async Task RunAsync(string parameters, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(parameters))
            {
                // Show the current token
                if (!string.IsNullOrWhiteSpace(Program.Settings.Token))
                {
                    this.Logger?.LogCommandOutput($"Your token is currently set to: {{value}}{Program.Settings.Token}{{reset}}.");
                }
                else
                {
                    this.Logger?.LogCommandOutput("You have currently no token set.");
                }

                this.Logger?.LogCommandOutput("You can change the token by appending the token to this command: {command}token {param}<your_token>");
                this.Logger?.LogCommandOutput("where {param}<your_token>{reset} is replaced with your token.");
            }
            else
            {
                // Set the token
                try
                {
                    var playerInfo = await SaliensApi.GetPlayerInfoAsync(parameters);

                    if (playerInfo != null)
                    {
                        Program.Settings.Token.Value = parameters;
                        this.Logger?.LogCommandOutput("Your token has been saved.");
                    }
                    else
                    {
                        this.Logger?.LogCommandOutput("{{err}}Invalid token.");
                    }
                }
                catch (WebException ex)
                {
                    this.Logger?.LogCommandOutput($"{{err}}Invalid response. {ex.Message}");
                }
            }
        }
Esempio n. 5
0
        private async Task JoinZone(int zonePosition)
        {
            for (int i = 0; i < 5; i++)
            {
                try
                {
                    this.Logger?.LogMessage($"{{action}}Joining {{zone}}zone {zonePosition}{{action}}...");
                    var stopwatch = new Stopwatch();
                    stopwatch.Start();
                    await SaliensApi.JoinZoneAsync(this.Token, zonePosition);

                    stopwatch.Stop();

                    var startDate = DateTime.Now;

                    // If the request took too long, resynchronize the start date
                    if (stopwatch.Elapsed > TimeSpan.FromSeconds(1))
                    {
                        var playerInfo = await SaliensApi.GetPlayerInfoAsync(this.Token, TimeSpan.FromSeconds(0));

                        var diff = (startDate - (DateTime.Now - playerInfo.TimeInZone));
                        if (diff > TimeSpan.FromSeconds(0))
                        {
                            this.Logger?.LogMessage($"{{action}}Recalibrated zone join time with {{value}}{diff.Negate().TotalSeconds.ToString("0.###")} seconds");
                            startDate = DateTime.Now - playerInfo.TimeInZone;
                        }
                    }

                    // States
                    this.ActiveZone                    = this.ActivePlanet.Zones[zonePosition];
                    this.ActiveZoneStartDate           = startDate;
                    this.PlayerInfo.ActiveZoneGame     = this.ActiveZone.GameId;
                    this.PlayerInfo.ActiveZonePosition = zonePosition.ToString();
                    this.PlayerInfo.TimeInZone         = TimeSpan.FromSeconds(0);
                    this.State = BotState.InZone;

                    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 zone: {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 zone: {ex.Message}");
                        ResetState();
                        throw;
                    }
                }
                catch (WebException ex)
                {
                    this.Logger?.LogMessage($"{{warn}}Failed to join zone: {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.OnPlanet;
            }
        }