Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. import Application from '../application';
  2.  
  3. import renderScreen from '../utils/render-screen';
  4. import computePercentage from '../utils/compute-percentage';
  5. import timer from './timer-presenter';
  6.  
  7. import state from '../data/state';
  8. import adapter from '../data/adapter';
  9.  
  10. import LevelArtistView from '../view/level-artist-view';
  11. import LevelGenreView from '../view/level-genre-view';
  12.  
  13. const Screen = {
  14. GAME_ARTIST: `artist`,
  15. GAME_GENRE: `genre`,
  16. SUCCESS: `result-success`,
  17. FAIL: `result-fail`
  18. };
  19.  
  20. export default class GamePresenter {
  21. constructor(data) {
  22. this.questionData = data;
  23.  
  24. this.handleArtistAnswerClick = (isAnswerCorrect) => {
  25. this.onQuestionAnswered(isAnswerCorrect);
  26. this.showScreen();
  27. };
  28.  
  29. this.handleGenreAnswerClick = () => {
  30. const isAnswerCorrect = this.checkGenreAnswers(this.view.answers);
  31. this.onQuestionAnswered(isAnswerCorrect);
  32. this.showScreen();
  33. };
  34.  
  35. Application.model.loadStats(adapter)
  36. .then((stats) => state.setStats(stats));
  37. }
  38.  
  39. init() {
  40. state.resetGame();
  41. timer.init(this.onTimerFinished);
  42. this.nextQuestion();
  43. this.showScreen();
  44. }
  45.  
  46. showScreen() {
  47. switch (state.screen) {
  48.  
  49. case Screen.GAME_ARTIST:
  50. this.view = new LevelArtistView(state.screenData);
  51. this.view.onAnswerClick = this.handleArtistAnswerClick;
  52. renderScreen(this.view);
  53. break;
  54.  
  55. case Screen.GAME_GENRE:
  56. this.view = new LevelGenreView(state.screenData);
  57. this.view.onAnswerClick = this.handleGenreAnswerClick;
  58. renderScreen(this.view);
  59. break;
  60.  
  61. case Screen.SUCCESS:
  62. timer.stopTimer();
  63. Application.showStats(state.score, state.screenData);
  64. break;
  65.  
  66. case Screen.FAIL:
  67. timer.stopTimer();
  68. Application.showFail();
  69. break;
  70. }
  71. }
  72.  
  73. nextQuestion() {
  74. const newScreen = this.questionData[state.currentQuestion].type;
  75. const screenData = this.questionData[state.currentQuestion];
  76.  
  77. state.nextQuestion();
  78. state.updateAnswerTime(state.timePassed);
  79.  
  80. return state.setGameScreen(newScreen, screenData);
  81. }
  82.  
  83. onQuestionAnswered(isAnswerCorrect) {
  84.  
  85. const isFinalQuestion = state.currentQuestion === state.questions;
  86. state.updateAnswerTime(state.timePassed - state.answerTime);
  87. if (isAnswerCorrect === `true` && state.answerTime < 10) {
  88. state.setScore(2);
  89. } else if (isAnswerCorrect === `true`) {
  90. state.setScore(1);
  91. }
  92.  
  93. const newLives = (isAnswerCorrect === `true`) ? state.lives : state.lives - 1;
  94.  
  95. if (newLives === 0) {
  96.  
  97. return state.setGameScreen(Screen.FAIL);
  98. }
  99.  
  100. if (isFinalQuestion) {
  101.  
  102. const data = {
  103. time: state.timePassed,
  104. score: state.score
  105. };
  106.  
  107. Application.model.sendStats(data, adapter);
  108.  
  109. const percentage = computePercentage({
  110. time: state.timePassed,
  111. score: state.score,
  112. statistics: state.stats,
  113. });
  114.  
  115. return state.setGameScreen(Screen.SUCCESS, percentage);
  116. }
  117.  
  118. this.nextQuestion();
  119. return state.setLives(newLives);
  120. }
  121.  
  122.  
  123. checkGenreAnswers(answers) {
  124. const correctAnswers = [];
  125. const trueGenre = state.screenData.genre;
  126.  
  127. answers.forEach((answer) => {
  128. if (answer === trueGenre) {
  129. correctAnswers.push(true);
  130. } else {
  131. correctAnswers.push(false);
  132. }
  133. });
  134.  
  135. return (correctAnswers.indexOf(false) !== -1) ? `false` : `true`;
  136. }
  137.  
  138. onTimerFinished() {
  139. Application.showFail();
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement