-
Notifications
You must be signed in to change notification settings - Fork 0
/
scores.js
187 lines (154 loc) · 3.75 KB
/
scores.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// create and update browser local storage
window.scoreStore = {
// players data
players: JSON.parse(localStorage.getItem('score-store') || '[]'),
//save / update data
save() {
this.players.sort(function (a, b) {
return parseFloat(b.percentage) - parseFloat(a.percentage);
}),
localStorage.setItem('score-store', JSON.stringify(this.players));
}
};
// main game functionality
window.game = function () {
return {
// import local storage data
...scoreStore,
// new player name
newPlayer: '',
//editing player
editedPlayer: null,
// first selected player
firstPlayer: null,
get playerOne() {
return this.players.find(obj => obj.id === parseInt(this.firstPlayer));
},
// second selected player
secondPlayer : null,
get playerTwo() {
return this.players.find(obj => obj.id === parseInt(this.secondPlayer));
},
//game options
options : {
rounds: 5,
margin: 2,
get roundsForWin() {
return this.rounds;
},
get marginForWin() {
return this.margin;
},
set roundsForWin(rounds) {
this.rounds = parseInt(rounds);
},
set marginForWin(margin) {
this.margin = parseInt(margin);
}
},
// current round score data
currentRoundScore: {
first: 0,
second: 0,
get firstScore() {
return this.first;
},
get secondScore() {
return this.second;
},
set firstScore(score) {
this.first = parseInt(score);
},
set secondScore(score) {
this.second = parseInt(score);
}
},
// create new player
addPlayer() {
if (!this.newPlayer) {
return;
}
this.players.push({
id: Date.now(),
name: this.newPlayer,
games: 0,
wins: 0,
percentage: 0.000,
});
this.save();
this.newPlayer = '';
},
// edit player name
editPlayer(player) {
player.cachedName = player.name;
this.editedPlayer = player;
},
// complete player editing
editComplete(player) {
if (player.name === '') {
return this.deletePlayer(player);
}
this.editedPlayer = null;
this.save();
flash({
text: 'Player updated',
type: 'success'
});
},
// delete player
deletePlayer(player) {
let position = this.players.indexOf(player);
this.players.splice(position, 1);
this.save();
flash({
text: 'Player deleted!',
type: 'failure'
});
},
// check if winning criteria are met
checkScores() {
if ( (this.currentRoundScore.first >= this.options.rounds)
&& (this.currentRoundScore.first - this.currentRoundScore.second) >= this.options.margin ) {
return this.declareWinner(this.playerOne, this.playerTwo);
}
if( (this.currentRoundScore.second >= this.options.rounds)
&& (this.currentRoundScore.second - this.currentRoundScore.first) >= this.options.margin ) {
return this.declareWinner(this.playerTwo, this.playerOne);
}
},
// save winner and loser game data
async declareWinner(winner, loser) {
flash({
text: winner.name + ' won the round!',
type: 'success'
});
await pause();
winner.games++;
winner.wins++;
this.calculatePercentage(winner);
loser.games++;
this.calculatePercentage(loser);
this.save();
this.resetCurrentRoundScore();
},
// calculate player winning percentage data
calculatePercentage(player) {
player.percentage = (parseFloat(player.wins) / parseFloat(player.games)).toFixed(3);
},
// reset current round score
resetCurrentRoundScore() {
this.currentRoundScore.first = 0;
this.currentRoundScore.second = 0;
}
}
}
// pop up flash messages
window.flash = function (message) {
window.dispatchEvent(new CustomEvent('flash', {
detail: { message }
}));
}
// pause execution for 1s
window.pause = function (milliseconds = 1000) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}