You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

51 lines
1.4 KiB

import * as HighScoreActions from '../../actions/high-score/high-score.actions';
import * as ModeActions from '../../actions/mode.actions';
import InitialsCtrl from './initials.controller';
import SETTINGS from '../../AppSettings';
let dispatch;
let found = -1;
let score = 0;
const HighScoreCtrl = {
setDispatch: (d) => dispatch = d,
updateScore: (finalScore) => {
score = finalScore;
dispatch(HighScoreActions.updateScore(finalScore));
},
checkForHighScore: () => {
const scores = JSON.parse(localStorage.getItem(SETTINGS.LOCAL_STORAGE_KEY));
for (let i = 0; i < scores.length; i++) {
if (scores[i].score < score) {
found = i;
break;
}
}
if (found === -1) {
dispatch(ModeActions.welcome());
}
},
keydown: (e) => {
if (e.keyCode === 13 || e.keyCode === 32) {
let scores = JSON.parse(localStorage.getItem(SETTINGS.LOCAL_STORAGE_KEY));
const initials = InitialsCtrl.getInitials();
scores.splice(found, 0, { initials: initials, score: score });
scores.pop();
localStorage.setItem(SETTINGS.LOCAL_STORAGE_KEY, JSON.stringify(scores));
found = -1;
score = 0;
dispatch(ModeActions.welcome());
}
}
};
export default HighScoreCtrl;