Ejemplo n.º 1
0
  /**
   * Look for abandoned stars. We look for stars which are far enough from established empires, but
   * still near the centre of the universe and not *too* far...
   */
  private boolean findAbandonedStar() throws RequestException {
    String sql =
        "SELECT star_id, empire_id"
            + " FROM abandoned_stars"
            + " WHERE distance_to_non_abandoned_empire > 200"
            + " ORDER BY (distance_to_non_abandoned_empire + distance_to_centre) ASC"
            + " LIMIT 10";
    try (SqlStmt stmt = DB.prepare(sql)) {
      SqlResult res = stmt.select();

      List<Pair<Integer, Integer>> stars = new ArrayList<Pair<Integer, Integer>>();
      while (res.next()) {
        int starID = res.getInt(1);
        int empireID = res.getInt(2);
        stars.add(new Pair<Integer, Integer>(starID, empireID));
      }

      if (stars.size() > 0) {
        Pair<Integer, Integer> starDetails = stars.get(new Random().nextInt(stars.size()));

        // we need to reset the empire on this star so that they move to a different star if the log
        // in again.
        new EmpireController()
            .resetEmpire(
                starDetails.two, "You have not logged in for a while and your star was reclaimed.");

        mStarID = starDetails.one;
        findPlanetOnStar(new StarController().getStar(mStarID));

        // the star is no longer abandoned!
        sql = "DELETE FROM abandoned_stars WHERE star_id = ?";
        try (SqlStmt stmt2 = DB.prepare(sql)) {
          stmt2.setInt(1, mStarID);
          stmt2.update();
        }
        return true;
      }
    } catch (Exception e) {
      throw new RequestException(e);
    }

    return false;
  }