Search in sources :

Example 1 with Gym

use of com.pokegoapi.api.gym.Gym in project PokeGOAPI-Java by Grover-c13.

the class FightGymExample method main.

/**
	 * Fights gyms in the nearby area.
	 */
public static void main(String[] args) {
    OkHttpClient http = new OkHttpClient();
    final PokemonGo api = new PokemonGo(http);
    try {
        //Login and set location
        HashProvider hasher = ExampleConstants.getHashProvider();
        api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
        api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
        List<Pokemon> pokemons = api.getInventories().getPokebank().getPokemons();
        //List to put all pokemon that can be used in a gym battle
        List<Pokemon> possiblePokemon = new ArrayList<>();
        for (Pokemon pokemon : pokemons) {
            //Check if pokemon has full health and is not deployed in a gym
            if (pokemon.getDeployedFortId().length() == 0) {
                if (pokemon.getStamina() < pokemon.getMaxStamina()) {
                    healPokemonFull(api, pokemon);
                    if (!(pokemon.isInjured() || pokemon.isFainted())) {
                        possiblePokemon.add(pokemon);
                    }
                    Thread.sleep(1000);
                } else {
                    possiblePokemon.add(pokemon);
                }
            } else {
                System.out.println(pokemon.getPokemonId() + " already deployed.");
            }
        }
        //Sort by highest CP
        Collections.sort(possiblePokemon, new Comparator<Pokemon>() {

            @Override
            public int compare(Pokemon primary, Pokemon secondary) {
                return Integer.compare(secondary.getCp(), primary.getCp());
            }
        });
        //Pick the top 6 pokemon from the possible list
        final Pokemon[] attackers = new Pokemon[6];
        for (int i = 0; i < 6; i++) {
            attackers[i] = possiblePokemon.get(i);
        }
        //Sort from closest to farthest
        MapObjects mapObjects = api.getMap().getMapObjects();
        List<Gym> gyms = new ArrayList<>(mapObjects.getGyms());
        Collections.sort(gyms, new Comparator<Gym>() {

            @Override
            public int compare(Gym primary, Gym secondary) {
                double lat = api.getLatitude();
                double lng = api.getLongitude();
                double distance1 = MapUtil.distFrom(primary.getLatitude(), primary.getLongitude(), lat, lng);
                double distance2 = MapUtil.distFrom(secondary.getLatitude(), secondary.getLongitude(), lat, lng);
                return Double.compare(distance1, distance2);
            }
        });
        for (Gym gym : gyms) {
            //Check if gym is attackable, and check if it is not owned by your team
            if (gym.isAttackable() && gym.getOwnedByTeam() != api.getPlayerProfile().getPlayerData().getTeam()) {
                //Walk to gym; Documented pathing in TravelToPokestopExample
                Point destination = new Point(gym.getLatitude(), gym.getLongitude());
                Path path = new Path(api.getPoint(), destination, 50.0);
                System.out.println("Traveling to " + destination + " at 50KMPH!");
                path.start(api);
                try {
                    while (!path.isComplete()) {
                        Point point = path.calculateIntermediate(api);
                        api.setLatitude(point.getLatitude());
                        api.setLongitude(point.getLongitude());
                        System.out.println("Time left: " + (int) (path.getTimeLeft(api) / 1000) + " seconds.");
                        Thread.sleep(2000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Beginning battle with gym.");
                //Create battle object
                Battle battle = gym.battle();
                //Start battle
                battle.start(new FightHandler(attackers));
                while (battle.isActive()) {
                    handleAttack(battle);
                }
                //Heal all pokemon after battle
                for (Pokemon pokemon : possiblePokemon) {
                    if (pokemon.getStamina() < pokemon.getMaxStamina()) {
                        healPokemonFull(api, pokemon);
                        Thread.sleep(1000);
                    }
                }
                //If prestige reaches 0, deploy your pokemon
                if (battle.getGym().getPoints() <= 0) {
                    Pokemon best = possiblePokemon.get(0);
                    System.out.println("Deploying " + best.getPokemonId() + " to gym.");
                    battle.getGym().deployPokemon(best);
                }
            }
        }
    } catch (RequestFailedException | InterruptedException e) {
        // failed to login, invalid credentials, auth issue or server issue.
        Log.e("Main", "Failed to login, captcha or server issue: ", e);
    }
}
Also used : Path(com.pokegoapi.util.path.Path) PtcCredentialProvider(com.pokegoapi.auth.PtcCredentialProvider) OkHttpClient(okhttp3.OkHttpClient) ArrayList(java.util.ArrayList) Point(com.pokegoapi.api.map.Point) MapObjects(com.pokegoapi.api.map.MapObjects) Point(com.pokegoapi.api.map.Point) Battle(com.pokegoapi.api.gym.Battle) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) PokemonGo(com.pokegoapi.api.PokemonGo) HashProvider(com.pokegoapi.util.hash.HashProvider) Gym(com.pokegoapi.api.gym.Gym) Pokemon(com.pokegoapi.api.pokemon.Pokemon)

Aggregations

PokemonGo (com.pokegoapi.api.PokemonGo)1 Battle (com.pokegoapi.api.gym.Battle)1 Gym (com.pokegoapi.api.gym.Gym)1 MapObjects (com.pokegoapi.api.map.MapObjects)1 Point (com.pokegoapi.api.map.Point)1 Pokemon (com.pokegoapi.api.pokemon.Pokemon)1 PtcCredentialProvider (com.pokegoapi.auth.PtcCredentialProvider)1 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)1 HashProvider (com.pokegoapi.util.hash.HashProvider)1 Path (com.pokegoapi.util.path.Path)1 ArrayList (java.util.ArrayList)1 OkHttpClient (okhttp3.OkHttpClient)1