From 4d1c3517eca0bbc7d0ed921b5b6ca545c972c133 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20N=C3=A9grier?= Date: Fri, 15 May 2020 23:40:05 +0200 Subject: [PATCH] When sharing user position, only position is sent now! --- back/src/Controller/IoSocketController.ts | 27 +++++------ front/src/Connexion.ts | 55 ++++++++++------------- front/src/Phaser/Game/GameManager.ts | 7 ++- front/src/Phaser/Game/GameScene.ts | 2 +- front/src/Phaser/Player/Player.ts | 4 +- 5 files changed, 40 insertions(+), 55 deletions(-) diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index 459fd5f..2c83a8b 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -11,7 +11,6 @@ import {World} from "../Model/World"; import {Group} from "_Model/Group"; import {UserInterface} from "_Model/UserInterface"; import {SetPlayerDetailsMessage} from "_Model/Websocket/SetPlayerDetailsMessage"; -import {MessageUserPositionInterface} from "../../../front/src/Connexion"; enum SockerIoEvent { CONNECTION = "connection", @@ -133,15 +132,15 @@ export class IoSocketController { socket.on(SockerIoEvent.USER_POSITION, (message: any) => { try { - let messageUserPosition = this.hydrateMessageReceive(message); - if (messageUserPosition instanceof Error) { - return socket.emit(SockerIoEvent.MESSAGE_ERROR, {message: messageUserPosition.message}); + let position = this.hydratePositionReceive(message); + if (position instanceof Error) { + return socket.emit(SockerIoEvent.MESSAGE_ERROR, {message: position.message}); } let Client = (socket as ExSocketInterface); // sending to all clients in room except sender - this.saveUserInformation(Client, messageUserPosition); + Client.position = position; //refresh position of all user in all rooms in real time this.refreshUserPosition(Client); @@ -345,15 +344,6 @@ export class IoSocketController { }); } - //permit to save user position in socket - saveUserInformation(socket: ExSocketInterface, message: MessageUserPosition) { - socket.position = message.position; - socket.roomId = message.roomId; - //socket.userId = message.userId; - socket.name = message.name; - socket.character = message.character; - } - refreshUserPosition(Client : ExSocketInterface) { //refresh position of all user in all rooms in real time let rooms = (this.Io.sockets.adapter.rooms as ExtRoomsInterface); @@ -369,7 +359,7 @@ export class IoSocketController { position: Client.position, name: Client.name, character: Client.character, - } as MessageUserPositionInterface; + }; let messageUserPosition = new MessageUserPosition(data); let world = this.Worlds.get(messageUserPosition.roomId); if (!world) { @@ -380,9 +370,12 @@ export class IoSocketController { } //Hydrate and manage error - hydrateMessageReceive(message: string): MessageUserPosition | Error { + hydratePositionReceive(message: any): Point | Error { try { - return new MessageUserPosition(message); + if (!message.x || !message.y || !message.direction) { + return new Error("invalid point message sent"); + } + return new Point(message.x, message.y, message.direction); } catch (err) { //TODO log error return new Error(err); diff --git a/front/src/Connexion.ts b/front/src/Connexion.ts index 5297175..da666b8 100644 --- a/front/src/Connexion.ts +++ b/front/src/Connexion.ts @@ -149,7 +149,7 @@ export class Connexion implements ConnexionInterface { GameManager: GameManager; - lastPositionShared: MessageUserPosition = null; + lastPositionShared: PointInterface = null; lastRoom: string|null = null; constructor(GameManager: GameManager) { @@ -185,26 +185,6 @@ export class Connexion implements ConnexionInterface { * @param character */ connectSocketServer(): Promise{ - //if try to reconnect with last position - if(this.lastRoom) { - //join the room - this.joinARoom( - this.lastRoom - ); - } - - if(this.lastPositionShared) { - - //share your first position - this.sharePosition( - this.lastPositionShared ? this.lastPositionShared.position.x : 0, - this.lastPositionShared ? this.lastPositionShared.position.y : 0, - this.lastPositionShared.character, - this.lastPositionShared.roomId, - this.lastPositionShared.position.direction - ); - } - //listen event this.positionOfAllUser(); this.disconnectServer(); @@ -219,6 +199,25 @@ export class Connexion implements ConnexionInterface { } as SetPlayerDetailsMessage, (id: string) => { this.userId = id; }); + + //if try to reconnect with last position + if(this.lastRoom) { + //join the room + this.joinARoom( + this.lastRoom + ); + } + + if(this.lastPositionShared) { + + //share your first position + this.sharePosition( + this.lastPositionShared ? this.lastPositionShared.x : 0, + this.lastPositionShared ? this.lastPositionShared.y : 0, + this.lastPositionShared.direction + ); + } + resolve(this); }); } @@ -252,19 +251,13 @@ export class Connexion implements ConnexionInterface { * @param roomId * @param direction */ - sharePosition(x : number, y : number, character : string, roomId : string, direction : string = "none") : void{ + sharePosition(x : number, y : number, direction : string = "none") : void{ if(!this.socket){ return; } - let messageUserPosition = new MessageUserPosition( - this.userId, - roomId, - new Point(x, y, direction), - this.name, - character - ); - this.lastPositionShared = messageUserPosition; - this.socket.emit(EventMessage.USER_POSITION, messageUserPosition); + let point = new Point(x, y, direction); + this.lastPositionShared = point; + this.socket.emit(EventMessage.USER_POSITION, point); } /** diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts index 1728d0d..a84653c 100644 --- a/front/src/Phaser/Game/GameManager.ts +++ b/front/src/Phaser/Game/GameManager.ts @@ -17,7 +17,6 @@ export interface HasMovedEvent { direction: string; x: number; y: number; - character: string; } export interface MapObject { @@ -71,8 +70,8 @@ export class GameManager { this.status = StatusGameManagerEnum.CURRENT_USER_CREATED; } - joinRoom(sceneKey : string, character: string){ - this.ConnexionInstance.joinARoom(sceneKey, character); + joinRoom(sceneKey : string){ + this.ConnexionInstance.joinARoom(sceneKey); } /** @@ -128,7 +127,7 @@ export class GameManager { } pushPlayerPosition(event: HasMovedEvent) { - this.ConnexionInstance.sharePosition(event.x, event.y, event.character, this.currentGameScene.scene.key, event.direction); + this.ConnexionInstance.sharePosition(event.x, event.y, event.direction); } loadMap(mapUrl: string, scene: ScenePlugin): string { diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 1e4d1e6..56c775e 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -284,7 +284,7 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat this.createCollisionObject(); //join room - this.GameManager.joinRoom(this.scene.key, this.CurrentPlayer.PlayerTexture); + this.GameManager.joinRoom(this.scene.key); //listen event to share position of user this.CurrentPlayer.on(hasMovedEventName, this.pushPlayerPosition.bind(this)) diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index dd33bd3..73aad77 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -81,12 +81,12 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G } if (x !== 0 || y !== 0) { this.move(x, y); - this.emit(hasMovedEventName, {direction, x: this.x, y: this.y, character: this.PlayerTexture}); + this.emit(hasMovedEventName, {direction, x: this.x, y: this.y}); } else { if (this.previousMove !== PlayerAnimationNames.None) { direction = PlayerAnimationNames.None; this.stop(); - this.emit(hasMovedEventName, {direction, x: this.x, y: this.y, character: this.PlayerTexture}); + this.emit(hasMovedEventName, {direction, x: this.x, y: this.y}); } } -- 2.25.1