Search in sources :

Example 36 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException 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.latitude).setPlayerLongitude(api.longitude);
    while (queuedActions.size() > 0) {
        ClientAction action = queuedActions.element();
        if (action.endTime < lastSendTime) {
            queuedActions.remove();
            long activePokemon = activeAttacker.pokemon.getId();
            if (action.pokemon != null) {
                activePokemon = action.pokemon.getId();
            }
            long start = action.startTime;
            BattleAction.Builder actionBuilder = BattleAction.newBuilder().setActionStartMs(start).setDurationMs(action.duration).setTargetIndex(-1).setActivePokemonId(activePokemon).setType(action.type);
            if (action.hasDamageWindow) {
                long damageWindowsStart = start + action.damageWindowStart;
                long damageWindowEnd = start + action.damageWindowEnd;
                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.GYM_BATTLE_ATTACK, message);
        api.requestHandler.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 37 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException 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.requestHandler.sendServerRequests(request, false);
    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)

Example 38 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.

the class PlayerProfile method getProfile.

/**
 * Performs a GET_PLAYER_PROFILE request.
 *
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public void getProfile() throws RequestFailedException {
    GetPlayerProfileMessage profileMessage = GetPlayerProfileMessage.newBuilder().setPlayerName("").build();
    ServerRequest profileRequest = new ServerRequest(RequestType.GET_PLAYER_PROFILE, profileMessage);
    api.requestHandler.sendServerRequests(profileRequest, true);
    try {
        GetPlayerProfileResponse response = GetPlayerProfileResponse.parseFrom(profileRequest.getData());
        if (response.getResult() == GetPlayerProfileResponse.Result.SUCCESS) {
            medals.clear();
            List<PlayerBadge> badges = response.getBadgesList();
            for (PlayerBadge badge : badges) {
                medals.put(badge.getBadgeType(), new Medal(api, badge));
            }
            this.startTime = response.getStartTime();
        }
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : GetPlayerProfileMessage(POGOProtos.Networking.Requests.Messages.GetPlayerProfileMessageOuterClass.GetPlayerProfileMessage) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) GetPlayerProfileResponse(POGOProtos.Networking.Responses.GetPlayerProfileResponseOuterClass.GetPlayerProfileResponse) PlayerBadge(POGOProtos.Data.PlayerBadgeOuterClass.PlayerBadge)

Example 39 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.

the class PlayerProfile method checkAndEquipBadges.

/**
 * Check and equip badges.
 *
 * @throws RequestFailedException if an exception occurred while sending requests
 * @deprecated use getMedals, which uses common requests to check for badges
 */
@Deprecated
public void checkAndEquipBadges() throws RequestFailedException {
    CheckAwardedBadgesMessage msg = CheckAwardedBadgesMessage.newBuilder().build();
    ServerRequest serverRequest = new ServerRequest(RequestType.CHECK_AWARDED_BADGES, msg);
    api.requestHandler.sendServerRequests(serverRequest, false);
    CheckAwardedBadgesResponse response;
    try {
        response = CheckAwardedBadgesResponse.parseFrom(serverRequest.getData());
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    this.updateAwardedMedals(response);
}
Also used : CheckAwardedBadgesMessage(POGOProtos.Networking.Requests.Messages.CheckAwardedBadgesMessageOuterClass.CheckAwardedBadgesMessage) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) CheckAwardedBadgesResponse(POGOProtos.Networking.Responses.CheckAwardedBadgesResponseOuterClass.CheckAwardedBadgesResponse)

Example 40 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.

the class PlayerProfile method markTutorial.

private void markTutorial(TutorialStateOuterClass.TutorialState state) throws RequestFailedException {
    final MarkTutorialCompleteMessage tutorialMessage = MarkTutorialCompleteMessage.newBuilder().addTutorialsCompleted(state).setSendMarketingEmails(false).setSendPushNotifications(false).build();
    ServerRequest request = new ServerRequest(RequestType.MARK_TUTORIAL_COMPLETE, tutorialMessage);
    api.requestHandler.sendServerRequests(request, true);
    try {
        playerData = MarkTutorialCompleteResponse.parseFrom(request.getData()).getPlayerData();
        updateProfile(playerData);
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) MarkTutorialCompleteMessage(POGOProtos.Networking.Requests.Messages.MarkTutorialCompleteMessageOuterClass.MarkTutorialCompleteMessage) ServerRequest(com.pokegoapi.main.ServerRequest)

Aggregations

RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)50 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)39 ServerRequest (com.pokegoapi.main.ServerRequest)37 ByteString (com.google.protobuf.ByteString)10 OkHttpClient (okhttp3.OkHttpClient)9 PokemonGo (com.pokegoapi.api.PokemonGo)7 PtcCredentialProvider (com.pokegoapi.auth.PtcCredentialProvider)7 HashProvider (com.pokegoapi.util.hash.HashProvider)7 ArrayList (java.util.ArrayList)6 TutorialListener (com.pokegoapi.api.listener.TutorialListener)4 Pokemon (com.pokegoapi.api.pokemon.Pokemon)4 GetPlayerMessage (POGOProtos.Networking.Requests.Messages.GetPlayerMessageOuterClass.GetPlayerMessage)3 Item (com.pokegoapi.api.inventory.Item)3 ItemBag (com.pokegoapi.api.inventory.ItemBag)3 Point (com.pokegoapi.api.map.Point)3 IOException (java.io.IOException)3 PokemonFamilyId (POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId)2 PokemonId (POGOProtos.Enums.PokemonIdOuterClass.PokemonId)2 TutorialState (POGOProtos.Enums.TutorialStateOuterClass.TutorialState)2 LevelUpRewardsMessage (POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass.LevelUpRewardsMessage)2