Search in sources :

Example 1 with AttackGymResponse

use of POGOProtos.Networking.Responses.AttackGymResponseOuterClass.AttackGymResponse 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)

Aggregations

BattleAction (POGOProtos.Data.Battle.BattleActionOuterClass.BattleAction)1 AttackGymMessage (POGOProtos.Networking.Requests.Messages.AttackGymMessageOuterClass.AttackGymMessage)1 AttackGymResponse (POGOProtos.Networking.Responses.AttackGymResponseOuterClass.AttackGymResponse)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)1 ServerRequest (com.pokegoapi.main.ServerRequest)1