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);
}
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);
}
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;
}
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;
}
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;
}
}
}
Aggregations