Fix feedback @moufmouf strategy of maps managing by back.
authorgparant <g.parant@thecodingmachine.com>
Sun, 10 May 2020 16:34:55 +0000 (18:34 +0200)
committergparant <g.parant@thecodingmachine.com>
Sun, 10 May 2020 16:34:55 +0000 (18:34 +0200)
README.md
back/src/Assets/Maps/Floor0/floor0.json
back/src/Assets/Maps/Floor1/floor1.json
back/src/Controller/AuthenticateController.ts
back/src/Controller/MapController.ts
back/src/Enum/EnvironmentVariable.ts
doc/images/exit_layer_map.png
front/src/Connexion.ts
front/src/Phaser/Game/GameManager.ts
front/src/Phaser/Game/GameScene.ts
front/src/Phaser/Login/LogincScene.ts

index 8fdee4e81eda17c05e3a05cbe65a0f952c1d3a5e..162279f3b700492097ee7a2f584f54e75debd325 100644 (file)
--- a/README.md
+++ b/README.md
@@ -47,7 +47,7 @@ A few things to notice:
 
 If you have exit scene
 - You must create layer "exit". The layer have cases where the gamer can switch to the next scene.
-- In layer properties, you must add "exitSceneKey" property. It represent a key map of the next scene. Be careful, if you want that the next map will be correctly loaded, you must check that the map exists in the list of the maps application. The variable that represents maps in the application is "ROOMS" constant variable.
+- In layer properties, you must add "exitSceneUrl" property. It represents a url map of the next scene. Be careful, if you want that the next map will be correctly loaded, you must check that the map files are in folder  `back/src/Assets/Maps/<your map folder>`. The files will be accessible by url `<HOST>/map/files/<your map folder>/...`.
 
 ![](doc/images/exit_layer_map.png)
 
index af7ab21f0c1a617a9430292d5864c5cb32dc4667..21a675c3fc914a4cfa59fb66341d58ad00991f4e 100644 (file)
@@ -47,9 +47,9 @@
          "opacity":1,
          "properties":[
                 {
-                 "name":"exitSceneKey",
+                 "name":"exitSceneUrl",
                  "type":"string",
-                 "value":"floor1"
+                 "value":"\/map\/files\/Floor1\/floor1.json"
                 }],
          "type":"tilelayer",
          "visible":true,
index fdf064f89b2c6979138fc0a00de712cea165c61b..d54840f58ce9a5ec1aef0d7c9dfa55c534024304 100644 (file)
@@ -41,9 +41,9 @@
          "opacity":1,
          "properties":[
                 {
-                 "name":"exitSceneKey",
+                 "name":"exitSceneUrl",
                  "type":"string",
-                 "value":"floor0"
+                 "value":"\/map\/files\/Floor0\/floor0.json"
                 }],
          "type":"tilelayer",
          "visible":true,
index 2cd733b4ad0d9baeb640d218498d8be86b143526..6eaa14ca3521d3449ddcda8f9372e80718aacf0e 100644 (file)
@@ -1,7 +1,7 @@
 import {Application, Request, Response} from "express";
 import Jwt from "jsonwebtoken";
 import {BAD_REQUEST, OK} from "http-status-codes";
