From 6ebec9ce68dc2856f1a9985ab2c3ff9f880975a5 Mon Sep 17 00:00:00 2001 From: _Bastler <_Bastler@bstly.de> Date: Sun, 16 May 2021 14:52:07 +0200 Subject: [PATCH] add exitUrl script --- front/src/Api/Events/ExitUrlEvent.ts | 11 +++++++++++ front/src/Api/IframeListener.ts | 7 +++++++ front/src/Phaser/Game/GameScene.ts | 9 +++++++++ front/src/iframe_api.ts | 10 ++++++++++ 4 files changed, 37 insertions(+) create mode 100644 front/src/Api/Events/ExitUrlEvent.ts diff --git a/front/src/Api/Events/ExitUrlEvent.ts b/front/src/Api/Events/ExitUrlEvent.ts new file mode 100644 index 00000000..470bc81e --- /dev/null +++ b/front/src/Api/Events/ExitUrlEvent.ts @@ -0,0 +1,11 @@ +import * as tg from "generic-type-guard"; + +export const isExitUrlEvent = + new tg.IsInterface().withProperties({ + url: tg.isString, + }).get(); + +/** + * A message sent from the iFrame to the game to trigger Exit Url + */ +export type ExitUrlEvent = tg.GuardedType; \ No newline at end of file diff --git a/front/src/Api/IframeListener.ts b/front/src/Api/IframeListener.ts index 19d030b6..ba9d50fb 100644 --- a/front/src/Api/IframeListener.ts +++ b/front/src/Api/IframeListener.ts @@ -12,6 +12,7 @@ import {ClosePopupEvent, isClosePopupEvent} from "./Events/ClosePopupEvent"; import {scriptUtils} from "./ScriptUtils"; import {GoToPageEvent, isGoToPageEvent} from "./Events/GoToPageEvent"; import {isOpenCoWebsite, OpenCoWebSiteEvent} from "./Events/OpenCoWebSiteEvent"; +import { isExitUrlEvent } from './Events/ExitUrlEvent'; /** @@ -55,6 +56,9 @@ class IframeListener { private readonly _unregisterIFrameStream: Subject = new Subject(); public readonly unregisterIFrameStream = this._unregisterIFrameStream.asObservable(); + private readonly _exitUrlStream: Subject = new Subject(); + public readonly exitUrlStream = this._exitUrlStream.asObservable(); + private readonly iframes = new Set(); private readonly scripts = new Map(); @@ -109,6 +113,9 @@ class IframeListener { } else if (payload.type === 'removeBubble'){ this._removeBubbleStream.next(); + } + else if (payload.type === 'exitUrl' && isExitUrlEvent(payload.data)){ + this._exitUrlStream.next(payload.data.url); } } diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index bcf997a4..e655a333 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -82,6 +82,7 @@ import CanvasTexture = Phaser.Textures.CanvasTexture; import GameObject = Phaser.GameObjects.GameObject; import FILE_LOAD_ERROR = Phaser.Loader.Events.FILE_LOAD_ERROR; import DOMElement = Phaser.GameObjects.DOMElement; +import EVENT_TYPE =Phaser.Scenes.Events; import {Subscription} from "rxjs"; import {worldFullMessageStream} from "../../Connexion/WorldFullMessageStream"; import { lazyLoadCompanionResource } from "../Companion/CompanionTexturesLoadingManager"; @@ -898,6 +899,14 @@ export class GameScene extends ResizableScene implements CenterListener { }, this.userInputManager); } })); + + this.iframeSubscriptionList.push(iframeListener.exitUrlStream.subscribe((url:string)=>{ + this.loadNextGame(url).then(()=>{ + this.events.once(EVENT_TYPE.POST_UPDATE,()=>{ + this.onMapExit(url); + }) + }) + })); } private getMapDirUrl(): string { diff --git a/front/src/iframe_api.ts b/front/src/iframe_api.ts index 290e11c2..af52a307 100644 --- a/front/src/iframe_api.ts +++ b/front/src/iframe_api.ts @@ -9,6 +9,7 @@ import {ClosePopupEvent, isClosePopupEvent} from "./Api/Events/ClosePopupEvent"; import {OpenTabEvent} from "./Api/Events/OpenTabEvent"; import {GoToPageEvent} from "./Api/Events/GoToPageEvent"; import {OpenCoWebSiteEvent} from "./Api/Events/OpenCoWebSiteEvent"; +import {ExitUrlEvent} from "./Api/Events/ExitUrlEvent"; interface WorkAdventureApi { sendChatMessage(message: string, author: string): void; @@ -25,6 +26,7 @@ interface WorkAdventureApi { restorePlayerControl(): void; displayBubble(): void; removeBubble(): void; + exitUrl(url : string): void; } declare global { @@ -210,6 +212,14 @@ window.WA = { } subject.subscribe(callback); }, + exitUrl(url : string) : void{ + window.parent.postMessage({ + "type" : 'exitUrl', + "data" : { + url + } as ExitUrlEvent + },'*'); + }, } window.addEventListener('message', message => {