From b148ca3708b07abfedb19ec6c948c08f4e6dc1b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 18 Sep 2020 18:16:26 +0200 Subject: [PATCH] Linting --- back/src/Controller/IoSocketController.ts | 20 +++++++++++++++----- back/src/Model/Websocket/ProtobufUtils.ts | 6 +++--- front/src/Connection.ts | 2 +- front/src/Network/ProtobufClientUtils.ts | 6 +++--- front/src/Phaser/Game/GameScene.ts | 3 +-- 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index f3eab483..985e6da2 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -32,7 +32,6 @@ import { import {UserMovesMessage} from "../../../messages/generated/messages_pb"; import Direction = PositionMessage.Direction; import {ProtobufUtils} from "../Model/Websocket/ProtobufUtils"; -import toPositionMessage = ProtobufUtils.toPositionMessage; enum SocketIoEvent { CONNECTION = "connection", @@ -284,6 +283,12 @@ export class IoSocketController { socket.on(SocketIoEvent.USER_POSITION, (message: unknown): void => { //console.log(SockerIoEvent.USER_POSITION, userMovesMessage); try { + if (!(message instanceof Buffer)) { + socket.emit(SocketIoEvent.MESSAGE_ERROR, {message: 'Invalid USER_POSITION message. Expecting binary buffer.'}); + console.warn('Invalid USER_POSITION message received (expecting binary buffer): ', message); + return; + } + const userMovesMessage = UserMovesMessage.deserializeBinary(new Uint8Array(message as ArrayBuffer)); const userMoves = userMovesMessage.toObject(); @@ -375,14 +380,19 @@ export class IoSocketController { }); // Let's send the user id to the user - socket.on(SocketIoEvent.SET_PLAYER_DETAILS, (message: any, answerFn) => { - console.log(SocketIoEvent.SET_PLAYER_DETAILS, message); + socket.on(SocketIoEvent.SET_PLAYER_DETAILS, (message: unknown, answerFn) => { + //console.log(SocketIoEvent.SET_PLAYER_DETAILS, message); + if (!(message instanceof Buffer)) { + socket.emit(SocketIoEvent.MESSAGE_ERROR, {message: 'Invalid SET_PLAYER_DETAILS message. Expecting binary buffer.'}); + console.warn('Invalid SET_PLAYER_DETAILS message received (expecting binary buffer): ', message); + return; + } const playerDetailsMessage = SetPlayerDetailsMessage.deserializeBinary(new Uint8Array(message)); const playerDetails = { name: playerDetailsMessage.getName(), characterLayers: playerDetailsMessage.getCharacterlayersList() }; - console.log(SocketIoEvent.SET_PLAYER_DETAILS, playerDetails); + //console.log(SocketIoEvent.SET_PLAYER_DETAILS, playerDetails); if (!isSetPlayerDetailsMessage(playerDetails)) { socket.emit(SocketIoEvent.MESSAGE_ERROR, {message: 'Invalid SET_PLAYER_DETAILS message.'}); console.warn('Invalid SET_PLAYER_DETAILS message received: ', playerDetails); @@ -547,7 +557,7 @@ export class IoSocketController { const userMovedMessage = new UserMovedMessage(); userMovedMessage.setUserid(clientUser.userId); - userMovedMessage.setPosition(toPositionMessage(clientUser.position)); + userMovedMessage.setPosition(ProtobufUtils.toPositionMessage(clientUser.position)); const subMessage = new SubMessage(); subMessage.setUsermovedmessage(userMovedMessage); diff --git a/back/src/Model/Websocket/ProtobufUtils.ts b/back/src/Model/Websocket/ProtobufUtils.ts index ff73b7c0..516a744e 100644 --- a/back/src/Model/Websocket/ProtobufUtils.ts +++ b/back/src/Model/Websocket/ProtobufUtils.ts @@ -1,11 +1,11 @@ import {PointInterface} from "./PointInterface"; import {PositionMessage} from "../../../../messages/generated/messages_pb"; import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface"; +import Direction = PositionMessage.Direction; -export namespace ProtobufUtils { - import Direction = PositionMessage.Direction; +export class ProtobufUtils { - export function toPositionMessage(point: PointInterface): PositionMessage { + public static toPositionMessage(point: PointInterface): PositionMessage { let direction: PositionMessage.DirectionMap[keyof PositionMessage.DirectionMap]; switch (point.direction) { case 'up': diff --git a/front/src/Connection.ts b/front/src/Connection.ts index 78f107fe..fa04feb8 100644 --- a/front/src/Connection.ts +++ b/front/src/Connection.ts @@ -152,7 +152,7 @@ export class Connection implements Connection { * Messages inside batched messages are extracted and sent to listeners directly. */ this.socket.on(EventMessage.BATCH, (batchedMessagesBinary: ArrayBuffer) => { - const batchMessage = BatchMessage.deserializeBinary(new Uint8Array(batchedMessagesBinary as ArrayBuffer)); + const batchMessage = BatchMessage.deserializeBinary(new Uint8Array(batchedMessagesBinary)); for (const message of batchMessage.getPayloadList()) { let event: string; diff --git a/front/src/Network/ProtobufClientUtils.ts b/front/src/Network/ProtobufClientUtils.ts index 311ba80d..6755025c 100644 --- a/front/src/Network/ProtobufClientUtils.ts +++ b/front/src/Network/ProtobufClientUtils.ts @@ -1,10 +1,10 @@ import {PositionMessage} from "../../../messages/generated/messages_pb"; import {PointInterface} from "../Connection"; +import Direction = PositionMessage.Direction; -export namespace ProtobufClientUtils { - import Direction = PositionMessage.Direction; +export class ProtobufClientUtils { - export function toPointInterface(position: PositionMessage): PointInterface { + public static toPointInterface(position: PositionMessage): PointInterface { let direction: string; switch (position.getDirection()) { case Direction.UP: diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index bf73b31d..c7a7fd3e 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -42,7 +42,6 @@ import {ActionableItem} from "../Items/ActionableItem"; import {UserInputManager} from "../UserInput/UserInputManager"; import {UserMovedMessage} from "../../../../messages/generated/messages_pb"; import {ProtobufClientUtils} from "../../Network/ProtobufClientUtils"; -import toPointInterface = ProtobufClientUtils.toPointInterface; export enum Textures { @@ -224,7 +223,7 @@ export class GameScene extends Phaser.Scene implements CenterListener { const messageUserMoved: MessageUserMovedInterface = { userId: message.getUserid(), - position: toPointInterface(position) + position: ProtobufClientUtils.toPointInterface(position) } this.updatePlayerPosition(messageUserMoved);