Esempio n. 1
0
        public void LoadData(ISession session, FortData fort)
        {
            var task = session.Client.Fort.GetGymDetails(fort.Id, fort.Latitude, fort.Longitude);

            task.Wait();
            if (task.IsCompleted && task.Result.Result == GymGetInfoResponse.Types.Result.Success)
            {
                var state = new POGOProtos.Data.Gym.GymState()
                {
                    FortData = fort
                };
                fort       = state.FortData;
                GymDetails = task.Result;
                LastCall   = DateTime.UtcNow;
            }
        }
Esempio n. 2
0
        private async Task <MethodResult <GymGetInfoResponse> > GymGetInfo(FortData pokestop)
        {
            var response = await _client.ClientSession.RpcClient.SendRemoteProcedureCallAsync(new Request
            {
                RequestType    = RequestType.GymGetInfo,
                RequestMessage = new GymGetInfoMessage
                {
                    GymId            = pokestop.Id,
                    GymLatDegrees    = pokestop.Latitude,
                    GymLngDegrees    = pokestop.Longitude,
                    PlayerLatDegrees = _client.ClientSession.Player.Latitude,
                    PlayerLngDegrees = _client.ClientSession.Player.Longitude
                }.ToByteString()
            });

            GymGetInfoResponse gymGetInfoResponse = null;

            try
            {
                gymGetInfoResponse = GymGetInfoResponse.Parser.ParseFrom(response);

                return(new MethodResult <GymGetInfoResponse>
                {
                    Data = gymGetInfoResponse,
                    Success = true
                });
            }
            catch (Exception ex)
            {
                if (response.IsEmpty)
                {
                    LogCaller(new LoggerEventArgs("Failed gym get info response", LoggerTypes.Exception, ex));
                }

                return(new MethodResult <GymGetInfoResponse>()
                {
                    Success = false, Data = new GymGetInfoResponse()
                    {
                        Result = GymGetInfoResponse.Types.Result.Unset
                    }
                });
            }
        }
        public static BattleState AttackGym(FortData gym, Client client)
        {
            GymsLogic.StopAttack = false;
            GymGetInfoResponse      gymGetInfoResponse      = null;
            GymStartSessionResponse gymStartSessionResponse = null;

            gymGetInfoResponse = client.Fort.GymGetInfo(gym.Id, gym.Latitude, gym.Longitude);

            // Get defenders info
            int currentDefender = 0;
            var defenders       = gymGetInfoResponse.GymStatusAndDefenders.GymDefender.Select(x => x.MotivatedPokemon.Pokemon).ToList();
            var defender        = gymGetInfoResponse.GymStatusAndDefenders.GymDefender[currentDefender];

            for (int i = 0; i < defenders.Count(); i++)
            {
                Logger.Debug($"(Gym) Defender {i + 1}: {gymGetInfoResponse.GymStatusAndDefenders.GymDefender[i].MotivatedPokemon.Pokemon.PokemonId}" +
                             $" | CpNow: {gymGetInfoResponse.GymStatusAndDefenders.GymDefender[i].MotivatedPokemon.CpNow}" +
                             $" | MotivationNow: {gymGetInfoResponse.GymStatusAndDefenders.GymDefender[i].MotivatedPokemon.MotivationNow}" +
                             $" | Id: {gymGetInfoResponse.GymStatusAndDefenders.GymDefender[i].MotivatedPokemon.Pokemon.Id}");
            }

            // Get atackers
            IEnumerable <PokemonData> selectedAttackers = getPokeAttackers(defender.MotivatedPokemon.Pokemon, GlobalVars.Gyms.Attackers);
            var pokeAttackers    = selectedAttackers as PokemonData[] ?? selectedAttackers.ToArray();
            var pokeAttackersIds = selectedAttackers.Select(x => x.Id);

            if (selectedAttackers.Count() < 6)
            {
                Logger.ColoredConsoleWrite(gymColorLog, "(Gym) Not enougth pokemons to fight.");
                return(BattleState.StateUnset);
            }

            Logger.ColoredConsoleWrite(gymColorLog, "(Gym) Defender: " + GymsLogic.strPokemon(defender.MotivatedPokemon.Pokemon) + $" [{defender.TrainerPublicProfile.Name} ({defender.TrainerPublicProfile.Level})]");
            GymsLogic.ShowPokemons(selectedAttackers);

            while (currentDefender < defenders.Count())
            {
                try
                {
                    // Initialization of attach agains a defender
                    gymStartSessionResponse = GymBattleStartSession(client, gym, defenders[currentDefender].Id, pokeAttackersIds);
                }
                catch (Exception ex)
                {
                    Logger.ExceptionInfo($"Can't start battle against defender {currentDefender}: " + ex.Message);
                    break;
                }

                if (gymStartSessionResponse == null || gymStartSessionResponse.Battle == null || gymStartSessionResponse.Result != GymStartSessionResponse.Types.Result.Success)
                {
                    return(BattleState.StateUnset);
                }

                // Battle loop
                if (gymStartSessionResponse.Battle.BattleLog.State == BattleState.Active)
                {
                    //Logger.ColoredConsoleWrite(gymColorLog, "(Gym) Battle Started");
                    RandomHelper.RandomSleep(2000);

                    // Attack each defender
                    BattleState attackDefender = AttackDefender(gymStartSessionResponse, client, gym);
                    if (attackDefender == BattleState.Victory)
                    {
                        currentDefender++;
                    }
                    else
                    {
                        return(attackDefender);
                    }
                }
            }
            return(BattleState.Victory);
        }
