Пример #1
0
  /**
   * Handle a "attack"-message.
   *
   * @param server The <code>FreeColServer</code> handling the message.
   * @param player The <code>Player</code> the message applies to.
   * @param connection The <code>Connection</code> message was received on.
   * @return An update encapsulating the attack or an error <code>Element</code> on failure.
   */
  public Element handle(FreeColServer server, Player player, Connection connection) {
    ServerPlayer serverPlayer = server.getPlayer(connection);

    Unit unit;
    try {
      unit = serverPlayer.getOurFreeColGameObject(unitId, Unit.class);
    } catch (Exception e) {
      return DOMMessage.clientError(e.getMessage());
    }

    Tile tile;
    try {
      tile = unit.getNeighbourTile(directionString);
    } catch (Exception e) {
      return DOMMessage.clientError(e.getMessage());
    }

    MoveType moveType = unit.getMoveType(tile);
    if (moveType == MoveType.ENTER_INDIAN_SETTLEMENT_WITH_SCOUT
        || moveType == MoveType.ENTER_FOREIGN_COLONY_WITH_SCOUT
        || moveType.isAttack()) {; // OK
    } else {
      return DOMMessage.clientError(
          "Illegal attack move for: "
              + unitId
              + " type: "
              + moveType
              + " from: "
              + unit.getLocation().getId()
              + " to: "
              + tile.getId());
    }

    Unit defender = tile.getDefendingUnit(unit);
    if (defender == null) {
      return DOMMessage.clientError(
          "Could not find defender"
              + " in tile: "
              + tile.getId()
              + " from: "
              + unit.getLocation().getId());
    }

    // Proceed to attack.
    return server.getInGameController().combat(serverPlayer, unit, defender, null);
  }