import {EventEmitter, Injectable} from '@angular/core'; import {Observable, of} from 'rxjs'; import {GameActionsService} from './game-actions.service'; import {MicroLessonMetricsService} from './metrics/micro-lesson-metrics.service'; import {PartCorrectness, UserAnswer} from 'ox-types'; @Injectable({ providedIn: 'root' }) export abstract class AnswerService { private _currentAnswer: UserAnswer; get currentAnswer(): UserAnswer { return this._currentAnswer; } set currentAnswer(value: UserAnswer) { this._currentAnswer = value; this.validAnswer.emit(this.isValidAnswer(this.currentAnswer)); } public tryAnswer = new EventEmitter(); public validAnswer = new EventEmitter(); protected constructor(private gameActions: GameActionsService, private metricsService: MicroLessonMetricsService) { } protected checkAnswer(answer: UserAnswer): Observable { return of(answer.parts.length > 0 && answer.parts.every(z => z.correctness === 'correct') ? 'correct' : 'wrong'); } public onTryAnswer(): void { this.tryAnswer.emit(this.currentAnswer); if (this.metricsService.currentMetrics.metricsType === 'results-without-correction') { this.gameActions.checkedAnswer.emit({answer: this.currentAnswer, correctness: 'unknown'}); } else { this.checkAnswer(this.currentAnswer).subscribe(value => { this.gameActions.checkedAnswer.emit({answer: this.currentAnswer, correctness: value}); }); } } // protected abstract isValidAnswer(answer: UserAnswer): boolean; protected isValidAnswer(answer: UserAnswer): boolean { return answer !== undefined; } }