Search in sources :

Example 21 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.

the class TransferMultiplePokemon method main.

/**
 * Transfers all bad pokemon from the player's inventory.
 */
public static void main(String[] args) {
    OkHttpClient http = new OkHttpClient();
    PokemonGo api = new PokemonGo(http);
    try {
        HashProvider hasher = ExampleConstants.getHashProvider();
        api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
        api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
        PokeBank pokebank = api.inventories.pokebank;
        List<Pokemon> pokemons = pokebank.pokemons;
        List<Pokemon> transferPokemons = new ArrayList<>();
        // Find all pokemon of bad types or with IV less than 25%
        for (Pokemon pokemon : pokemons) {
            PokemonId id = pokemon.getPokemonId();
            double iv = pokemon.getIvInPercentage();
            if (iv < 90) {
                if (id == PokemonId.RATTATA || id == PokemonId.PIDGEY || id == PokemonId.CATERPIE || id == PokemonId.WEEDLE || id == PokemonId.MAGIKARP || id == PokemonId.ZUBAT || iv < 25) {
                    transferPokemons.add(pokemon);
                }
            }
        }
        System.out.println("Releasing " + transferPokemons.size() + " pokemon.");
        Pokemon[] transferArray = transferPokemons.toArray(new Pokemon[transferPokemons.size()]);
        Map<PokemonFamilyId, Integer> responses = pokebank.releasePokemon(transferArray);
        // Loop through all responses and find the total amount of candies earned for each family
        Map<PokemonFamilyId, Integer> candies = new HashMap<>();
        for (Map.Entry<PokemonFamilyId, Integer> entry : responses.entrySet()) {
            int candyAwarded = entry.getValue();
            PokemonFamilyId family = entry.getKey();
            Integer candy = candies.get(family);
            if (candy == null) {
                // candies map does not yet contain the amount if null, so set it to 0
                candy = 0;
            }
            // Add the awarded candies from this request
            candy += candyAwarded;
            candies.put(family, candy);
        }
        for (Map.Entry<PokemonFamilyId, Integer> entry : candies.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue() + " candies awarded");
        }
    } catch (RequestFailedException e) {
        // failed to login, invalid credentials, auth issue or server issue.
        Log.e("Main", "Failed to login. Invalid credentials, captcha or server issue: ", e);
    }
}
Also used : PtcCredentialProvider(com.pokegoapi.auth.PtcCredentialProvider) PokeBank(com.pokegoapi.api.inventory.PokeBank) OkHttpClient(okhttp3.OkHttpClient) PokemonId(POGOProtos.Enums.PokemonIdOuterClass.PokemonId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PokemonFamilyId(POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) PokemonGo(com.pokegoapi.api.PokemonGo) HashProvider(com.pokegoapi.util.hash.HashProvider) Pokemon(com.pokegoapi.api.pokemon.Pokemon) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.

the class TutorialHandleExample method main.

/**
 * Goes through the tutorial with custom responses.
 *
 * @param args args
 */
public static void main(String[] args) {
    OkHttpClient http = new OkHttpClient();
    final PokemonGo api = new PokemonGo(http);
    try {
        // Add listener to listen for all tutorial related events, must be registered before login is called,
        // otherwise it will not be used
        api.addListener(new TutorialListener() {

            @Override
            public String claimName(PokemonGo api, String lastFailure) {
                // Last attempt to set a codename failed, set a random one by returning null
                if (lastFailure != null) {
                    System.out.println("Codename \"" + lastFailure + "\" is already taken. Using random name.");
                    return null;
                }
                System.out.println("Selecting codename");
                // Set the PTC name as the POGO username
                return ExampleConstants.LOGIN;
            }

            @Override
            public StarterPokemon selectStarter(PokemonGo api) {
                // Catch Charmander as your starter pokemon
                System.out.println("Selecting starter pokemon");
                return StarterPokemon.CHARMANDER;
            }

            @Override
            public PlayerAvatar selectAvatar(PokemonGo api) {
                System.out.println("Selecting player avatar");
                return new PlayerAvatar(PlayerGender.FEMALE, Avatar.Skin.YELLOW.id(), Avatar.Hair.BLACK.id(), Avatar.FemaleShirt.BLUE.id(), Avatar.FemalePants.BLACK_PURPLE_STRIPE.id(), Avatar.FemaleHat.BLACK_YELLOW_POKEBALL.id(), Avatar.FemaleShoes.BLACK_YELLOW_STRIPE.id(), Avatar.Eye.BROWN.id(), Avatar.FemaleBackpack.GRAY_BLACK_YELLOW_POKEBALL.id());
            }
        });
        HashProvider hasher = ExampleConstants.getHashProvider();
        api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
        api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
    } catch (RequestFailedException e) {
        Log.e("Main", "Failed to login!", e);
    }
}
Also used : PtcCredentialProvider(com.pokegoapi.auth.PtcCredentialProvider) OkHttpClient(okhttp3.OkHttpClient) TutorialListener(com.pokegoapi.api.listener.TutorialListener) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) PokemonGo(com.pokegoapi.api.PokemonGo) HashProvider(com.pokegoapi.util.hash.HashProvider) PlayerAvatar(com.pokegoapi.api.player.PlayerAvatar) StarterPokemon(com.pokegoapi.api.pokemon.StarterPokemon)

