Example #1
0
  private void savePlayerScores(int gameId, List<PlayerScore> playerScores) {
    synchronized (GameDBHelper.class) {
      int newId = -1;

      for (PlayerScore playerScore : playerScores) {

        Pair<String, String> historyAsStrings = Delta.toJoinedStrings(playerScore.getHistory());

        if (playerScore.getId() != -1) {
          // already exists; update

          updatePlayerScore(
              playerScore.getId(),
              playerScore.getName(),
              playerScore.getScore(),
              playerScore.getPlayerNumber(),
              historyAsStrings.getFirst(),
              historyAsStrings.getSecond(),
              playerScore.getLastUpdate(),
              PlayerColor.serialize(playerScore.getPlayerColor()));

        } else {
          // else insert new rows in the table

          if (newId == -1) {
            newId = getMaxPlayerScoreId() + 1;
          } else {
            newId++;
          }

          ContentValues values = new ContentValues();
          values.put(COLUMN_ID, newId);
          values.put(COLUMN_GAME_ID, gameId);
          values.put(COLUMN_HISTORY, historyAsStrings.getFirst());
          values.put(COLUMN_HISTORY_TIMESTAMPS, historyAsStrings.getSecond());
          values.put(COLUMN_NAME, playerScore.getName());
          values.put(COLUMN_PLAYER_NUMBER, playerScore.getPlayerNumber());
          values.put(COLUMN_SCORE, playerScore.getScore());
          values.put(COLUMN_COLOR, PlayerColor.serialize(playerScore.getPlayerColor()));
          values.put(COLUMN_LAST_UPDATE, playerScore.getLastUpdate());
          db.insert(TABLE_PLAYER_SCORES, null, values);

          // set the new id on the PlayerScore
          playerScore.setId(newId);

          log.d("new playerScore id is %s", newId);
        }
      }
    }
  }
Example #2
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;
  }