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;
}
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);
}
}
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);
}
}
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);
}
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);
}
}
Aggregations