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 InitialsCtrl from './initials.controller'; |
||||||
import SETTINGS from '../../AppSettings'; |
import SETTINGS from '../../AppSettings'; |
||||||
|
|
||||||
let dispatch; |
let dispatch; |
||||||
|
let found = -1; |
||||||
|
let score = 0; |
||||||
|
|
||||||
let finalScore = 800; |
const HighScoreCtrl = { |
||||||
|
|
||||||
const scores = [ |
|
||||||
{ initials: "AAA", score: "0" }, |
|
||||||
{ initials: "AAA", score: "0" }, |
|
||||||
{ initials: "AAA", score: "0" }, |
|
||||||
{ initials: "AAA", score: "0" }, |
|
||||||
{ initials: "AAA", score: "0" }, |
|
||||||
]; |
|
||||||
|
|
||||||
const WelcomeCtrl = { |
|
||||||
setDispatch: (d) => dispatch = d, |
setDispatch: (d) => dispatch = d, |
||||||
|
|
||||||
isHighScore: () => finalScore > 0, |
updateScore: (finalScore) => { |
||||||
|
score = finalScore; |
||||||
|
dispatch(HighScoreActions.updateScore(finalScore)); |
||||||
|
}, |
||||||
|
|
||||||
gameOver: (score) => { |
checkForHighScore: () => { |
||||||
let found = -1; |
const scores = JSON.parse(localStorage.getItem(SETTINGS.LOCAL_STORAGE_KEY)); |
||||||
finalScore = -1; |
|
||||||
|
|
||||||
for (let i = 0; i < scores.length; i++) { |
for (let i = 0; i < scores.length; i++) { |
||||||
if (scores[i].score < score) { |
if (scores[i].score < score) { |
||||||
found = i; |
found = i; |
||||||
|
break; |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
finalScore = -1; |
console.log(found) |
||||||
if (found > -1) { |
|
||||||
// scores.splice(found, { initials: 'ZZZ', score: score });
|
|
||||||
// scores.pop();
|
|
||||||
finalScore = score; |
|
||||||
} |
|
||||||
}, |
|
||||||
|
|
||||||
update: () => { |
|
||||||
const scores = localStorage.getItem(SETTINGS.LOCAL_STORAGE_KEY); |
|
||||||
|
|
||||||
if (scores !== null) { |
if (found === -1) { |
||||||
dispatch(WelcomeActions.updateScores(scores)); |
dispatch(ModeActions.welcome()); |
||||||
} |
} |
||||||
}, |
}, |
||||||
|
|
||||||
keydown: (e) => { |
keydown: (e) => { |
||||||
if ((gameOverFlag === true && e.keyCode === 13) || (gameOverFlag === true && e.keyCode === 32)) { |
if (e.keyCode === 13 || e.keyCode === 32) { |
||||||
} |
let scores = JSON.parse(localStorage.getItem(SETTINGS.LOCAL_STORAGE_KEY)); |
||||||
else if (gameOverFlag === true) { |
const initials = InitialsCtrl.getInitials(); |
||||||
InitialsCtrl.keydown(e); |
|
||||||
} |
|
||||||
else if (e.keyCode === 32) { |
|
||||||
this.props.dispatch(ModeActions.options()); |
|
||||||
} |
|
||||||
}, |
|
||||||
|
|
||||||
doTheThing: () => { |
scores.splice(found, 0, { initials: initials, score: score }); |
||||||
const scores = localStorage.getItem(SETTINGS.LOCAL_STORAGE_KEY); |
scores.pop(); |
||||||
|
localStorage.setItem(SETTINGS.LOCAL_STORAGE_KEY, JSON.stringify(scores)); |
||||||
|
|
||||||
if (scores !== null) { |
found = -1; |
||||||
return scores[0].score; |
score = 0; |
||||||
} |
|
||||||
|
|
||||||
return 0; |
dispatch(ModeActions.welcome()); |
||||||
// localStorage.setItem(SETTINGS.LOCAL_STORAGE_KEY, scores);
|
} |
||||||
}, |
} |
||||||
}; |
}; |
||||||
|
|
||||||
export default WelcomeCtrl; |
export default HighScoreCtrl; |
||||||
|
Loading…
Reference in new issue