Search in sources :

Example 41 with RequestFailedException

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

the class Pokemon method setFavoritePokemon.

/**
 * Function to mark the pokemon as favorite or not.
 *
 * @param markFavorite Mark Pokemon as Favorite?
 * @return the SetFavoritePokemonResponse.Result
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public SetFavoritePokemonResponse.Result setFavoritePokemon(boolean markFavorite) throws RequestFailedException {
    SetFavoritePokemonMessage reqMsg = SetFavoritePokemonMessage.newBuilder().setPokemonId(getId()).setIsFavorite(markFavorite).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.SET_FAVORITE_POKEMON, reqMsg);
    api.requestHandler.sendServerRequests(serverRequest, true);
    SetFavoritePokemonResponse response;
    try {
        response = SetFavoritePokemonResponse.parseFrom(serverRequest.getData());
        if (response.getResult() == SetFavoritePokemonResponse.Result.SUCCESS) {
            favorite = markFavorite ? 1 : 0;
        }
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    api.inventories.pokebank.removePokemon(this);
    return response.getResult();
}
Also used : SetFavoritePokemonResponse(POGOProtos.Networking.Responses.SetFavoritePokemonResponseOuterClass.SetFavoritePokemonResponse) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) SetFavoritePokemonMessage(POGOProtos.Networking.Requests.Messages.SetFavoritePokemonMessageOuterClass.SetFavoritePokemonMessage) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest)

Example 42 with RequestFailedException

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

the class Pokemon method renamePokemon.

/**
 * Rename pokemon nickname pokemon response . result.
 *
 * @param nickname the nickname
 * @return the nickname pokemon response . result
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public NicknamePokemonResponse.Result renamePokemon(String nickname) throws RequestFailedException {
    NicknamePokemonMessage reqMsg = NicknamePokemonMessage.newBuilder().setPokemonId(getId()).setNickname(nickname).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.NICKNAME_POKEMON, reqMsg);
    api.requestHandler.sendServerRequests(serverRequest, true);
    NicknamePokemonResponse response;
    try {
        response = NicknamePokemonResponse.parseFrom(serverRequest.getData());
        if (response.getResult() == NicknamePokemonResponse.Result.SUCCESS) {
            this.nickname = nickname;
        }
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    api.inventories.pokebank.removePokemon(this);
    return response.getResult();
}
Also used : NicknamePokemonMessage(POGOProtos.Networking.Requests.Messages.NicknamePokemonMessageOuterClass.NicknamePokemonMessage) NicknamePokemonResponse(POGOProtos.Networking.Responses.NicknamePokemonResponseOuterClass.NicknamePokemonResponse) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest)

Example 43 with RequestFailedException

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

the class Pokemon method useRevive.

/**
 * Use a revive item on the pokemon. Will check if there is enough revive & if the pokemon need
 * to be revived.
 *
 * @param itemId {@link ItemId} of the Revive to use.
 * @return Result, ERROR_CANNOT_USE if the requirements aren't met
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public UseItemReviveResponse.Result useRevive(ItemId itemId) throws RequestFailedException {
    Item item = api.inventories.itemBag.getItem(itemId);
    if (!item.isRevive() || item.count < 1 || !isFainted())
        return UseItemReviveResponse.Result.ERROR_CANNOT_USE;
    UseItemReviveMessage reqMsg = UseItemReviveMessage.newBuilder().setItemId(itemId).setPokemonId(getId()).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.USE_ITEM_REVIVE, reqMsg);
    api.requestHandler.sendServerRequests(serverRequest, true);
    UseItemReviveResponse response;
    try {
        response = UseItemReviveResponse.parseFrom(serverRequest.getData());
        if (response.getResult() == UseItemReviveResponse.Result.SUCCESS) {
            item.setCount(item.count - 1);
            this.stamina = response.getStamina();
        }
        return response.getResult();
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : Item(com.pokegoapi.api.inventory.Item) UseItemReviveMessage(POGOProtos.Networking.Requests.Messages.UseItemReviveMessageOuterClass.UseItemReviveMessage) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) UseItemReviveResponse(POGOProtos.Networking.Responses.UseItemReviveResponseOuterClass.UseItemReviveResponse) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest)

Example 44 with RequestFailedException

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

the class PokeBank method releasePokemon.

/**
 * Releases multiple pokemon in a single request
 *
 * @param releasePokemon the pokemon to release
 * @return the amount of candies for each pokemon family
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public Map<PokemonFamilyId, Integer> releasePokemon(Pokemon... releasePokemon) throws RequestFailedException {
    ReleasePokemonMessage.Builder releaseBuilder = ReleasePokemonMessage.newBuilder();
    for (Pokemon pokemon : releasePokemon) {
        if (!pokemon.isDeployed() && !pokemon.isFavorite()) {
            releaseBuilder.addPokemonIds(pokemon.getId());
        }
    }
    ServerRequest releaseRequest = new ServerRequest(RequestType.RELEASE_POKEMON, releaseBuilder.build());
    ServerRequestEnvelope envelope = ServerRequestEnvelope.createCommons(releaseRequest, api);
    Map<PokemonFamilyId, Integer> lastCandies = new HashMap<>(api.inventories.candyjar.getCandies());
    ServerResponse response = api.requestHandler.sendServerRequests(envelope);
    try {
        ByteString inventoryData = response.get(RequestType.GET_HOLOHOLO_INVENTORY);
        GetHoloInventoryResponse inventoryResponse = GetHoloInventoryResponse.parseFrom(inventoryData);
        ReleasePokemonResponse releaseResponse = ReleasePokemonResponse.parseFrom(releaseRequest.getData());
        Map<PokemonFamilyId, Integer> candyCount = new HashMap<>();
        if (releaseResponse.getResult() == Result.SUCCESS && inventoryResponse.getSuccess()) {
            synchronized (this.lock) {
                this.pokemons.removeAll(Arrays.asList(releasePokemon));
            }
            for (Pokemon pokemon : releasePokemon) {
                api.inventories.pokebank.removePokemon(pokemon);
            }
            List<InventoryItem> items = inventoryResponse.getInventoryDelta().getInventoryItemsList();
            for (InventoryItem item : items) {
                InventoryItemData data = item.getInventoryItemData();
                if (data != null && data.hasCandy()) {
                    Candy candy = data.getCandy();
                    PokemonFamilyId family = candy.getFamilyId();
                    Integer lastCandy = lastCandies.get(family);
                    if (lastCandy == null) {
                        lastCandy = 0;
                    }
                    candyCount.put(family, candy.getCandy() - lastCandy);
                }
            }
            api.inventories.updateInventories(inventoryResponse);
        }
        return candyCount;
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : ServerResponse(com.pokegoapi.main.ServerResponse) InventoryItem(POGOProtos.Inventory.InventoryItemOuterClass.InventoryItem) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) PokemonFamilyId(POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId) ServerRequestEnvelope(com.pokegoapi.main.ServerRequestEnvelope) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) ReleasePokemonMessage(POGOProtos.Networking.Requests.Messages.ReleasePokemonMessageOuterClass.ReleasePokemonMessage) Candy(POGOProtos.Inventory.CandyOuterClass.Candy) ReleasePokemonResponse(POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse) Pokemon(com.pokegoapi.api.pokemon.Pokemon) ServerRequest(com.pokegoapi.main.ServerRequest) InventoryItemData(POGOProtos.Inventory.InventoryItemDataOuterClass.InventoryItemData) GetHoloInventoryResponse(POGOProtos.Networking.Responses.GetHoloInventoryResponseOuterClass.GetHoloInventoryResponse)

Example 45 with RequestFailedException

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

the class News method markUnreadNews.

/**
 * Mark Unread News to read
 */
