Search in sources :

Example 1 with BattleState

use of POGOProtos.Data.Battle.BattleStateOuterClass.BattleState in project PokeGOAPI-Java by Grover-c13.

the class Battle method updateLog.

/**
 * Updates this battle with the given log
 *
 * @param handler to handle this battle
 * @param log the log to update with
 * @return if this battle should move on to the next defender
 */
private boolean updateLog(BattleHandler handler, BattleLog log) {
    serverTimeOffset = log.getServerMs() - api.currentTimeMillis();
    battleType = log.getBattleType();
    startTime = log.getBattleStartTimestampMs();
    endTime = log.getBattleEndTimestampMs();
    if (log.getBattleActionsCount() > 0) {
        long latestTime = Long.MIN_VALUE;
        for (BattleAction action : log.getBattleActionsList()) {
            if (action.getActionStartMs() > latestTime) {
                lastRetrievedAction = action;
                latestTime = action.getActionStartMs();
            }
        }
    }
    results = null;
    for (BattleAction action : log.getBattleActionsList()) {
        BattleResults results = action.getBattleResults();
        if (results.hasGymState()) {
            this.results = action.getBattleResults();
        }
    }
    if (results != null) {
        gym.updatePoints(results.getGymPointsDelta());
        gymPointsDelta += results.getGymPointsDelta();
    }
    BattleState state = log.getState();
    active = defenderIndex < defenderCount && !(state == BattleState.TIMED_OUT || state == BattleState.STATE_UNSET);
    if (state != battleState) {
        switch(state) {
            case TIMED_OUT:
                gym.clearDetails();
                handler.onTimedOut(api, this);
                break;
            case DEFEATED:
                gym.clearDetails();
                handler.onDefeated(api, this);
                break;
            case VICTORY:
                if (!active) {
                    gym.updateState(results.getGymState());
                    handler.onVictory(api, this, gymPointsDelta, gym.getPoints());
                }
                break;
            default:
                break;
        }
        if (!active) {
            try {
                api.inventories.updateInventories();
            } catch (Exception e) {
                handler.onException(api, this, e);
            }
        }
        battleState = state;
    }
    for (BattleAction action : log.getBattleActionsList()) {
        ServerAction serverAction = new ServerAction(action);
        if (!handledActions.contains(serverAction)) {
            serverActionQueue.add(serverAction);
            handledActions.add(serverAction);
        }
    }
    lastServerTime = log.getServerMs();
    return battleState != BattleState.ACTIVE && battleState != BattleState.STATE_UNSET && battleState != BattleState.TIMED_OUT;
}
Also used : BattleResults(POGOProtos.Data.Battle.BattleResultsOuterClass.BattleResults) BattleAction(POGOProtos.Data.Battle.BattleActionOuterClass.BattleAction) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) BattleState(POGOProtos.Data.Battle.BattleStateOuterClass.BattleState)

Aggregations

BattleAction (POGOProtos.Data.Battle.BattleActionOuterClass.BattleAction)1 BattleResults (POGOProtos.Data.Battle.BattleResultsOuterClass.BattleResults)1 BattleState (POGOProtos.Data.Battle.BattleStateOuterClass.BattleState)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)1