Search in sources :

Example 11 with ServerRequest

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

the class PlayerProfile method setupAvatar.

/**
	 * Setup an avatar for the current account
	 *
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public void setupAvatar() throws RequestFailedException {
    SecureRandom random = new SecureRandom();
    PlayerGender gender = random.nextInt(100) % 2 == 0 ? PlayerGender.FEMALE : PlayerGender.MALE;
    PlayerAvatar avatar = new PlayerAvatar(gender, random.nextInt(PlayerAvatar.getAvailableSkins()), random.nextInt(PlayerAvatar.getAvailableHair()), random.nextInt(PlayerAvatar.getAvailableShirts(gender)), random.nextInt(PlayerAvatar.getAvailablePants(gender)), random.nextInt(PlayerAvatar.getAvailableHats()), random.nextInt(PlayerAvatar.getAvailableShoes()), random.nextInt(PlayerAvatar.getAvailableEyes()), random.nextInt(PlayerAvatar.getAvailableBags(gender)));
    List<TutorialListener> listeners = api.getListeners(TutorialListener.class);
    for (TutorialListener listener : listeners) {
        PlayerAvatar listenerAvatar = listener.selectAvatar(api);
        if (listenerAvatar != null) {
            avatar = listenerAvatar;
            break;
        }
    }
    final SetAvatarMessage setAvatarMessage = SetAvatarMessage.newBuilder().setPlayerAvatar(avatar.getAvatar()).build();
    ServerRequest request = new ServerRequest(RequestType.SET_AVATAR, setAvatarMessage);
    api.getRequestHandler().sendServerRequests(request, true);
    try {
        SetAvatarResponse setAvatarResponse = SetAvatarResponse.parseFrom(request.getData());
        playerData = setAvatarResponse.getPlayerData();
        updateProfile(playerData);
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    markTutorial(TutorialStateOuterClass.TutorialState.AVATAR_SELECTION);
    api.getAssetDigest();
}
Also used : TutorialListener(com.pokegoapi.api.listener.TutorialListener) SetAvatarMessage(POGOProtos.Networking.Requests.Messages.SetAvatarMessageOuterClass.SetAvatarMessage) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) SecureRandom(java.security.SecureRandom) ServerRequest(com.pokegoapi.main.ServerRequest) SetAvatarResponse(POGOProtos.Networking.Responses.SetAvatarResponseOuterClass.SetAvatarResponse)

Example 12 with ServerRequest

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

the class PlayerProfile method setBuddy.

/**
	 * Sets the current buddy
	 *
	 * @param pokemon the pokemon to set as your buddy
	 * @return if this task was successfull
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public boolean setBuddy(Pokemon pokemon) throws RequestFailedException {
    SetBuddyPokemonMessageOuterClass.SetBuddyPokemonMessage message = SetBuddyPokemonMessageOuterClass.SetBuddyPokemonMessage.newBuilder().setPokemonId(pokemon.getId()).build();
    ServerRequest request = new ServerRequest(RequestType.SET_BUDDY_POKEMON, message);
    api.getRequestHandler().sendServerRequests(request);
    try {
        SetBuddyPokemonResponse response = SetBuddyPokemonResponse.parseFrom(request.getData());
        buddy = new Buddy(api, response.getUpdatedBuddy());
        return response.hasUpdatedBuddy();
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) SetBuddyPokemonMessageOuterClass(POGOProtos.Networking.Requests.Messages.SetBuddyPokemonMessageOuterClass) SetBuddyPokemonResponse(POGOProtos.Networking.Responses.SetBuddyPokemonResponseOuterClass.SetBuddyPokemonResponse) Buddy(com.pokegoapi.api.pokemon.Buddy)

Example 13 with ServerRequest

use of com.pokegoapi.main.ServerRequest 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.getRequestHandler().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.getRequestHandler().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)

Example 14 with ServerRequest

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

the class PlayerProfile method acceptLevelUpRewards.

/**
	 * Accept the rewards granted and the items unlocked by gaining a trainer level up. Rewards are retained by the
	 * server until a player actively accepts them.
	 * The rewarded items are automatically inserted into the players item bag.
	 *
	 * @param level the trainer level that you want to accept the rewards for
	 * @return a PlayerLevelUpRewards object containing information about the items rewarded and unlocked for this level
	 * @throws RequestFailedException if an exception occurred while sending requests
	 * @throws InsufficientLevelException if you have not yet reached the desired level
	 * @see PlayerLevelUpRewards
	 */
