Search in sources :

Example 1 with ReleasePokemonResponse

use of POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse in project PokeGOAPI-Java by Grover-c13.

the class Pokemon method transferPokemon.

/**
 * Transfers the pokemon.
 *
 * @return the result
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public Result transferPokemon() throws RequestFailedException {
    if (this.isFavorite() || this.isDeployed()) {
        return Result.FAILED;
    }
    ReleasePokemonMessage reqMsg = ReleasePokemonMessage.newBuilder().setPokemonId(getId()).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.RELEASE_POKEMON, reqMsg);
    api.requestHandler.sendServerRequests(serverRequest, true);
    ReleasePokemonResponse response;
    try {
        response = ReleasePokemonResponse.parseFrom(serverRequest.getData());
    } catch (InvalidProtocolBufferException e) {
        return ReleasePokemonResponse.Result.FAILED;
    }
    if (response.getResult() == Result.SUCCESS) {
        api.inventories.pokebank.removePokemon(this);
    }
    return response.getResult();
}
Also used : ReleasePokemonMessage(POGOProtos.Networking.Requests.Messages.ReleasePokemonMessageOuterClass.ReleasePokemonMessage) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ReleasePokemonResponse(POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse) ServerRequest(com.pokegoapi.main.ServerRequest)

Example 2 with ReleasePokemonResponse

use of POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse 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)

Aggregations

ReleasePokemonMessage (POGOProtos.Networking.Requests.Messages.ReleasePokemonMessageOuterClass.ReleasePokemonMessage)2 ReleasePokemonResponse (POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 ServerRequest (com.pokegoapi.main.ServerRequest)2 PokemonFamilyId (POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId)1 Candy (POGOProtos.Inventory.CandyOuterClass.Candy)1 InventoryItemData (POGOProtos.Inventory.InventoryItemDataOuterClass.InventoryItemData)1 InventoryItem (POGOProtos.Inventory.InventoryItemOuterClass.InventoryItem)1 GetHoloInventoryResponse (POGOProtos.Networking.Responses.GetHoloInventoryResponseOuterClass.GetHoloInventoryResponse)1 ByteString (com.google.protobuf.ByteString)1 Pokemon (com.pokegoapi.api.pokemon.Pokemon)1 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)1 ServerRequestEnvelope (com.pokegoapi.main.ServerRequestEnvelope)1 ServerResponse (com.pokegoapi.main.ServerResponse)1 HashMap (java.util.HashMap)1