-import {SECRET_KEY, ROOM_STARTED, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
+import {SECRET_KEY, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
 import { uuid } from 'uuidv4';
 
 export class AuthenticateController{
@@ -26,7 +26,7 @@ export class AuthenticateController{
             let token = Jwt.sign({email: param.email, userId: userId}, SECRET_KEY, {expiresIn: '24h'});
             return res.status(OK).send({
                 token: token,
-                startedRoom: {key: ROOM_STARTED, url: URL_ROOM_STARTED},
+                mapUrlStart: URL_ROOM_STARTED,
                 userId: userId,
             });
         });
index 5a19644b994fbd49c81fecd06239acb36f93c797..65ca795610c7d3711034930d4d3c723ee5cbbe1d 100644 (file)
@@ -1,7 +1,7 @@
 import express from "express";
 import {Application, Request, Response} from "express";
 import {OK} from "http-status-codes";
-import {ROOM_STARTED, ROOMS, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable";
+import {URL_ROOM_STARTED} from "../Enum/EnvironmentVariable";
 
 export class MapController {
     App: Application;
@@ -20,8 +20,7 @@ export class MapController {
     getMaps() {
         this.App.get("/maps", (req: Request, res: Response) => {
             return res.status(OK).send({
-                mapStart: {key: ROOM_STARTED, url: URL_ROOM_STARTED},
-                maps: ROOMS
+                mapUrlStart: URL_ROOM_STARTED
             });
         });
     }
index 87a41c91f52136bdb6d2a488c87eea60ad8b0a12..8ee1830253104057e1f60e49f43a590db07f4f64 100644 (file)
@@ -1,18 +1,11 @@
 const SECRET_KEY = process.env.SECRET_KEY || "THECODINGMACHINE_SECRET_KEY";
-const ROOM_STARTED = "floor0";
-const URL_ROOM_STARTED = "/map/files/Floor0";
-const ROOMS = [
-    {key: "floor0", url: "/map/files/Floor0"},
-    {key: "floor1", url: "/map/files/Floor1"},
-]
+const URL_ROOM_STARTED = "/map/files/Floor0/floor0.json";
 const MINIMUM_DISTANCE = process.env.MINIMUM_DISTANCE ? Number(process.env.MINIMUM_DISTANCE) : 64;
 const GROUP_RADIUS = process.env.GROUP_RADIUS ? Number(process.env.GROUP_RADIUS) : 48;
 
 export {
     SECRET_KEY,
-    ROOM_STARTED,
     URL_ROOM_STARTED,
-    ROOMS,
     MINIMUM_DISTANCE,
     GROUP_RADIUS
 }
index 122610c007633609cdb2ad8d92ac3765391667ab..2a9a5b275c282f56945a8d7c205b1217f528b769 100644 (file)
Binary files a/doc/images/exit_layer_map.png and b/doc/images/exit_layer_map.png differ
index e7d07efe678d5e322e2e6c614b51d348f6a3a26e..69b6647b188a86251babad9965df1eb1dc0e8ffa 100644 (file)
@@ -3,6 +3,7 @@ import {GameManager} from "./Phaser/Game/GameManager";
 const SocketIo = require('socket.io-client');
 import Axios from "axios";
 import {API_URL} from "./Enum/EnvironmentVariable";
+import {getMapKeyByUrl} from "./Phaser/Login/LogincScene";
 
 enum EventMessage{
     WEBRTC_SIGNAL = "webrtc-signal",
@@ -182,7 +183,8 @@ export class Connexion implements ConnexionInterface {
         return Axios.post(`${API_URL}/login`, {email: this.email})
             .then((res) => {
                 this.token = res.data.token;
-                this.startedRoom = res.data.startedRoom.key;
+
+                this.startedRoom = getMapKeyByUrl(res.data.mapUrlStart);
                 this.userId = res.data.userId;
 
                 this.socket = SocketIo(`${API_URL}`, {
index 9e147e04af342296b9fbdcd83e92c02b4756ef0e..16c19cacaba01d6982ea176dcfb2764ead404501 100644 (file)
@@ -5,6 +5,7 @@ import {
     ListMessageUserPositionInterface
 } from "../../Connexion";
 import {SimplePeerInterface, SimplePeer} from "../../WebRtc/SimplePeer";
+import {API_URL} from "../../Enum/EnvironmentVariable";
 
 export enum StatusGameManagerEnum {
     IN_PROGRESS = 1,
@@ -30,7 +31,6 @@ export class GameManager {
     private playerName: string;
     SimplePeer : SimplePeerInterface;
     private characterUserSelected: string;
-    Maps: Array<MapObject>;
 
     constructor() {
         this.status = StatusGameManagerEnum.IN_PROGRESS;
@@ -50,7 +50,6 @@ export class GameManager {
 
     loadMaps(){
         return this.ConnexionInstance.loadMaps().then((data) => {
-            this.Maps = data.maps;
             return data;
         }).catch((err) => {
             throw err;
index 782714925c2f6e02faaf630bd0da121a96613388..d4d98c55dd11e8b32a935a4862bbd1ab3b990080 100644 (file)
@@ -12,6 +12,7 @@ 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"
@@ -64,17 +65,18 @@ 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`));
             this.map.tilesets.forEach((tileset) => {
                 if (typeof tileset.name === 'undefined' || typeof tileset.image === 'undefined') {
                     console.warn("Don't know how to handle tileset ", tileset)
                     return;
                 }
                 //TODO strategy to add access token
-                this.load.image(tileset.name, `${this.MapUrlFile}/${tileset.image}`);
+                this.load.image(tileset.name, `${url}/${tileset.image}`);
             })
         });
         //TODO strategy to add access token
-        this.load.tilemapTiledJSON(this.MapKey, `${this.MapUrlFile}/${this.MapKey}.json`);
+        this.load.tilemapTiledJSON(this.MapKey, this.MapUrlFile);
 
         //add player png
         PLAYER_RESOURCES.forEach((playerResource: any) => {
@@ -164,16 +166,17 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
      */
     private loadNextGame(layer: ITiledMapLayer, mapWidth: number, tileWidth: number, tileHeight: number){
         let properties : any = layer.properties;
-        let nextSceneKey = properties.find((property:any) => property.name === "exitSceneKey");
-        let nextMap : MapObject = gameManager.Maps.find((map: MapObject) => map.key === nextSceneKey.value);
+        let exitSceneUrl = properties.find((property:any) => property.name === "exitSceneUrl");
 
-        let gameIndex = this.scene.getIndex(nextMap.key);
+        let exitSceneKey = getMapKeyByUrl(exitSceneUrl.value);
+
+        let gameIndex = this.scene.getIndex(exitSceneKey);
         let game : Phaser.Scene = null;
         if(gameIndex === -1){
-            game = new GameScene(nextMap.key, `${API_URL}${nextMap.url}`);
-            this.scene.add(nextSceneKey, game, false);
+            game = new GameScene(exitSceneKey, `${API_URL}${exitSceneUrl.value}`);
+            this.scene.add(exitSceneKey, game, false);
         }else{
-            game = this.scene.get(nextMap.key);
+            game = this.scene.get(exitSceneKey);
         }
         if(!game){
             return;
@@ -192,7 +195,7 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
                 yStart: (y * tileWidth),
                 xEnd: ((x +1) * tileHeight),
                 yEnd: ((y + 1) * tileHeight),
-                key: nextMap.key
+                key: exitSceneKey
             })
         });
     }
index b747fdf697d8aff489d88c5ddfb96d528ebe79f1..35d372d2e8dfb7b41e67cb8d969e8bd764fc8b6e 100644 (file)
@@ -10,6 +10,11 @@ import {cypressAsserter} from "../../Cypress/CypressAsserter";
 import {GroupCreatedUpdatedMessageInterface, MessageUserPositionInterface} from "../../Connexion";
 import {API_URL} from "../../Enum/EnvironmentVariable";
 
+export function getMapKeyByUrl(mapUrlStart: string){
+    let tab = mapUrlStart.split("/");
+    return tab[tab.length -1].substr(0, tab[tab.length -1].indexOf(".json"));
+}
+
 //todo: put this constants in a dedicated file
 export const LoginSceneName = "LoginScene";
 enum LoginTextures {
@@ -98,9 +103,10 @@ export class LogincScene extends Phaser.Scene implements GameSceneInterface {
                 if (!scene) {
                     return;
                 }
-                let game = new GameScene(scene.mapStart.key, `${API_URL}${scene.mapStart.url}`);
-                this.scene.add(scene.mapStart.key, game, false);
-                this.scene.start(scene.mapStart.key);
+                let key = getMapKeyByUrl(scene.mapUrlStart);
+                let game = new GameScene(key,`${API_URL}${scene.mapUrlStart}`);
+                this.scene.add(key, game, false);
+                this.scene.start(key);
                 return scene;
             }).catch((err) => {
                 console.error(err);