Exemple #1
0
        public void AddQuest(Pokestop quest)
        {
            if (Webhooks?.Count == 0)
            {
                return;
            }

            //if (!_sentEvents.Contains(quest))
            {
                lock (_questLock)
                {
                    _questQueue.Enqueue(quest);
                }
            }
        }
Exemple #2
0
        public void AddInvasion(Pokestop invasion)
        {
            if (Webhooks?.Count == 0)
            {
                return;
            }

            //if (!_sentEvents.Contains(invasion))
            {
                lock (_invasionLock)
                {
                    _invasionQueue.Enqueue(invasion);
                }
            }
        }
Exemple #3
0
        public void AddLure(Pokestop lure)
        {
            if (Webhooks?.Count == 0)
            {
                return;
            }

            //if (!_sentEvents.Contains(lure))
            {
                lock (_lureLock)
                {
                    _lureQueue.Enqueue(lure);
                }
            }
        }
Exemple #4
0
        public void AddPokestop(Pokestop pokestop)
        {
            if (Webhooks?.Count == 0)
            {
                return;
            }

            //if (!_sentEvents.Contains(pokestop))
            {
                lock (_pokestopLock)
                {
                    _pokestopQueue.Enqueue(pokestop);
                }
            }
        }
Exemple #5
0
 public static string GetReward(this Pokestop pokestop)
 {
     return(pokestop.QuestRewards?.FirstOrDefault()?.GetReward());
 }
        public async Task <ITask> GetTask(string uuid, string accountUsername, bool startup)
        {
            switch (Type)
            {
            case AutoType.Quest:
                if (_bootstrapCellIds.Count > 0)
                {
                    return(await GetBootstrapTask());
                }

                // TODO: Check InstanceController.NoRequireAccount (username == null and account == null)

                // Check if any stops in area
                if (_allStops.Count == 0)
                {
                    return(null);
                }

                // Check if any pokestops without quests
                if (_todayStops.Count == 0)
                {
                    // Check last completed date
                    var now = DateTime.UtcNow.ToTotalSeconds();
                    if (now - _lastCompletionCheck >= 600)
                    {
                        await OnComplete();

                        return(null);
                    }
                    _lastCompletionCheck = now;

                    var ids      = _allStops.ConvertAll(x => x.Id);
                    var newStops = new List <Pokestop>();
                    try
                    {
                        newStops = await _pokestopRepository.GetByIdsAsync(ids).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"[{Name}] Failed to get list of Pokestops with ids {string.Join(",", ids)}: {ex}");
                    }

                    foreach (var stop in newStops)
                    {
                        if (!_todayStopsTries.ContainsKey(stop.Id))
                        {
                            _todayStopsTries.Add(stop.Id, 0);
                        }
                        var tryCount = _todayStopsTries[stop.Id];
                        if (stop.QuestType == null && stop.Enabled && tryCount <= RetryLimit)
                        {
                            _todayStops.Add(stop.Id, stop);
                        }
                    }
                    // Check if all stops have quests, if so call on complete
                    if (_todayStops.Count == 0)
                    {
                        await OnComplete();

                        return(null);
                    }
                }

                Coordinate lastCoord = null;
                ulong      lastTime  = 0;
                Account    account   = null;
                try
                {
                    if (!string.IsNullOrEmpty(accountUsername))
                    {
                        account = await _accountRepository.GetByIdAsync(accountUsername).ConfigureAwait(false);

                        var lastLat = account.LastEncounterLatitude;
                        var lastLon = account.LastEncounterLongitude;
                        if (lastLat.HasValue && lastLon.HasValue)
                        {
                            lastCoord = new Coordinate(lastLat ?? 0, lastLon ?? 0);
                        }
                        lastTime = account.LastEncounterTime ?? 0;
                    }
                    else
                    {
                        //lastLat = Double(DBController.global.getValueForKey(key: "AIC_\(uuid)_last_lat") ?? "")
                        //lastLon = Double(DBController.global.getValueForKey(key: "AIC_\(uuid)_last_lon") ?? "")
                        //lastTime = UInt32(DBController.global.getValueForKey(key: "AIC_\(uuid)_last_time") ?? "")
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError($"[{Name}] Error: {ex}");
                }

                // TODO: Check cooldown

                if (!string.IsNullOrEmpty(accountUsername) && account != null)
                {
                    if (account.Spins >= SpinLimit)
                    {
                        _logger.LogWarning($"[{Name}] {accountUsername} hit max spin limit {account.Spins}/{SpinLimit}, switching accounts...");
                        return(new QuestTask
                        {
                            Action = ActionType.SwitchAccount,
                            MinimumLevel = MinimumLevel,
                            MaximumLevel = MaximumLevel,
                        });
                    }
                }

                ulong    encounterTime;
                Pokestop nextPokestop = null;
                if (lastCoord != null)
                {
                    // TODO: Lock stops
                    var todayStopsC = _todayStops;
                    if (todayStopsC.Count == 0)
                    {
                        return(null);
                    }

                    Pokestop closest         = null;
                    var      closestDistance = 10000000000000000d;
                    foreach (var(stopId, stop) in todayStopsC)
                    {
                        var coord = new Coordinate(stop.Longitude, stop.Latitude);
                        var dist  = coord.DistanceTo(lastCoord);
                        if (dist < closestDistance)
                        {
                            closest         = stop;
                            closestDistance = dist;
                        }
                    }
                    if (closest == null)
                    {
                        return(null);
                    }

                    nextPokestop = closest;

                    var nearbyPokestops = new List <Pokestop>();
                    var pokestopCoord   = new Coordinate(nextPokestop.Latitude, nextPokestop.Longitude);
                    foreach (var(id, stop) in todayStopsC)
                    {
                        // Revert back to 40m once reverted ingame
                        if (pokestopCoord.DistanceTo(new Coordinate(stop.Latitude, stop.Longitude)) <= 80)
                        {
                            nearbyPokestops.Add(stop);
                        }
                    }
                    foreach (var stop in nearbyPokestops)
                    {
                        if (_todayStops.ContainsKey(stop.Id))
                        {
                            _todayStops.Remove(stop.Id);
                        }
                    }

                    var now = DateTime.UtcNow.ToTotalSeconds();
                    if (lastTime == 0)
                    {
                        encounterTime = now;
                    }
                    else
                    {
                        var encounterTimeT = lastTime + GetCooldownAmount(closestDistance);
                        if (encounterTimeT < now)
                        {
                            encounterTime = now;
                        }
                        else
                        {
                            encounterTime = (ulong)encounterTimeT;
                        }
                        if (encounterTime - now >= 7200)
                        {
                            encounterTime = now + 7200;
                        }
                    }
                    if (nextPokestop != null)
                    {
                        _todayStops.Remove(nextPokestop.Id);
                    }
                }
                else
                {
                    var stop = _todayStops.FirstOrDefault().Value;
                    if (stop == null)
                    {
                        return(null);
                    }

                    nextPokestop  = stop;
                    encounterTime = DateTime.UtcNow.ToTotalSeconds();
                    _todayStops.Remove(_todayStops.Keys.FirstOrDefault());
                }
                await _accountRepository.SpinAsync(accountUsername).ConfigureAwait(false);

                if (_todayStopsTries.ContainsKey(nextPokestop.Id))
                {
                    _todayStopsTries[nextPokestop.Id]++;
                }

                if (!string.IsNullOrEmpty(accountUsername) && account != null)
                {
                    await _accountRepository.SetLastEncounterAsync(accountUsername, nextPokestop.Latitude, nextPokestop.Longitude, encounterTime).ConfigureAwait(false);
                }
                else
                {
                    // TODO: Account cooldowns
                }

                var delayT = DateTime.UtcNow.ToTotalSeconds() - encounterTime;
                var delay  = Convert.ToDouble(delayT == 0 ? 0 : delayT + 1);
                _logger.LogDebug($"[{Name}] Delaying by {delay}");
                if (_todayStops.Count == 0)
                {
                    _lastCompletionCheck = DateTime.UtcNow.ToTotalSeconds();
                    var ids      = _allStops.ConvertAll(x => x.Id);
                    var newStops = new List <Pokestop>();
                    try
                    {
                        newStops = await _pokestopRepository.GetByIdsAsync(ids).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"[{Name}] Failed to get list of Pokestops by ids {string.Join(",", ids)}: {ex}");
                    }
                    // Check if there's still any pokestops left that haven't had quest scanned
                    foreach (var stop in newStops)
                    {
                        if (stop.QuestType == null && stop.Enabled)
                        {
                            _todayStops.Add(stop.Id, stop);
                        }
                    }
                    // If there's no pokestops left that need quests, instance complete
                    if (_todayStops.Count == 0)
                    {
                        await OnComplete();
                    }
                }
                return(new QuestTask
                {
                    Area = Name,
                    Action = ActionType.ScanQuest,
                    Latitude = nextPokestop.Latitude,
                    Longitude = nextPokestop.Longitude,
                    Delay = delay,
                    MinimumLevel = MinimumLevel,
                    MaximumLevel = MaximumLevel,
                });
            }
            return(null);
        }
Exemple #7
0
 public static string GetReward(this Pokestop pokestop)
 {
     return(pokestop.QuestRewards[0].GetReward());
 }