Esempio n. 1
0
  private List<Game> convertToGames(Cursor cursor) {
    List<Game> result = new ArrayList<Game>();

    Game currentGame = null;

    while (cursor.moveToNext()) {

      int currentId = cursor.getInt(0);

      if (currentGame == null || currentGame.getId() != currentId) { // new
        // Game

        currentGame = new Game();
        currentGame.setId(currentId);
        currentGame.setDateStarted(cursor.getLong(1));
        currentGame.setDateSaved(cursor.getLong(2));
        currentGame.setName(cursor.getString(3));
        result.add(currentGame);
      }

      List<PlayerScore> playerScores = new ArrayList<PlayerScore>();

      // build up all the PlayerScores
      do {

        if (cursor.getInt(0) != currentId) {
          cursor.moveToPrevious(); // went too far
          break;
        }

        PlayerScore playerScore = new PlayerScore();

        playerScore.setId(cursor.getInt(4));
        playerScore.setName(cursor.getString(5));
        playerScore.setScore(cursor.getLong(6));
        playerScore.setPlayerNumber(cursor.getInt(7));
        playerScore.setHistory(
            Delta.fromJoinedStrings(
                StringUtil.nullToEmpty(cursor.getString(8)),
                StringUtil.nullToEmpty(cursor.getString(9))));
        playerScore.setLastUpdate(cursor.getLong(10));
        playerScore.setPlayerColor(PlayerColor.deserialize(cursor.getString(11)));
        playerScores.add(playerScore);

      } while (cursor.moveToNext());

      Collections.sort(playerScores, PlayerScore.sortByPlayerNumber());

      currentGame.setPlayerScores(playerScores);
    }

    return result;
  }
Esempio n. 2
0
  /**
   * Add green color for positive entries and red color for negative entries, and convert ints to
   * strings.
   *
   * @param currentTime
   */
  private Spannable fromHistory(List<Integer> history, long currentTime) {

    history = historyToShow(history, currentTime);

    if (history.isEmpty()) {
      return null;
    }

    history = CollectionUtil.reversedCopy(history);

    // if e.g. there is a double-digit delta (e.g. "+10"), then all other
    // strings need to be padded
    // so that they line up correctly
    // but ensure there's always at least enough space for 3 chars (e.g. '+10'), because
    // I think it looks nicer and more consistent with most games
    int maxChars =
        Math.max(
            MIN_NUM_HISTORY_CHARS,
            CollectionUtil.maxValue(history, Functions.INTEGER_TO_LENGTH_WITH_SIGN));

    List<Spannable> spannables = CollectionUtil.transform(history, historyToSpan(maxChars));

    Spannable result =
        new SpannableString(
            StringUtil.joinSpannables("\n", CollectionUtil.toArray(spannables, Spannable.class)));

    return result;
  }
Esempio n. 3
0
 public static Set<String> getAvailableSettingSets(Context context) {
   String available = getMeta(context).getString(AVAILABLE_SETTINGS_SET, "");
   return new HashSet<String>(StringUtil.split(available, ','));
 }
Esempio n. 4
0
 public static boolean isValidSettingSetName(String name) {
   return !StringUtil.isEmptyOrWhitespace(name)
       && name.length() <= 50 // come on, dude - too long
       && !name.contains(","); // used for comma-separated list
 }
Esempio n. 5
0
  public List<GameSummary> findAllGameSummaries() {
    synchronized (GameDBHelper.class) {
      String[] columns = {
        "g." + COLUMN_ID,
        "g." + COLUMN_NAME,
        "g." + COLUMN_DATE_SAVED,
        // player names; the "separator" is a trick to ensure that we can cleanly separate the
        // response,
        // and put it into the proper order, since group_concat is always unordered in sqlite
        "group_concat((ps.name || '"
            + GROUP_CONCAT_INNER_SEPARATOR
            + "' || ps.playerNumber), '"
            + GROUP_CONCAT_SEPARATOR
            + "')",
        "max(length(ps.history) - length(replace(ps.history, ',', '')) + 1)" // num rounds
      };

      String table =
          TABLE_GAMES
              + " g join "
              + TABLE_PLAYER_SCORES
              + " ps "
              + " on g."
              + COLUMN_ID
              + " = ps."
              + COLUMN_GAME_ID;
      String groupBy = "g." + COLUMN_ID;

      Cursor cursor = null;

      try {

        cursor = db.query(table, columns, null, null, groupBy, null, null);

        List<GameSummary> result = new ArrayList<GameSummary>();

        // re-use sparse array for performance
        SparseArray<String> playerNumbersToNames = new SparseArray<String>();

        while (cursor.moveToNext()) {
          GameSummary gameSummary = new GameSummary();

          gameSummary.setId(cursor.getInt(0));
          gameSummary.setName(cursor.getString(1));
          gameSummary.setDateSaved(cursor.getLong(2));

          String playerNumbersAndNames = cursor.getString(3);
          // sort by player number, get player names in order (no way to do this in sqlite,
          // unfortunately)

          playerNumbersToNames.clear();
          for (String playerNumberAndName :
              StringUtil.split(playerNumbersAndNames, GROUP_CONCAT_SEPARATOR)) {
            int idx = playerNumberAndName.indexOf(GROUP_CONCAT_INNER_SEPARATOR);
            String playerName = playerNumberAndName.substring(0, idx);
            int playerNumber =
                Integer.parseInt(
                    playerNumberAndName.substring(idx + GROUP_CONCAT_INNER_SEPARATOR.length()));
            playerNumbersToNames.put(playerNumber, playerName);
          }
          List<String> playerNames = new ArrayList<String>(playerNumbersToNames.size());
          for (int i = 0, len = playerNumbersToNames.size(); i < len; i++) {
            int playerNumber = playerNumbersToNames.keyAt(i);
            playerNames.add(playerNumbersToNames.get(playerNumber));
          }
          gameSummary.setPlayerNames(playerNames);

          gameSummary.setNumRounds(cursor.getInt(4));

          result.add(gameSummary);
        }

        return result;

      } finally {
        if (cursor != null) {
          cursor.close();
        }
      }
    }
  }