Skip to content

Instantly share code, notes, and snippets.

@dmitru
Last active June 29, 2017 07:13
Show Gist options
  • Save dmitru/1e38155efd8c01bfa54278c86423d6bf to your computer and use it in GitHub Desktop.
Save dmitru/1e38155efd8c01bfa54278c86423d6bf to your computer and use it in GitHub Desktop.
import Application from '../application';
import renderScreen from '../utils/render-screen';
import computePercentage from '../utils/compute-percentage';
import timer from './timer-presenter';
import state from '../data/state';
import adapter from '../data/adapter';
import LevelArtistView from '../view/level-artist-view';
import LevelGenreView from '../view/level-genre-view';
const Screen = {
GAME_ARTIST: `artist`,
GAME_GENRE: `genre`,
SUCCESS: `result-success`,
FAIL: `result-fail`
};
export default class GamePresenter {
constructor(data) {
this.questionData = data;
this.handleGenreAnswerClick = this.handleGenreAnswerClick.bind(this);
// the same for handleArtistAnswerClick...
Application.model.loadStats(adapter)
.then((stats) => state.setStats(stats));
}
init() {
state.resetGame();
timer.init(this.onTimerFinished);
this.nextQuestion();
this.showScreen();
}
handleGenreAnswerClick() {
const isAnswerCorrect = this.checkGenreAnswers(this.view.answers);
this.onQuestionAnswered(isAnswerCorrect);
this.showScreen();
}
showScreen() {
switch (state.screen) {
case Screen.GAME_ARTIST:
this.view = new LevelArtistView(state.screenData);
this.view.onAnswerClick = this.handleArtistAnswerClick;
renderScreen(this.view);
break;
case Screen.GAME_GENRE:
this.view = new LevelGenreView(state.screenData);
this.view.onAnswerClick = this.handleGenreAnswerClick;
renderScreen(this.view);
break;
case Screen.SUCCESS:
timer.stopTimer();
Application.showStats(state.score, state.screenData);
break;
case Screen.FAIL:
timer.stopTimer();
Application.showFail();
break;
}
}
nextQuestion() {
const newScreen = this.questionData[state.currentQuestion].type;
const screenData = this.questionData[state.currentQuestion];
state.nextQuestion();
state.updateAnswerTime(state.timePassed);
return state.setGameScreen(newScreen, screenData);
}
onQuestionAnswered(isAnswerCorrect) {
const isFinalQuestion = state.currentQuestion === state.questions;
state.updateAnswerTime(state.timePassed - state.answerTime);
if (isAnswerCorrect === `true` && state.answerTime < 10) {
state.setScore(2);
} else if (isAnswerCorrect === `true`) {
state.setScore(1);
}
const newLives = (isAnswerCorrect === `true`) ? state.lives : state.lives - 1;
if (newLives === 0) {
return state.setGameScreen(Screen.FAIL);
}
if (isFinalQuestion) {
const data = {
time: state.timePassed,
score: state.score
};
Application.model.sendStats(data, adapter);
const percentage = computePercentage({
time: state.timePassed,
score: state.score,
statistics: state.stats,
});
return state.setGameScreen(Screen.SUCCESS, percentage);
}
this.nextQuestion();
return state.setLives(newLives);
}
checkGenreAnswers(answers) {
const correctAnswers = [];
const trueGenre = state.screenData.genre;
answers.forEach((answer) => {
if (answer === trueGenre) {
correctAnswers.push(true);
} else {
correctAnswers.push(false);
}
});
return (correctAnswers.indexOf(false) !== -1) ? `false` : `true`;
}
onTimerFinished() {
Application.showFail();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment