Calculates the countdown by the server

parent 9174dd68
<h2 class="question-title">{{'component.liveResults.question_number' | translate}}&nbsp;{{questionIndex + 1}}</h2>
<div [innerHTML]="sanitizeHTML(questionText)"></div>
<div *ngIf="questionText"
[innerHTML]="sanitizeHTML(questionText)"></div>
<hr/>
<hr *ngIf="answers?.length"/>
<div *ngFor="let elem of answers; let i = index">
<p class="d-flex align-items-center">
......
......@@ -138,7 +138,7 @@ describe('QuizResultsComponent', () => {
currentQuizName: '',
ticket: '',
}));
component.countdown = new Countdown(quizService.currentQuestion(), new Date().getTime());
component.countdown = new Countdown(quizService.currentQuestion().timer);
expect(component.countdown.isRunning).toBeTruthy();
expect(component.showStopCountdownButton()).toBeTruthy();
......
......@@ -282,10 +282,6 @@ export class QuizResultsComponent implements OnInit, OnDestroy {
}
const question = this.quizService.currentQuestion();
if (question.timer && (new Date().getTime() - question.timer * 1000 - currentStateData.payload.startTimestamp) < 0) {
this.countdown = new Countdown(question, currentStateData.payload.startTimestamp);
}
this.generateAnswers(question);
if (this.attendeeService.attendees.every(nick => {
const val = nick.responses[this.quizService.quiz.currentQuestionIndex].value;
......@@ -367,6 +363,11 @@ export class QuizResultsComponent implements OnInit, OnDestroy {
case MessageProtocol.Added:
this.attendeeService.addMember(data.payload.member);
break;
case MessageProtocol.Countdown:
if (!this.countdown) {
this.countdown = new Countdown(data.payload.value);
}
break;
case MessageProtocol.Removed:
this.attendeeService.removeMember(data.payload.name);
break;
......@@ -399,7 +400,7 @@ export class QuizResultsComponent implements OnInit, OnDestroy {
private handleMessagesForOwner(data: IMessage): void {
switch (data.step) {
case MessageProtocol.Start:
this.countdown = new Countdown(this.quizService.currentQuestion(), this.quizService.quiz.currentStartTimestamp);
this.countdown = null;
break;
default:
return;
......
......@@ -110,11 +110,11 @@ export class VotingComponent implements OnDestroy {
}
public displayRangedButtons(): boolean {
return this.quizService.currentQuestion().TYPE === QuestionType.RangedQuestion;
return this.quizService && this.quizService.currentQuestion().TYPE === QuestionType.RangedQuestion;
}
public displayFreetextInput(): boolean {
return this.quizService.currentQuestion().TYPE === QuestionType.FreeTextQuestion;
return this.quizService && this.quizService.currentQuestion().TYPE === QuestionType.FreeTextQuestion;
}
public normalizeAnswerOptionIndex(index: number): string {
......@@ -191,15 +191,6 @@ export class VotingComponent implements OnDestroy {
this.questionTextService.changeMultiple(this.quizService.currentQuestion().answerOptionList.map(answer => answer.answerText));
this.questionTextService.change(this.quizService.currentQuestion().questionText);
this.quizApiService.getQuizStartTime().subscribe((startTime) => {
this.countdown = new Countdown(this.quizService.currentQuestion(), startTime);
this.countdown.onChange.subscribe((value) => {
if (!value || value < 1) {
this.sendResponses('results');
}
});
});
}
public ngOnDestroy(): void {
......@@ -218,6 +209,16 @@ export class VotingComponent implements OnDestroy {
case MessageProtocol.UpdatedResponse:
this.attendeeService.modifyResponse(data.payload);
break;
case MessageProtocol.Countdown:
if (!this.countdown) {
this.countdown = new Countdown(data.payload.value);
this.countdown.onChange.subscribe((value) => {
if (!value || value < 1) {
this.sendResponses('results');
}
});
}
break;
case MessageProtocol.Reset:
this.attendeeService.clearResponses();
this.quizService.quiz.currentQuestionIndex = -1;
......
import { EventEmitter } from '@angular/core';
import { AbstractQuestionEntity } from '../entities/question/AbstractQuestionEntity';
export class Countdown {
public onChange = new EventEmitter<number>();
......@@ -20,25 +19,24 @@ export class Countdown {
this._remainingTime = value;
}
private readonly _time: number;
private readonly _interval: any;
constructor(question: AbstractQuestionEntity, startTimestamp: number) {
this._time = question.timer;
const endTimestamp = startTimestamp + this._time * 1000;
this._remainingTime = Math.round((endTimestamp - new Date().getTime()) / 1000);
console.log('Init countdown', startTimestamp, endTimestamp, this._time, this._remainingTime);
if (this._remainingTime <= 0) {
constructor(timer: number) {
if (timer <= 0) {
return;
}
this._remainingTime = timer;
this._isRunning = true;
this._interval = setInterval(() => {
this._remainingTime--;
this.onChange.next(this._remainingTime);
if (this._remainingTime <= 0) {
this._isRunning = false;
clearInterval(this._interval);
this.stop();
}
this.onChange.next(this._remainingTime);
}, 1000);
}
......
......@@ -4,6 +4,7 @@ export enum StatusProtocol {
}
export enum MessageProtocol {
Countdown = 'Countdown', //
Updated = 'Updated', //
UpdatedResponse = 'UpdatedResponse', //
Added = 'Added', //
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment