Removing the Message class and merging it with MessageUserPosition (since it is only ever used it in MessageUserPosition)

Taking advantage of the TypeScript Constructor Assignment too to reduce the amount of code!
This commit is contained in:
David Négrier 2020-05-16 15:44:40 +02:00
parent 3b6ace03fa
commit b20357c1ee
3 changed files with 5 additions and 47 deletions

View file

@ -115,13 +115,7 @@ export class IoSocketController {
//add function to refresh position user in real time. //add function to refresh position user in real time.
this.refreshUserPosition(Client); this.refreshUserPosition(Client);
let messageUserPosition = new MessageUserPosition({ let messageUserPosition = new MessageUserPosition(Client.id, Client.name, Client.character,new Point(0, 0, 'none'));
userId: Client.id,
roomId: Client.roomId,
name: Client.name,
character: Client.character,
position: new Point(0, 0, 'none')
});
socket.to(roomId).emit(SockerIoEvent.JOIN_ROOM, messageUserPosition); socket.to(roomId).emit(SockerIoEvent.JOIN_ROOM, messageUserPosition);
} catch (e) { } catch (e) {
@ -353,14 +347,7 @@ export class IoSocketController {
rooms.refreshUserPosition(rooms, this.Io); rooms.refreshUserPosition(rooms, this.Io);
// update position in the world // update position in the world
let data = { let messageUserPosition = new MessageUserPosition(Client.id, Client.name, Client.character, Client.position);
userId: Client.id,
roomId: Client.roomId,
position: Client.position,
name: Client.name,
character: Client.character,
};
let messageUserPosition = new MessageUserPosition(data);
let world = this.Worlds.get(Client.roomId); let world = this.Worlds.get(Client.roomId);
if (!world) { if (!world) {
return; return;

View file

@ -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;
}
}

View file

@ -2,25 +2,11 @@ import {Message} from "./Message";
import {PointInterface} from "./PointInterface"; import {PointInterface} from "./PointInterface";
export class Point implements PointInterface{ export class Point implements PointInterface{
x: number; constructor(public x : number, public y : number, public direction : string = "none") {
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;
} }
} }
export class MessageUserPosition extends Message{ export class MessageUserPosition {
position: PointInterface; constructor(public userId: string, public name: string, public character: string, public position: PointInterface) {
constructor(message: any) {
super(message);
this.position = new Point(message.position.x, message.position.y, message.position.direction);
} }
} }