import {EventEmitter, Injectable} from '@angular/core'; import {ScreenTypeOx, PartCorrectness, UserAnswer} from 'ox-types'; import {SoundOxService} from './sound-ox.service'; import {GameActionsService} from './game-actions.service'; import {withLatestFrom} from 'rxjs/operators'; import {AppInfoOxService} from './app-info-ox.service'; @Injectable({ providedIn: 'root' }) export class FeedbackOxService { public surrenderEnd = new EventEmitter(); public feedbackAnimation = new EventEmitter<{ correctness: PartCorrectness, answer: UserAnswer }>(); public endFeedback = new EventEmitter(); // TODO this is just a placerholder for previous versions. // Games should inherit from this service and choose what to do. // This is needed cause you may want to play a different sound after X time public playFeedBackSounds = true; constructor(private soundService: SoundOxService, private appInfo: AppInfoOxService, private gameActions: GameActionsService) { this.gameActions.checkedAnswer.subscribe(isCorrect => { // if (this.playFeedBackSounds) { // if (isCorrect) { // this.soundService.playRightSound(ScreenTypeOx.Game); // } else { // this.soundService.playWrongSound(ScreenTypeOx.Game); // } // } this.feedbackAnimation.emit(isCorrect); }); this.endFeedback.pipe(withLatestFrom(this.gameActions.checkedAnswer)).subscribe((params: any) => { if (this.playFeedBackSounds) { 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.soundService.playRightSound(ScreenTypeOx.Game); } else if (params[1].correctness === 'wrong') { this.soundService.playWrongSound(ScreenTypeOx.Game); } } }); } }