From b4f971c50115c180edbecf86742b73ab5e9be0bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Thu, 24 Sep 2020 10:05:16 +0200 Subject: [PATCH] Switched group position to protobuf --- back/src/Controller/IoSocketController.ts | 27 +++++++++++++++++------ back/src/Model/World.ts | 2 +- front/src/Connection.ts | 27 ++++++++++++----------- messages/messages.proto | 2 ++ messages/package.json | 2 +- 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index 4a1c9240..bf1452ac 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -27,7 +27,7 @@ import { SetPlayerDetailsMessage, SubMessage, UserMovedMessage, - BatchMessage, GroupUpdateMessage, PointMessage + BatchMessage, GroupUpdateMessage, PointMessage, GroupDeleteMessage } from "../../../messages/generated/messages_pb"; import {UserMovesMessage} from "../../../messages/generated/messages_pb"; import Direction = PositionMessage.Direction; @@ -573,7 +573,7 @@ export class IoSocketController { clientListener.emit(SocketIoEvent.USER_LEFT, clientUser.userId); //console.log("Sending USER_LEFT event"); } else if (thing instanceof Group) { - clientListener.emit(SocketIoEvent.GROUP_DELETE, thing.getId()); + this.emitDeleteGroupEvent(clientListener, thing.getId()); } else { console.error('Unexpected type for Movable.'); } @@ -584,10 +584,7 @@ export class IoSocketController { // Dispatch groups position to newly connected user world.getGroups().forEach((group: Group) => { - Client.emit(SocketIoEvent.GROUP_CREATE_UPDATE, { - position: group.getPosition(), - groupId: group.getId() - } as GroupUpdateInterface); + this.emitCreateUpdateGroupEvent(Client, group); }); //join world world.join(Client, Client.position); @@ -603,7 +600,23 @@ export class IoSocketController { groupUpdateMessage.setGroupid(group.getId()); groupUpdateMessage.setPosition(pointMessage); - socket.emit(SocketIoEvent.GROUP_CREATE_UPDATE, groupUpdateMessage.serializeBinary().buffer); + const subMessage = new SubMessage(); + subMessage.setGroupupdatemessage(groupUpdateMessage); + + const client : ExSocketInterface = socket as ExSocketInterface; + emitInBatch(client, SocketIoEvent.GROUP_CREATE_UPDATE, subMessage); + //socket.emit(SocketIoEvent.GROUP_CREATE_UPDATE, groupUpdateMessage.serializeBinary().buffer); + } + + private emitDeleteGroupEvent(socket: Socket, groupId: number): void { + const groupDeleteMessage = new GroupDeleteMessage(); + groupDeleteMessage.setGroupid(groupId); + + const subMessage = new SubMessage(); + subMessage.setGroupdeletemessage(groupDeleteMessage); + + const client : ExSocketInterface = socket as ExSocketInterface; + emitInBatch(client, SocketIoEvent.GROUP_DELETE, subMessage); } /** diff --git a/back/src/Model/World.ts b/back/src/Model/World.ts index 452c6c17..321b3e1b 100644 --- a/back/src/Model/World.ts +++ b/back/src/Model/World.ts @@ -90,9 +90,9 @@ export class World { this.positionNotifier.updatePosition(user, userPosition, user.position); const oldGroupPosition = user.group?.getPosition(); - user.group?.updatePosition(); user.position = userPosition; + user.group?.updatePosition(); if (user.silent) { return; diff --git a/front/src/Connection.ts b/front/src/Connection.ts index 02752c10..459e58c1 100644 --- a/front/src/Connection.ts +++ b/front/src/Connection.ts @@ -2,7 +2,7 @@ import Axios from "axios"; import {API_URL} from "./Enum/EnvironmentVariable"; import {MessageUI} from "./Logger/MessageUI"; import { - BatchMessage, GroupUpdateMessage, + BatchMessage, GroupDeleteMessage, GroupUpdateMessage, PositionMessage, SetPlayerDetailsMessage, UserMovedMessage, UserMovesMessage, @@ -160,6 +160,12 @@ export class Connection implements Connection { if (message.hasUsermovedmessage()) { event = EventMessage.USER_MOVED; payload = message.getUsermovedmessage(); + } else if (message.hasGroupupdatemessage()) { + event = EventMessage.GROUP_CREATE_UPDATE; + payload = message.getGroupupdatemessage(); + } else if (message.hasGroupdeletemessage()) { + event = EventMessage.GROUP_DELETE; + payload = message.getGroupdeletemessage(); } else { throw new Error('Unexpected batch message type'); } @@ -230,7 +236,6 @@ export class Connection implements Connection { if(!this.socket){ return; } - const point = new Point(x, y, direction, moving); const positionMessage = new PositionMessage(); positionMessage.setX(Math.floor(x)); positionMessage.setY(Math.floor(y)); @@ -264,6 +269,8 @@ export class Connection implements Connection { userMovesMessage.setPosition(positionMessage); userMovesMessage.setViewport(viewportMessage); + //console.log('Sending position ', positionMessage.getX(), positionMessage.getY()); + this.socket.emit(EventMessage.USER_POSITION, userMovesMessage.serializeBinary().buffer); } @@ -301,16 +308,7 @@ export class Connection implements Connection { } public onGroupUpdatedOrCreated(callback: (groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface) => void): void { - // TODO: READ THIS FROM BINARY FORMAT - // TODO: READ THIS FROM BINARY FORMAT - // TODO: READ THIS FROM BINARY FORMAT - // TODO: CHANGE THIS EVENT TO BE PART OF THE BATCHES - // TODO: CHANGE THIS EVENT TO BE PART OF THE BATCHES - // TODO: CHANGE THIS EVENT TO BE PART OF THE BATCHES - // TODO: CHANGE THIS EVENT TO BE PART OF THE BATCHES - // TODO: CHANGE THIS EVENT TO BE PART OF THE BATCHES - this.socket.on(EventMessage.GROUP_CREATE_UPDATE, (buffer: ArrayBuffer) => { - const message = GroupUpdateMessage.deserializeBinary(new Uint8Array(buffer)); + this.onBatchMessage(EventMessage.GROUP_CREATE_UPDATE, (message: GroupUpdateMessage) => { const position = message.getPosition(); if (position === undefined) { throw new Error('Missing position in GROUP_CREATE_UPDATE'); @@ -321,12 +319,15 @@ export class Connection implements Connection { position: position.toObject() } + //console.log('Group position: ', position.toObject()); callback(groupCreateUpdateMessage); }); } public onGroupDeleted(callback: (groupId: number) => void): void { - this.socket.on(EventMessage.GROUP_DELETE, callback) + this.onBatchMessage(EventMessage.GROUP_DELETE, (message: GroupDeleteMessage) => { + callback(message.getGroupid()); + }); } public onConnectError(callback: (error: object) => void): void { diff --git a/messages/messages.proto b/messages/messages.proto index 9cf6de8a..8b081fe6 100644 --- a/messages/messages.proto +++ b/messages/messages.proto @@ -50,6 +50,8 @@ message UserMovedMessage { message SubMessage { oneof message { UserMovedMessage userMovedMessage = 1; + GroupUpdateMessage groupUpdateMessage = 2; + GroupDeleteMessage groupDeleteMessage = 3; } } diff --git a/messages/package.json b/messages/package.json index b70d4900..6f2bc3f1 100644 --- a/messages/package.json +++ b/messages/package.json @@ -5,7 +5,7 @@ "main": "generated/src/proto/messages_pb.js", "scripts": { "proto": "protoc --plugin=\"protoc-gen-ts=./node_modules/.bin/protoc-gen-ts\" --js_out=\"import_style=commonjs,binary:generated\" --ts_out=\"generated\" messages.proto", - "proto:watch": "inotifywait -q -m -e close_write messages.proto | while read -r filename event; do yarn run proto; done" + "proto:watch": "yarn run proto || inotifywait -q -m -e close_write messages.proto | while read -r filename event; do yarn run proto; done" }, "repository": { "type": "git",