Search in sources :

Example 1 with GetInventoryResponse

use of POGOProtos.Networking.Responses.GetInventoryResponseOuterClass.GetInventoryResponse in project PokeGOAPI-Java by Grover-c13.

the class Inventories method updateInventories.

/**
	 * Updates the inventories with the latest data.
	 *
	 * @param forceUpdate For a full update if true
	 * @return the response to the update message
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public GetInventoryResponse updateInventories(boolean forceUpdate) throws RequestFailedException {
    if (forceUpdate) {
        lastInventoryUpdate = 0;
        itemBag.reset();
        pokebank.reset();
        candyjar.reset();
        pokedex.reset();
        synchronized (this.lock) {
            incubators.clear();
        }
        hatchery.reset();
    }
    GetInventoryMessage invReqMsg = GetInventoryMessage.newBuilder().setLastTimestampMs(lastInventoryUpdate).build();
    ServerRequest inventoryRequest = new ServerRequest(RequestTypeOuterClass.RequestType.GET_INVENTORY, invReqMsg);
    api.getRequestHandler().sendServerRequests(inventoryRequest);
    GetInventoryResponse response;
    try {
        response = GetInventoryResponse.parseFrom(inventoryRequest.getData());
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    return response;
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) GetInventoryMessage(POGOProtos.Networking.Requests.Messages.GetInventoryMessageOuterClass.GetInventoryMessage) ServerRequest(com.pokegoapi.main.ServerRequest) GetInventoryResponse(POGOProtos.Networking.Responses.GetInventoryResponseOuterClass.GetInventoryResponse)

Example 2 with GetInventoryResponse

use of POGOProtos.Networking.Responses.GetInventoryResponseOuterClass.GetInventoryResponse 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());
        }
    }
    ServerRequestEnvelope envelope = ServerRequestEnvelope.createCommons();
    ServerRequest releaseRequest = envelope.add(RequestType.RELEASE_POKEMON, releaseBuilder.build());
    Map<PokemonFamilyId, Integer> lastCandies = new HashMap<>(api.getInventories().getCandyjar().getCandies());
    ServerResponse response = api.getRequestHandler().sendServerRequests(envelope);
    try {
        GetInventoryResponse inventoryResponse = GetInventoryResponse.parseFrom(response.get(RequestType.GET_INVENTORY));
        ReleasePokemonResponse releaseResponse = ReleasePokemonResponse.parseFrom(releaseRequest.getData());
        Map<PokemonFamilyId, Integer> candyCount = new HashMap<>();
        if (releaseResponse.getResult() == Result.SUCCESS && inventoryResponse.getSuccess()) {
            synchronized (this.lock) {
                for (Pokemon pokemon : releasePokemon) {
                    this.pokemons.remove(pokemon);
                }
            }
            for (Pokemon pokemon : releasePokemon) {
                api.getInventories().getPokebank().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.getInventories().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) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) PokemonFamilyId(POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId) GetInventoryResponse(POGOProtos.Networking.Responses.GetInventoryResponseOuterClass.GetInventoryResponse) 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)

Aggregations

GetInventoryResponse (POGOProtos.Networking.Responses.GetInventoryResponseOuterClass.GetInventoryResponse)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)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 GetInventoryMessage (POGOProtos.Networking.Requests.Messages.GetInventoryMessageOuterClass.GetInventoryMessage)1 ReleasePokemonMessage (POGOProtos.Networking.Requests.Messages.ReleasePokemonMessageOuterClass.ReleasePokemonMessage)1 ReleasePokemonResponse (POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse)1 Pokemon (com.pokegoapi.api.pokemon.Pokemon)1 ServerRequestEnvelope (com.pokegoapi.main.ServerRequestEnvelope)1 ServerResponse (com.pokegoapi.main.ServerResponse)1 HashMap (java.util.HashMap)1