// import {EventEmitter, Injectable} from '@angular/core'; // import {ExerciseData, MetricsPerResource} from '../../models/new-metrics'; // import {AnswerService} from '../answer.service'; // import {GameActionsService} from '../game-actions.service'; // import {AppInfoOxService} from '../app-info-ox.service'; // import {ScoreStarsService} from './score-stars.service'; // import {LevelService} from '../level.service'; // import {ChallengeService} from '../challenge.service'; // import {SubLevelService} from '../sub-level.service'; // import {filter} from 'rxjs/operators'; // import { // MicroLessonInfoOxDefault, // MicroLessonMetrics // } from 'ox-types'; // // @Injectable({ // providedIn: 'root' // }) // export class MicroLessonMetrics2Service { // // currentMetricsNEW: MetricsPerResource; // // currentMetrics: MicroLessonMetrics; // // // exercisesBySubLevel: number[]; // // finishedMetrics: EventEmitter> = new EventEmitter(true); // updateMetrics: EventEmitter> = new EventEmitter(true); // startedMetrics: EventEmitter = new EventEmitter(true); // // constructor(// private timer: TimerOxService, // private answerService: AnswerService, // private gameActions: GameActionsService, private appInfo: AppInfoOxService, // private scoreStars: ScoreStarsService, // private levelService: LevelService, // private answer: AnswerService, private challenge: ChallengeService, // private subLevelService: SubLevelService) { // this.gameActions.startGame.subscribe(value => { // this.gameStart(value.info, value.level); // }); // // this.subLevelService.currentSublevel.subscribe(sublevel => { // // if (this.currentMetrics) { // // this.currentMetrics.lastSubLevelReached = sublevel; // // } // // }); // this.gameActions.restartGame.subscribe(value => { // this.metricsValuesAsStart(); // this.currentMetricsNEW.level = value ? value.level : undefined; // // this.currentMetrics.level = value ? value.level : undefined; // // this.timer.stop(); // this.startedMetrics.emit(this.currentMetricsNEW as any); // // this.startedMetrics.emit(this.currentMetrics); // }); // this.gameActions.surrender.subscribe(() => { // // this.currentMetrics.surrender++; // this.currentMetricsNEW.surrenderAnswers++; // // const lastExerciseMetric = this.getLastExerciseMetric(); // // lastExerciseMetric.surrender = true; // // lastExerciseMetric.surrenderTime = new Date(); // // lastExerciseMetric.finishTime = lastExerciseMetric.surrenderTime; // const lastExerciseMetric = this.getLastExerciseMetricNew(); // lastExerciseMetric.userInput.surrendered = true; // lastExerciseMetric.finishTime = new Date(); // this.updateNetTime(); // // this.timer.pause(); // this.metricsUpdated(); // }); // this.gameActions.microLessonCompleted.subscribe(() => { // // TODO remove this. Should have a end challenge service or someting like that. // // const lastExerciseMetric = this.getLastExerciseMetric(); // // if (lastExerciseMetric.finishTime === undefined) { // // lastExerciseMetric.finishTime = new Date(); // // } // const lastExerciseMetric = this.getLastExerciseMetricNew(); // if (lastExerciseMetric.finishTime === undefined) { // lastExerciseMetric.finishTime = new Date(); // } // this.gameFinished(); // }); // this.gameActions.showHint.subscribe(() => { // this.currentMetricsNEW.hints++; // // this.currentMetrics.hints++; // // const lastMetric = this.getLastExerciseMetric(); // // this.getLastExerciseMetric().hints.push({answersBeforeHint: lastMetric.answers.length, askTime: new Date()}); // const lastMetric = this.getLastExerciseMetricNew(); // lastMetric.userInput.requestedHints++; // .push({answersBeforeHint: lastMetric.answers.length, askTime: new Date()}); // // TODO check if here must also send updated metrics event. // }); // // this.challenge.currentExercise.pipe(filter(exercise => exercise !== undefined)).subscribe(__ => { // this.addMetric(); // // this.gameActions.showNextChallenge.subscribe(__ => { // TODO validate this change. // // The problem is that if metrics listen to showNextChallenge this metric is corrupted, cause // // in challenge service show next chllenge triggers currentChallenge and generates a // // race condition, if metrics listen the event before challenge service this will throw, // // If not, all the exercises will have a wrong value for the metric cause it will be the previous one. // // const currentExercise = this.challenge.currentExercise.value; // // if (this.currentMetrics.exerciseMetrics.length === 0) { // // this.timer.start(); // // } else if (!this.timer.isRunning()) { // // this.timer.resume(); // // } // // this.currentMetrics.exerciseMetrics.push( // // { // // level: this.levelService.currentLevel.value, // // subLevel: currentExercise.subLevel, // // initialTime: new Date(), // // bonusTime: currentExercise.bonusTime, // // exercise: currentExercise.exerciseData, // // answers: [], // // hints: [], // // surrender: false, // // gameSurprisesUsed: [] // // }); // }); // // this.answerService.tryAnswer.subscribe(value => { // console.log('TODO try answer in metrics'); // console.log('TODO try answer in metrics'); // console.log('TODO try answer in metrics'); // console.log('TODO try answer in metrics'); // console.log('TODO try answer in metrics'); // // this.getLastExerciseMetric() // // .answers.push({correctness: 'indeterminate', tryDate: new Date(), answer: value}); // }); // this.gameActions.checkedAnswer.subscribe(isCorrect => { // console.log('TODO checkedAnswer in metrics'); // // const answerByExercise = this.getLastExerciseMetric(); // // const answerByExerciseNew = this.getLastExerciseMetricNew(); // // TODO set this on answer // // answerByExerciseNew.userInput.answers.push(this.answer.getCurrentUserInputAnswer()); // // answerByExerciseNew.userInput.answers.push(this.answer.getCurrentUserInputAnswer()); // // if (isCorrect) { // // // this.currentMetrics.rightAnswers++; // // this.currentMetrics.rightAnswers++; // // answerByExerciseNew.finalStatus = 'correct'; // // answerByExercise.answers[answerByExercise.answers.length - 1].correctness = 'right'; // // answerByExercise.finishTime = new Date(); // // this.updateNetTime(); // // } else { // // // this.currentMetrics.wrongAnswers++; // // this.currentMetrics.wrongAnswers++; // // answerByExercise.answers[answerByExercise.answers.length - 1].correctness = 'wrong'; // // } // // this.metricsUpdated(); // }); // } // // // private getLastExerciseMetric(): ExerciseMetrics { // // return this.currentMetrics.exerciseMetrics[this.currentMetrics.exerciseMetrics.length - 1]; // // } // // private getLastExerciseMetricNew(): ExerciseData { // return this.currentMetricsNEW.expandableInfo.exercisesData[this.currentMetricsNEW.expandableInfo.exercisesData.length - 1]; // } // // public gameStart(info: MicroLessonInfoOxDefault, level?: number): void { // // TODO check if this table will be continue to be used or not. // // TODO probably not cause may be in the microlesson config file, and catch there by the game score calculator. // // const table = this.appInfo.getMetricsTable(info.microLessonId, level); // this.currentMetricsNEW = {} as MetricsPerResource; // this.currentMetricsNEW.metricsType = this.appInfo.microLessonInfo?.creatorInfo?.metricsType || 'results'; // this.currentMetricsNEW.level = level; // // this.currentMetrics = {} as MicroLessonMetrics; // // this.currentMetrics.metricsType = this.appInfo.microLessonInfo?.creatorInfo?.metricsType || 'results'; // // this.currentMetrics.level = level; // this.metricsValuesAsStart(); // // TODO check this, exercise to up level is used in several games // // this.currentMetrics.exercisesToUpLevel = this.appInfo.getExercisesToUpSubLevel(level); // // this.startedMetrics.emit(this.currentMetrics); // console.log('startedMetrics', this.currentMetricsNEW); // this.startedMetrics.emit(this.currentMetricsNEW as any); // } // // metricsValuesAsStart(): void { // // this.currentMetrics.rightAnswers = 0; // // this.currentMetrics.wrongAnswers = 0; // // this.currentMetrics.hints = 0; // // this.currentMetrics.netTime = 0; // // this.currentMetrics.surrender = 0; // // this.currentMetrics.exerciseMetrics = []; // // this.currentMetrics.microLessonId = this.appInfo.microLessonInfo.microLessonId; // this.currentMetricsNEW.rightAnswers = 0; // this.currentMetricsNEW.wrongAnswers = 0; // this.currentMetricsNEW.hints = 0; // this.currentMetricsNEW.netTime = 0; // this.currentMetricsNEW.surrenderAnswers = 0; // this.currentMetricsNEW.expandableInfo.exercisesData = []; // this.currentMetricsNEW.resourceUid = this.appInfo.microLessonInfo.microLessonId; // } // // private gameFinished(): void { // // // this.scoreStars.calculateScore(this.currentMetrics); // // this.scoreStars.calculateStars(this.currentMetrics); // // this.finishedMetrics.emit(this.currentMetrics); // console.log('Game finished', this.currentMetricsNEW); // this.scoreStars.calculateScore(this.currentMetricsNEW as any); // this.scoreStars.calculateStars(this.currentMetricsNEW as any); // this.finishedMetrics.emit(this.currentMetricsNEW as any); // } // // private updateNetTime(): void { // const aux = this.getLastExerciseMetricNew(); // // const aux = this.getLastExerciseMetric(); // const init = aux.initialTime; // const finish = aux.finishTime; // const rest = (+finish) - (+init); // const toSeconds = rest / 1000; // // this.currentMetrics.netTime += toSeconds; // this.currentMetricsNEW.netTime += toSeconds; // } // // private metricsUpdated(): void { // console.log('metricsUpdated', this.currentMetricsNEW); // // this.updateMetrics.emit(this.currentMetrics); // this.updateMetrics.emit(this.currentMetricsNEW as any); // } // // // updateSurpriseMetric(currentSurprisesUsed: SurpriseInfo[]): void { // // console.log('Fix update surprise metrics things!!'); // // console.log('Fix update surprise metrics things!!'); // // console.log('Fix update surprise metrics things!!'); // // console.log('Fix update surprise metrics things!!'); // // // const lastMetric = this.getLastExerciseMetric(); // // // const lastMetric = this.getLastExerciseMetricNew(); // // // lastMetric.gameSurprisesUsed = currentSurprisesUsed; // // } // // public addMetric(): void { // const asd: ExerciseData = { // initialTime: new Date(), // finishTime: undefined, // schemaType: 'long-answer', // context: { // additionalInfo: [], // orderedOptions: [], // schemaData: undefined, // statement: { // parts: [], // }, // // opciones cambiantes como en el juego de burubujas o el juego de las vocales con los troniquitos // // opciones desordenadas? // // orderedOptions?: CorrectablePart[]; // }, // // timeInfo?: { // // maxPerExercise?: number, // // usedTime: number // // }; // timeInExercise: 0, // TODO set time // userInput: { // answers: [], // requestedHints: 0, // surrendered: false, // }, // finalStatus: 'to-answer', // }; // this.currentMetricsNEW.expandableInfo.exercisesData.push(asd); // } // // public startMetrics(info: MicroLessonInfoOxDefault, level?: number): void { // this.currentMetricsNEW = {} as MetricsPerResource; // // this.currentMetrics = {} as MicroLessonMetrics; // this.currentMetricsNEW.metricsType = this.appInfo.microLessonInfo?.creatorInfo?.metricsType || 'results'; // this.currentMetricsNEW.level = level; // this.metricsValuesAsStart(); // // TODO poner esto en newMetrics // // this.currentMetrics.exercisesToUpLevel = this.appInfo.getExercisesToUpSubLevel(level); // // this.startedMetrics.emit(this.currentMetrics); // console.log('startedMetrics', this.currentMetricsNEW); // this.startedMetrics.emit(this.currentMetricsNEW); // } // }