import * as ScorebarActions from '../../actions/board/scorebar.actions'; import SETTINGS from '../../AppSettings'; const stored = JSON.parse(localStorage.getItem(SETTINGS.LOCAL_STORAGE_KEY)) || [{ score: 0 }]; let dispatch; let lives = SETTINGS.LIVES; let currentScore = 0; let previousHighScore = stored[0].score; let highScore = stored[0].score; let gameOver = false; const ScorebarCtrl = { setDispatch: d => dispatch = d, getLives: () => lives, getCurrentScore: () => currentScore, isGameOver: () => gameOver, update: () => { // Note that loss of points may cause new high to drop below previous. if (currentScore > previousHighScore) { highScore = currentScore; } else { highScore = previousHighScore; } dispatch(ScorebarActions.update(lives, currentScore, highScore)); }, munchSucceeded: () => { currentScore += 10; ScorebarCtrl.update(); }, munchFailed: () => { lives--; currentScore -= 5; ScorebarCtrl.update(); }, eatenByTroggle: () => { lives--; ScorebarCtrl.update(); }, levelUp: (level) => { currentScore += 25; if (level % 3 === 0) { lives++; } ScorebarCtrl.update(); }, gameOver: () => { gameOver = true; }, reset: () => { lives = SETTINGS.LIVES; currentScore = 0; gameOver = false; ScorebarCtrl.update(); } }; export default ScorebarCtrl;