Removing the Message class and merging it with MessageUserPosition (since it is only...
authorDavid Négrier <d.negrier@thecodingmachine.com>
Sat, 16 May 2020 13:44:40 +0000 (15:44 +0200)
committerDavid Négrier <d.negrier@thecodingmachine.com>
Sat, 16 May 2020 13:44:45 +0000 (15:44 +0200)
Taking advantage of the TypeScript Constructor Assignment too to reduce the amount of code!

back/src/Controller/IoSocketController.ts
back/src/Model/Websocket/Message.ts [deleted file]
back/src/Model/Websocket/MessageUserPosition.ts

index 93dd801075a2352bcb1e18b53f87758377c34763..9ea88991e4dc6f76ddbde0fc6327bd2b7569a290 100644 (file)
@@ -115,13 +115,7 @@ export class IoSocketController {
                     //add function to refresh position user in real time.
                     this.refreshUserPosition(Client);
 
-                    let messageUserPosition = new MessageUserPosition({
-                        userId: Client.id,
-                        roomId: Client.roomId,
-                        name: Client.name,
-                        character: Client.character,
-                        position: new Point(0, 0, 'none')
-                    });
+                    let messageUserPosition = new MessageUserPosition(Client.id, Client.name, Client.character,new Point(0, 0, 'none'));
 
                     socket.to(roomId).emit(SockerIoEvent.JOIN_ROOM, messageUserPosition);
                 } catch (e) {
@@ -353,14 +347,7 @@ export class IoSocketController {
         rooms.refreshUserPosition(rooms, this.Io);
 
         // update position in the world
-        let data = {
-            userId: Client.id,
-            roomId: Client.roomId,
-            position: Client.position,
-            name: Client.name,
-            character: Client.character,
-        };
-        let messageUserPosition = new MessageUserPosition(data);
+        let messageUserPosition = new MessageUserPosition(Client.id, Client.name, Client.character, Client.position);
         let world = this.Worlds.get(Client.roomId);
         if (!world) {
             return;
diff --git a/back/src/Model/Websocket/Message.ts b/back/src/Model/Websocket/Message.ts
deleted file mode 100644 (file)
index 2e92815..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-export class Message {
-    userId: string;
-    name: string;
-    character: string;
-
-    constructor(data: any) {
-        if (!data.userId) {
-            console.error("Got invalid message", data);
-            throw Error("userId cannot be null");
-        }
-        this.userId = data.userId;
-        this.name = data.name;
-        this.character = data.character;
-    }
-}
index 4f27c26a5e17dec02409a099618642066d9e4320..8a0c1d5140624b120f8e68509a1ca8d70c7eab04 100644 (file)
@@ -2,25 +2,11 @@ import {Message} from "./Message";
 import {PointInterface} from "./PointInterface";
 
 export class Point implements PointInterface{
-    x: number;
-    y: number;
-    direction: string;
-
-    constructor(x : number, y : number, direction : string = "none") {
-        if(x === null || y === null){
-            throw Error("position x and y cannot be null");
-        }
-        this.x = x;
-        this.y = y;
-        this.direction = direction;
+    constructor(public x : number, public y : number, public direction : string = "none") {
     }
 }
 
-export class MessageUserPosition extends Message{
-    position: PointInterface;
-
-    constructor(message: any) {
-        super(message);
-        this.position = new Point(message.position.x, message.position.y, message.position.direction);
+export class MessageUserPosition {
+    constructor(public userId: string, public name: string, public character: string, public position: PointInterface) {
     }
 }