158 lines
3.7 KiB
JavaScript
158 lines
3.7 KiB
JavaScript
export class ScoreEntry {
|
|
constructor(score, total) {
|
|
this.score = score;
|
|
this.total = total;
|
|
}
|
|
};
|
|
|
|
export class Scorecard {
|
|
|
|
constructor() {
|
|
this.reset();
|
|
}
|
|
|
|
reset() {
|
|
this._player1Scores = [];
|
|
this._player2Scores = [];
|
|
this._player3Scores = [];
|
|
this._player4Scores = [];
|
|
}
|
|
|
|
addPlayer1Score(newScore) {
|
|
this._addScore(this._player1Scores, newScore);
|
|
}
|
|
|
|
removeLastPlayer1Score() {
|
|
this._player1Scores.pop();
|
|
}
|
|
|
|
addPlayer2Score(newScore) {
|
|
this._addScore(this._player2Scores, newScore);
|
|
}
|
|
|
|
removeLastPlayer2Score() {
|
|
this._player2Scores.pop();
|
|
}
|
|
|
|
addPlayer3Score(newScore) {
|
|
this._addScore(this._player3Scores, newScore);
|
|
}
|
|
|
|
removeLastPlayer3Score() {
|
|
this._player3Scores.pop();
|
|
}
|
|
|
|
addPlayer4Score(newScore) {
|
|
this._addScore(this._player4Scores, newScore);
|
|
}
|
|
|
|
removeLastPlayer4Score() {
|
|
this._player4Scores.pop();
|
|
}
|
|
|
|
_addScore(playerScores, newScore) {
|
|
let lastEntry;
|
|
if (playerScores.length === 0) {
|
|
lastEntry = new ScoreEntry(0, 0, 0, false, false);
|
|
} else {
|
|
lastEntry = playerScores[playerScores.length - 1];
|
|
}
|
|
|
|
let newEntry = this._newEntryFromPrevious(newScore, lastEntry);
|
|
|
|
playerScores.push(newEntry);
|
|
}
|
|
|
|
length() {
|
|
return Math.max(this._player1Scores.length, this._player2Scores.length,
|
|
this._player3Scores.length, this._player4Scores.length);
|
|
}
|
|
|
|
pairs() {
|
|
let pairs = [];
|
|
|
|
for (let i = 0; i < this.length(); i++) {
|
|
pairs.push({
|
|
p1: (i < this._player1Scores.length ? this._player1Scores[i] : null),
|
|
p2: (i < this._player2Scores.length ? this._player2Scores[i] : null),
|
|
p3: (i < this._player3Scores.length ? this._player3Scores[i] : null),
|
|
p4: (i < this._player4Scores.length ? this._player4Scores[i] : null),
|
|
})
|
|
}
|
|
|
|
return pairs;
|
|
}
|
|
|
|
_newEntryFromPrevious(score, previousScoreEntry) {
|
|
if (previousScoreEntry === null) {
|
|
return new ScoreEntry(score, score, (score === 0 ? 1 : 0));
|
|
}
|
|
|
|
let newTotal = previousScoreEntry.total + score;
|
|
return new ScoreEntry(score, newTotal);
|
|
}
|
|
|
|
toJson() {
|
|
return {
|
|
"version": 1,
|
|
"p1": { "scores": this._player1Scores.map(p => p.score) },
|
|
"p2": { "scores": this._player2Scores.map(p => p.score) },
|
|
"p3": { "scores": this._player3Scores.map(p => p.score) },
|
|
"p4": { "scores": this._player4Scores.map(p => p.score) },
|
|
};
|
|
}
|
|
|
|
static fromJson(o) {
|
|
let scorecard = new Scorecard();
|
|
o["p1"]["scores"].forEach(x => scorecard.addPlayer1Score(x));
|
|
o["p2"]["scores"].forEach(x => scorecard.addPlayer2Score(x));
|
|
if (o["p3"]) o["p3"]["scores"].forEach(x => scorecard.addPlayer3Score(x));
|
|
if (o["p4"]) o["p4"]["scores"].forEach(x => scorecard.addPlayer4Score(x));
|
|
return scorecard;
|
|
}
|
|
}
|
|
|
|
class StoreDAO {
|
|
|
|
constructor(localStorage) {
|
|
this._localStorage = localStorage;
|
|
}
|
|
|
|
save(scoreCard) {
|
|
this._localStorage.setItem('generic-scorecard-4p', JSON.stringify(scoreCard.toJson()));
|
|
}
|
|
|
|
loadOrCreate() {
|
|
try {
|
|
console.log("Loading scorecard");
|
|
let scoreCardJson = this._localStorage.getItem('generic-scorecard-4p');
|
|
if (scoreCardJson !== null) {
|
|
return Scorecard.fromJson(JSON.parse(scoreCardJson));
|
|
}
|
|
} catch (e) {
|
|
console.log(`Could not restore game: ${e}`);
|
|
}
|
|
|
|
return new Scorecard();
|
|
}
|
|
|
|
clear() {
|
|
this._localStorage.removeItem('generic-scorecard-4p');
|
|
}
|
|
}
|
|
|
|
class DummyStoreDAO {
|
|
save(scoreCard) { }
|
|
loadOrCreate() {
|
|
return new Scorecard();
|
|
}
|
|
clear() { }
|
|
}
|
|
|
|
export function getStoreDAO() {
|
|
if (!!window.localStorage) {
|
|
return new StoreDAO(window.localStorage);
|
|
} else {
|
|
return new DummyStoreDAO();
|
|
}
|
|
} |