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",
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);
});
}
- //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);
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) {
}
//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);
GameManager: GameManager;
- lastPositionShared: MessageUserPosition = null;
+ lastPositionShared: PointInterface = null;
lastRoom: string|null = null;
constructor(GameManager: GameManager) {
* @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();
} 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);
});
}
* @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);
}
/**
direction: string;
x: number;
y: number;
- character: string;
}
export interface MapObject {
this.status = StatusGameManagerEnum.CURRENT_USER_CREATED;
}
- joinRoom(sceneKey : string, character: string){
- this.ConnexionInstance.joinARoom(sceneKey, character);
+ joinRoom(sceneKey : string){
+ this.ConnexionInstance.joinARoom(sceneKey);
}
/**
}
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 {
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))
}
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});
}
}