From 4af46b1b3fe10d51c472bdc1a9023c6495504c05 Mon Sep 17 00:00:00 2001 From: arp Date: Thu, 8 Oct 2020 18:51:24 +0200 Subject: [PATCH] simplified mapUrl parsing --- back/src/Controller/AuthenticateController.ts | 7 +- front/src/Connexion/ConnectionManager.ts | 14 +++- .../src/Phaser/Entity/GameSceneDescriptor.ts | 6 ++ front/src/Phaser/Game/GameManager.ts | 66 +++++++++++++++---- front/src/Phaser/Game/GameScene.ts | 27 ++++---- front/src/Phaser/Login/EnableCameraScene.ts | 46 ++----------- 6 files changed, 93 insertions(+), 73 deletions(-) create mode 100644 front/src/Phaser/Entity/GameSceneDescriptor.ts diff --git a/back/src/Controller/AuthenticateController.ts b/back/src/Controller/AuthenticateController.ts index 7a8a95dd..a178b530 100644 --- a/back/src/Controller/AuthenticateController.ts +++ b/back/src/Controller/AuthenticateController.ts @@ -36,6 +36,7 @@ export class AuthenticateController extends BaseController { //todo: what to do if the organizationMemberToken is already used? const organizationMemberToken:string|null = param.organizationMemberToken; + const mapSlug:string|null = param.mapSlug; try { let userUuid; @@ -48,10 +49,14 @@ export class AuthenticateController extends BaseController { userUuid = data.userUuid; mapUrlStart = data.mapUrlStart; newUrl = this.getNewUrlOnAdminAuth(data) + } else if (mapSlug !== null) { + userUuid = uuid(); + mapUrlStart = mapSlug; + newUrl = null; } else { userUuid = uuid(); mapUrlStart = host.replace('api.', 'maps.') + URL_ROOM_STARTED; - newUrl = null; + newUrl = '_/global/'+mapUrlStart; } const authToken = Jwt.sign({userUuid: userUuid}, SECRET_KEY, {expiresIn: '24h'}); diff --git a/front/src/Connexion/ConnectionManager.ts b/front/src/Connexion/ConnectionManager.ts index 217f9e8d..a9d15dd9 100644 --- a/front/src/Connexion/ConnectionManager.ts +++ b/front/src/Connexion/ConnectionManager.ts @@ -17,10 +17,20 @@ class ConnectionManager { private authToken:string|null = null; private userUuid: string|null = null; + //todo: get map infos from url in anonym case public async init(): Promise { + let organizationMemberToken = null; + let teamSlug = null; + let mapSlug = null; const match = /\/register\/(.+)/.exec(window.location.toString()); - const organizationMemberToken = match ? match[1] : null; - this.initPromise = Axios.post(`${API_URL}/login`, {organizationMemberToken}).then(res => res.data); + if (match) { + organizationMemberToken = match[1]; + } else { + const match = /\/_\/(.+)\/(.+)/.exec(window.location.toString()); + teamSlug = match ? match[1] : null; + mapSlug = match ? match[2] : null; + } + this.initPromise = Axios.post(`${API_URL}/login`, {organizationMemberToken, teamSlug, mapSlug}).then(res => res.data); const data = await this.initPromise this.authToken = data.authToken; this.userUuid = data.userUuid; diff --git a/front/src/Phaser/Entity/GameSceneDescriptor.ts b/front/src/Phaser/Entity/GameSceneDescriptor.ts new file mode 100644 index 00000000..df114c28 --- /dev/null +++ b/front/src/Phaser/Entity/GameSceneDescriptor.ts @@ -0,0 +1,6 @@ +export class GameSceneDescriptor { + + constructor(MapKey : string, MapUrlFile: string, instance: string, key: string) { + this.roomId = '';// + } +} \ No newline at end of file diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts index 960ce7e2..5188d2fe 100644 --- a/front/src/Phaser/Game/GameManager.ts +++ b/front/src/Phaser/Game/GameManager.ts @@ -1,4 +1,4 @@ -import {GameScene} from "./GameScene"; +import {GameScene, GameSceneInitInterface} from "./GameScene"; import { StartMapInterface } from "../../Connexion/ConnexionModels"; @@ -13,6 +13,11 @@ export interface HasMovedEvent { y: number; } +export interface loadMapResponseInterface { + key: string, + startLayerName: string; +} + export class GameManager { private playerName!: string; private characterLayers!: string[]; @@ -29,15 +34,6 @@ export class GameManager { this.characterLayers = layers; } - loadStartMap() : Promise { - return connectionManager.getMapUrlStart().then(mapUrlStart => { - return { - mapUrlStart: mapUrlStart, - startInstance: "global", //todo: is this property still usefull? - } - }); - } - getPlayerName(): string { return this.playerName; } @@ -46,8 +42,47 @@ export class GameManager { return this.characterLayers; } - loadMap(mapUrl: string, scene: Phaser.Scenes.ScenePlugin, instance: string): string { - const sceneKey = GameScene.getMapKeyByUrl(mapUrl); + /** + * Returns the map URL and the instance from the current URL + */ + private findMapUrl(): [string, string]|null { + const path = window.location.pathname; + if (!path.startsWith('/_/')) { + return null; + } + const instanceAndMap = path.substr(3); + const firstSlash = instanceAndMap.indexOf('/'); + if (firstSlash === -1) { + return null; + } + const instance = instanceAndMap.substr(0, firstSlash); + return [window.location.protocol+'//'+instanceAndMap.substr(firstSlash+1), instance]; + } + + public loadStartingMap(scene: Phaser.Scenes.ScenePlugin): Promise { + // Do we have a start URL in the address bar? If so, let's redirect to this address + const instanceAndMapUrl = this.findMapUrl(); + if (instanceAndMapUrl !== null) { + const [mapUrl, instance] = instanceAndMapUrl; + const key = gameManager.loadMap(mapUrl, scene, instance); + const startLayerName = window.location.hash ? window.location.hash.substr(1) : ''; + return Promise.resolve({key, startLayerName}); + + } else { + // If we do not have a map address in the URL, let's ask the server for a start map. + return connectionManager.getMapUrlStart().then((mapUrlStart: string) => { + const key = gameManager.loadMap(window.location.protocol + "//" + mapUrlStart, scene, 'global'); + return {key, startLayerName: ''} + }).catch((err) => { + console.error(err); + throw err; + }); + } + + } + + public loadMap(mapUrl: string, scene: Phaser.Scenes.ScenePlugin, instance: string): string { + const sceneKey = this.getMapKeyByUrl(mapUrl); const gameIndex = scene.getIndex(sceneKey); if(gameIndex === -1){ @@ -56,6 +91,13 @@ export class GameManager { } return sceneKey; } + + public getMapKeyByUrl(mapUrlStart: string) : string { + // FIXME: the key should be computed from the full URL of the map. + const startPos = mapUrlStart.indexOf('://')+3; + const endPos = mapUrlStart.indexOf(".json"); + return mapUrlStart.substring(startPos, endPos); + } } export const gameManager = new GameManager(); diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index b3a5f104..a9591f21 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -138,17 +138,17 @@ export class GameScene extends Phaser.Scene implements CenterListener { private outlinedItem: ActionableItem|null = null; private userInputManager!: UserInputManager; - static createFromUrl(mapUrlFile: string, instance: string, key: string|null = null): GameScene { - const mapKey = GameScene.getMapKeyByUrl(mapUrlFile); - if (key === null) { - key = mapKey; + static createFromUrl(mapUrlFile: string, instance: string, gameSceneKey: string|null = null): GameScene { + const mapKey = gameManager.getMapKeyByUrl(mapUrlFile); + if (gameSceneKey === null) { + gameSceneKey = mapKey; } - return new GameScene(mapKey, mapUrlFile, instance, key); + return new GameScene(mapKey, mapUrlFile, instance, gameSceneKey); } - constructor(MapKey : string, MapUrlFile: string, instance: string, key: string) { + constructor(MapKey : string, MapUrlFile: string, instance: string, gameSceneKey: string) { super({ - key: key + key: gameSceneKey }); this.GameManager = gameManager; @@ -588,9 +588,9 @@ export class GameScene extends Phaser.Scene implements CenterListener { this.simplePeer.closeAllConnections(); this.simplePeer.unregister(); - const key = 'somekey'+Math.round(Math.random()*10000); - const game : Phaser.Scene = GameScene.createFromUrl(this.MapUrlFile, this.instance, key); - this.scene.add(key, game, true, + const gameSceneKey = 'somekey'+Math.round(Math.random()*10000); + const game : Phaser.Scene = GameScene.createFromUrl(this.MapUrlFile, this.instance, gameSceneKey); + this.scene.add(gameSceneKey, game, true, { initPosition: { x: this.CurrentPlayer.x, @@ -1136,12 +1136,7 @@ export class GameScene extends Phaser.Scene implements CenterListener { this.groups.delete(groupId); } - public static getMapKeyByUrl(mapUrlStart: string) : string { - // FIXME: the key should be computed from the full URL of the map. - const startPos = mapUrlStart.indexOf('://')+3; - const endPos = mapUrlStart.indexOf(".json"); - return mapUrlStart.substring(startPos, endPos); - } + /** * Sends to the server an event emitted by one of the ActionableItems. diff --git a/front/src/Phaser/Login/EnableCameraScene.ts b/front/src/Phaser/Login/EnableCameraScene.ts index 6ac1ad47..672facbb 100644 --- a/front/src/Phaser/Login/EnableCameraScene.ts +++ b/front/src/Phaser/Login/EnableCameraScene.ts @@ -94,7 +94,7 @@ export class EnableCameraScene extends Phaser.Scene { this.add.existing(this.logo); this.input.keyboard.on('keyup-ENTER', () => { - return this.login(); + this.login(); }); this.getElementByIdOrFail('webRtcSetup').classList.add('active'); @@ -258,7 +258,7 @@ export class EnableCameraScene extends Phaser.Scene { this.soundMeterSprite.setVolume(this.soundMeter.getVolume()); } - private async login(): Promise { + private async login(): Promise { this.getElementByIdOrFail('webRtcSetup').style.display = 'none'; this.soundMeter.stop(); window.removeEventListener('resize', this.repositionCallback); @@ -266,46 +266,8 @@ export class EnableCameraScene extends Phaser.Scene { mediaManager.stopCamera(); mediaManager.stopMicrophone(); - // Do we have a start URL in the address bar? If so, let's redirect to this address - const instanceAndMapUrl = this.findMapUrl(); - if (instanceAndMapUrl !== null) { - const [mapUrl, instance] = instanceAndMapUrl; - const key = gameManager.loadMap(mapUrl, this.scene, instance); - this.scene.start(key, { - startLayerName: window.location.hash ? window.location.hash.substr(1) : undefined - } as GameSceneInitInterface); - return { - mapUrlStart: mapUrl, - startInstance: instance - }; - } else { - // If we do not have a map address in the URL, let's ask the server for a start map. - return gameManager.loadStartMap().then((startMap: StartMapInterface) => { - const key = gameManager.loadMap(window.location.protocol + "//" + startMap.mapUrlStart, this.scene, startMap.startInstance); - this.scene.start(key); - return startMap; - }).catch((err) => { - console.error(err); - throw err; - }); - } - } - - /** - * Returns the map URL and the instance from the current URL - */ - private findMapUrl(): [string, string]|null { - const path = window.location.pathname; - if (!path.startsWith('/_/')) { - return null; - } - const instanceAndMap = path.substr(3); - const firstSlash = instanceAndMap.indexOf('/'); - if (firstSlash === -1) { - return null; - } - const instance = instanceAndMap.substr(0, firstSlash); - return [window.location.protocol+'//'+instanceAndMap.substr(firstSlash+1), instance]; + let {key, startLayerName} = await gameManager.loadStartingMap(this.scene); + this.scene.start(key, {startLayerName}); } private async getDevices() {