Fix update world back end and deleting group in front end
authorgparant <g.parant@thecodingmachine.com>
Mon, 11 May 2020 11:17:02 +0000 (13:17 +0200)
committergparant <g.parant@thecodingmachine.com>
Mon, 11 May 2020 11:17:02 +0000 (13:17 +0200)
back/src/Controller/IoSocketController.ts
back/src/Model/Websocket/ExtRooms.ts
front/src/Phaser/Game/GameScene.ts

index e0840c69b44c7f9b46a26449ef2926ef8d543143..c855c66a3521c8f30f5e81f86cfaafae347f5973 100644 (file)
@@ -109,7 +109,7 @@ export class IoSocketController {
                 this.saveUserInformation(Client, messageUserPosition);
 
                 //add function to refresh position user in real time.
-                this.refreshUserPosition();
+                this.refreshUserPosition(Client);
 
                 socket.to(messageUserPosition.roomId).emit(SockerIoEvent.JOIN_ROOM, messageUserPosition.toString());
             });
@@ -120,11 +120,13 @@ export class IoSocketController {
                     return socket.emit(SockerIoEvent.MESSAGE_ERROR, JSON.stringify({message: messageUserPosition.message}));
                 }
 
+                let Client = (socket as ExSocketInterface);
+
                 // sending to all clients in room except sender
-                this.saveUserInformation((socket as ExSocketInterface), messageUserPosition);
+                this.saveUserInformation(Client, messageUserPosition);
 
                 //refresh position of all user in all rooms in real time
-                this.refreshUserPosition();
+                this.refreshUserPosition(Client);
             });
 
             socket.on(SockerIoEvent.WEBRTC_SIGNAL, (message: string) => {
@@ -155,7 +157,7 @@ export class IoSocketController {
                 this.sendDisconnectedEvent(Client);
 
                 //refresh position of all user in all rooms in real time
-                this.refreshUserPosition();
+                this.refreshUserPosition(Client);
 
                 //leave room
                 this.leaveRoom(Client);
@@ -299,13 +301,29 @@ export class IoSocketController {
         socket.character = message.character;
     }
 
-    refreshUserPosition() {
+    refreshUserPosition(Client : ExSocketInterface) {
         //refresh position of all user in all rooms in real time
         let rooms = (this.Io.sockets.adapter.rooms as ExtRoomsInterface);
         if (!rooms.refreshUserPosition) {
             rooms.refreshUserPosition = RefreshUserPositionFunction;
         }
-        rooms.refreshUserPosition(rooms, this.Io, this.Worlds);
+        rooms.refreshUserPosition(rooms, this.Io);
+
+        // update position in the worl
+        let data = {
+            userId: Client.userId,
+            roomId: Client.roomId,
+            position: Client.position,
+            name: Client.name,
+            character: Client.character,
+        };
+        let messageUserPosition = new MessageUserPosition(data);
+        let world = this.Worlds.get(messageUserPosition.roomId);
+        if (!world) {
+            return;
+        }
+        world.updatePosition(messageUserPosition);
+        this.Worlds.set(messageUserPosition.roomId, world);
     }
 
     //Hydrate and manage error
index 22db4fdc809b4c81be0678786c0bd33d2b83dc25..bb49283db19d5cbe1aaed4abf22da5e99524c3d6 100644 (file)
@@ -38,18 +38,6 @@ let RefreshUserPositionFunction = function(rooms : ExtRooms, Io: socketIO.Server
             dataArray = [data];
         }
         mapPositionUserByRoom.set(data.roomId, dataArray);
-
-        // update position in the worl
-        if (!Worlds) {
-            return;
-        }
-        let messageUserPosition = new MessageUserPosition(data);
-        let world = Worlds.get(messageUserPosition.roomId);
-        if (!world) {
-            return;
-        }
-        world.updatePosition(messageUserPosition);
-        Worlds.set(messageUserPosition.roomId, world);
     }
     rooms.userPositionMapByRoom = Array.from(mapPositionUserByRoom);
 }
index 26b4fe95392d29402f60a4fcb056bf3d0d22e08e..86988a8b6b74fb2285ce786da3780e01e733d248 100644 (file)
@@ -411,6 +411,9 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
     }
 
     deleteGroup(groupId: string): void {
+        if(!this.groups.get(groupId)){
+            return;
+        }
         this.groups.get(groupId).destroy();
         this.groups.delete(groupId);
     }