Example 23 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.

the class PlayerProfile method claimCodeName.

/**
 * Setup an user name for our account
 *
 * @param lastFailure the last name used that was already taken; null for first try.
 * @return the claimed codename
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public String claimCodeName(String lastFailure) throws RequestFailedException {
    if (getPlayerData().getRemainingCodenameClaims() <= 0) {
        throw new RuntimeException("You have no remaining codename claims!");
    }
    String name = randomCodenameGenerator();
    List<TutorialListener> listeners = api.getListeners(TutorialListener.class);
    for (TutorialListener listener : listeners) {
        String listenerName = listener.claimName(api, lastFailure);
        if (listenerName != null) {
            name = listenerName;
            break;
        }
    }
    ClaimCodenameMessage claimCodenameMessage = ClaimCodenameMessage.newBuilder().setCodename(name).build();
    ServerRequest request = new ServerRequest(RequestType.CLAIM_CODENAME, claimCodenameMessage);
    api.requestHandler.sendServerRequests(request, true);
    String updatedCodename;
    try {
        ClaimCodenameResponse claimCodenameResponse = ClaimCodenameResponse.parseFrom(request.getData());
        if (claimCodenameResponse.getStatus() != ClaimCodenameResponse.Status.SUCCESS) {
            return claimCodeName(name);
        }
        updatedCodename = claimCodenameResponse.getCodename();
        if (claimCodenameResponse.hasUpdatedPlayer()) {
            updateProfile(claimCodenameResponse.getUpdatedPlayer());
        }
        if (updatedCodename != null) {
            markTutorial(TutorialStateOuterClass.TutorialState.NAME_SELECTION);
            final GetPlayerMessage getPlayerReqMsg = GetPlayerMessage.newBuilder().setPlayerLocale(playerLocale.getPlayerLocale()).build();
            request = new ServerRequest(RequestType.GET_PLAYER, getPlayerReqMsg);
            api.requestHandler.sendServerRequests(request, true);
            updateProfile(GetPlayerResponse.parseFrom(request.getData()));
        }
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    return updatedCodename;
}
Also used : GetPlayerMessage(POGOProtos.Networking.Requests.Messages.GetPlayerMessageOuterClass.GetPlayerMessage) TutorialListener(com.pokegoapi.api.listener.TutorialListener) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ClaimCodenameResponse(POGOProtos.Networking.Responses.ClaimCodenameResponseOuterClass.ClaimCodenameResponse) ServerRequest(com.pokegoapi.main.ServerRequest) ClaimCodenameMessage(POGOProtos.Networking.Requests.Messages.ClaimCodenameMessageOuterClass.ClaimCodenameMessage)

Example 24 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.

the class PlayerProfile method setBuddy.

/**
 * Sets the current buddy
 *
 * @param pokemon the pokemon to set as your buddy
 * @return if this task was successfull
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public boolean setBuddy(Pokemon pokemon) throws RequestFailedException {
    SetBuddyPokemonMessageOuterClass.SetBuddyPokemonMessage message = SetBuddyPokemonMessageOuterClass.SetBuddyPokemonMessage.newBuilder().setPokemonId(pokemon.getId()).build();
    ServerRequest request = new ServerRequest(RequestType.SET_BUDDY_POKEMON, message);
    api.requestHandler.sendServerRequests(request, true);
    try {
        SetBuddyPokemonResponse response = SetBuddyPokemonResponse.parseFrom(request.getData());
        buddy = new Buddy(api, response.getUpdatedBuddy());
        return response.hasUpdatedBuddy();
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) SetBuddyPokemonMessageOuterClass(POGOProtos.Networking.Requests.Messages.SetBuddyPokemonMessageOuterClass) SetBuddyPokemonResponse(POGOProtos.Networking.Responses.SetBuddyPokemonResponseOuterClass.SetBuddyPokemonResponse) Buddy(com.pokegoapi.api.pokemon.Buddy)

Example 25 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.

the class PlayerProfile method setupAvatar.

/**
 * Setup an avatar for the current account
 *
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public void setupAvatar() throws RequestFailedException {
    SecureRandom random = new SecureRandom();
    PlayerGender gender = random.nextInt(100) % 2 == 0 ? PlayerGender.FEMALE : PlayerGender.MALE;
    PlayerAvatar avatar = new PlayerAvatar(gender, random.nextInt(PlayerAvatar.getAvailableSkins()), random.nextInt(PlayerAvatar.getAvailableHair()), random.nextInt(PlayerAvatar.getAvailableShirts(gender)), random.nextInt(PlayerAvatar.getAvailablePants(gender)), random.nextInt(PlayerAvatar.getAvailableHats()), random.nextInt(PlayerAvatar.getAvailableShoes()), random.nextInt(PlayerAvatar.getAvailableEyes()), random.nextInt(PlayerAvatar.getAvailableBags(gender)));
    List<TutorialListener> listeners = api.getListeners(TutorialListener.class);
    for (TutorialListener listener : listeners) {
        PlayerAvatar listenerAvatar = listener.selectAvatar(api);
        if (listenerAvatar != null) {
            avatar = listenerAvatar;
            break;
        }
    }
    final SetAvatarMessage setAvatarMessage = SetAvatarMessage.newBuilder().setPlayerAvatar(avatar.avatar).build();
    ServerRequest request = new ServerRequest(RequestType.SET_AVATAR, setAvatarMessage);
    api.requestHandler.sendServerRequests(request, true);
    try {
        SetAvatarResponse setAvatarResponse = SetAvatarResponse.parseFrom(request.getData());
        playerData = setAvatarResponse.getPlayerData();
        updateProfile(playerData);
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    markTutorial(TutorialStateOuterClass.TutorialState.AVATAR_SELECTION);
    api.getAssetDigest();
}
Also used : TutorialListener(com.pokegoapi.api.listener.TutorialListener) SetAvatarMessage(POGOProtos.Networking.Requests.Messages.SetAvatarMessageOuterClass.SetAvatarMessage) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) SecureRandom(java.security.SecureRandom) ServerRequest(com.pokegoapi.main.ServerRequest) SetAvatarResponse(POGOProtos.Networking.Responses.SetAvatarResponseOuterClass.SetAvatarResponse)

Aggregations

RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)50 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)39 ServerRequest (com.pokegoapi.main.ServerRequest)37 ByteString (com.google.protobuf.ByteString)10 OkHttpClient (okhttp3.OkHttpClient)9 PokemonGo (com.pokegoapi.api.PokemonGo)7 PtcCredentialProvider (com.pokegoapi.auth.PtcCredentialProvider)7 HashProvider (com.pokegoapi.util.hash.HashProvider)7 ArrayList (java.util.ArrayList)6 TutorialListener (com.pokegoapi.api.listener.TutorialListener)4 Pokemon (com.pokegoapi.api.pokemon.Pokemon)4 GetPlayerMessage (POGOProtos.Networking.Requests.Messages.GetPlayerMessageOuterClass.GetPlayerMessage)3 Item (com.pokegoapi.api.inventory.Item)3 ItemBag (com.pokegoapi.api.inventory.ItemBag)3 Point (com.pokegoapi.api.map.Point)3 IOException (java.io.IOException)3 PokemonFamilyId (POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId)2 PokemonId (POGOProtos.Enums.PokemonIdOuterClass.PokemonId)2 TutorialState (POGOProtos.Enums.TutorialStateOuterClass.TutorialState)2 LevelUpRewardsMessage (POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass.LevelUpRewardsMessage)2