@Override
  protected void get() throws RequestException {
    int year = Integer.parseInt(getUrlParameter("year"));
    int month = Integer.parseInt(getUrlParameter("month"));

    String sql =
        "SELECT empire_id, rank, total_stars, total_colonies, total_buildings,"
            + " total_ships, total_population"
            + " FROM empire_rank_histories"
            + " WHERE date >= ?"
            + " AND date < ?"
            + " ORDER BY rank, date DESC";
    try (SqlStmt stmt = DB.prepare(sql)) {
      stmt.setDateTime(1, new DateTime(year, month, 1, 0, 0, DateTimeZone.UTC));
      stmt.setDateTime(2, new DateTime(year, month, 1, 0, 0, DateTimeZone.UTC).plusMonths(1));
      SqlResult res = stmt.select();

      int lastRank = 0;
      Messages.EmpireRanks.Builder empire_ranks_pb = Messages.EmpireRanks.newBuilder();
      empire_ranks_pb.setDate(
          new DateTime(year, month, 1, 0, 0, DateTimeZone.UTC).getMillis() / 1000);
      while (res.next()) {
        int rank = res.getInt(2);
        if (rank == lastRank) {
          continue;
        }
        lastRank = rank;

        Messages.EmpireRank.Builder empire_rank_pb = Messages.EmpireRank.newBuilder();
        empire_rank_pb.setEmpireKey(Integer.toString(res.getInt(1)));
        empire_rank_pb.setRank(rank);
        empire_rank_pb.setTotalStars(res.getInt(3));
        empire_rank_pb.setTotalColonies(res.getInt(4));
        empire_rank_pb.setTotalBuildings(res.getInt(5));
        empire_rank_pb.setTotalShips(res.getInt(6));
        empire_rank_pb.setTotalPopulation(res.getInt(7));
        empire_ranks_pb.addRanks(empire_rank_pb);
      }

      setResponseBody(empire_ranks_pb.build());
    } catch (Exception e) {
      throw new RequestException(e);
    }
  }
  /**
   * 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;
  }
Beispiel #3
0
  private static void ensureFilter() {
    if (!sProfaneWords.isEmpty()) {
      return;
    }

    String sql = "SELECT * FROM chat_profane_words";
    try (SqlStmt stmt = DB.prepare(sql)) {
      SqlResult res = stmt.select();
      while (res.next()) {
        int profanityLevel = res.getInt("profanity_level");
        String words = res.getString("words");

        for (String word : words.split("\\s+")) {
          sProfaneWords.put(word.trim().toLowerCase(), profanityLevel);
        }
      }
    } catch (Exception e) {
      log.error("Error fetching profane words list.", e);
    }
  }
  @Override
  protected void get() throws RequestException {
    int empireID = Integer.parseInt(getUrlParameter("empire_id"));
    if (!getSession().isAdmin()) {
      throw new RequestException(403); // TODO: allow you to get your own...
    }

    String sql = "SELECT reason FROM empire_cash_audit WHERE empire_id = ? ORDER BY time DESC";
    try (SqlStmt stmt = DB.prepare(sql)) {
      stmt.setInt(1, empireID);
      SqlResult res = stmt.select();

      Messages.CashAuditRecords.Builder cash_audit_records_pb =
          Messages.CashAuditRecords.newBuilder();
      while (res.next()) {
        cash_audit_records_pb.addRecords(Messages.CashAuditRecord.parseFrom(res.getBytes(1)));
      }
      setResponseBody(cash_audit_records_pb.build());
    } catch (Exception e) {
      throw new RequestException(e);
    }
  }
  private ArrayList<Integer> findSectors(boolean tryNonEmptyFirst) throws RequestException {
    String sql =
        "SELECT id FROM sectors WHERE num_colonies = 0" + " ORDER BY distance_to_centre ASC";
    ArrayList<Integer> ids = new ArrayList<Integer>();
    try (SqlStmt stmt = DB.prepare(sql)) {
      SqlResult res = stmt.select();
      while (res.next()) {
        ids.add(res.getInt(1));
        if (ids.size() > 10) {
          break;
        }
      }
    } catch (Exception e) {
      throw new RequestException(e);
    }
    if (!ids.isEmpty()) {
      return ids;
    }

    // if we get here, the universe needs to be expanded by a bit so we have some new sectors
    // to colonies...
    new SectorGenerator().expandUniverse();
    return findSectors(tryNonEmptyFirst);
  }