Skip to content

Commit e2fba87

Browse files
committed
Add some tests for game
1 parent 2b40203 commit e2fba87

File tree

5 files changed

+1080
-20
lines changed

5 files changed

+1080
-20
lines changed

lib/game.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
2-
3-
module.exports = (getBallLevel) => {
1+
module.exports = (getBallLevel, sleepFunction) => {
42
getBallLevel = getBallLevel || (level => Math.floor(Math.random() * level))
3+
sleepFunction = sleepFunction || (cb => setTimeout(cb, 300))
54

65
const thresholds = [0, 0, 5, 30, 90, 150, 230, 280, 350, 450]
76
var blocksKilled = 0
87
var updateCallback = () => {}
8+
var queuedUpdate = null
99
var dotsId = 0
1010
var state = {
1111
dots: [],
@@ -30,7 +30,11 @@ module.exports = (getBallLevel) => {
3030

3131
function setState (newState) {
3232
state = Object.assign({}, state, newState)
33-
updateCallback(newState)
33+
if (queuedUpdate) return
34+
queuedUpdate = setTimeout(() => {
35+
updateCallback(state)
36+
queuedUpdate = null
37+
})
3438
}
3539

3640
function newDot (x, level) {
@@ -52,7 +56,6 @@ module.exports = (getBallLevel) => {
5256
return
5357
}
5458
setState({
55-
...state,
5659
floatingDots: state.floatingDots.map(d => {
5760
return {
5861
...d,
@@ -142,15 +145,14 @@ module.exports = (getBallLevel) => {
142145
}
143146
}
144147

145-
const newDots = dots.filter(d => keep[d.x][d.y]).concat(freshDots)
146-
return newDots
148+
return dots.filter(d => keep[d.x][d.y]).concat(freshDots)
147149
}
148150

149151
function updateScore (score) {
150-
if (typeof window === 'undefined') return
151152
const highScore = Math.max(score, state.highScore)
153+
setState({ score, highScore })
154+
if (typeof window === 'undefined') return
152155
window.localStorage['score'] = highScore
153-
setState({ highScore, score })
154156
}
155157

156158
function moveDotsRight () {
@@ -188,19 +190,17 @@ module.exports = (getBallLevel) => {
188190
}
189191
})
190192
setState({
191-
...state,
192193
dots: inactiveDots.concat(newDots),
193194
floatingDots: []
194195
})
195-
setTimeout(() => cleanupBoard([], state.dots), 300)
196+
sleepFunction(() => cleanupBoard([], state.dots))
196197
}
197198

198199
function cleanupBoard (oldRun, newRun) {
199200
oldRun = newRun
200201
newRun = cleanupDots(oldRun)
201202
const level = thresholds.findIndex(l => l > blocksKilled)
202203
setState({
203-
...state,
204204
level: level < 0 ? colors.length - 1 : level,
205205
dots: newRun
206206
})
@@ -213,18 +213,16 @@ module.exports = (getBallLevel) => {
213213
})
214214
}
215215
return setState({
216-
...state,
217216
floatingDots: [newDot(4), newDot(5)]
218217
})
219218
}
220-
setTimeout(() => {
219+
sleepFunction(() => {
221220
newRun = settleDots(newRun)
222221
setState({
223-
...state,
224222
dots: newRun
225223
})
226-
setTimeout(() => cleanupBoard(oldRun, newRun), 300)
227-
}, 300)
224+
sleepFunction(() => cleanupBoard(oldRun, newRun))
225+
})
228226
}
229227

230228
function rotateDots () {
@@ -287,7 +285,6 @@ module.exports = (getBallLevel) => {
287285
}))
288286
}
289287
setState({
290-
...state,
291288
floatingDots: newFloatingDots
292289
})
293290
}

0 commit comments

Comments
 (0)