public void markUnreadNews() {
    if (currentNews == null || currentNews.getNewsArticlesCount() <= 0) {
        // do nothing
        return;
    }
    // Stored enabled and un-read article
    List<String> unReadNewsList = new ArrayList<>();
    for (NewsArticle newsArticle : currentNews.getNewsArticlesList()) {
        if (newsArticle.getEnabled() && !newsArticle.getArticleRead())
            unReadNewsList.add(newsArticle.getId());
    }
    Log.i(TAG, "markUnreadNews total Article count:" + unReadNewsList.size());
    if (unReadNewsList.size() > 0) {
        MarkReadNewsArticleMessage msg = MarkReadNewsArticleMessage.newBuilder().addAllNewsIds(unReadNewsList).build();
        ServerRequest request = new ServerRequest(RequestTypeOuterClass.RequestType.MARK_READ_NEWS_ARTICLE, msg);
        ServerRequestEnvelope envelope = ServerRequestEnvelope.create(request);
        try {
            api.requestHandler.sendServerRequests(envelope);
            MarkReadNewsArticleResponse response = MarkReadNewsArticleResponse.parseFrom(request.getData());
            if (response.getResult() == MarkReadNewsArticleResponse.Result.SUCCESS) {
                Log.i(TAG, "Mark News Article -> success");
            } else {
                Log.w(TAG, "Mark News Article -> !success");
            }
        } catch (RequestFailedException e) {
            e.printStackTrace();
            Log.e(TAG, "RequestFailedException: cause:" + e.getCause() + " message:" + e.getMessage());
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
            Log.e(TAG, "InvalidProtocolBufferException: cause:" + e.getCause() + " message:" + e.getMessage());
        }
    } else {
        Log.i(TAG, "no unmarked news found -> skipped");
    }
}
Also used : ServerRequestEnvelope(com.pokegoapi.main.ServerRequestEnvelope) NewsArticle(POGOProtos.Data.News.NewsArticleOuterClass.NewsArticle) MarkReadNewsArticleMessage(POGOProtos.Networking.Requests.Messages.MarkReadNewsArticleMessageOuterClass.MarkReadNewsArticleMessage) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) ArrayList(java.util.ArrayList) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) MarkReadNewsArticleResponse(POGOProtos.Networking.Responses.MarkReadNewsArticleResponseOuterClass.MarkReadNewsArticleResponse)

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