Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.handleArtistAnswerClick = (isAnswerCorrect) => {
- this.onQuestionAnswered(isAnswerCorrect);
- this.showScreen();
- };
- this.handleGenreAnswerClick = () => {
- const isAnswerCorrect = this.checkGenreAnswers(this.view.answers);
- this.onQuestionAnswered(isAnswerCorrect);
- this.showScreen();
- };
- Application.model.loadStats(adapter)
- .then((stats) => state.setStats(stats));
- }
- init() {
- state.resetGame();
- timer.init(this.onTimerFinished);
- this.nextQuestion();
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement