Search in sources :

Example 6 with ServerRequest

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

the class DiskEncounter method encounter.

@Override
public EncounterResult encounter() throws RequestFailedException {
    DiskEncounterMessage message = DiskEncounterMessage.newBuilder().setEncounterId(pokemon.getEncounterId()).setFortId(pokemon.getSpawnPointId()).setPlayerLatitude(api.getLatitude()).setPlayerLongitude(api.getLongitude()).build();
    ServerRequest request = new ServerRequest(RequestType.DISK_ENCOUNTER, message);
    ByteString responseData = api.getRequestHandler().sendServerRequests(request, true);
    try {
        DiskEncounterResponse response = DiskEncounterResponse.parseFrom(responseData);
        encounterResult = EncounterResult.from(response.getResult());
        activeItem = response.getActiveItem();
        captureProbabilities = response.getCaptureProbability();
        encounteredPokemon = response.getPokemonData();
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    return encounterResult;
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) ByteString(com.google.protobuf.ByteString) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) DiskEncounterMessage(POGOProtos.Networking.Requests.Messages.DiskEncounterMessageOuterClass.DiskEncounterMessage) DiskEncounterResponse(POGOProtos.Networking.Responses.DiskEncounterResponseOuterClass.DiskEncounterResponse) ServerRequest(com.pokegoapi.main.ServerRequest)

Example 7 with ServerRequest

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

the class Encounter method throwPokeball.

/**
	 * Throws a pokeball in this encounter
	 *
	 * @param pokeball the pokeball to throw
	 * @param throwProperties the throw properties for this throw
	 * @return the result from the pokeball throw
	 * @throws RequestFailedException if the throw request fails
	 * @throws NoSuchItemException if the requested pokeball does not exist
	 */
public CatchPokemonResponse.CatchStatus throwPokeball(ItemId pokeball, ThrowProperties throwProperties) throws RequestFailedException, NoSuchItemException {
    if (isActive()) {
        ItemBag bag = api.getInventories().getItemBag();
        Item item = bag.getItem(pokeball);
        if (item.getCount() > 0) {
            CatchPokemonMessage message = CatchPokemonMessage.newBuilder().setEncounterId(pokemon.getEncounterId()).setSpawnPointId(pokemon.getSpawnPointId()).setPokeball(pokeball).setNormalizedHitPosition(throwProperties.getNormalizedHitPosition()).setNormalizedReticleSize(throwProperties.getNormalizedReticleSize()).setSpinModifier(throwProperties.getSpinModifier()).setHitPokemon(throwProperties.shouldHitPokemon()).build();
            ServerRequest request = new ServerRequest(RequestType.CATCH_POKEMON, message);
            ByteString responseData = api.getRequestHandler().sendServerRequests(request, true);
            try {
                CatchPokemonResponse response = CatchPokemonResponse.parseFrom(responseData);
                status = response.getStatus();
                if (hasCaptured()) {
                    captureAward = response.getCaptureAward();
                    capturedPokemon = response.getCapturedPokemonId();
                    captureReason = response.getCaptureReason();
                }
                if (status == CatchStatus.CATCH_SUCCESS || status == CatchStatus.CATCH_FLEE) {
                    pokemon.setDespawned(true);
                }
                if (status == CatchStatus.CATCH_SUCCESS) {
                    api.getPlayerProfile().updateProfile();
                }
                if (status != CatchStatus.CATCH_ERROR) {
                    item.setCount(item.getCount() - 1);
                }
            } catch (InvalidProtocolBufferException e) {
                throw new RequestFailedException(e);
            }
        } else {
            throw new NoSuchItemException();
        }
    }
    return status;
}
Also used : Item(com.pokegoapi.api.inventory.Item) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) ByteString(com.google.protobuf.ByteString) CatchPokemonResponse(POGOProtos.Networking.Responses.CatchPokemonResponseOuterClass.CatchPokemonResponse) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) CatchPokemonMessage(POGOProtos.Networking.Requests.Messages.CatchPokemonMessageOuterClass.CatchPokemonMessage) NoSuchItemException(com.pokegoapi.exceptions.NoSuchItemException) ItemBag(com.pokegoapi.api.inventory.ItemBag)

Example 8 with ServerRequest

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

the class Encounter method encounter.

/**
	 * Encounters this pokemon
	 *
	 * @return the result from the attempted encounter
	 * @throws RequestFailedException if the encounter request fails
	 */
