From 6c6789411ada78963fe7ed3faa408e54d03764fc Mon Sep 17 00:00:00 2001 From: jonny Date: Wed, 21 Apr 2021 11:20:17 +0200 Subject: [PATCH 1/9] added loadPage Api call --- front/src/Api/Events/LoadPageEvent.ts | 13 +++++++++++++ front/src/Api/IframeListener.ts | 7 +++++++ front/src/Phaser/Game/GameScene.ts | 6 ++++++ front/src/iframe_api.ts | 11 +++++++++++ 4 files changed, 37 insertions(+) create mode 100644 front/src/Api/Events/LoadPageEvent.ts diff --git a/front/src/Api/Events/LoadPageEvent.ts b/front/src/Api/Events/LoadPageEvent.ts new file mode 100644 index 00000000..6f8b9bcf --- /dev/null +++ b/front/src/Api/Events/LoadPageEvent.ts @@ -0,0 +1,13 @@ +import * as tg from "generic-type-guard"; + + + +export const isLoadPageEvent = + new tg.IsInterface().withProperties({ + url: tg.isString, + }).get(); + +/** + * A message sent from the iFrame to the game to add a message in the chat. + */ +export type LoadPageEvent = tg.GuardedType; diff --git a/front/src/Api/IframeListener.ts b/front/src/Api/IframeListener.ts index c875ebbb..f20e055c 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 { isLoadPageEvent } from './Events/LoadPageEvent'; /** @@ -31,6 +32,10 @@ class IframeListener { private readonly _goToPageStream: Subject = new Subject(); public readonly goToPageStream = this._goToPageStream.asObservable(); + + private readonly _loadPageStream: Subject = new Subject(); + public readonly loadPageStream = this._loadPageStream.asObservable(); + private readonly _openCoWebSiteStream: Subject = new Subject(); public readonly openCoWebSiteStream = this._openCoWebSiteStream.asObservable(); @@ -103,6 +108,8 @@ class IframeListener { } else if (payload.type === 'removeBubble'){ this._removeBubbleStream.next(); + }else if (payload.type === 'loadPage' && isLoadPageEvent(payload.data)){ + this._loadPageStream.next(payload.data.url); } } diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 990f702c..ca13491c 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -841,6 +841,12 @@ ${escapedMessage} this.iframeSubscriptionList.push(iframeListener.enablePlayerControlStream.subscribe(()=>{ this.userInputManager.restoreControls(); })); + this.iframeSubscriptionList.push(iframeListener.loadPageStream.subscribe((url:string)=>{ + this.loadNextGame(url).then(()=>{ + this.scene.systems.settings.isTransition=true + this.onMapExit(url) + }) + })); let scriptedBubbleSprite : Sprite; this.iframeSubscriptionList.push(iframeListener.displayBubbleStream.subscribe(()=>{ scriptedBubbleSprite = new Sprite(this,this.CurrentPlayer.x + 25,this.CurrentPlayer.y,'circleSprite-white'); diff --git a/front/src/iframe_api.ts b/front/src/iframe_api.ts index 18d8d172..fb25c015 100644 --- a/front/src/iframe_api.ts +++ b/front/src/iframe_api.ts @@ -9,6 +9,7 @@ import {ClosePopupEvent} from "./Api/Events/ClosePopupEvent"; import {OpenTabEvent} from "./Api/Events/OpenTabEvent"; import {GoToPageEvent} from "./Api/Events/GoToPageEvent"; import {OpenCoWebSiteEvent} from "./Api/Events/OpenCoWebSiteEvent"; +import { LoadPageEvent } from './Api/Events/LoadPageEvent'; interface WorkAdventureApi { sendChatMessage(message: string, author: string): void; @@ -18,6 +19,7 @@ interface WorkAdventureApi { openPopup(targetObject: string, message: string, buttons: ButtonDescriptor[]): Popup; openTab(url : string): void; goToPage(url : string): void; + loadPage(url : string): void; openCoWebSite(url : string): void; closeCoWebSite(): void; disablePlayerControl() : void; @@ -122,6 +124,15 @@ window.WA = { },'*'); }, + loadPage(url : string) : void{ + window.parent.postMessage({ + "type" : 'loadPage', + "data" : { + url + } as LoadPageEvent + },'*'); + }, + openCoWebSite(url : string) : void{ window.parent.postMessage({ "type" : 'openCoWebSite', From 006195e8cc78998cd51cf70925551780f5524b67 Mon Sep 17 00:00:00 2001 From: jonny Date: Wed, 21 Apr 2021 11:54:37 +0200 Subject: [PATCH 2/9] rewrote to run in event loop --- front/src/Phaser/Game/GameScene.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index ca13491c..56647cfc 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -74,6 +74,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"; @@ -843,8 +844,9 @@ ${escapedMessage} })); this.iframeSubscriptionList.push(iframeListener.loadPageStream.subscribe((url:string)=>{ this.loadNextGame(url).then(()=>{ - this.scene.systems.settings.isTransition=true - this.onMapExit(url) + this.events.once(EVENT_TYPE.POST_UPDATE,()=>{ + this.onMapExit(url); + }) }) })); let scriptedBubbleSprite : Sprite; From 8f6c65384a7a17c3df659fed8b63e3cd06327d70 Mon Sep 17 00:00:00 2001 From: jonny Date: Thu, 29 Apr 2021 10:43:51 +0200 Subject: [PATCH 3/9] added example script for map exit --- maps/tests/goToPageScript.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/maps/tests/goToPageScript.js b/maps/tests/goToPageScript.js index 2b2cf33b..0404bdb4 100644 --- a/maps/tests/goToPageScript.js +++ b/maps/tests/goToPageScript.js @@ -1,14 +1,16 @@ +/// var zoneName = "popUpGoToPageZone"; var urlPricing = "https://workadventu.re/pricing"; var urlGettingStarted = "https://workadventu.re/getting-started"; -var isCoWebSiteOpened = false; +var urlRelativeMap = "script_api.json"; +var isCoWebSiteOpened = false; WA.onChatMessage((message => { - WA.sendChatMessage('Poly Parrot says: "'+message+'"', 'Poly Parrot'); + WA.sendChatMessage('Poly Parrot says: "' + message + '"', 'Poly Parrot'); })); WA.onEnterZone(zoneName, () => { - WA.openPopup("popUp","Open Links",[ + WA.openPopup("popUp", "Open Links", [ { label: "Open Tab", className: "popUpElement", @@ -18,27 +20,34 @@ WA.onEnterZone(zoneName, () => { }) }, { - label: "Go To Page", className : "popUpElement", - callback:(popup => { + label: "Go To Page", className: "popUpElement", + callback: (popup => { WA.goToPage(urlPricing); popup.close(); }) - } - , + }, { - label: "openCoWebSite", className : "popUpElement", - callback:(popup => { + label: "openCoWebSite", className: "popUpElement", + callback: (popup => { WA.openCoWebSite(urlPricing); isCoWebSiteOpened = true; popup.close(); }) + }, { + label: "load grouped map", + className: "popUpElement", + callback: (popup => { + WA.loadPage(urlRelativeMap); + popup.close(); + }) + }]); }) WA.onLeaveZone(zoneName, () => { - if (isCoWebSiteOpened) { + if(isCoWebSiteOpened) { WA.closeCoWebSite(); isCoWebSiteOpened = false; } From 99926a89b260f4ba9f570f71faac23f6f47fa211 Mon Sep 17 00:00:00 2001 From: jonny Date: Thu, 29 Apr 2021 10:55:01 +0200 Subject: [PATCH 4/9] added goToPage.json with the loadPAge Api call to the index.html --- maps/tests/index.html | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/maps/tests/index.html b/maps/tests/index.html index f53bbae9..bb25b19c 100644 --- a/maps/tests/index.html +++ b/maps/tests/index.html @@ -41,6 +41,14 @@ Testing scripting API with a script + + + + Success Failure Pending + + + Testing scripting API with a script + From 99bd9d88d9db60c0fa750720b7306ee52f90e3bb Mon Sep 17 00:00:00 2001 From: jonny Date: Thu, 29 Apr 2021 10:56:56 +0200 Subject: [PATCH 5/9] renamed api method "exitSceneTo" --- front/src/iframe_api.ts | 4 ++-- maps/tests/goToPageScript.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/front/src/iframe_api.ts b/front/src/iframe_api.ts index fb25c015..56cffb1c 100644 --- a/front/src/iframe_api.ts +++ b/front/src/iframe_api.ts @@ -19,7 +19,7 @@ interface WorkAdventureApi { openPopup(targetObject: string, message: string, buttons: ButtonDescriptor[]): Popup; openTab(url : string): void; goToPage(url : string): void; - loadPage(url : string): void; + exitSceneTo(url : string): void; openCoWebSite(url : string): void; closeCoWebSite(): void; disablePlayerControl() : void; @@ -124,7 +124,7 @@ window.WA = { },'*'); }, - loadPage(url : string) : void{ + exitSceneTo(url : string) : void{ window.parent.postMessage({ "type" : 'loadPage', "data" : { diff --git a/maps/tests/goToPageScript.js b/maps/tests/goToPageScript.js index 0404bdb4..af5b7f9f 100644 --- a/maps/tests/goToPageScript.js +++ b/maps/tests/goToPageScript.js @@ -39,7 +39,7 @@ WA.onEnterZone(zoneName, () => { label: "load grouped map", className: "popUpElement", callback: (popup => { - WA.loadPage(urlRelativeMap); + WA.exitSceneTo(urlRelativeMap); popup.close(); }) From 7712bd685b6257c31bdb146776f5342e72b4abdc Mon Sep 17 00:00:00 2001 From: jonny Date: Tue, 15 Jun 2021 15:19:45 +0200 Subject: [PATCH 6/9] fixed merge errors --- front/src/Phaser/Game/GameScene.ts | 4 ++-- front/src/iframe_api.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index b09f5a1e..b5876d5a 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -1102,10 +1102,10 @@ ${escapedMessage} } //todo: push that into the gameManager - private loadNextGame(exitSceneIdentifier: string): void { + private loadNextGame(exitSceneIdentifier: string): Promise { const {roomId, hash} = Room.getIdFromIdentifier(exitSceneIdentifier, this.MapUrlFile, this.instance); const room = new Room(roomId); - gameManager.loadMap(room, this.scene).catch(() => {}); + return gameManager.loadMap(room, this.scene).catch(() => {}); } private startUser(layer: ITiledMapTileLayer): PositionInterface { diff --git a/front/src/iframe_api.ts b/front/src/iframe_api.ts index ea987f21..d4146fd1 100644 --- a/front/src/iframe_api.ts +++ b/front/src/iframe_api.ts @@ -9,11 +9,11 @@ import type { ClosePopupEvent } from "./Api/Events/ClosePopupEvent"; import type { OpenTabEvent } from "./Api/Events/OpenTabEvent"; import type { GoToPageEvent } from "./Api/Events/GoToPageEvent"; import type { OpenCoWebSiteEvent } from "./Api/Events/OpenCoWebSiteEvent"; -import type {PlaySoundEvent} from "./Api/Events/PlaySoundEvent"; -import type {StopSoundEvent} from "./Api/Events/StopSoundEvent"; -import type {LoadSoundEvent} from "./Api/Events/LoadSoundEvent"; +import type { PlaySoundEvent } from "./Api/Events/PlaySoundEvent"; +import type { StopSoundEvent } from "./Api/Events/StopSoundEvent"; +import type { LoadSoundEvent } from "./Api/Events/LoadSoundEvent"; import SoundConfig = Phaser.Types.Sound.SoundConfig; -import { LoadPageEvent } from './Api/Events/LoadPageEvent'; +import type { LoadPageEvent } from './Api/Events/LoadPageEvent'; interface WorkAdventureApi { sendChatMessage(message: string, author: string): void; From c1e202b7b113bbd06411d0dee8fbd57a59afed97 Mon Sep 17 00:00:00 2001 From: jonny Date: Wed, 16 Jun 2021 17:16:00 +0200 Subject: [PATCH 7/9] renamed to goToRoom --- front/src/iframe_api.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/front/src/iframe_api.ts b/front/src/iframe_api.ts index d4146fd1..89279a64 100644 --- a/front/src/iframe_api.ts +++ b/front/src/iframe_api.ts @@ -21,10 +21,10 @@ interface WorkAdventureApi { onEnterZone(name: string, callback: () => void): void; onLeaveZone(name: string, callback: () => void): void; openPopup(targetObject: string, message: string, buttons: ButtonDescriptor[]): Popup; - openTab(url : string): void; - goToPage(url : string): void; - exitSceneTo(url : string): void; - openCoWebSite(url : string): void; + openTab(url: string): void; + goToPage(url: string): void; + goToRoom(url: string): void; + openCoWebSite(url: string): void; closeCoWebSite(): void; disablePlayerControls(): void; restorePlayerControls(): void; @@ -168,7 +168,7 @@ window.WA = { }, '*'); }, - exitSceneTo(url : string) : void{ + goToRoom(url: string): void { window.parent.postMessage({ "type" : 'loadPage', "data" : { From 1147a21ddeaf4e32147c6e0de15c4a5653ae32f9 Mon Sep 17 00:00:00 2001 From: jonny Date: Wed, 16 Jun 2021 17:34:00 +0200 Subject: [PATCH 8/9] fixed documentation --- docs/maps/api-reference.md | 19 +++++++++++++++++++ maps/tests/goToPageScript.js | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/maps/api-reference.md b/docs/maps/api-reference.md index 9891a88a..80d57ea9 100644 --- a/docs/maps/api-reference.md +++ b/docs/maps/api-reference.md @@ -187,6 +187,25 @@ Example: WA.goToPage('https://www.wikipedia.org/'); ``` +### Going to a different map from the script + +``` + +goToRoom(url: string): void +``` + +Load the map at url without unloading workadventure + +relative urls: "../subFolder/map.json[#start-layer-name]" +global urls: "/_/global/domain/path/map.json[#start-layer-name]" + +Example: + +```javascript +WA.goToRoom('../otherMap/map.json'); +WA.goToRoom("/_/global/.json#start-layer-2") +``` + ### Opening/closing a web page in an iFrame ``` diff --git a/maps/tests/goToPageScript.js b/maps/tests/goToPageScript.js index af5b7f9f..bccbc5d2 100644 --- a/maps/tests/goToPageScript.js +++ b/maps/tests/goToPageScript.js @@ -39,7 +39,7 @@ WA.onEnterZone(zoneName, () => { label: "load grouped map", className: "popUpElement", callback: (popup => { - WA.exitSceneTo(urlRelativeMap); + WA.goToRoom(urlRelativeMap); popup.close(); }) From 8d2baa8d1a8e03a08063ffc06e3b3a4f2069c3f2 Mon Sep 17 00:00:00 2001 From: jonny Date: Wed, 16 Jun 2021 17:36:52 +0200 Subject: [PATCH 9/9] added workadventure specific url --- docs/maps/api-reference.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/maps/api-reference.md b/docs/maps/api-reference.md index 80d57ea9..b9525d3c 100644 --- a/docs/maps/api-reference.md +++ b/docs/maps/api-reference.md @@ -202,6 +202,7 @@ global urls: "/_/global/domain/path/map.json[#start-layer-name]" Example: ```javascript +WA.goToRoom("/@/tcm/workadventure/floor0") // workadventure urls WA.goToRoom('../otherMap/map.json'); WA.goToRoom("/_/global/.json#start-layer-2") ```