-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.js
63 lines (63 loc) · 2.62 KB
/
data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var persistentData = {
init: function() {
// Initialize items if none are currently found
if (localStorage.getItem('scores') == null) {
this.clear();
}
},
clear: function() {
// Clear out the scores and last play time
localStorage.setItem('scores', '0,0,0');
localStorage.setItem('opponent_scores', '0,0,0');
localStorage.setItem('last_play', 'Never');
},
getScores: function() {
var scores = localStorage.getItem('scores').split(',');
return {win: Number(scores[0]), loss: Number(scores[1]), tie: Number(scores[2])};
},
getOpponentScores: function() {
var scores = localStorage.getItem('opponent_scores').split(',');
return {win: Number(scores[0]), loss: Number(scores[1]), tie: Number(scores[2])};
},
getLastPlay: function() {
return localStorage.getItem('last_play');
},
setLastPlay: function() {
localStorage.setItem('last_play', new Date().toLocaleString());
},
hasNeverPlayed: function() {
return this.getLastPlay() == "Never";
},
setScores: function(scores) {
localStorage.setItem('scores', [scores.win, scores.loss, scores.tie].join(','));
},
setOpponentScores: function(scores) {
localStorage.setItem('opponent_scores', [scores.win, scores.loss, scores.tie].join(','));
},
modifyScores: function(win, loss, tie) {
// Get both the user scores and the opponent scores
var scores = this.getScores();
var opponentScores = this.getOpponentScores();
// User scores a win will increase the scores and increase the opponent losses
scores.win = scores.win + win;
opponentScores.loss = opponentScores.loss + win;
// Vice versa with the losses increasing the opponent wins
scores.loss = scores.loss + loss;
opponentScores.win = opponentScores.win + loss;
// And, a tie is a tie for both
scores.tie = scores.tie + tie;
opponentScores.tie = opponentScores.tie + tie;
// Update both of the scores
this.setScores(scores);
this.setOpponentScores(opponentScores);
},
// Debug function which dumps information to the console
dump: function() {
var scores = this.getScores();
var oppScores = this.getOpponentScores();
var lastPlay = this.getLastPlay();
console.log('Player scores, W:' + scores.win + ' L:' + scores.loss + ' T:' + scores.tie);
console.log('Opponent scores, W:' + oppScores.win + ' L:' + oppScores.loss + ' T:' + oppScores.tie);
console.log('Last played: ' + lastPlay);
}
}