When sharing user position, only position is sent now!

This commit is contained in:
David Négrier 2020-05-15 23:40:05 +02:00
parent cdfa9acf01
commit 4d1c3517ec
5 changed files with 40 additions and 55 deletions

View file

@ -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);

View file

@ -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<ConnexionInterface>{
//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);
}
/**

View file

@ -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 {

View file

@ -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))

View file

@ -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});
}
}