import {Injectable} from '@angular/core'; import {GameActionsService} from './game-actions.service'; import {AppInfoOxService} from './app-info-ox.service'; import {BehaviorSubject} from 'rxjs'; import {LevelService} from './level.service'; @Injectable({ providedIn: 'root' }) export class SubLevelService { public currentSublevel = new BehaviorSubject(1); private subLevelProgress: number; private progressAddedInThisChallenge: boolean; private maxSubLevel: number; private currentExercisesToReach: number[]; constructor(private gameActions: GameActionsService, private levelService: LevelService, private appInfoOxService: AppInfoOxService) { this.levelService.currentLevel.subscribe((newLevel: number) => { if (newLevel !== -1) { this.setValuesByLevel(newLevel); } }); this.gameActions.showNextChallenge.subscribe(_ => { this.progressAddedInThisChallenge = false; }); this.gameActions.startGame.subscribe(_ => { this.asStart(); }); this.gameActions.checkedAnswer.subscribe((correct) => { this.updateCurrentSubLevel(correct.correctness === 'correct'); }); this.gameActions.restartGame.subscribe(_ => { this.asStart(); }); } private setValuesByLevel(newLevel: number) { this.maxSubLevel = this.appInfoOxService.getMaxSublevel(newLevel); // this.maxSubLevel = this.appInfoOxService.getMicroLessonLevelConfiguration(newLevel) // .exercisesToUpSubLevel.length; let carry = 0; this.currentExercisesToReach = this.appInfoOxService.getExercisesToUpSubLevel(newLevel) .map((value: number, index: number) => { // this.currentExercisesToReach = this.appInfoOxService.getMicroLessonLevelConfiguration(newLevel) // .exercisesToUpSubLevel.map((value: number, index: number) => { carry += value; return carry; }); } private asStart() { this.progressAddedInThisChallenge = false; this.subLevelProgress = 0; this.currentSublevel.next(1); } private updateCurrentSubLevel(correct: boolean) { if (!this.progressAddedInThisChallenge) { const diff = correct ? 1 : -1; this.subLevelProgress = Math.max(0, this.subLevelProgress + diff); this.progressAddedInThisChallenge = true; const newSubLevel = this.currentExercisesToReach.findIndex(toReach => this.subLevelProgress < toReach) + 1; if (this.currentSublevel.value !== newSubLevel) { this.currentSublevel.next(newSubLevel); } } } getMaxSubLevel() { return this.maxSubLevel; } }