Permit to dissociate data by room
authorgparant <g.parant@thecodingmachine.com>
Sun, 10 May 2020 11:58:32 +0000 (13:58 +0200)
committergparant <g.parant@thecodingmachine.com>
Sun, 10 May 2020 11:58:32 +0000 (13:58 +0200)
 - Update share room id.
 - Join room when a scene is loaded.
 - Add a room in constant variable.

back/src/Controller/AuthenticateController.ts
back/src/Controller/IoSocketController.ts
back/src/Controller/MapController.ts
back/src/Enum/EnvironmentVariable.ts
front/src/Connexion.ts
front/src/Phaser/Game/GameManager.ts
front/src/Phaser/Game/GameScene.ts

index 0b27290b34bf09abdac9272024aed6a0ceea3f19..2cd733b4ad0d9baeb640d218498d8be86b143526 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} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
+import {SECRET_KEY, ROOM_STARTED, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
 import { uuid } from 'uuidv4';
 
 export class AuthenticateController{
@@ -23,15 +23,11 @@ export class AuthenticateController{
             }
             //TODO check user email for The Coding Machine game
             let userId = uuid();
-            let token = Jwt.sign({email: param.email, roomId: ROOM, userId: userId}, SECRET_KEY, {expiresIn: '24h'});
+            let token = Jwt.sign({email: param.email, userId: userId}, SECRET_KEY, {expiresIn: '24h'});
             return res.status(OK).send({
                 token: token,
-                roomId: ROOM,
+                startedRoom: {key: ROOM_STARTED, url: URL_ROOM_STARTED},
                 userId: userId,
-                maps: [
-                    "floor0",
-                    "floor1"
-                ]
             });
         });
     }
index e055c143d06bcc0584eaaf74930f245e7d97f25a..75eb8a9078ebd9d28b5f23be4e9d1fe273f2a713 100644 (file)
@@ -104,6 +104,15 @@ export class IoSocketController {
                     return socket.emit(SockerIoEvent.MESSAGE_ERROR, JSON.stringify({message: messageUserPosition.message}))
                 }
 
+                if((socket as ExSocketInterface).roomId === messageUserPosition.roomId){
+                    return;
+                }
+
+                //lease previous room
+                if((socket as ExSocketInterface).roomId){
+                    socket.leave((socket as ExSocketInterface).roomId);
+                }
+
                 //join user in room
                 socket.join(messageUserPosition.roomId);
 
