Implement Distance Merge Request

This commit is contained in:
gparant 2020-04-27 00:44:25 +02:00
parent c907048c12
commit 881bb04eb0
3 changed files with 27 additions and 8 deletions

View File

@ -6,10 +6,12 @@ import {ExSocketInterface} from "../Model/Websocket/ExSocketInterface"; //TODO f
import Jwt, {JsonWebTokenError} from "jsonwebtoken"; import Jwt, {JsonWebTokenError} from "jsonwebtoken";
import {SECRET_KEY} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..." import {SECRET_KEY} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
import {ExtRooms, RefreshUserPositionFunction} from "../Model/Websocket/ExtRoom"; import {ExtRooms, RefreshUserPositionFunction} from "../Model/Websocket/ExtRoom";
import {ExtRoomsInterface} from "_Model/Websocket/ExtRoomsInterface"; import {ExtRoomsInterface} from "../Model/Websocket/ExtRoomsInterface";
import {World} from "../Model/World";
export class IoSocketController{ export class IoSocketController{
Io: socketIO.Server; Io: socketIO.Server;
World: World;
constructor(server : http.Server) { constructor(server : http.Server) {
this.Io = socketIO(server); this.Io = socketIO(server);
@ -29,6 +31,7 @@ export class IoSocketController{
this.ioConnection(); this.ioConnection();
this.shareUsersPosition(); this.shareUsersPosition();
this.World = new World(this.connectedUser, this.disConnectedUser);
} }
ioConnection() { ioConnection() {
@ -50,6 +53,9 @@ export class IoSocketController{
//join user in room //join user in room
socket.join(messageUserPosition.roomId); socket.join(messageUserPosition.roomId);
//join user in world
this.World.join(messageUserPosition);
// sending to all clients in room except sender // sending to all clients in room except sender
this.saveUserInformation((socket as ExSocketInterface), messageUserPosition); this.saveUserInformation((socket as ExSocketInterface), messageUserPosition);
@ -67,6 +73,9 @@ export class IoSocketController{
return socket.emit("message-error", JSON.stringify({message: messageUserPosition.message})); return socket.emit("message-error", JSON.stringify({message: messageUserPosition.message}));
} }
// update position in the worl
this.World.updatePosition(messageUserPosition);
// sending to all clients in room except sender // sending to all clients in room except sender
this.saveUserInformation((socket as ExSocketInterface), messageUserPosition); this.saveUserInformation((socket as ExSocketInterface), messageUserPosition);
@ -135,8 +144,7 @@ export class IoSocketController{
//Hydrate and manage error //Hydrate and manage error
hydrateMessageReceive(message : string) : MessageUserPosition | Error{ hydrateMessageReceive(message : string) : MessageUserPosition | Error{
try { try {
let data = JSON.parse(message); return new MessageUserPosition(JSON.parse(message));
return new MessageUserPosition(data);
}catch (err) { }catch (err) {
//TODO log error //TODO log error
return new Error(err); return new Error(err);
@ -180,4 +188,16 @@ export class IoSocketController{
this.shareUsersPosition(); this.shareUsersPosition();
}, 10); }, 10);
} }
//connected user
connectedUser(user1 : string, user2 : string){
console.log("connectedUser => user1", user1);
console.log("connectedUser => user2", user2);
}
//connected user
disConnectedUser(user1 : string, user2 : string){
console.log("disConnectedUser => user1", user1);
console.log("disConnectedUser => user2", user2);
}
} }

View File

@ -3,7 +3,7 @@ export class Message {
roomId: string; roomId: string;
constructor(data: any) { constructor(data: any) {
if(!data.userId || !data.roomId){ if (!data.userId || !data.roomId) {
throw Error("userId or roomId cannot be null"); throw Error("userId or roomId cannot be null");
} }
this.userId = data.userId; this.userId = data.userId;
@ -13,7 +13,7 @@ export class Message {
toJson() { toJson() {
return { return {
userId: this.userId, userId: this.userId,
roomId: this.roomId, roomId: this.roomId
} }
} }
} }

View File

@ -27,10 +27,9 @@ export class Point implements PointInterface{
export class MessageUserPosition extends Message{ export class MessageUserPosition extends Message{
position: PointInterface; position: PointInterface;
constructor(message: string) { constructor(message: any) {
super(message); super(message);
let data = JSON.parse(message); this.position = new Point(message.position.x, message.position.y, message.position.direction);
this.position = new Point(data.position.x, data.position.y, data.position.direction);
} }
toString() { toString() {