import {Injectable} from '@angular/core'; import {AppInfoOxService} from './app-info-ox.service'; import {FeedbackOxService} from './feedback-ox.service'; import {GameActionsService} from './game-actions.service'; import {AnswerService} from './answer.service'; import {LevelService} from './level.service'; import {PartCorrectness, UserAnswer} from 'ox-types'; @Injectable({ providedIn: 'root' }) export class EndGameService { private challengesCompleted: number; public sendEndEvent = true; private gameHasEnded: boolean; constructor(private gameActions: GameActionsService, private appInfo: AppInfoOxService, private levelService: LevelService, private answerService: AnswerService, private feedbackService: FeedbackOxService) { this.gameActions.microLessonCompleted.subscribe(_ => { this.gameHasEnded = true; }); this.feedbackService.endFeedback.subscribe(_ => { this.checkEnded(); }); this.feedbackService.surrenderEnd.subscribe(_ => { this.checkEnded(); }); this.gameActions.checkedAnswer.subscribe(correct => { const correctness = correct.correctness; if (correctness === 'correct' || (correctness === 'unknown') && ['results-without-correction', 'use'].includes(this.appInfo.microLessonInfo.creatorInfo?.metricsType)) { ++this.challengesCompleted; } }); this.gameActions.surrender.subscribe(x => { ++this.challengesCompleted; }); this.gameActions.restartGame.subscribe(x => { this.challengesCompleted = 0; this.gameHasEnded = false; }); this.gameActions.startGame.subscribe(x => { this.challengesCompleted = 0; this.gameHasEnded = false; }); } // TODO add lives, time and other possibles ends. private checkEnded() { if (this.gameIsEnded() && this.sendEndEvent && !this.gameHasEnded) { this.gameHasEnded = true; this.gameActions.microLessonCompleted.emit(); } } public gameIsEnded(): boolean { const challengesType = this.appInfo .getChallengesModeAndValue(this.levelService.currentLevel.value); const totalChallenges = challengesType.value; return this.gameHasEnded || (totalChallenges !== undefined && this.challengesCompleted >= totalChallenges); } public getChallengeCompleteCount(): number { return this.challengesCompleted; } }