protected EncounterResult encounter() throws RequestFailedException {
    EncounterMessage message = EncounterMessage.newBuilder().setEncounterId(pokemon.getEncounterId()).setSpawnPointId(pokemon.getSpawnPointId()).setPlayerLatitude(api.getLatitude()).setPlayerLongitude(api.getLongitude()).build();
    ServerRequest request = new ServerRequest(RequestType.ENCOUNTER, message);
    ByteString responseData = api.getRequestHandler().sendServerRequests(request, true);
    try {
        EncounterResponse response = EncounterResponse.parseFrom(responseData);
        encounterResult = EncounterResult.from(response.getStatus());
        activeItem = response.getActiveItem();
        captureProbabilities = response.getCaptureProbability();
        encounteredPokemon = response.getWildPokemon().getPokemonData();
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    return encounterResult;
}
Also used : EncounterResponse(POGOProtos.Networking.Responses.EncounterResponseOuterClass.EncounterResponse) UseItemEncounterResponse(POGOProtos.Networking.Responses.UseItemEncounterResponseOuterClass.UseItemEncounterResponse) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) ByteString(com.google.protobuf.ByteString) EncounterMessage(POGOProtos.Networking.Requests.Messages.EncounterMessageOuterClass.EncounterMessage) UseItemEncounterMessage(POGOProtos.Networking.Requests.Messages.UseItemEncounterMessageOuterClass.UseItemEncounterMessage) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest)

Example 9 with ServerRequest

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

the class Encounter method useItem.

/**
	 * Uses an item in this encounter
	 *
	 * @param itemId the item to use
	 * @return the result from this action
	 * @throws RequestFailedException if the use request fails
	 */
public UseItemEncounterResponse.Status useItem(ItemId itemId) throws RequestFailedException {
    if (isActive()) {
        ItemBag bag = api.getInventories().getItemBag();
        Item item = bag.getItem(itemId);
        if (item.getCount() > 0) {
            if (activeItem == null) {
                UseItemEncounterMessage message = UseItemEncounterMessage.newBuilder().setEncounterId(pokemon.getEncounterId()).setSpawnPointGuid(pokemon.getSpawnPointId()).setItem(itemId).build();
                ServerRequest request = new ServerRequest(RequestType.USE_ITEM_ENCOUNTER, message);
                ByteString responseData = api.getRequestHandler().sendServerRequests(request, true);
                try {
                    UseItemEncounterResponse response = UseItemEncounterResponse.parseFrom(responseData);
                    activeItem = response.getActiveItem();
                    captureProbabilities = response.getCaptureProbability();
                    if (response.getStatus() == Status.SUCCESS) {
                        item.setCount(item.getCount() - 1);
                    }
                    return response.getStatus();
                } catch (InvalidProtocolBufferException e) {
                    throw new RequestFailedException(e);
                }
            } else {
                return UseItemEncounterResponse.Status.ACTIVE_ITEM_EXISTS;
            }
        } else {
            return UseItemEncounterResponse.Status.NO_ITEM_IN_INVENTORY;
        }
    }
    return UseItemEncounterResponse.Status.ALREADY_COMPLETED;
}
Also used : Item(com.pokegoapi.api.inventory.Item) UseItemEncounterResponse(POGOProtos.Networking.Responses.UseItemEncounterResponseOuterClass.UseItemEncounterResponse) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) ByteString(com.google.protobuf.ByteString) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) UseItemEncounterMessage(POGOProtos.Networking.Requests.Messages.UseItemEncounterMessageOuterClass.UseItemEncounterMessage) ItemBag(com.pokegoapi.api.inventory.ItemBag)

Example 10 with ServerRequest

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

the class PlayerProfile method encounterTutorialComplete.

/**
	 * Encounter tutorial complete. In other words, catch the first Pokémon
	 *
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public void encounterTutorialComplete() throws RequestFailedException {
    StarterPokemon starter = StarterPokemon.random();
    List<TutorialListener> listeners = api.getListeners(TutorialListener.class);
    for (TutorialListener listener : listeners) {
        StarterPokemon pokemon = listener.selectStarter(api);
        if (pokemon != null) {
            starter = pokemon;
            break;
        }
    }
    final EncounterTutorialCompleteMessage.Builder builder = EncounterTutorialCompleteMessage.newBuilder().setPokemonId(starter.getPokemon());
    ServerRequest request = new ServerRequest(RequestType.ENCOUNTER_TUTORIAL_COMPLETE, builder.build());
    api.getRequestHandler().sendServerRequests(request, true);
    final GetPlayerMessage getPlayerReqMsg = GetPlayerMessage.newBuilder().setPlayerLocale(playerLocale.getPlayerLocale()).build();
    request = new ServerRequest(RequestType.GET_PLAYER, getPlayerReqMsg);
    api.getRequestHandler().sendServerRequests(request, true);
    try {
        updateProfile(GetPlayerResponse.parseFrom(request.getData()));
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : GetPlayerMessage(POGOProtos.Networking.Requests.Messages.GetPlayerMessageOuterClass.GetPlayerMessage) EncounterTutorialCompleteMessage(POGOProtos.Networking.Requests.Messages.EncounterTutorialCompleteMessageOuterClass.EncounterTutorialCompleteMessage) TutorialListener(com.pokegoapi.api.listener.TutorialListener) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) StarterPokemon(com.pokegoapi.api.pokemon.StarterPokemon)

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