Search in sources :

Example 1 with Delta

use of com.nolanlawson.keepscore.db.Delta in project KeepScore by nolanlawson.

the class GameActivity method createNewGame.

private void createNewGame() {
    String[] playerNames = getIntent().getStringArrayExtra(EXTRA_PLAYER_NAMES);
    String[] playerColors = getIntent().getStringArrayExtra(EXTRA_PLAYER_COLORS);
    game = new Game();
    playerScores = new ArrayList<PlayerScore>();
    game.setDateStarted(System.currentTimeMillis());
    game.setPlayerScores(playerScores);
    for (int i = 0; i < playerNames.length; i++) {
        PlayerScore playerScore = new PlayerScore();
        playerScore.setName(playerNames[i]);
        playerScore.setPlayerColor(PlayerColor.deserialize(playerColors[i]));
        playerScore.setPlayerNumber(i);
        playerScore.setHistory(new ArrayList<Delta>());
        playerScore.setScore(PreferenceHelper.getIntPreference(R.string.CONSTANT_pref_initial_score, R.string.CONSTANT_pref_initial_score_default, GameActivity.this));
        playerScores.add(playerScore);
    }
    log.d("created new game: %s", game);
    log.d("created new playerScores: %s", playerScores);
}
Also used : Game(com.nolanlawson.keepscore.db.Game) Delta(com.nolanlawson.keepscore.db.Delta) PlayerScore(com.nolanlawson.keepscore.db.PlayerScore)

Example 2 with Delta

use of com.nolanlawson.keepscore.db.Delta in project KeepScore by nolanlawson.

the class HistoryRoundChartFragment method createByChartLayout.

private void createByChartLayout(Activity activity) {
    List<LineChartLine> data = new ArrayList<LineChartLine>();
    for (PlayerScore playerScore : game.getPlayerScores()) {
        List<Integer> dataPoints = new ArrayList<Integer>();
        // have to include the starting score as well
        long runningTally = playerScore.getScore() - CollectionUtil.sum(CollectionUtil.transform(playerScore.getHistory(), Delta.GET_VALUE));
        dataPoints.add((int) runningTally);
        for (Delta delta : playerScore.getHistory()) {
            runningTally += delta.getValue();
            dataPoints.add((int) runningTally);
        }
        LineChartLine line = new LineChartLine();
        line.setDataPoints(dataPoints);
        line.setLabel(playerScore.toDisplayName(activity).toString());
        data.add(line);
    }
    byRoundLineChartView.setLineColors(createLineColors(game, activity));
    byRoundLineChartView.loadData(data);
}
Also used : Delta(com.nolanlawson.keepscore.db.Delta) ArrayList(java.util.ArrayList) LineChartLine(com.nolanlawson.keepscore.widget.chart.LineChartLine) PlayerScore(com.nolanlawson.keepscore.db.PlayerScore)

Example 3 with Delta

use of com.nolanlawson.keepscore.db.Delta in project KeepScore by nolanlawson.

the class HistoryTimelineFragment method smoothData.

