import {Injectable} from '@angular/core'; import {BehaviorSubject} from 'rxjs'; import {GameActionsService} from './game-actions.service'; import {FeedbackOxService} from './feedback-ox.service'; import {withLatestFrom} from 'rxjs/operators'; @Injectable({ providedIn: 'root' }) export class SurrenderService { allowSurrender = new BehaviorSubject(false); public currentChallengesErrors: number; private errorsToAllowSurrender: number; constructor(private gameActions: GameActionsService, private feedBackService: FeedbackOxService) { this.errorsToAllowSurrender = 2; this.gameActions.showNextChallenge.subscribe(x => { this.currentChallengesErrors = 0; this.allowSurrender.next(false); }); this.gameActions.restartGame.subscribe(x => { this.currentChallengesErrors = 0; this.allowSurrender.next(false); }); this.gameActions.surrender.subscribe(x => { this.allowSurrender.next(false); }); this.feedBackService.endFeedback.pipe(withLatestFrom(this.gameActions.checkedAnswer)).subscribe(params => { if (params[1].correctness !== 'correct' && params[1].correctness !== 'unknown') { this.onError(); } else { this.allowSurrender.next(false); } }); } private onError() { this.currentChallengesErrors++; if (this.currentChallengesErrors >= this.errorsToAllowSurrender) { this.allowSurrender.next(true); } } }