parent
7bfab9163b
commit
09868fc8d1
12 changed files with 105 additions and 187 deletions
@ -0,0 +1,15 @@ |
||||
export const HIGHSCORE_ACTION = 'HIGHSCORE_ACTION'; |
||||
export const UPDATE_INITIALS = 'HIGHSCORE_UPDATE_INITIALS'; |
||||
export const UPDATE_SCORE = 'HIGHSCORE_UPDATE_SCORE'; |
||||
|
||||
export const updateInitials = (initials) => ({ |
||||
type: HIGHSCORE_ACTION, |
||||
action: UPDATE_INITIALS, |
||||
initials: initials |
||||
}); |
||||
|
||||
export const updateScore = (score) => ({ |
||||
type: HIGHSCORE_ACTION, |
||||
action: UPDATE_SCORE, |
||||
score: score |
||||
}); |
@ -1,71 +1,53 @@ |
||||
import * as WelcomeActions from '../../actions/welcome/welcome.actions'; |
||||
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; |
||||
|
||||
let finalScore = 800; |
||||
|
||||
const scores = [ |
||||
{ initials: "AAA", score: "0" }, |
||||
{ initials: "AAA", score: "0" }, |
||||
{ initials: "AAA", score: "0" }, |
||||
{ initials: "AAA", score: "0" }, |
||||
{ initials: "AAA", score: "0" }, |
||||
]; |
||||
|
||||
const WelcomeCtrl = { |
||||
const HighScoreCtrl = { |
||||
setDispatch: (d) => dispatch = d, |
||||
|
||||
isHighScore: () => finalScore > 0, |
||||
updateScore: (finalScore) => { |
||||
score = finalScore; |
||||
dispatch(HighScoreActions.updateScore(finalScore)); |
||||
}, |
||||
|
||||
gameOver: (score) => { |
||||
let found = -1; |
||||
finalScore = -1; |
||||
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; |
||||
} |
||||
} |
||||
|
||||
finalScore = -1; |
||||
if (found > -1) { |
||||
// scores.splice(found, { initials: 'ZZZ', score: score });
|
||||
// scores.pop();
|
||||
finalScore = score; |
||||
} |
||||
}, |
||||
|
||||
update: () => { |
||||
const scores = localStorage.getItem(SETTINGS.LOCAL_STORAGE_KEY); |
||||
console.log(found) |
||||
|
||||
if (scores !== null) { |
||||
dispatch(WelcomeActions.updateScores(scores)); |
||||
if (found === -1) { |
||||
dispatch(ModeActions.welcome()); |
||||
} |
||||
}, |
||||
|
||||
keydown: (e) => { |
||||
if ((gameOverFlag === true && e.keyCode === 13) || (gameOverFlag === true && e.keyCode === 32)) { |
||||
} |
||||
else if (gameOverFlag === true) { |
||||
InitialsCtrl.keydown(e); |
||||
} |
||||
else if (e.keyCode === 32) { |
||||
this.props.dispatch(ModeActions.options()); |
||||
} |
||||
}, |
||||
if (e.keyCode === 13 || e.keyCode === 32) { |
||||
let scores = JSON.parse(localStorage.getItem(SETTINGS.LOCAL_STORAGE_KEY)); |
||||
const initials = InitialsCtrl.getInitials(); |
||||
|
||||
doTheThing: () => { |
||||
const scores = localStorage.getItem(SETTINGS.LOCAL_STORAGE_KEY); |
||||
scores.splice(found, 0, { initials: initials, score: score }); |
||||
scores.pop(); |
||||
localStorage.setItem(SETTINGS.LOCAL_STORAGE_KEY, JSON.stringify(scores)); |
||||
|
||||
if (scores !== null) { |
||||
return scores[0].score; |
||||
} |
||||
found = -1; |
||||
score = 0; |
||||
|
||||
return 0; |
||||
// localStorage.setItem(SETTINGS.LOCAL_STORAGE_KEY, scores);
|
||||
}, |
||||
dispatch(ModeActions.welcome()); |
||||
} |
||||
} |
||||
}; |
||||
|
||||
export default WelcomeCtrl; |
||||
export default HighScoreCtrl; |
||||
|
Loading…
Reference in new issue