public PlayerLevelUpRewards acceptLevelUpRewards(int level) throws RequestFailedException {
    // Check if we even have achieved this level yet
    if (level > stats.getLevel()) {
        throw new InsufficientLevelException();
    }
    LevelUpRewardsMessage msg = LevelUpRewardsMessage.newBuilder().setLevel(level).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.LEVEL_UP_REWARDS, msg);
    api.getRequestHandler().sendServerRequests(serverRequest, true);
    LevelUpRewardsResponse response;
    try {
        response = LevelUpRewardsResponse.parseFrom(serverRequest.getData());
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    // Add the awarded items to our bag
    ItemBag bag = api.getInventories().getItemBag();
    bag.addAwardedItems(response);
    // Build a new rewards object and return it
    return new PlayerLevelUpRewards(response);
}
Also used : InsufficientLevelException(com.pokegoapi.exceptions.InsufficientLevelException) LevelUpRewardsMessage(POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass.LevelUpRewardsMessage) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) LevelUpRewardsResponse(POGOProtos.Networking.Responses.LevelUpRewardsResponseOuterClass.LevelUpRewardsResponse) ItemBag(com.pokegoapi.api.inventory.ItemBag)

Example 15 with ServerRequest

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

the class Pokemon method transferPokemon.

/**
	 * Transfers the pokemon.
	 *
	 * @return the result
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public Result transferPokemon() throws RequestFailedException {
    if (this.isFavorite() || this.isDeployed()) {
        return Result.FAILED;
    }
    ReleasePokemonMessage reqMsg = ReleasePokemonMessage.newBuilder().setPokemonId(getId()).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.RELEASE_POKEMON, reqMsg);
    api.getRequestHandler().sendServerRequests(serverRequest, true);
    ReleasePokemonResponse response;
    try {
        response = ReleasePokemonResponse.parseFrom(serverRequest.getData());
    } catch (InvalidProtocolBufferException e) {
        return ReleasePokemonResponse.Result.FAILED;
    }
    if (response.getResult() == Result.SUCCESS) {
        api.getInventories().getPokebank().removePokemon(this);
    }
    return response.getResult();
}
Also used : ReleasePokemonMessage(POGOProtos.Networking.Requests.Messages.ReleasePokemonMessageOuterClass.ReleasePokemonMessage) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ReleasePokemonResponse(POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse) ServerRequest(com.pokegoapi.main.ServerRequest)

Aggregations

InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)42 ServerRequest (com.pokegoapi.main.ServerRequest)42 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)35 ByteString (com.google.protobuf.ByteString)13 GetPlayerMessage (POGOProtos.Networking.Requests.Messages.GetPlayerMessageOuterClass.GetPlayerMessage)3 Item (com.pokegoapi.api.inventory.Item)3 ItemBag (com.pokegoapi.api.inventory.ItemBag)3 TutorialListener (com.pokegoapi.api.listener.TutorialListener)3 FortDeployPokemonMessage (POGOProtos.Networking.Requests.Messages.FortDeployPokemonMessageOuterClass.FortDeployPokemonMessage)2 LevelUpRewardsMessage (POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass.LevelUpRewardsMessage)2 ReleasePokemonMessage (POGOProtos.Networking.Requests.Messages.ReleasePokemonMessageOuterClass.ReleasePokemonMessage)2 UseItemEncounterMessage (POGOProtos.Networking.Requests.Messages.UseItemEncounterMessageOuterClass.UseItemEncounterMessage)2 LevelUpRewardsResponse (POGOProtos.Networking.Responses.LevelUpRewardsResponseOuterClass.LevelUpRewardsResponse)2 UseItemEncounterResponse (POGOProtos.Networking.Responses.UseItemEncounterResponseOuterClass.UseItemEncounterResponse)2 EvolutionResult (com.pokegoapi.api.map.pokemon.EvolutionResult)2 BattleAction (POGOProtos.Data.Battle.BattleActionOuterClass.BattleAction)1 PlayerBadge (POGOProtos.Data.PlayerBadgeOuterClass.PlayerBadge)1 PokemonFamilyId (POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId)1 TutorialState (POGOProtos.Enums.TutorialStateOuterClass.TutorialState)1 Candy (POGOProtos.Inventory.CandyOuterClass.Candy)1