import {ElementRef, Inject} from '@angular/core'; import {TranslocoService} from '@ngneat/transloco'; import {InWumboxService} from '../services/in-wumbox.service'; import {CommunicationOxService, I18nService, ResourceOx, PreloaderOxService, ResourceType} from 'ox-core'; import { MicroLessonCommunicationService } from '../services/communication/micro-lesson-communication.service'; import {AppInfoOxService} from '../services/app-info-ox.service'; import {MicroLessonMetricsService} from '../services/metrics/micro-lesson-metrics.service'; import {HttpClient} from '@angular/common/http'; import {LevelService} from '../services/level.service'; import {ChallengeService} from '../services/challenge.service'; import {EndGameService} from '../services/end-game.service'; import {GameActionsService} from '../services/game-actions.service'; import {ProgressService} from '../services/progress.service'; import {SoundOxService} from '../services/sound-ox.service'; import {PostMessageBridgeFactory} from 'ngox-post-message'; import {ScreenTypeOx} from 'ox-types'; import {AssetsLoadedForGameBridge, LoadAssetsForGameBridge, TutorialEndOxBridge, TutorialMetric} from 'ox-types'; import {timer} from 'rxjs'; export abstract class BaseMicroLessonApp { loaded = false; protected addGeneralAudios = true; public showingTutorial = false; constructor(private preloader: PreloaderOxService, private translocoService: TranslocoService, private wumboxService: InWumboxService, private communicationOxService: CommunicationOxService, private microLessonCommunicationService: MicroLessonCommunicationService, private progressService: ProgressService, private elementRef: ElementRef, private gameActions: GameActionsService, private endGame: EndGameService, private i18nService: I18nService, private levelService: LevelService, private http: HttpClient, private challenge: ChallengeService, private appInfo: AppInfoOxService, private microLessonMetrics: MicroLessonMetricsService, // Todo // Todo private feedbackAnimation: FeedbackPatternAnimationService, protected sound: SoundOxService, @Inject(PostMessageBridgeFactory) private bridgeFactory: PostMessageBridgeFactory) { gameActions.showTutorial.subscribe(z => { this.showingTutorial = true; }); this.gameActions.restartGame.subscribe(x => { this.challenge.currentExercise.next(undefined); this.challenge.onRestartGame(); this.gameActions.showNextChallenge.emit(); }); this.microLessonCommunicationService.setBridgesMLToManger(); this.wumboxService.inWumbox.subscribe(x => { const media = (this.appInfo.microLessonInfo as any).customMedia; const vals = media ? Object.values(media) : []; const keys = media ? Object.keys(media) : []; const resources = vals.map((v, i) => { return {name: keys[i], value: v as string}; }); // as { name: string, value: string }[]; const audios = resources.filter(aa => aa.name.includes('.mp3')); this.challenge.inWumbox(resources); this.loadGameResources(audios); this.levelService.currentLevel.next(1); }); preloader.basePath = this.getBasePath(); this.gameActions.startGame.subscribe(x => { // console.log('Manager send to start game'); this.challenge.beforeStartGame(); this.levelService.currentLevel.next(1); this.gameActions.showNextChallenge.emit(); }); preloader.addResourcesToLoad(this.getGameResourcesToLoad()); } private loadGameResources(audios: { name: string; value: string }[] = []): void { this.getGameResourcesToLoad().forEach( x => this.preloader.addResourceToLoad(x)); if (this.addGeneralAudios) { ['sounds/bubble.mp3', 'sounds/clickSurrender.mp3', 'sounds/hint.mp3', 'sounds/click.mp3', 'sounds/cantClick.mp3', 'sounds/woosh.mp3', 'sounds/rightAnswer.mp3', 'sounds/wrongAnswer.mp3'] .forEach(x => { this.preloader.addResourceToLoad(new ResourceOx(x, ResourceType.Audio, [ScreenTypeOx.Game], false)); }); } audios.forEach(a => { const tempReso = new ResourceOx(a.name, ResourceType.Audio, [ScreenTypeOx.Game], false); tempReso.dowloadUrl = a.value; this.preloader.resources.set(a.name, tempReso); }); const resourcesToLoad = Array.from(this.preloader.resources.values()).length; this.microLessonCommunicationService.sendMessageMLToManager(LoadAssetsForGameBridge, '' + resourcesToLoad); this.preloader.loadAll().subscribe(x => { // console.log('All loaded. Inside game.'); this.microLessonCommunicationService.sendMessageMLToManager(AssetsLoadedForGameBridge, '' + resourcesToLoad); this.loaded = true; }); } protected abstract getBasePath(): string; protected abstract getGameResourcesToLoad(): ResourceOx[]; onTutorialEnd(tutorialMetric: TutorialMetric): void { timer(200).subscribe( z => this.showingTutorial = false); this.challenge.currentExercise.next(undefined); this.microLessonCommunicationService.sendMessageMLToManager(TutorialEndOxBridge, JSON.stringify(tutorialMetric)); } } export function getResourceArrayFromUrlList(urlList: string[], resourceType: ResourceType, isLocal: boolean): ResourceOx[] { return urlList.map(listElement => new ResourceOx(listElement, resourceType, [ScreenTypeOx.Game], isLocal)); }