Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.46 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import { translate } from 'react-i18next';
  3. import PropTypes from 'prop-types';
  4. import { Progress } from 'react-sweet-progress';
  5. import WellDonePage from '../WellDonePage/index'
  6. import WrongAnsweredPage from '../WrongAnsweredPage/index'
  7. import QuestionVideoPage from '../QuestionVideoPage/index'
  8. import config from '../../config'
  9. import {QUESTION_CLOSE, QUESTION_ONLINE} from "../../containers/Home/constants"
  10. import PlayersCount from "../../components/PlayersCount/index";
  11. import QuestionAt from '../../components/QuestionAt'
  12. import QuestionSpectating from '../../components/QuestionSpectating';
  13. import Result from '../../components/Result';
  14. import "react-sweet-progress/lib/style.css";
  15. import {calcPercent, calcPercentOfOption, getCurrentTimestamp} from '../../utils/helper';
  16. import {QUESTION_SHOW, QUESTION_RESULT, QUESTION_SPECTATING, QUESTION_VIDEO, QUESTION_WAIT} from "./constants";
  17. import {RESULT_CORRECT, RESULT_DEFAULT, RESULT_INCORRECT} from "../../components/Result/constants";
  18. import QuestionTieBreakPage from '../QuestionTieBreakPage';
  19. import {TIEBREAK} from '../QuestionTieBreakPage/constants';
  20.  
  21. let timer;
  22. let countDownTimer;
  23.  
  24. const getQuestionTimes = (question) => {
  25. let tQuestionOnline = getCurrentTimestamp();
  26. let tQuestionShow = 0;
  27. let tQuestionResults = 0;
  28. let tQuestionNext = 0;
  29. let tQuestionEnd = 0;
  30.  
  31. if (question.status === QUESTION_ONLINE) {
  32. tQuestionShow = tQuestionOnline + question.countup * 1000;
  33. tQuestionResults = tQuestionShow + question.countdown * 1000;
  34. tQuestionNext = tQuestionResults + config.showResultSeconds * 1000;
  35. tQuestionEnd = tQuestionNext + config.waitNextQuestionCountdown * 1000;
  36. }
  37.  
  38. return {
  39. tQuestionOnline, tQuestionShow, tQuestionResults, tQuestionNext, tQuestionEnd
  40. }
  41. };
  42.  
  43. class QuestionPage extends Component {
  44.  
  45. constructor(props) {
  46. super(props);
  47.  
  48. const { question, isSpectating } = props;
  49.  
  50. let tQuestions = getQuestionTimes(question);
  51. console.log("loaded question first. question status: ", question.status);
  52. console.log("calculate question times: ", tQuestions);
  53.  
  54. this.state = {
  55. ...tQuestions,
  56. questionStatus: question.status,
  57. questionText: question.text,
  58. questionCountDown: question.countdown,
  59. questionUuid: question.uuid,
  60. questionAnswers: question.answers || [],
  61. questionPicture: question.picture,
  62. countdown: question.countdown,
  63. selectedAnswer: -1,
  64. isCorrect: 2,
  65. isAnswered: false,
  66. isQuestionFailed: false,
  67. showedResult: false,
  68. countup: 0,
  69. isSpectating,
  70. currentPage: isSpectating ? QUESTION_SPECTATING : QUESTION_VIDEO,
  71. isUsedFeebies: false,
  72. isFirstQuestion: true,
  73. isPresented: false,
  74. }
  75. }
  76.  
  77. static getDerivedStateFromProps (nextProps, prevState) {
  78.  
  79. const { question } = nextProps;
  80.  
  81. if (prevState.questionStatus === QUESTION_CLOSE && question.status === QUESTION_ONLINE ) {
  82. let tQuestions = getQuestionTimes(question);
  83.  
  84. console.log("calculate question times: ", tQuestions);
  85. console.log('time: ', getCurrentTimestamp() ,'currentPage: ', QUESTION_VIDEO);
  86.  
  87. return {
  88. ...tQuestions,
  89. questionText: question.text,
  90. questionPicture: question.picture,
  91. questionCountDown: question.countdown,
  92. questionUuid: question.uuid,
  93. questionStatus: question.status,
  94. questionAnswers: question.answers,
  95. currentPage: QUESTION_VIDEO,
  96. countdown: question.countdown, // question countdown.
  97. countup: 0, // Video play countup.
  98. selectedAnswer: -1,
  99. isAnswered: false,
  100. isCorrect: 2,
  101. showedResult: false,
  102. isSpectating: prevState.isSpectating || !prevState.isCorrect || nextProps.isSpectating,
  103. isUsedFeebies: false,
  104. isFirstQuestion: false
  105. }
  106. } else {
  107. return {
  108. questionStatus: question.status,
  109. }
  110. }
  111. }
  112.  
  113.  
  114.  
  115. componentDidMount() {
  116. const __this = this;
  117.  
  118. timer = window.setInterval(function () {
  119. __this.timerAction();
  120. }, 1000)
  121. }
  122.  
  123. logTimeAndPage = (currentPage) => {
  124. console.log('time: ', getCurrentTimestamp() ,'currentPage: ', currentPage);
  125. };
  126.  
  127. timerAction() {
  128. const __this = this;
  129. let currentTime = getCurrentTimestamp();
  130.  
  131. const { tQuestionShow, tQuestionResults, tQuestionNext, tQuestionEnd } = this.state;
  132.  
  133. if (this.state.currentPage !== QUESTION_SPECTATING) {
  134.  
  135. if (currentTime <= tQuestionShow) {
  136.  
  137. if (this.props.question.i === 1 && !this.state.isPresented) {
  138.  
  139. console.log('time: ', getCurrentTimestamp() ,'currentPage: ', QUESTION_VIDEO);
  140.  
  141. __this.props.setOscPresence();
  142.  
  143. this.setState({currentPage: QUESTION_VIDEO, isPresented: true});
  144.  
  145. } else if (this.state.currentPage !== QUESTION_VIDEO) {
  146.  
  147. this.setState({currentPage: QUESTION_VIDEO});
  148. }
  149.  
  150. } else if (currentTime > tQuestionShow && currentTime <= tQuestionResults) {
  151.  
  152.  
  153. if (this.state.currentPage !== QUESTION_SHOW) {
  154.  
  155. this.logTimeAndPage(QUESTION_SHOW);
  156.  
  157. this.setState({currentPage: QUESTION_SHOW});
  158.  
  159. let interval = 1000 * this.state.questionCountDown / (this.state.questionCountDown + 1);
  160.  
  161. countDownTimer = window.setInterval(function () {
  162.  
  163. if (__this.state.countdown > 0) {
  164.  
  165. const countdown = __this.state.countdown - 1;
  166. __this.setState({countdown})
  167.  
  168. }
  169.  
  170. }, interval);
  171. }
  172.  
  173. } else if (currentTime > tQuestionResults && currentTime <= tQuestionNext) {
  174.  
  175. if (this.state.currentPage !== QUESTION_RESULT && this.props.question.correct !== undefined ) {
  176.  
  177. this.logTimeAndPage(QUESTION_RESULT);
  178.  
  179. clearInterval(countDownTimer);
  180.  
  181. this.setState({
  182. currentPage: QUESTION_RESULT,
  183. isAnswered: true,
  184. isCorrect: parseInt(this.state.selectedAnswer, 10) === this.props.question.correct ? 1 : 0
  185. });
  186. } else {
  187. this.setState({
  188. isAnswered: true
  189. })
  190. }
  191.  
  192. } else if (currentTime > tQuestionNext && currentTime <= tQuestionEnd) {
  193.  
  194. if (this.state.currentPage !== QUESTION_WAIT && this.props.question.nQ !== this.props.question.i) {
  195.  
  196. if (this.state.isCorrect === 0) {
  197. this.setState({isQuestionFailed: true});
  198. }
  199.  
  200. this.logTimeAndPage(QUESTION_WAIT);
  201. this.setState({currentPage: QUESTION_WAIT});
  202. }
  203. }
  204. }
  205. }
  206.  
  207. componentWillUnmount() {
  208. clearInterval(timer);
  209. clearInterval(countDownTimer);
  210. }
  211.  
  212. goToVideo = () => {
  213. this.setState({currentPage: QUESTION_VIDEO});
  214. };
  215.  
  216. clickAnswer = (answer) => {
  217.  
  218. if (this.state.isAnswered || this.state.isQuestionFailed || this.state.isSpectating) return;
  219.  
  220. this.props.selectAnswer(answer, this.state.questionUuid);
  221.  
  222. this.setState({
  223. selectedAnswer: answer,
  224. isAnswered: true,
  225. });
  226. };
  227.  
  228. useFreebies = () => {
  229. this.setState({isQuestionFailed: false, isCorrect: true, isUsedFreebies: true});
  230. this.props.useFreebies();
  231. };
  232.  
  233. continueWatching = () => {
  234. console.log('continue watching');
  235. };
  236.  
  237. startSpectating = () => {
  238. this.setState({isSpectating: true});
  239. };
  240.  
  241. render() {
  242. const { question, playersCount, t } = this.props;
  243. const { countdown, selectedAnswer, questionAnswers, isCorrect, isSpectating, currentPage, questionPicture, questionCountDown, questionText, isUsedFeebies, isFirstQuestion } = this.state;
  244.  
  245. if( question.type === TIEBREAK )
  246. return <QuestionTieBreakPage
  247. question={question}
  248. isSpectating={false}
  249. />
  250.  
  251. if (isSpectating && isFirstQuestion) {
  252. return <QuestionSpectating
  253. playersCount={playersCount}
  254. survivors={question.survivors}
  255. />
  256. }
  257.  
  258. if(currentPage === QUESTION_VIDEO) {
  259.  
  260. return <QuestionVideoPage
  261. questionCount={question.nQ}
  262. questionIndex={question.i}
  263. playersCount={playersCount}/>
  264.  
  265. }
  266.  
  267. if(currentPage === QUESTION_WAIT) {
  268.  
  269. if ( isSpectating ) {
  270.  
  271. return <QuestionSpectating
  272. playersCount={playersCount}
  273. survivors={question.survivors}
  274. />
  275.  
  276. } else {
  277.  
  278. if (isCorrect === 1 && !isUsedFeebies){
  279.  
  280. return <WellDonePage
  281. countdown={config.waitNextQuestionCountdown}
  282. survivors={question.survivors}
  283. goToVideo={this.goToVideo}
  284. />
  285.  
  286. } else {
  287.  
  288. return <WrongAnsweredPage
  289. useFreebies={this.useFreebies}
  290. continueWatching={this.continueWatching}
  291. questionIndex={this.props.question.i}
  292. questionCount={question.nQ}
  293. playersCount={this.props.question.survivors}
  294. countdown={config.waitNextQuestionCountdown}
  295. goToVideo={this.goToVideo}
  296. />
  297.  
  298. }
  299. }
  300. }
  301.  
  302. const percent = calcPercent(questionCountDown, this.state.countdown);
  303.  
  304. return (<div className="main-wrap">
  305. <header className="header header-with-bg header-progress-bar">
  306. <div className="container">
  307.  
  308. <PlayersCount count={playersCount}/>
  309.  
  310. { !isSpectating && currentPage === QUESTION_RESULT &&
  311. <div className="answer-label">
  312. {isCorrect === 1 ?
  313. <div className="answer-label--message correct">{t('correct')}</div> :
  314. isCorrect === 0 ? <div className="answer-label--message incorrect">{t('incorrect')}</div> : null }
  315. </div>
  316. }
  317.  
  318. { isSpectating &&
  319. <div className="answer-label">
  320. <div className="answer-label--message spectating">{t('spectating')}</div>
  321. </div>
  322. }
  323. </div>
  324.  
  325. <Progress
  326. status="default"
  327. percent={percent}
  328. theme={{
  329. default: {
  330. symbol: '‍',
  331. color: 'rgb(204, 204, 204)'
  332. }
  333. }}
  334. className="countdown-line"
  335. />
  336.  
  337. <div className="background mobile"><img src={questionPicture} alt="" title=""/></div>
  338. </header>
  339.  
  340. <div className="content">
  341. <div className="container">
  342. <div className="question">
  343. <div className="question--header">
  344. <div className="row">
  345. <div className="col-8">
  346. <div className="question--title">{ questionText }</div>
  347. </div>
  348. <div className="col-4">
  349. {currentPage === QUESTION_SHOW &&
  350. <div className="circle-countdown-wrap">
  351. <Progress
  352. type="circle"
  353. percent={percent}
  354. status={ countdown === 0 ? 'zero' : 'default' }
  355. theme={{
  356. default: {
  357. symbol: countdown,
  358. color: 'rgb(95, 235, 14)',
  359. trailColor: 'rgb(24, 51, 1)',
  360. },
  361. zero: {
  362. symbol: '0',
  363. color: 'rgb(95, 235, 14)',
  364. trailColor: 'rgb(24, 51, 1)',
  365. }
  366. }}
  367. width={53}
  368. strokeWidth={4}
  369. className="countdown-circle"
  370. />
  371. </div>
  372. }
  373.  
  374. { currentPage === QUESTION_RESULT &&
  375. <QuestionAt
  376. cls="position-right"
  377. questionCount={question.nQ}
  378. questionIndex={this.props.question.i}/>
  379. }
  380. </div>
  381. </div>
  382. </div>
  383. <div className="question--content">
  384.  
  385. { currentPage === QUESTION_SHOW ? <ul className="question--answers">
  386. { questionAnswers.map((item, index) => {
  387.  
  388. let cls = "";
  389. if (index === selectedAnswer) {
  390. cls = "selected"
  391. }
  392.  
  393. return <li key={index}>
  394. <a className={cls} onClick={() => {this.clickAnswer(index)}}>{item}</a>
  395. </li>
  396. })
  397. }
  398. </ul> : <ul className="question--answers question--answers-results">
  399. { questionAnswers.map((item, index) => {
  400.  
  401. let type = RESULT_DEFAULT;
  402.  
  403. // Calculate percent.
  404. const percent = calcPercentOfOption(this.props.question.nAnswers, index);
  405.  
  406. if (index === selectedAnswer) {
  407. type = RESULT_INCORRECT
  408. }
  409.  
  410. if (index === question.correct) {
  411. type = RESULT_CORRECT
  412. }
  413.  
  414. return (<li key={index}>
  415. <Result type={type} percent={percent} content={item} />
  416. </li>)
  417. })
  418. }
  419. </ul>
  420.  
  421. }
  422. </div>
  423. </div>
  424. </div>
  425. </div>
  426.  
  427. <div className="fullscreen-bg mobile"><img src="/assets/images/backgrounds/black-background.jpg" alt="background"/></div>
  428. </div>
  429. );
  430. }
  431. }
  432.  
  433. QuestionPage.propTypes = {
  434. question: PropTypes.object,
  435. isSpectating: PropTypes.bool,
  436. playersCount: PropTypes.number,
  437. useFreebies: PropTypes.func,
  438. selectAnswer: PropTypes.func,
  439. setOscPresence: PropTypes.func
  440. };
  441.  
  442. export default translate('translations')(QuestionPage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement