Redirecting to the correct map after login screen based on URL provided
authorDavid Négrier <d.negrier@thecodingmachine.com>
Tue, 12 May 2020 22:02:39 +0000 (00:02 +0200)
committerDavid Négrier <d.negrier@thecodingmachine.com>
Tue, 12 May 2020 22:06:58 +0000 (00:06 +0200)
back/src/Assets/Maps/Floor0/floor0.json
back/src/Assets/Maps/Floor1/floor1.json
back/src/Assets/Maps/Lyon/floortileset.png [new file with mode: 0644]
back/src/Assets/Maps/Lyon/lyon.json
back/src/Assets/Maps/Lyon/tilesets_deviant_milkian_1.png [new file with mode: 0644]
back/src/Controller/MapController.ts
front/src/Connexion.ts
front/src/Enum/EnvironmentVariable.ts
front/src/Phaser/Game/GameManager.ts
front/src/Phaser/Game/GameScene.ts
front/src/Phaser/Login/LogincScene.ts

index 631cb84c802f22b00bfe096fce97ceeff70222f9..be96afafecc098c3cbfe3d4dcb33df33b5fbc19e 100644 (file)
@@ -49,7 +49,7 @@
                 {
                  "name":"exitSceneUrl",
                  "type":"string",
-                 "value":"\/Floor1\/floor1.json"
+                 "value":"..\/Floor1\/floor1.json"
                 }],
          "type":"tilelayer",
          "visible":true,
@@ -67,7 +67,7 @@
                 {
                  "name":"exitSceneUrl",
                  "type":"string",
-                 "value":"\/Lyon\/lyon.json"
+                 "value":"..\/Lyon\/lyon.json"
                 }],
          "type":"tilelayer",
          "visible":true,
index 6791b98a8daabd191f5f921b77b254bff49697ef..b443e68fc2c98c69797255bbb3a131707c1a9444 100644 (file)
@@ -43,7 +43,7 @@
                 {
                  "name":"exitSceneUrl",
                  "type":"string",
-                 "value":"\/Floor0\/floor0.json"
+                 "value":"..\/Floor0\/floor0.json"
                 }],
          "type":"tilelayer",
          "visible":true,
diff --git a/back/src/Assets/Maps/Lyon/floortileset.png b/back/src/Assets/Maps/Lyon/floortileset.png
new file mode 100644 (file)
index 0000000..c672dcb
Binary files /dev/null and b/back/src/Assets/Maps/Lyon/floortileset.png differ
index 69fdff1debf39fc6dfddcfe44970366153d08b4d..0ed62a9c441a9537cab341d7e0a836553f4fd9ec 100644 (file)
@@ -37,7 +37,7 @@
                 {
                  "name":"exitSceneUrl",
                  "type":"string",
-                 "value":"\/Floor0\/floor0.json"
+                 "value":"..\/Floor0\/floor0.json"
                 }],
          "type":"tilelayer",
          "visible":true,
diff --git a/back/src/Assets/Maps/Lyon/tilesets_deviant_milkian_1.png b/back/src/Assets/Maps/Lyon/tilesets_deviant_milkian_1.png
new file mode 100644 (file)
index 0000000..4740013
Binary files /dev/null and b/back/src/Assets/Maps/Lyon/tilesets_deviant_milkian_1.png differ
index 65ca795610c7d3711034930d4d3c723ee5cbbe1d..bc7546cc84615a11e0de111fa2a147974818b828 100644 (file)
@@ -20,7 +20,7 @@ export class MapController {
     getMaps() {
         this.App.get("/maps", (req: Request, res: Response) => {
             return res.status(OK).send({
-                mapUrlStart: URL_ROOM_STARTED
+                mapUrlStart: req.headers.host + "/map/files" + URL_ROOM_STARTED
             });
         });
     }
index 69b6647b188a86251babad9965df1eb1dc0e8ffa..f7d1e7db10750309d0992a1bfa75877aff7e673f 100644 (file)
@@ -140,7 +140,6 @@ export interface ConnexionInterface {
     token: string;
     email: string;
     userId: string;
-    startedRoom: string;
 
     createConnexion(characterSelected: string): Promise<any>;
 
@@ -167,7 +166,6 @@ export class Connexion implements ConnexionInterface {
     token: string;
     email: string;
     userId: string;
-    startedRoom: string;
 
     GameManager: GameManager;
 
@@ -183,8 +181,6 @@ export class Connexion implements ConnexionInterface {
         return Axios.post(`${API_URL}/login`, {email: this.email})
             .then((res) => {
                 this.token = res.data.token;
-
-                this.startedRoom = getMapKeyByUrl(res.data.mapUrlStart);
                 this.userId = res.data.userId;
 
                 this.socket = SocketIo(`${API_URL}`, {
@@ -194,10 +190,10 @@ export class Connexion implements ConnexionInterface {
                 });
 
                 //join the room
-                this.joinARoom(this.startedRoom, characterSelected);
+                //this.joinARoom(this.startedRoom, characterSelected);
 
                 //share your first position
-                this.sharePosition(0, 0, characterSelected, this.startedRoom);
+                //this.sharePosition(0, 0, characterSelected, this.startedRoom);
 
                 this.positionOfAllUser();
 
index 50296fe675f524088d1ac8fe4cbc07493747b115..30250d5edda5096491888c71d64dce699e635030 100644 (file)
@@ -3,13 +3,11 @@ const API_URL = process.env.API_URL || "http://api.workadventure.localhost";
 const ROOM = [process.env.ROOM || "THECODINGMACHINE"];
 const RESOLUTION = 3;
 const ZOOM_LEVEL = 1/*3/4*/;
-const MAP_FILE_URL = `${API_URL}/map/files`;
 
 export {
     DEBUG_MODE,
     API_URL,
     RESOLUTION,
     ZOOM_LEVEL,
-    ROOM,
-    MAP_FILE_URL
+    ROOM
 }
index 428e5fb0c9e5b5cc6d0c9bda8d1acf521580fd6f..78436429aa62900625f8b7998ae70452ad4225af 100644 (file)
@@ -5,7 +5,6 @@ import {
     ListMessageUserPositionInterface
 } from "../../Connexion";
 import {SimplePeerInterface, SimplePeer} from "../../WebRtc/SimplePeer";
-import {MAP_FILE_URL} from "../../Enum/EnvironmentVariable";
 import {getMapKeyByUrl} from "../Login/LogincScene";
 import SceneManager = Phaser.Scenes.SceneManager;
 import ScenePlugin = Phaser.Scenes.ScenePlugin;
@@ -135,7 +134,7 @@ export class GameManager {
         let gameIndex = scene.getIndex(sceneKey);
         let game : Phaser.Scene = null;
         if(gameIndex === -1){
-            game = new GameScene(sceneKey, `${MAP_FILE_URL}${mapUrl}`);
+            game = new GameScene(sceneKey, mapUrl);
             scene.add(sceneKey, game, false);
         }
         return sceneKey;
index d00ad6048474ab2dddf2ccbe7879357fc622adf4..701cbe3174072cfa414c8da62b67da9528f9308a 100644 (file)
@@ -1,14 +1,13 @@
 import {GameManager, gameManager, HasMovedEvent, MapObject, StatusGameManagerEnum} from "./GameManager";
 import {GroupCreatedUpdatedMessageInterface, MessageUserPositionInterface} from "../../Connexion";
 import {CurrentGamerInterface, GamerInterface, hasMovedEventName, Player} from "../Player/Player";
-import { DEBUG_MODE, MAP_FILE_URL, RESOLUTION, ROOM, ZOOM_LEVEL} from "../../Enum/EnvironmentVariable";
+import { DEBUG_MODE, RESOLUTION, ROOM, ZOOM_LEVEL} from "../../Enum/EnvironmentVariable";
 import {ITiledMap, ITiledMapLayer, ITiledTileSet} from "../Map/ITiledMap";
 import {PLAYER_RESOURCES} from "../Entity/PlayableCaracter";
 import Texture = Phaser.Textures.Texture;
 import Sprite = Phaser.GameObjects.Sprite;
 import CanvasTexture = Phaser.Textures.CanvasTexture;
 import CreateSceneFromObjectConfig = Phaser.Types.Scenes.CreateSceneFromObjectConfig;
-import {getMapKeyByUrl} from "../Login/LogincScene";
 
 export enum Textures {
     Player = "male1"
@@ -61,7 +60,7 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
             // Triggered when the map is loaded
             // Load tiles attached to the map recursively
             this.map = data.data;
-            let url = this.MapUrlFile.substr(0, this.MapUrlFile.indexOf(`${this.MapKey}.json`));
+            let url = this.MapUrlFile.substr(0, this.MapUrlFile.lastIndexOf('/'));
             this.map.tilesets.forEach((tileset) => {
                 if (typeof tileset.name === 'undefined' || typeof tileset.image === 'undefined') {
                     console.warn("Don't know how to handle tileset ", tileset)
@@ -184,20 +183,11 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
     private loadNextGame(layer: ITiledMapLayer, mapWidth: number, tileWidth: number, tileHeight: number){
         let exitSceneUrl = this.getExitSceneUrl(layer);
 
-        let exitSceneKey = gameManager.loadMap(exitSceneUrl, this.scene);
-        /*let exitSceneKey = getMapKeyByUrl(exitSceneUrl);
+        // TODO: eventually compute a relative URL
+        let absoluteExitSceneUrl = new URL(exitSceneUrl, this.MapUrlFile).href;
+        console.log('absoluteExitSceneUrl ', absoluteExitSceneUrl);
+        let exitSceneKey = gameManager.loadMap(absoluteExitSceneUrl, this.scene);
 
-        let gameIndex = this.scene.getIndex(exitSceneKey);
-        let game : Phaser.Scene = null;
-        if(gameIndex === -1){
-            game = new GameScene(exitSceneKey, `${MAP_FILE_URL}${exitSceneUrl}`);
-            this.scene.add(exitSceneKey, game, false);
-        }else{
-            game = this.scene.get(exitSceneKey);
-        }
-        if(!game){
-            return;
-        }*/
         let tiles : any = layer.data;
         tiles.forEach((objectKey : number, key: number) => {
             if(objectKey === 0){
index 6eac78055780570ce05bdbfddb72868c0ac0d135..3136060c353c6a1cd79440fbb7608773a7f9d21a 100644 (file)
@@ -8,9 +8,15 @@ import Rectangle = Phaser.GameObjects.Rectangle;
 import {PLAYER_RESOURCES} from "../Entity/PlayableCaracter";
 import {cypressAsserter} from "../../Cypress/CypressAsserter";
 import {GroupCreatedUpdatedMessageInterface, MessageUserPositionInterface} from "../../Connexion";
-import {MAP_FILE_URL} from "../../Enum/EnvironmentVariable";
 
 export function getMapKeyByUrl(mapUrlStart: string){
+    // FIXME: the key should be computed from the full URL of the map.
+    let startPos = mapUrlStart.indexOf('://')+3;
+    let endPos = mapUrlStart.indexOf(".json");
+    console.log('MAP KEY '+mapUrlStart.substring(startPos, endPos));
+    return mapUrlStart.substring(startPos, endPos);
+
+
     let tab = mapUrlStart.split("/");
     return tab[tab.length -1].substr(0, tab[tab.length -1].indexOf(".json"));
 }
@@ -99,23 +105,40 @@ export class LogincScene extends Phaser.Scene implements GameSceneInterface {
 
     private async login(name: string) {
         return gameManager.connect(name, this.selectedPlayer.texture.key).then(() => {
-            return gameManager.loadMaps().then((scene : any) => {
-                if (!scene) {
-                    return;
-                }
-                let key = gameManager.loadMap(scene.mapUrlStart, this.scene);
+            // Do we have a start URL in the address bar? If so, let's redirect to this address
+            let mapUrl = this.findMapUrl();
+            if (mapUrl !== null) {
+                let key = gameManager.loadMap(mapUrl, this.scene);
                 this.scene.start(key);
-                return scene;
-            }).catch((err) => {
-                console.error(err);
-                throw err;
-            });
+                return mapUrl;
+            } else {
+                // If we do not have a map address in the URL, let's ask the server for a start map.
+                return gameManager.loadMaps().then((scene : any) => {
+                    if (!scene) {
+                        return;
+                    }
+                    let key = gameManager.loadMap(window.location.protocol+"//"+scene.mapUrlStart, this.scene);
+                    this.scene.start(key);
+                    return scene;
+                }).catch((err) => {
+                    console.error(err);
+                    throw err;
+                });
+            }
         }).catch((err) => {
             console.error(err);
             throw err;
         });
     }
 
+    private findMapUrl(): string|null {
+        let path = window.location.pathname;
+        if (!path.startsWith('/_/')) {
+            return null;
+        }
+        return window.location.protocol+'//'+path.substr(3);
+    }
+
     Map: Phaser.Tilemaps.Tilemap;
 
     initAnimation(): void {