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 {Group} from "_Model/Group";
import {UserInterface} from "_Model/UserInterface"; import {UserInterface} from "_Model/UserInterface";
import {SetPlayerDetailsMessage} from "_Model/Websocket/SetPlayerDetailsMessage"; import {SetPlayerDetailsMessage} from "_Model/Websocket/SetPlayerDetailsMessage";
import {MessageUserPositionInterface} from "../../../front/src/Connexion";
enum SockerIoEvent { enum SockerIoEvent {
CONNECTION = "connection", CONNECTION = "connection",
@ -133,15 +132,15 @@ export class IoSocketController {
socket.on(SockerIoEvent.USER_POSITION, (message: any) => { socket.on(SockerIoEvent.USER_POSITION, (message: any) => {
try { try {
let messageUserPosition = this.hydrateMessageReceive(message); let position = this.hydratePositionReceive(message);
if (messageUserPosition instanceof Error) { if (position instanceof Error) {
return socket.emit(SockerIoEvent.MESSAGE_ERROR, {message: messageUserPosition.message}); return socket.emit(SockerIoEvent.MESSAGE_ERROR, {message: position.message});
} }
let Client = (socket as ExSocketInterface); let Client = (socket as ExSocketInterface);
// sending to all clients in room except sender // 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 //refresh position of all user in all rooms in real time
this.refreshUserPosition(Client); 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) { refreshUserPosition(Client : ExSocketInterface) {
//refresh position of all user in all rooms in real time //refresh position of all user in all rooms in real time
let rooms = (this.Io.sockets.adapter.rooms as ExtRoomsInterface); let rooms = (this.Io.sockets.adapter.rooms as ExtRoomsInterface);
@ -369,7 +359,7 @@ export class IoSocketController {
position: Client.position, position: Client.position,
name: Client.name, name: Client.name,
character: Client.character, character: Client.character,
} as MessageUserPositionInterface; };
let messageUserPosition = new MessageUserPosition(data); let messageUserPosition = new MessageUserPosition(data);
let world = this.Worlds.get(messageUserPosition.roomId); let world = this.Worlds.get(messageUserPosition.roomId);
if (!world) { if (!world) {
@ -380,9 +370,12 @@ export class IoSocketController {
} }
//Hydrate and manage error //Hydrate and manage error
hydrateMessageReceive(message: string): MessageUserPosition | Error { hydratePositionReceive(message: any): Point | Error {
try { 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) { } catch (err) {
//TODO log error //TODO log error
return new Error(err); return new Error(err);

View File

@ -149,7 +149,7 @@ export class Connexion implements ConnexionInterface {
GameManager: GameManager; GameManager: GameManager;
lastPositionShared: MessageUserPosition = null; lastPositionShared: PointInterface = null;
lastRoom: string|null = null; lastRoom: string|null = null;
constructor(GameManager: GameManager) { constructor(GameManager: GameManager) {
@ -185,26 +185,6 @@ export class Connexion implements ConnexionInterface {
* @param character * @param character
*/ */
connectSocketServer(): Promise<ConnexionInterface>{ 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 //listen event
this.positionOfAllUser(); this.positionOfAllUser();
this.disconnectServer(); this.disconnectServer();
@ -219,6 +199,25 @@ export class Connexion implements ConnexionInterface {
} as SetPlayerDetailsMessage, (id: string) => { } as SetPlayerDetailsMessage, (id: string) => {
this.userId = id; 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); resolve(this);
}); });
} }
@ -252,19 +251,13 @@ export class Connexion implements ConnexionInterface {
* @param roomId * @param roomId
* @param direction * @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){ if(!this.socket){
return; return;
} }
let messageUserPosition = new MessageUserPosition( let point = new Point(x, y, direction);
this.userId, this.lastPositionShared = point;
roomId, this.socket.emit(EventMessage.USER_POSITION, point);
new Point(x, y, direction),
this.name,
character
);
this.lastPositionShared = messageUserPosition;
this.socket.emit(EventMessage.USER_POSITION, messageUserPosition);
} }
/** /**

View File

@ -17,7 +17,6 @@ export interface HasMovedEvent {
direction: string; direction: string;
x: number; x: number;
y: number; y: number;
character: string;
} }
export interface MapObject { export interface MapObject {
@ -71,8 +70,8 @@ export class GameManager {
this.status = StatusGameManagerEnum.CURRENT_USER_CREATED; this.status = StatusGameManagerEnum.CURRENT_USER_CREATED;
} }
joinRoom(sceneKey : string, character: string){ joinRoom(sceneKey : string){
this.ConnexionInstance.joinARoom(sceneKey, character); this.ConnexionInstance.joinARoom(sceneKey);
} }
/** /**
@ -128,7 +127,7 @@ export class GameManager {
} }
pushPlayerPosition(event: HasMovedEvent) { 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 { loadMap(mapUrl: string, scene: ScenePlugin): string {

View File

@ -284,7 +284,7 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
this.createCollisionObject(); this.createCollisionObject();
//join room //join room
this.GameManager.joinRoom(this.scene.key, this.CurrentPlayer.PlayerTexture); this.GameManager.joinRoom(this.scene.key);
//listen event to share position of user //listen event to share position of user
this.CurrentPlayer.on(hasMovedEventName, this.pushPlayerPosition.bind(this)) 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) { if (x !== 0 || y !== 0) {
this.move(x, y); 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 { } else {
if (this.previousMove !== PlayerAnimationNames.None) { if (this.previousMove !== PlayerAnimationNames.None) {
direction = PlayerAnimationNames.None; direction = PlayerAnimationNames.None;
this.stop(); 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});
} }
} }