import {EventEmitter, Injectable} from '@angular/core'; import {FeedbackOxService} from './feedback-ox.service'; import {AppInfoOxService} from './app-info-ox.service'; import {GameActionsService} from './game-actions.service'; import {AnswerService} from './answer.service'; import {LevelService} from './level.service'; import {withLatestFrom} from 'rxjs/operators'; import {PartCorrectness, UserAnswer} from 'ox-types'; @Injectable({ providedIn: 'root' }) export class ProgressService { startProgress: EventEmitter; changeInProgress: EventEmitter; private totalToComplete: number; private currentCompleted: number; constructor(private feedbackService: FeedbackOxService, private gameActions: GameActionsService, private levelService: LevelService, private answerService: AnswerService, private appInfo: AppInfoOxService) { this.startProgress = new EventEmitter(); this.changeInProgress = new EventEmitter(); this.gameActions.restartGame.subscribe(x => { this.onStartProgress(); }); this.gameActions.startGame.subscribe(x => { this.onStartProgress(); }); this.feedbackService.surrenderEnd.subscribe(x => { this.completeOne(); }); this.feedbackService.endFeedback.pipe(withLatestFrom(this.gameActions.checkedAnswer)).subscribe(params => { const correctness = (params[1] as { correctness: PartCorrectness, answer: UserAnswer }).correctness; if (correctness === 'correct' || (correctness === 'unknown') && ['results-without-correction', 'use'].includes(this.appInfo.microLessonInfo.creatorInfo?.metricsType)) { this.completeOne(); } }); } private completeOne() { this.currentCompleted++; this.changeInProgress.emit(Math.round((this.currentCompleted * 100) / this.totalToComplete)); } onStartProgress() { this.totalToComplete = this.appInfo.getTotalChallengeCount(this.levelService.currentLevel.value); this.currentCompleted = 0; this.startProgress.emit(); } }