Search in sources :

Example 26 with ServerRequest

use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.

the class ItemBag method useIncense.

/**
	 * use an incense
	 *
	 * @param type type of item
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public void useIncense(ItemId type) throws RequestFailedException {
    UseIncenseMessage useIncenseMessage = UseIncenseMessage.newBuilder().setIncenseType(type).setIncenseTypeValue(type.getNumber()).build();
    ServerRequest useIncenseRequest = new ServerRequest(RequestType.USE_INCENSE, useIncenseMessage);
    api.getRequestHandler().sendServerRequests(useIncenseRequest, true);
    try {
        UseIncenseResponse response = UseIncenseResponse.parseFrom(useIncenseRequest.getData());
        Log.i("Main", "Use incense result: " + response.getResult());
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) UseIncenseMessage(POGOProtos.Networking.Requests.Messages.UseIncenseMessageOuterClass.UseIncenseMessage) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) UseIncenseResponse(POGOProtos.Networking.Responses.UseIncenseResponseOuterClass.UseIncenseResponse)

Example 27 with ServerRequest

use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.

the class ItemBag method removeItem.

/**
	 * Discards the given item.
	 *
	 * @param id the id
	 * @param quantity the quantity
	 * @return the result
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public Result removeItem(ItemId id, int quantity) throws RequestFailedException {
    Item item = getItem(id);
    if (item.getCount() < quantity) {
        throw new IllegalArgumentException("You cannot remove more quantity than you have");
    }
    RecycleInventoryItemMessage msg = RecycleInventoryItemMessage.newBuilder().setItemId(id).setCount(quantity).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.RECYCLE_INVENTORY_ITEM, msg);
    api.getRequestHandler().sendServerRequests(serverRequest);
    RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse response;
    try {
        response = RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse.parseFrom(serverRequest.getData());
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    if (response.getResult() == RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse.Result.SUCCESS) {
        item.setCount(response.getNewCount());
        if (item.getCount() <= 0) {
            removeItem(item.getItemId());
        }
    }
    return response.getResult();
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) RecycleInventoryItemResponseOuterClass(POGOProtos.Networking.Responses.RecycleInventoryItemResponseOuterClass) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) RecycleInventoryItemMessage(POGOProtos.Networking.Requests.Messages.RecycleInventoryItemMessageOuterClass.RecycleInventoryItemMessage)

Example 28 with ServerRequest

use of com.pokegoapi.main.ServerRequest 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)

Example 29 with ServerRequest

use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.

the class Battle method sendActions.

/**
	 * Sends all currently queued actions to the server
	 *
	 * @param handler to handle this battle
	 * @return if this battle should switch to the next defender
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
private boolean sendActions(BattleHandler handler) throws RequestFailedException {
    AttackGymMessage.Builder builder = AttackGymMessage.newBuilder().setGymId(gym.getId()).setBattleId(battleId).setPlayerLatitude(api.getLatitude()).setPlayerLongitude(api.getLongitude());
    while (queuedActions.size() > 0) {
        ClientAction action = queuedActions.element();
        if (action.getEndTime() < lastSendTime) {
            queuedActions.remove();
            long activePokemon = activeAttacker.getPokemon().getId();
            if (action.getPokemon() != null) {
                activePokemon = action.getPokemon().getId();
            }
            long start = action.getStartTime();
            BattleAction.Builder actionBuilder = BattleAction.newBuilder().setActionStartMs(start).setDurationMs(action.getDuration()).setTargetIndex(-1).setActivePokemonId(activePokemon).setType(action.getType());
            if (action.isHasDamageWindow()) {
                long damageWindowsStart = start + action.getDamageWindowStart();
                long damageWindowEnd = start + action.getDamageWindowEnd();
                actionBuilder.setDamageWindowsStartTimestampMs(damageWindowsStart);
                actionBuilder.setDamageWindowsEndTimestampMs(damageWindowEnd);
            }
            builder.addAttackActions(actionBuilder.build());
        } else {
            break;
        }
    }
    if (lastRetrievedAction != null && sentActions) {
        builder.setLastRetrievedAction(lastRetrievedAction);
    }
    if (builder.getAttackActionsCount() > 0) {
        AttackGymMessage message = builder.build();
        ServerRequest request = new ServerRequest(RequestType.ATTACK_GYM, message);
        api.getRequestHandler().sendServerRequests(request, true);
        boolean nextDefender;
        try {
            AttackGymResponse response = AttackGymResponse.parseFrom(request.getData());
            nextDefender = handleAttackResponse(handler, response);
        } catch (InvalidProtocolBufferException e) {
            throw new RequestFailedException(e);
        }
        sentActions = true;
        return nextDefender;
    }
    return false;
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) AttackGymResponse(POGOProtos.Networking.Responses.AttackGymResponseOuterClass.AttackGymResponse) BattleAction(POGOProtos.Data.Battle.BattleActionOuterClass.BattleAction) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) AttackGymMessage(POGOProtos.Networking.Requests.Messages.AttackGymMessageOuterClass.AttackGymMessage) ServerRequest(com.pokegoapi.main.ServerRequest)

Example 30 with ServerRequest

use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.

the class PlayerProfile method updateProfile.

/**
	 * Updates the player profile with the latest data.
	 *
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public void updateProfile() throws RequestFailedException {
    GetPlayerMessage message = GetPlayerMessage.newBuilder().setPlayerLocale(playerLocale.getPlayerLocale()).build();
    ServerRequest request = new ServerRequest(RequestType.GET_PLAYER, message);
    api.getRequestHandler().sendServerRequests(request);
    try {
        updateProfile(GetPlayerResponse.parseFrom(request.getData()));
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : GetPlayerMessage(POGOProtos.Networking.Requests.Messages.GetPlayerMessageOuterClass.GetPlayerMessage) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest)

Aggregations

InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)42 ServerRequest (com.pokegoapi.main.ServerRequest)42 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)35 ByteString (com.google.protobuf.ByteString)13 GetPlayerMessage (POGOProtos.Networking.Requests.Messages.GetPlayerMessageOuterClass.GetPlayerMessage)3 Item (com.pokegoapi.api.inventory.Item)3 ItemBag (com.pokegoapi.api.inventory.ItemBag)3 TutorialListener (com.pokegoapi.api.listener.TutorialListener)3 FortDeployPokemonMessage (POGOProtos.Networking.Requests.Messages.FortDeployPokemonMessageOuterClass.FortDeployPokemonMessage)2 LevelUpRewardsMessage (POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass.LevelUpRewardsMessage)2 ReleasePokemonMessage (POGOProtos.Networking.Requests.Messages.ReleasePokemonMessageOuterClass.ReleasePokemonMessage)2 UseItemEncounterMessage (POGOProtos.Networking.Requests.Messages.UseItemEncounterMessageOuterClass.UseItemEncounterMessage)2 LevelUpRewardsResponse (POGOProtos.Networking.Responses.LevelUpRewardsResponseOuterClass.LevelUpRewardsResponse)2 UseItemEncounterResponse (POGOProtos.Networking.Responses.UseItemEncounterResponseOuterClass.UseItemEncounterResponse)2 EvolutionResult (com.pokegoapi.api.map.pokemon.EvolutionResult)2 BattleAction (POGOProtos.Data.Battle.BattleActionOuterClass.BattleAction)1 PlayerBadge (POGOProtos.Data.PlayerBadgeOuterClass.PlayerBadge)1 PokemonFamilyId (POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId)1 TutorialState (POGOProtos.Enums.TutorialStateOuterClass.TutorialState)1 Candy (POGOProtos.Inventory.CandyOuterClass.Candy)1