Search in sources :

Example 41 with ByteString

use of com.google.protobuf.ByteString 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 42 with ByteString

use of com.google.protobuf.ByteString 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 43 with ByteString

use of com.google.protobuf.ByteString 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 44 with ByteString

use of com.google.protobuf.ByteString 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 45 with ByteString

use of com.google.protobuf.ByteString in project PokeGOAPI-Java by Grover-c13.

the class RequestHandler method sendInternal.

/**
	 * Sends an already built request envelope
	 *
	 * @param serverResponse the response to append to
	 * @param requests list of ServerRequests to be sent
	 * @param platformRequests list of ServerPlatformRequests to be sent
	 * @param builder the request envelope builder
	 * @throws RequestFailedException if this message fails to send
	 */
private ServerResponse sendInternal(ServerResponse serverResponse, ServerRequest[] requests, ServerPlatformRequest[] platformRequests, RequestEnvelope.Builder builder) throws RequestFailedException {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    RequestEnvelope request = builder.build();
    try {
        request.writeTo(stream);
    } catch (IOException e) {
        Log.wtf(TAG, "Failed to write request to bytearray ouput stream. This should never happen", e);
    }
    RequestBody body = RequestBody.create(null, stream.toByteArray());
    okhttp3.Request httpRequest = new okhttp3.Request.Builder().url(apiEndpoint).post(body).build();
    try (Response response = client.newCall(httpRequest).execute()) {
        if (response.code() != 200) {
            throw new RequestFailedException("Got a unexpected http code : " + response.code());
        }
        ResponseEnvelope responseEnvelop;
        try (InputStream content = response.body().byteStream()) {
            responseEnvelop = ResponseEnvelope.parseFrom(content);
        } catch (IOException e) {
            // retrieved garbage from the server
            throw new RequestFailedException("Received malformed response : " + e);
        }
        if (responseEnvelop.getApiUrl() != null && responseEnvelop.getApiUrl().length() > 0) {
            apiEndpoint = "https://" + responseEnvelop.getApiUrl() + "/rpc";
        }
        if (responseEnvelop.hasAuthTicket()) {
            this.authTicket = responseEnvelop.getAuthTicket();
        }
        boolean empty = false;
        StatusCode statusCode = responseEnvelop.getStatusCode();
        if (statusCode != StatusCode.REDIRECT && statusCode != StatusCode.INVALID_AUTH_TOKEN) {
            for (int i = 0; i < responseEnvelop.getReturnsCount(); i++) {
                ByteString returned = responseEnvelop.getReturns(i);
                ServerRequest serverRequest = requests[i];
                if (returned != null) {
                    serverResponse.addResponse(serverRequest.getType(), returned);
                    if (serverRequest.getType() == RequestType.GET_PLAYER) {
                        if (GetPlayerResponse.parseFrom(returned).getBanned()) {
                            throw new BannedException("Cannot send request, your account has been banned!");
                        }
                    }
                } else {
                    empty = true;
                }
            }
        }
        for (int i = 0; i < responseEnvelop.getPlatformReturnsCount(); i++) {
            PlatformResponse platformResponse = responseEnvelop.getPlatformReturns(i);
            ByteString returned = platformResponse.getResponse();
            if (returned != null) {
                serverResponse.addResponse(platformResponse.getType(), returned);
            }
        }
        if (statusCode != StatusCode.OK && statusCode != StatusCode.OK_RPC_URL_IN_RESPONSE) {
            if (statusCode == StatusCode.INVALID_AUTH_TOKEN) {
                try {
                    authTicket = null;
                    api.getAuthInfo(true);
                    return sendInternal(serverResponse, requests, platformRequests);
                } catch (LoginFailedException | InvalidCredentialsException e) {
                    throw new RequestFailedException("Failed to refresh auth token!", e);
                } catch (RequestFailedException e) {
                    throw new RequestFailedException("Failed to send request with refreshed auth token!", e);
                }
            } else if (statusCode == StatusCode.REDIRECT) {
                // API_ENDPOINT was not correctly set, should be at this point, though, so redo the request
                return sendInternal(serverResponse, requests, platformRequests, builder);
            } else if (statusCode == StatusCode.BAD_REQUEST) {
                if (api.getPlayerProfile().isBanned()) {
                    throw new BannedException("Cannot send request, your account has been banned!");
                } else {
                    throw new BadRequestException("A bad request was sent!");
                }
            } else {
                throw new RequestFailedException("Failed to send request: " + statusCode);
            }
        }
        if (empty) {
            throw new RequestFailedException("Received empty response from server!");
        }
    } catch (IOException e) {
        throw new RequestFailedException(e);
    } catch (RequestFailedException e) {
        throw e;
    }
    return serverResponse;
}
Also used : InputStream(java.io.InputStream) ByteString(com.google.protobuf.ByteString) PlatformRequest(POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass.RequestEnvelope.PlatformRequest) Request(POGOProtos.Networking.Requests.RequestOuterClass.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) StatusCode(POGOProtos.Networking.Envelopes.ResponseEnvelopeOuterClass.ResponseEnvelope.StatusCode) PlatformResponse(POGOProtos.Networking.Envelopes.ResponseEnvelopeOuterClass.ResponseEnvelope.PlatformResponse) Response(okhttp3.Response) GetPlayerResponse(POGOProtos.Networking.Responses.GetPlayerResponseOuterClass.GetPlayerResponse) ResponseEnvelope(POGOProtos.Networking.Envelopes.ResponseEnvelopeOuterClass.ResponseEnvelope) LoginFailedException(com.pokegoapi.exceptions.request.LoginFailedException) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidCredentialsException(com.pokegoapi.exceptions.request.InvalidCredentialsException) BannedException(com.pokegoapi.exceptions.request.BannedException) RequestEnvelope(POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass.RequestEnvelope) BadRequestException(com.pokegoapi.exceptions.request.BadRequestException) PlatformResponse(POGOProtos.Networking.Envelopes.ResponseEnvelopeOuterClass.ResponseEnvelope.PlatformResponse) RequestBody(okhttp3.RequestBody)

Aggregations

ByteString (com.google.protobuf.ByteString)159 Test (org.junit.Test)36 ArrayList (java.util.ArrayList)31 IOException (java.io.IOException)27 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)18 ServerRequest (com.pokegoapi.main.ServerRequest)13 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)10 Map (java.util.Map)10 HashMap (java.util.HashMap)9 List (java.util.List)9 AggregateResponse (org.apache.hadoop.hbase.protobuf.generated.AggregateProtos.AggregateResponse)9 BeforeTest (org.testng.annotations.BeforeTest)9 Test (org.testng.annotations.Test)9 Connection (org.apache.hadoop.hbase.client.Connection)8 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)7 Scan (org.apache.hadoop.hbase.client.Scan)7 DeleteRangeResponse (com.coreos.jetcd.api.DeleteRangeResponse)6 RangeResponse (com.coreos.jetcd.api.RangeResponse)6 RpcCallback (com.google.protobuf.RpcCallback)6 RpcController (com.google.protobuf.RpcController)6