@@ -308,7 +317,7 @@ export class IoSocketController {
         }
         arrayMap.forEach((value: any) => {
             let roomId = value[0];
-            this.Io.in(roomId).emit(SockerIoEvent.USER_POSITION, JSON.stringify(arrayMap));
+            this.Io.in(roomId).emit(SockerIoEvent.USER_POSITION, JSON.stringify(value));
         });
         this.seTimeOutInProgress = setTimeout(() => {
             this.shareUsersPosition();
index 6f38d50786c728903c591c9571ae9faf77ff2e97..b7e5ffd9e2a3da93496b8b7618e56b2c811ac899 100644 (file)
@@ -2,6 +2,7 @@ import express from "express";
 import path from "path";
 import {Application, Request, Response} from "express";
 import {OK} from "http-status-codes";
+import {ROOM_STARTED, ROOMS, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable";
 
 export class MapController {
     App: Application;
@@ -20,11 +21,8 @@ export class MapController {
     getMpas() {
         this.App.get("/maps", (req: Request, res: Response) => {
             return res.status(OK).send({
-                mapStart: {key: "floor0", url: "/map/files/Floor0"},
-                maps: [
-                    {key: "floor0", url: "/map/files/Floor0"},
-                    {key: "floor1", url: "/map/files/Floor1"},
-                ]
+                mapStart: {key: ROOM_STARTED, url: URL_ROOM_STARTED},
+                maps: ROOMS
             });
         });
     }
index cfd98fcd6e6c7a099f4a2fe6f8dfac4368ae1123..87a41c91f52136bdb6d2a488c87eea60ad8b0a12 100644 (file)
@@ -1,11 +1,18 @@
 const SECRET_KEY = process.env.SECRET_KEY || "THECODINGMACHINE_SECRET_KEY";
-const ROOM = process.env.ROOM || "THECODINGMACHINE";
+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 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,
+    ROOM_STARTED,
+    URL_ROOM_STARTED,
+    ROOMS,
     MINIMUM_DISTANCE,
     GROUP_RADIUS
 }
index 2c695bd702327f95b1f5f769b762789dce344007..d1c3938d76a968510897e2abe48313a41a57b80d 100644 (file)
@@ -147,7 +147,7 @@ export interface ConnexionInterface {
 
     joinARoom(roomId: string, character: string): void;
 
-    sharePosition(x: number, y: number, direction: string, character: string): void;
+    sharePosition(x: number, y: number, direction: string, roomId: string, character: string): void;
 
     positionOfAllUser(): void;
 
@@ -182,7 +182,7 @@ 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.roomId;
+                this.startedRoom = res.data.startedRoom.key;
                 this.userId = res.data.userId;
 
                 this.socket = SocketIo(`${API_URL}`, {
@@ -195,7 +195,7 @@ export class Connexion implements ConnexionInterface {
                 this.joinARoom(this.startedRoom, characterSelected);
 
                 //share your first position
-                this.sharePosition(0, 0, characterSelected);
+                this.sharePosition(0, 0, characterSelected, this.startedRoom);
 
                 this.positionOfAllUser();
 
@@ -229,7 +229,7 @@ export class Connexion implements ConnexionInterface {
     joinARoom(roomId: string, character: string): void {
         let messageUserPosition = new MessageUserPosition(
             this.userId,
-            this.startedRoom,
+            roomId,
             new Point(0, 0),
             this.email,
             character
@@ -242,15 +242,16 @@ export class Connexion implements ConnexionInterface {
      * @param x
      * @param y
      * @param character
+     * @param roomId
      * @param direction
      */
-    sharePosition(x : number, y : number, character : string, direction : string = "none") : void{
+    sharePosition(x : number, y : number, character : string, roomId : string, direction : string = "none") : void{
         if(!this.socket){
             return;
         }
         let messageUserPosition = new MessageUserPosition(
             this.userId,
-            ROOM[0],
+            roomId,
             new Point(x, y, direction),
             this.email,
             character
@@ -276,10 +277,9 @@ export class Connexion implements ConnexionInterface {
     positionOfAllUser(): void {
         this.socket.on(EventMessage.USER_POSITION, (message: string) => {
             let dataList = JSON.parse(message);
-            dataList.forEach((UserPositions: any) => {
-                let listMessageUserPosition = new ListMessageUserPosition(UserPositions[0], UserPositions[1]);
-                this.GameManager.shareUserPosition(listMessageUserPosition);
-            });
+            let UserPositions : Array<any> = Object.values(dataList);
+            let listMessageUserPosition =  new ListMessageUserPosition(UserPositions[0], UserPositions[1]);
+            this.GameManager.shareUserPosition(listMessageUserPosition);
         });
     }
 
index 0455d5e91f6823cb3ed6b5283c51444dc02a704a..9e147e04af342296b9fbdcd83e92c02b4756ef0e 100644 (file)
@@ -71,6 +71,10 @@ export class GameManager {
         this.status = StatusGameManagerEnum.CURRENT_USER_CREATED;
     }
 
+    joinRoom(sceneKey : string, character: string){
+        this.ConnexionInstance.joinARoom(sceneKey, character);
+    }
+
     /**
      * Share position in game
      * @param ListMessageUserPosition
@@ -120,7 +124,7 @@ export class GameManager {
     }
 
     pushPlayerPosition(event: HasMovedEvent) {
-        this.ConnexionInstance.sharePosition(event.x, event.y, event.character, event.direction);
+        this.ConnexionInstance.sharePosition(event.x, event.y, event.character, this.currentGameScene.scene.key, event.direction);
     }
 }
 
index 65b0945715d8ac0856d1961d438f8160484a7ace..09bf38f2815979e655cd890e742e4d51de1d574d 100644 (file)
@@ -139,7 +139,7 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
 
 
         // Let's generate the circle for the group delimiter
-
+        //TODO they are error with cercle
         this.circleTexture = this.textures.createCanvas('circleSprite', 96, 96);
         if(!this.circleTexture || this.circleTexture.context){
             return;
@@ -268,6 +268,11 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
         //create collision
         this.createCollisionWithPlayer();
         this.createCollisionObject();
+
+        //join room
+        this.GameManager.joinRoom(this.scene.key, this.CurrentPlayer.PlayerTexture);
+
+        //listen event to share position of user
         this.CurrentPlayer.on(hasMovedEventName, this.pushPlayerPosition.bind(this))
     }