private SparseArray<SparseArray<Long>> smoothData(Game game) {
    long roundedStartTimeInMs = Math.round(Math.floor(game.getDateStarted() * 1.0 / TIMELINE_ROUNDING_IN_MS)) * TIMELINE_ROUNDING_IN_MS;
    // first, plot all players' deltas with their timestamps on the same timeline (x axis), rounded to
    // the nearest ten seconds
    SparseArray<SparseArray<Long>> timeline = new SparseArray<SparseArray<Long>>();
    for (int i = 0; i < game.getPlayerScores().size(); i++) {
        PlayerScore playerScore = game.getPlayerScores().get(i);
        // have to include the starting score as well
        long startingScore = playerScore.getScore() - CollectionUtil.sum(CollectionUtil.transform(playerScore.getHistory(), Delta.GET_VALUE));
        timeline.put(0, SparseArrays.create(i, startingScore));
        long runningTally = startingScore;
        for (Delta delta : playerScore.getHistory()) {
            runningTally += delta.getValue();
            long timeSinceStartInMs = delta.getTimestamp() - roundedStartTimeInMs;
            int roundedTimeSinceStartInSecs = (int) TimeUnit.MILLISECONDS.toSeconds(Math.round(Math.floor(timeSinceStartInMs * 1.0 / TIMELINE_ROUNDING_IN_MS)) * TIMELINE_ROUNDING_IN_MS);
            if (roundedTimeSinceStartInSecs == 0) {
                // just in case someone was actually fast enough to log the first score in <5 seconds, bump
                // it up to the first mark instead
                roundedTimeSinceStartInSecs = (int) TimeUnit.MILLISECONDS.toSeconds(TIMELINE_ROUNDING_IN_MS);
            }
            log.d("roundedStartTimeInMs: %s, timeSinceStartInMs: %s, roundedTimeSinceStartInSecs: %s", roundedStartTimeInMs, timeSinceStartInMs, roundedTimeSinceStartInSecs);
            SparseArray<Long> existingScoresAtThisTime = timeline.get(roundedTimeSinceStartInSecs);
            if (existingScoresAtThisTime == null) {
                timeline.put(roundedTimeSinceStartInSecs, SparseArrays.create(i, runningTally));
            } else {
                // If the same player updated his score twice within the same rounded span,
                // then just add the two values together
                existingScoresAtThisTime.put(i, runningTally);
            }
        }
    }
    return timeline;
}
Also used : SparseArray(android.util.SparseArray) Delta(com.nolanlawson.keepscore.db.Delta) PlayerScore(com.nolanlawson.keepscore.db.PlayerScore)

Example 4 with Delta

use of com.nolanlawson.keepscore.db.Delta in project KeepScore by nolanlawson.

the class SerializationTest method createRandomHistory.

private List<Delta> createRandomHistory(int score) {
    List<Delta> history = new ArrayList<Delta>();
    for (int deltaValue, sum = 0; sum < score; sum += deltaValue) {
        deltaValue = Math.min(random.nextInt(15) + 1, score - sum);
        history.add(new Delta(0L, deltaValue));
    }
    return history;
}
Also used : Delta(com.nolanlawson.keepscore.db.Delta) ArrayList(java.util.ArrayList)

Example 5 with Delta

use of com.nolanlawson.keepscore.db.Delta in project KeepScore by nolanlawson.

the class PlayerView method reexecuteChange.

public void reexecuteChange(RecordedChange recordedChange) {
    synchronized (lock) {
        switch(recordedChange.getType()) {
            case AddNew:
                playerScore.getHistory().add(recordedChange.getDelta());
                playerScore.setScore(playerScore.getScore() + recordedChange.getDelta().getValue());
                break;
            case DeleteLast:
            case DeleteLastZero:
                playerScore.getHistory().remove(playerScore.getHistory().size() - 1);
                playerScore.setScore(playerScore.getScore() - recordedChange.getDelta().getValue());
                break;
            case ModifyLast:
            default:
                int lastIdx = playerScore.getHistory().size() - 1;
                Delta lastDelta = playerScore.getHistory().get(lastIdx);
                lastDelta.setValue(lastDelta.getValue() + recordedChange.getDelta().getValue());
                playerScore.setScore(playerScore.getScore() + recordedChange.getDelta().getValue());
                break;
        }
    }
}
Also used : Delta(com.nolanlawson.keepscore.db.Delta)

Aggregations

Delta (com.nolanlawson.keepscore.db.Delta)15 PlayerScore (com.nolanlawson.keepscore.db.PlayerScore)5 RecordedChange (com.nolanlawson.keepscore.data.RecordedChange)3 ArrayList (java.util.ArrayList)3 Spannable (android.text.Spannable)2 Game (com.nolanlawson.keepscore.db.Game)2 SpannableString (android.text.SpannableString)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 SparseArray (android.util.SparseArray)1 Button (android.widget.Button)1 GameDBHelper (com.nolanlawson.keepscore.db.GameDBHelper)1 LineChartLine (com.nolanlawson.keepscore.widget.chart.LineChartLine)1