Search in sources :

Example 1 with ClaimCodenameMessage

use of POGOProtos.Networking.Requests.Messages.ClaimCodenameMessageOuterClass.ClaimCodenameMessage in project PokeGOAPI-Java by Grover-c13.

the class PlayerProfile method claimCodeName.

/**
 * Setup an user name for our account
 *
 * @param lastFailure the last name used that was already taken; null for first try.
 * @return the claimed codename
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public String claimCodeName(String lastFailure) throws RequestFailedException {
    if (getPlayerData().getRemainingCodenameClaims() <= 0) {
        throw new RuntimeException("You have no remaining codename claims!");
    }
    String name = randomCodenameGenerator();
    List<TutorialListener> listeners = api.getListeners(TutorialListener.class);
    for (TutorialListener listener : listeners) {
        String listenerName = listener.claimName(api, lastFailure);
        if (listenerName != null) {
            name = listenerName;
            break;
        }
    }
    ClaimCodenameMessage claimCodenameMessage = ClaimCodenameMessage.newBuilder().setCodename(name).build();
    ServerRequest request = new ServerRequest(RequestType.CLAIM_CODENAME, claimCodenameMessage);
    api.requestHandler.sendServerRequests(request, true);
    String updatedCodename;
    try {
        ClaimCodenameResponse claimCodenameResponse = ClaimCodenameResponse.parseFrom(request.getData());
        if (claimCodenameResponse.getStatus() != ClaimCodenameResponse.Status.SUCCESS) {
            return claimCodeName(name);
        }
        updatedCodename = claimCodenameResponse.getCodename();
        if (claimCodenameResponse.hasUpdatedPlayer()) {
            updateProfile(claimCodenameResponse.getUpdatedPlayer());
        }
        if (updatedCodename != null) {
            markTutorial(TutorialStateOuterClass.TutorialState.NAME_SELECTION);
            final GetPlayerMessage getPlayerReqMsg = GetPlayerMessage.newBuilder().setPlayerLocale(playerLocale.getPlayerLocale()).build();
            request = new ServerRequest(RequestType.GET_PLAYER, getPlayerReqMsg);
            api.requestHandler.sendServerRequests(request, true);
            updateProfile(GetPlayerResponse.parseFrom(request.getData()));
        }
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    return updatedCodename;
}
Also used : GetPlayerMessage(POGOProtos.Networking.Requests.Messages.GetPlayerMessageOuterClass.GetPlayerMessage) TutorialListener(com.pokegoapi.api.listener.TutorialListener) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ClaimCodenameResponse(POGOProtos.Networking.Responses.ClaimCodenameResponseOuterClass.ClaimCodenameResponse) ServerRequest(com.pokegoapi.main.ServerRequest) ClaimCodenameMessage(POGOProtos.Networking.Requests.Messages.ClaimCodenameMessageOuterClass.ClaimCodenameMessage)

Aggregations

ClaimCodenameMessage (POGOProtos.Networking.Requests.Messages.ClaimCodenameMessageOuterClass.ClaimCodenameMessage)1 GetPlayerMessage (POGOProtos.Networking.Requests.Messages.GetPlayerMessageOuterClass.GetPlayerMessage)1 ClaimCodenameResponse (POGOProtos.Networking.Responses.ClaimCodenameResponseOuterClass.ClaimCodenameResponse)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 TutorialListener (com.pokegoapi.api.listener.TutorialListener)1 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)1 ServerRequest (com.pokegoapi.main.ServerRequest)1