Search in sources :

Example 1 with ServerRequestEnvelope

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

the class PokemonGo method initialize.

private void initialize() throws RequestFailedException {
    if (getRequestHandler() != null) {
        getRequestHandler().exit();
    }
    requestHandler = new RequestHandler(this, client);
    getRequestHandler().sendServerRequests(ServerRequestEnvelope.create());
    playerProfile.updateProfile();
    ServerRequest downloadConfigRequest = new ServerRequest(RequestType.DOWNLOAD_REMOTE_CONFIG_VERSION, CommonRequests.getDownloadRemoteConfigVersionMessageRequest(this));
    getRequestHandler().sendServerRequests(downloadConfigRequest, true, RequestType.GET_BUDDY_WALKED, RequestType.GET_INCENSE_POKEMON);
    getAssetDigest();
    try {
        ByteString configVersionData = downloadConfigRequest.getData();
        if (PokemonMeta.checkVersion(DownloadRemoteConfigVersionResponse.parseFrom(configVersionData))) {
            DownloadItemTemplatesMessage message = CommonRequests.getDownloadItemTemplatesRequest();
            ServerRequest request = new ServerRequest(RequestType.DOWNLOAD_ITEM_TEMPLATES, message);
            PokemonMeta.update(getRequestHandler().sendServerRequests(request, true), true);
        }
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    playerProfile.getProfile();
    try {
        LevelUpRewardsMessage rewardsMessage = LevelUpRewardsMessage.newBuilder().setLevel(playerProfile.getLevel()).build();
        ServerRequestEnvelope envelope = ServerRequestEnvelope.createCommons();
        ServerRequest request = envelope.add(RequestType.LEVEL_UP_REWARDS, rewardsMessage);
        getRequestHandler().sendServerRequests(envelope);
        LevelUpRewardsResponse levelUpRewardsResponse = LevelUpRewardsResponse.parseFrom(request.getData());
        if (levelUpRewardsResponse.getResult() == Result.SUCCESS) {
            inventories.getItemBag().addAwardedItems(levelUpRewardsResponse);
        }
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    List<LoginListener> loginListeners = getListeners(LoginListener.class);
    for (LoginListener listener : loginListeners) {
        listener.onLogin(this);
    }
    loggingIn = false;
    active = true;
    // From now one we will start to check our accounts is ready to fire requests.
    // Actually, we can receive valid responses even with this first check,
    // that mark the tutorial state into LEGAL_SCREEN.
    // Following, we are going to check if the account binded to this session
    // have an avatar, a nickname, and all the other things that are usually filled
    // on the official client BEFORE sending any requests such as the getMapObject etc.
    ArrayList<TutorialState> tutorialStates = playerProfile.getTutorialState().getTutorialStates();
    if (tutorialStates.isEmpty()) {
        playerProfile.activateAccount();
    }
    if (!tutorialStates.contains(TutorialState.AVATAR_SELECTION)) {
        playerProfile.setupAvatar();
    }
    heartbeat.start();
    if (!tutorialStates.contains(TutorialState.POKEMON_CAPTURE)) {
        playerProfile.encounterTutorialComplete();
    }
    int remainingCodenameClaims = getPlayerProfile().getPlayerData().getRemainingCodenameClaims();
    if (!tutorialStates.contains(TutorialState.NAME_SELECTION) && remainingCodenameClaims > 0) {
        playerProfile.claimCodeName();
    }
    if (!tutorialStates.contains(TutorialState.FIRST_TIME_EXPERIENCE_COMPLETE)) {
        playerProfile.firstTimeExperienceComplete();
    }
}
Also used : LevelUpRewardsMessage(POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass.LevelUpRewardsMessage) ByteString(com.google.protobuf.ByteString) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) Point(com.pokegoapi.api.map.Point) ServerRequestEnvelope(com.pokegoapi.main.ServerRequestEnvelope) LoginListener(com.pokegoapi.api.listener.LoginListener) RequestHandler(com.pokegoapi.main.RequestHandler) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) DownloadItemTemplatesMessage(POGOProtos.Networking.Requests.Messages.DownloadItemTemplatesMessageOuterClass.DownloadItemTemplatesMessage) ServerRequest(com.pokegoapi.main.ServerRequest) LevelUpRewardsResponse(POGOProtos.Networking.Responses.LevelUpRewardsResponseOuterClass.LevelUpRewardsResponse) TutorialState(POGOProtos.Enums.TutorialStateOuterClass.TutorialState)

Example 2 with ServerRequestEnvelope

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

the class PokemonGo method getAssetDigest.

/**
	 * Second requests block. Public since it could be re-fired at any time
	 *
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public void getAssetDigest() throws RequestFailedException {
    ServerRequestEnvelope envelope = ServerRequestEnvelope.createCommons(RequestType.GET_BUDDY_WALKED, RequestType.GET_INCENSE_POKEMON);
    envelope.add(RequestType.GET_ASSET_DIGEST, CommonRequests.getGetAssetDigestMessageRequest(this));
    getRequestHandler().sendServerRequests(envelope);
}
Also used : ServerRequestEnvelope(com.pokegoapi.main.ServerRequestEnvelope)

Example 3 with ServerRequestEnvelope

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

ServerRequestEnvelope (com.pokegoapi.main.ServerRequestEnvelope)3 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 TutorialState (POGOProtos.Enums.TutorialStateOuterClass.TutorialState)1 Candy (POGOProtos.Inventory.CandyOuterClass.Candy)1 InventoryItemData (POGOProtos.Inventory.InventoryItemDataOuterClass.InventoryItemData)1 InventoryItem (POGOProtos.Inventory.InventoryItemOuterClass.InventoryItem)1 DownloadItemTemplatesMessage (POGOProtos.Networking.Requests.Messages.DownloadItemTemplatesMessageOuterClass.DownloadItemTemplatesMessage)1 LevelUpRewardsMessage (POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass.LevelUpRewardsMessage)1 ReleasePokemonMessage (POGOProtos.Networking.Requests.Messages.ReleasePokemonMessageOuterClass.ReleasePokemonMessage)1 GetInventoryResponse (POGOProtos.Networking.Responses.GetInventoryResponseOuterClass.GetInventoryResponse)1 LevelUpRewardsResponse (POGOProtos.Networking.Responses.LevelUpRewardsResponseOuterClass.LevelUpRewardsResponse)1 ReleasePokemonResponse (POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse)1 ByteString (com.google.protobuf.ByteString)1 LoginListener (com.pokegoapi.api.listener.LoginListener)1 Point (com.pokegoapi.api.map.Point)1 Pokemon (com.pokegoapi.api.pokemon.Pokemon)1 RequestHandler (com.pokegoapi.main.RequestHandler)1