From: gparant Date: Sun, 26 Apr 2020 22:44:25 +0000 (+0200) Subject: Implement Distance Merge Request X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=881bb04eb04002dc306fbc5c1c5c4ff3cd639bc3;p=libreadventure.git Implement Distance Merge Request --- diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index c69a64e..77f3ac9 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -6,10 +6,12 @@ import {ExSocketInterface} from "../Model/Websocket/ExSocketInterface"; //TODO f import Jwt, {JsonWebTokenError} from "jsonwebtoken"; import {SECRET_KEY} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..." 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{ Io: socketIO.Server; + World: World; constructor(server : http.Server) { this.Io = socketIO(server); @@ -29,6 +31,7 @@ export class IoSocketController{ this.ioConnection(); this.shareUsersPosition(); + this.World = new World(this.connectedUser, this.disConnectedUser); } ioConnection() { @@ -50,6 +53,9 @@ export class IoSocketController{ //join user in room socket.join(messageUserPosition.roomId); + //join user in world + this.World.join(messageUserPosition); + // sending to all clients in room except sender this.saveUserInformation((socket as ExSocketInterface), messageUserPosition); @@ -67,6 +73,9 @@ export class IoSocketController{ 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 this.saveUserInformation((socket as ExSocketInterface), messageUserPosition); @@ -135,8 +144,7 @@ export class IoSocketController{ //Hydrate and manage error hydrateMessageReceive(message : string) : MessageUserPosition | Error{ try { - let data = JSON.parse(message); - return new MessageUserPosition(data); + return new MessageUserPosition(JSON.parse(message)); }catch (err) { //TODO log error return new Error(err); @@ -180,4 +188,16 @@ export class IoSocketController{ this.shareUsersPosition(); }, 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); + } } diff --git a/back/src/Model/Websocket/Message.ts b/back/src/Model/Websocket/Message.ts index d726968..da26546 100644 --- a/back/src/Model/Websocket/Message.ts +++ b/back/src/Model/Websocket/Message.ts @@ -3,7 +3,7 @@ export class Message { roomId: string; constructor(data: any) { - if(!data.userId || !data.roomId){ + if (!data.userId || !data.roomId) { throw Error("userId or roomId cannot be null"); } this.userId = data.userId; @@ -13,7 +13,7 @@ export class Message { toJson() { return { userId: this.userId, - roomId: this.roomId, + roomId: this.roomId } } } \ No newline at end of file diff --git a/back/src/Model/Websocket/MessageUserPosition.ts b/back/src/Model/Websocket/MessageUserPosition.ts index 117c99c..65f8f6c 100644 --- a/back/src/Model/Websocket/MessageUserPosition.ts +++ b/back/src/Model/Websocket/MessageUserPosition.ts @@ -27,10 +27,9 @@ export class Point implements PointInterface{ export class MessageUserPosition extends Message{ position: PointInterface; - constructor(message: string) { + constructor(message: any) { super(message); - let data = JSON.parse(message); - this.position = new Point(data.position.x, data.position.y, data.position.direction); + this.position = new Point(message.position.x, message.position.y, message.position.direction); } toString() {