Esempio n. 4
0
        private static AttackGymResponse AttackGym(FortData gym, Client client,
                                                   IEnumerable <PokemonData> pokeAttackers, ulong defenderId, int numDefenders, ulong buddyPokemonId)
        {
            StopAttack = false;
            var pokeAttackersIds = pokeAttackers.Select(x => x.Id);
            var moveSettings     = GetMoveSettings(client);
            GymGetInfoResponse     gymDetails = null;
            StartGymBattleResponse resp       = null;

            // Sometimes we get a null from startgymBattle so we try to start battle 3 times
            var numTries = 3;
            var startOk  = false;

            do
            {
                gymDetails = client.Fort.GymGetInfo(gym.Id, gym.Latitude, gym.Longitude);
                RandomHelper.RandomSleep(200);
                try {
                    resp = client.Fort.StartGymBattle(gym.Id, defenderId, pokeAttackersIds).Result;
                    if (resp != null && resp.BattleLog != null)
                    {
                        startOk = true;
                    }
                    else
                    {
                        Logger.Info("(Gym) - Start Gym Failed.");
                        Logger.Debug("(Gym) - resp:" + resp);
                    }
                } catch (Exception ex1) {
                    Logger.Info("(Gym) - Start Gym Failed. Reason:" + ex1);
                }
                numTries--;
            } while (!startOk && numTries > 0 && !StopAttack);

            if (!startOk)
            {
                Logger.Info("(Gym) - Start Gym Failed. After of 3 tries");
                return(null);
            }

            if (resp.BattleLog.State == BattleState.Active)
            {
                Logger.ColoredConsoleWrite(gymColorLog, "(Gym) - Battle Started");
                RandomHelper.RandomSleep(2000);
                var battleActions       = new List <BattleAction>();
                var lastRetrievedAction = new BattleAction();
                var battleStartMs       = resp.BattleLog.BattleStartTimestampMs;
                var attResp             = client.Fort.AttackGym(gym.Id, resp.BattleId, battleActions, lastRetrievedAction);
                var inBattle            = (attResp.Result == AttackGymResponse.Types.Result.Success);
                inBattle = inBattle && (attResp.BattleLog.State == BattleState.Active);
                var count  = 1;
                var energy = 0;
                Logger.Debug("attResp: " + attResp);
                while (inBattle && !StopAttack)
                {
                    var timeMs        = attResp.BattleLog.ServerMs;
                    var move1Settings = moveSettings.FirstOrDefault(x => x.MoveSettings.MovementId == attResp.ActiveAttacker.PokemonData.Move1).MoveSettings;
                    var move2Settings = moveSettings.FirstOrDefault(x => x.MoveSettings.MovementId == attResp.ActiveAttacker.PokemonData.Move2).MoveSettings;
                    battleActions = new List <BattleAction>();

                    var baseAction = new BattleAction();
                    baseAction.ActionStartMs = timeMs + RandomNumbers.Next(110, 170);
                    baseAction.TargetIndex   = -1;
                    if (attResp.ActiveDefender != null)
                    {
                        baseAction.TargetPokemonId = attResp.ActiveDefender.PokemonData.Id;
                    }
                    if (attResp.ActiveAttacker.PokemonData.Stamina > 0)
                    {
                        baseAction.ActivePokemonId = attResp.ActiveAttacker.PokemonData.Id;
                    }
                    // One each ten times we try to evade attack
                    if (RandomNumbers.Next(1, 10) == 1)
                    {
                        var dodgeAction = new BattleAction();
                        dodgeAction.ActionStartMs   = baseAction.ActionStartMs;
                        dodgeAction.TargetIndex     = baseAction.TargetIndex;
                        dodgeAction.TargetPokemonId = baseAction.TargetPokemonId;
                        dodgeAction.ActivePokemonId = baseAction.ActivePokemonId;
                        dodgeAction.Type            = BattleActionType.ActionDodge;
                        dodgeAction.DurationMs      = 500;
                        battleActions.Add(dodgeAction);
                        Logger.Debug("Dodge Action Added");
                        baseAction.ActionStartMs = dodgeAction.ActionStartMs + dodgeAction.DurationMs;
                    }
                    if (energy >= Math.Abs(move2Settings.EnergyDelta))
                    {
                        var specialAttack = new BattleAction();
                        specialAttack.ActionStartMs   = baseAction.ActionStartMs;
                        specialAttack.TargetIndex     = baseAction.TargetIndex;
                        specialAttack.TargetPokemonId = baseAction.TargetPokemonId;
                        specialAttack.ActivePokemonId = baseAction.ActivePokemonId;
                        specialAttack.Type            = BattleActionType.ActionSpecialAttack;
                        specialAttack.DurationMs      = move2Settings.DurationMs;
                        specialAttack.DamageWindowsStartTimestampMs = specialAttack.ActionStartMs + move2Settings.DamageWindowStartMs;
                        specialAttack.DamageWindowsEndTimestampMs   = specialAttack.ActionStartMs + move2Settings.DamageWindowEndMs;
                        specialAttack.EnergyDelta = move2Settings.EnergyDelta;
                        battleActions.Add(specialAttack);
                        Logger.Debug("Special Attack Added");
                        baseAction.ActionStartMs = specialAttack.ActionStartMs + specialAttack.DurationMs;
                    }
                    else
                    {
                        // One each nine times we do not attack
                        if (RandomNumbers.Next(1, 9) != 1)
                        {
                            var normalAttack = new BattleAction();
                            normalAttack.ActionStartMs   = baseAction.ActionStartMs;
                            normalAttack.TargetIndex     = baseAction.TargetIndex;
                            normalAttack.TargetPokemonId = baseAction.TargetPokemonId;
                            normalAttack.ActivePokemonId = baseAction.ActivePokemonId;
                            normalAttack.Type            = BattleActionType.ActionAttack;
                            normalAttack.DurationMs      = baseAction.DurationMs;
                            normalAttack.DamageWindowsStartTimestampMs = normalAttack.ActionStartMs + move1Settings.DamageWindowStartMs;
                            normalAttack.DamageWindowsEndTimestampMs   = normalAttack.ActionStartMs + move1Settings.DamageWindowEndMs;
                            normalAttack.EnergyDelta = move1Settings.EnergyDelta;
                            battleActions.Add(normalAttack);
                            Logger.Debug("Normal Attack Added");
                            baseAction.ActionStartMs = normalAttack.ActionStartMs + normalAttack.DurationMs;
                        }
                    }

                    lastRetrievedAction = attResp.BattleLog.BattleActions.LastOrDefault(x => x.ActivePokemonId != attResp.ActiveAttacker.PokemonData.Id);

                    var str = string.Join(",", battleActions);
                    Logger.Debug("(Gym) - battleActions: " + str);
                    attResp = client.Fort.AttackGym(gym.Id, resp.BattleId, battleActions, lastRetrievedAction);
                    Logger.Debug("attResp: " + attResp);
                    Logger.Debug("attResp BattleActions: ");
                    ShowBattleActions(attResp.BattleLog.BattleActions);
                    inBattle = (attResp.Result == AttackGymResponse.Types.Result.Success);
                    if (inBattle)
                    {
                        inBattle = (attResp.BattleLog.State == BattleState.Active);

                        if (attResp.ActiveAttacker != null)
                        {
                            energy = attResp.ActiveAttacker.CurrentEnergy;
                            var health         = attResp.ActiveAttacker.CurrentHealth;
                            var activeAttacker = attResp.ActiveAttacker.PokemonData.PokemonId;
                            Logger.Debug($"(Gym) - Attacker: {activeAttacker} Energy={energy}, Health={health}");
                        }

                        if (attResp.ActiveDefender != null)
                        {
                            var energyDef      = attResp.ActiveDefender.CurrentEnergy;
                            var health         = attResp.ActiveDefender.CurrentHealth;
                            var activeDefender = attResp.ActiveDefender.PokemonData.PokemonId;
                            Logger.Debug($"(Gym) - Defender: {activeDefender} Energy={energyDef}, Health={health}");
                        }

                        count++;
                        // Wait until all attack are done. but not more than 1.5 seconds.
                        var waitTime = (int)(baseAction.ActionStartMs - attResp.BattleLog.ServerMs);
                        if (waitTime < 0)
                        {
                            waitTime = 0;
                        }
                        else if (waitTime > 1200)
                        {
                            waitTime = 1200;
                        }
                        RandomHelper.RandomSleep(waitTime, waitTime + 100);
                    }
                }

                Logger.ColoredConsoleWrite(gymColorLog, $"(Gym) - Battle Finished in {count} Rounds.");
                if (attResp.Result == AttackGymResponse.Types.Result.Success)
                {
                    switch (attResp.BattleLog.State)
                    {
                    case BattleState.Defeated:
                        Logger.ColoredConsoleWrite(gymColorLog, "(Gym) - We have lost");
                        if (numDefenders > 1)
                        {
                            AddVisited(gym.Id, 3600000);
                        }
                        break;

                    case BattleState.Victory:
                        Logger.ColoredConsoleWrite(gymColorLog, "(Gym) - We have won");
                        var nextDefenderID = -1L;
                        foreach (var element in attResp.BattleLog.BattleActions)
                        {
                            if (element.BattleResults != null)
                            {
                                Logger.Info("(Gym) - Gym points: " + element.BattleResults.GymPointsDelta);
                                Logger.Info("(Gym) - Experience Awarded: " + element.BattleResults.PlayerExperienceAwarded);
                                Logger.Debug("(Gym) - Next Pokemon: " + element.BattleResults.NextDefenderPokemonId);
                                if (element.BattleResults.NextDefenderPokemonId != -1)
                                {
                                    nextDefenderID = element.BattleResults.NextDefenderPokemonId;
                                }
                            }
                        }
                        if (numDefenders > 1)
                        {
                            Logger.Debug("(Gym) - Leaving Battle");
                            var times = 3;
                            do
                            {
                                attResp = LeaveBattle(gym, client, resp, attResp, lastRetrievedAction, nextDefenderID);
                                times--;
                            } while (attResp.Result != AttackGymResponse.Types.Result.Success && times > 0);
                            const int secondsBetweenAttacks = 300;
                            Logger.Info($"Waiting {secondsBetweenAttacks} seconds before of a new battle.");
                            for (var i = 0; i < secondsBetweenAttacks + 1; i++)
                            {
                                if (StopAttack)
                                {
                                    AddVisited(gym.Id, 3600000);
                                    break;
                                }
                                if ((i != 0) && (i % 10 == 0))
                                {
                                    Logger.Info($"Seconds Left {secondsBetweenAttacks-i}");
                                }
                                if (i % 30 == 0)
                                {
                                    Logic.objClient.Map.GetMapObjects().Wait();
                                }
                                Task.Delay(1000).Wait();
                            }
                        }
                        else
                        {
                            ReviveAndCurePokemons(client);
                            var pokemons = (client.Inventory.GetPokemons()).ToList();
                            RandomHelper.RandomSleep(400);
                            gymDetails = client.Fort.GymGetInfo(gym.Id, gym.Latitude, gym.Longitude);
                            Logger.Debug("(Gym) - Gym Details: " + gymDetails);
                            if (gymDetails.GymStatusAndDefenders.GymDefender.Count < 1)
                            {
                                putInGym(client, gym, getPokeToPut(client, buddyPokemonId, gym.GuardPokemonCp), pokemons);
                            }
                        }
                        break;

                    case BattleState.TimedOut:
                        Logger.ColoredConsoleWrite(gymColorLog, "(Gym) - Timed Out");
                        AddVisited(gym.Id, 1800000);
                        break;
                    }
                }
                else
                {
                    Logger.ColoredConsoleWrite(gymColorLog, "(Gym) - Battle Failed: " + attResp.Result);
                }
                return(attResp);
            }
            return(null);
        }