2020-04-27 15:03:05 +02:00
|
|
|
import {GameManager} from "./Phaser/Game/GameManager";
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-04-05 20:57:14 +02:00
|
|
|
const SocketIo = require('socket.io-client');
|
|
|
|
import Axios from "axios";
|
2020-05-10 17:31:27 +02:00
|
|
|
import {API_URL} from "./Enum/EnvironmentVariable";
|
2020-05-10 18:34:55 +02:00
|
|
|
import {getMapKeyByUrl} from "./Phaser/Login/LogincScene";
|
2020-05-13 20:22:42 +02:00
|
|
|
import {MessageUI} from "./Logger/MessageUI";
|
2020-04-05 20:57:14 +02:00
|
|
|
|
2020-04-25 16:05:33 +02:00
|
|
|
enum EventMessage{
|
|
|
|
WEBRTC_SIGNAL = "webrtc-signal",
|
|
|
|
WEBRTC_START = "webrtc-start",
|
2020-05-02 20:46:02 +02:00
|
|
|
WEBRTC_JOIN_ROOM = "webrtc-join-room",
|
2020-04-25 16:05:33 +02:00
|
|
|
JOIN_ROOM = "join-room",
|
|
|
|
USER_POSITION = "user-position",
|
2020-05-02 20:46:02 +02:00
|
|
|
MESSAGE_ERROR = "message-error",
|
2020-05-08 00:35:36 +02:00
|
|
|
WEBRTC_DISCONNECT = "webrtc-disconect",
|
|
|
|
GROUP_CREATE_UPDATE = "group-create-update",
|
|
|
|
GROUP_DELETE = "group-delete",
|
2020-05-13 20:22:42 +02:00
|
|
|
|
|
|
|
CONNECT_ERROR = "connect_error",
|
2020-05-14 23:19:48 +02:00
|
|
|
RECONNECT = "reconnect",
|
|
|
|
ATTRIBUTE_USER_ID = "attribute-user-id" // Sent from server to client just after the connexion is established to give the client its unique id.
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
class Message {
|
2020-04-05 20:57:14 +02:00
|
|
|
userId: string;
|
|
|
|
roomId: string;
|
2020-05-03 22:24:14 +02:00
|
|
|
name: string;
|
2020-05-08 15:18:22 +02:00
|
|
|
character: string;
|
2020-04-05 20:57:14 +02:00
|
|
|
|
2020-05-08 15:18:22 +02:00
|
|
|
constructor(userId : string, roomId : string, name: string, character: string) {
|
2020-04-05 20:57:14 +02:00
|
|
|
this.userId = userId;
|
|
|
|
this.roomId = roomId;
|
2020-05-03 22:24:14 +02:00
|
|
|
this.name = name;
|
2020-05-08 15:18:22 +02:00
|
|
|
this.character = character;
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
export interface PointInterface {
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
direction : string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Point implements PointInterface{
|
2020-04-05 20:57:14 +02:00
|
|
|
x: number;
|
|
|
|
y: number;
|
2020-04-07 21:02:23 +02:00
|
|
|
direction : string;
|
2020-04-05 20:57:14 +02:00
|
|
|
|
2020-04-07 21:02:23 +02:00
|
|
|
constructor(x : number, y : number, direction : string = "none") {
|
2020-04-05 20:57:14 +02:00
|
|
|
if(x === null || y === null){
|
|
|
|
throw Error("position x and y cannot be null");
|
|
|
|
}
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
2020-04-07 21:02:23 +02:00
|
|
|
this.direction = direction;
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
export interface MessageUserPositionInterface {
|
|
|
|
userId: string;
|
|
|
|
roomId: string;
|
2020-05-03 22:24:14 +02:00
|
|
|
name: string;
|
2020-05-08 15:18:22 +02:00
|
|
|
character: string;
|
2020-04-10 12:54:05 +02:00
|
|
|
position: PointInterface;
|
|
|
|
}
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
class MessageUserPosition extends Message implements MessageUserPositionInterface{
|
2020-04-05 20:57:14 +02:00
|
|
|
position: PointInterface;
|
|
|
|
|
2020-05-08 15:18:22 +02:00
|
|
|
constructor(userId : string, roomId : string, point : Point, name: string, character: string) {
|
|
|
|
super(userId, roomId, name, character);
|
2020-04-05 20:57:14 +02:00
|
|
|
this.position = point;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
export interface ListMessageUserPositionInterface {
|
2020-04-25 16:05:33 +02:00
|
|
|
roomId: string;
|
2020-04-10 12:54:05 +02:00
|
|
|
listUsersPosition: Array<MessageUserPosition>;
|
|
|
|
}
|
2020-04-25 16:05:33 +02:00
|
|
|
|
|
|
|
class ListMessageUserPosition {
|
|
|
|
roomId: string;
|
2020-04-10 12:54:05 +02:00
|
|
|
listUsersPosition: Array<MessageUserPosition>;
|
|
|
|
|
2020-04-25 16:05:33 +02:00
|
|
|
constructor(roomId: string, data: any) {
|
2020-04-10 12:54:05 +02:00
|
|
|
this.roomId = roomId;
|
|
|
|
this.listUsersPosition = new Array<MessageUserPosition>();
|
|
|
|
data.forEach((userPosition: any) => {
|
|
|
|
this.listUsersPosition.push(new MessageUserPosition(
|
|
|
|
userPosition.userId,
|
|
|
|
userPosition.roomId,
|
|
|
|
new Point(
|
|
|
|
userPosition.position.x,
|
|
|
|
userPosition.position.y,
|
|
|
|
userPosition.position.direction
|
2020-05-03 22:24:14 +02:00
|
|
|
),
|
2020-05-06 01:50:01 +02:00
|
|
|
userPosition.name,
|
2020-05-08 15:18:22 +02:00
|
|
|
userPosition.character
|
2020-04-10 12:54:05 +02:00
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-05-08 00:35:36 +02:00
|
|
|
export interface PositionInterface {
|
|
|
|
x: number,
|
|
|
|
y: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface GroupCreatedUpdatedMessageInterface {
|
|
|
|
position: PositionInterface,
|
|
|
|
groupId: string
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:57:00 +02:00
|
|
|
export interface ConnexionInterface {
|
2020-04-25 16:05:33 +02:00
|
|
|
socket: any;
|
|
|
|
token: string;
|
|
|
|
email: string;
|
2020-04-12 13:57:00 +02:00
|
|
|
userId: string;
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-05-08 15:18:22 +02:00
|
|
|
createConnexion(characterSelected: string): Promise<any>;
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-05-09 19:41:21 +02:00
|
|
|
loadMaps(): Promise<any>;
|
|
|
|
|
2020-05-08 15:18:22 +02:00
|
|
|
joinARoom(roomId: string, character: string): void;
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-05-10 13:58:32 +02:00
|
|
|
sharePosition(x: number, y: number, direction: string, roomId: string, character: string): void;
|
2020-04-25 16:05:33 +02:00
|
|
|
|
|
|
|
positionOfAllUser(): void;
|
|
|
|
|
|
|
|
/*webrtc*/
|
2020-04-26 19:12:01 +02:00
|
|
|
sendWebrtcSignal(signal: any, roomId: string, userId?: string, receiverId?: string): void;
|
2020-04-25 16:05:33 +02:00
|
|
|
|
|
|
|
receiveWebrtcSignal(callBack: Function): void;
|
|
|
|
|
|
|
|
receiveWebrtcStart(callBack: Function): void;
|
2020-05-02 20:46:02 +02:00
|
|
|
|
|
|
|
disconnectMessage(callBack: Function): void;
|
2020-04-12 13:57:00 +02:00
|
|
|
}
|
2020-04-25 16:05:33 +02:00
|
|
|
|
|
|
|
export class Connexion implements ConnexionInterface {
|
|
|
|
socket: any;
|
|
|
|
token: string;
|
|
|
|
email: string;
|
2020-04-10 12:54:05 +02:00
|
|
|
userId: string;
|
2020-04-05 20:57:14 +02:00
|
|
|
|
2020-04-27 15:03:05 +02:00
|
|
|
GameManager: GameManager;
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-05-13 20:22:42 +02:00
|
|
|
lastPositionShared: MessageUserPosition = null;
|
|
|
|
|
2020-04-27 15:03:05 +02:00
|
|
|
constructor(email : string, GameManager: GameManager) {
|
2020-04-05 20:57:14 +02:00
|
|
|
this.email = email;
|
2020-04-07 20:41:35 +02:00
|
|
|
this.GameManager = GameManager;
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 19:41:21 +02:00
|
|
|
/**
|
|
|
|
* @param characterSelected
|
|
|
|
*/
|
2020-05-08 15:18:22 +02:00
|
|
|
createConnexion(characterSelected: string): Promise<ConnexionInterface> {
|
2020-05-14 23:19:48 +02:00
|
|
|
/*return Axios.post(`${API_URL}/login`, {email: this.email})
|
2020-04-05 20:57:14 +02:00
|
|
|
.then((res) => {
|
|
|
|
this.token = res.data.token;
|
2020-05-14 23:19:48 +02:00
|
|
|
this.userId = res.data.userId;*/
|
2020-04-05 20:57:14 +02:00
|
|
|
|
|
|
|
this.socket = SocketIo(`${API_URL}`, {
|
2020-05-14 23:19:48 +02:00
|
|
|
/*query: {
|
2020-04-05 20:57:14 +02:00
|
|
|
token: this.token
|
2020-05-14 23:19:48 +02:00
|
|
|
}*/
|
2020-04-05 20:57:14 +02:00
|
|
|
});
|
|
|
|
|
2020-05-13 20:22:42 +02:00
|
|
|
this.connectSocketServer();
|
2020-05-08 00:35:36 +02:00
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
// TODO: maybe trigger promise only when connexion is established?
|
|
|
|
let promise = new Promise<ConnexionInterface>((resolve, reject) => {
|
|
|
|
/*console.log('PROMISE CREATED')
|
|
|
|
this.socket.on('connection', () => {
|
|
|
|
console.log('CONNECTED');
|
|
|
|
resolve(this);
|
|
|
|
});*/
|
|
|
|
resolve(this);
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
|
|
|
|
/* return res.data;
|
2020-04-05 20:57:14 +02:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
throw err;
|
2020-05-14 23:19:48 +02:00
|
|
|
});*/
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
2020-05-10 17:31:27 +02:00
|
|
|
|
2020-05-13 20:22:42 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param character
|
|
|
|
*/
|
|
|
|
connectSocketServer(character : string = null){
|
|
|
|
//if try to reconnect with last position
|
|
|
|
if(this.lastPositionShared) {
|
|
|
|
//join the room
|
|
|
|
this.joinARoom(
|
|
|
|
this.lastPositionShared.roomId,
|
|
|
|
this.lastPositionShared.character
|
|
|
|
);
|
|
|
|
|
|
|
|
//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
|
2020-05-14 23:19:48 +02:00
|
|
|
this.attributeUserId();
|
2020-05-13 20:22:42 +02:00
|
|
|
this.positionOfAllUser();
|
|
|
|
this.disconnectServer();
|
|
|
|
this.errorMessage();
|
|
|
|
this.groupUpdatedOrCreated();
|
|
|
|
this.groupDeleted();
|
|
|
|
}
|
|
|
|
|
2020-05-10 17:31:27 +02:00
|
|
|
//TODO add middleware with access token to secure api
|
|
|
|
loadMaps() : Promise<any> {
|
|
|
|
return Axios.get(`${API_URL}/maps`)
|
|
|
|
.then((res) => {
|
|
|
|
return res.data;
|
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
throw err;
|
|
|
|
});
|
2020-05-09 19:41:21 +02:00
|
|
|
}
|
2020-04-05 20:57:14 +02:00
|
|
|
|
|
|
|
/**
|
2020-05-06 01:50:01 +02:00
|
|
|
*
|
2020-04-05 20:57:14 +02:00
|
|
|
* @param roomId
|
2020-05-08 15:18:22 +02:00
|
|
|
* @param character
|
2020-04-05 20:57:14 +02:00
|
|
|
*/
|
2020-05-08 15:18:22 +02:00
|
|
|
joinARoom(roomId: string, character: string): void {
|
2020-05-06 01:50:01 +02:00
|
|
|
let messageUserPosition = new MessageUserPosition(
|
|
|
|
this.userId,
|
2020-05-10 13:58:32 +02:00
|
|
|
roomId,
|
2020-05-06 01:50:01 +02:00
|
|
|
new Point(0, 0),
|
|
|
|
this.email,
|
2020-05-08 15:18:22 +02:00
|
|
|
character
|
2020-05-06 01:50:01 +02:00
|
|
|
);
|
2020-05-15 22:04:49 +02:00
|
|
|
this.socket.emit(EventMessage.JOIN_ROOM, messageUserPosition);
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-07 21:02:23 +02:00
|
|
|
*
|
2020-04-05 20:57:14 +02:00
|
|
|
* @param x
|
|
|
|
* @param y
|
2020-05-08 15:18:22 +02:00
|
|
|
* @param character
|
2020-05-10 13:58:32 +02:00
|
|
|
* @param roomId
|
2020-04-07 21:02:23 +02:00
|
|
|
* @param direction
|
2020-04-05 20:57:14 +02:00
|
|
|
*/
|
2020-05-10 13:58:32 +02:00
|
|
|
sharePosition(x : number, y : number, character : string, roomId : string, direction : string = "none") : void{
|
2020-04-07 20:46:30 +02:00
|
|
|
if(!this.socket){
|
|
|
|
return;
|
|
|
|
}
|
2020-05-06 01:50:01 +02:00
|
|
|
let messageUserPosition = new MessageUserPosition(
|
|
|
|
this.userId,
|
2020-05-10 13:58:32 +02:00
|
|
|
roomId,
|
2020-05-06 01:50:01 +02:00
|
|
|
new Point(x, y, direction),
|
|
|
|
this.email,
|
2020-05-08 15:18:22 +02:00
|
|
|
character
|
2020-05-06 01:50:01 +02:00
|
|
|
);
|
2020-05-13 20:22:42 +02:00
|
|
|
this.lastPositionShared = messageUserPosition;
|
2020-05-15 22:04:49 +02:00
|
|
|
this.socket.emit(EventMessage.USER_POSITION, messageUserPosition);
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 23:19:48 +02:00
|
|
|
attributeUserId(): void {
|
|
|
|
// This event is received as soon as the connexion is established.
|
|
|
|
// It allows informing the browser of its own user id.
|
|
|
|
this.socket.on(EventMessage.ATTRIBUTE_USER_ID, (userId: string) => {
|
|
|
|
console.log('Received my user id: ', userId);
|
|
|
|
this.userId = userId;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-05 20:57:14 +02:00
|
|
|
/**
|
|
|
|
* The data sent is an array with information for each user :
|
|
|
|
* [
|
|
|
|
* {
|
|
|
|
* userId: <string>,
|
|
|
|
* roomId: <string>,
|
|
|
|
* position: {
|
|
|
|
* x : <number>,
|
2020-04-07 21:03:33 +02:00
|
|
|
* y : <number>,
|
|
|
|
* direction: <string>
|
2020-04-05 20:57:14 +02:00
|
|
|
* }
|
|
|
|
* },
|
|
|
|
* ...
|
|
|
|
* ]
|
|
|
|
**/
|
2020-04-25 16:05:33 +02:00
|
|
|
positionOfAllUser(): void {
|
|
|
|
this.socket.on(EventMessage.USER_POSITION, (message: string) => {
|
2020-05-15 22:04:49 +02:00
|
|
|
let dataList = message;
|
2020-05-10 13:58:32 +02:00
|
|
|
let UserPositions : Array<any> = Object.values(dataList);
|
|
|
|
let listMessageUserPosition = new ListMessageUserPosition(UserPositions[0], UserPositions[1]);
|
|
|
|
this.GameManager.shareUserPosition(listMessageUserPosition);
|
2020-04-05 20:57:14 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-08 00:35:36 +02:00
|
|
|
private groupUpdatedOrCreated(): void {
|
|
|
|
this.socket.on(EventMessage.GROUP_CREATE_UPDATE, (groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface) => {
|
|
|
|
//console.log('Group ', groupCreateUpdateMessage.groupId, " position :", groupCreateUpdateMessage.position.x, groupCreateUpdateMessage.position.y)
|
|
|
|
this.GameManager.shareGroupPosition(groupCreateUpdateMessage);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private groupDeleted(): void {
|
|
|
|
this.socket.on(EventMessage.GROUP_DELETE, (groupId: string) => {
|
|
|
|
this.GameManager.deleteGroup(groupId);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-26 19:12:01 +02:00
|
|
|
sendWebrtcSignal(signal: any, roomId: string, userId? : string, receiverId? : string) {
|
2020-05-15 22:04:49 +02:00
|
|
|
return this.socket.emit(EventMessage.WEBRTC_SIGNAL, {
|
2020-04-26 17:43:21 +02:00
|
|
|
userId: userId ? userId : this.userId,
|
2020-04-26 19:12:01 +02:00
|
|
|
receiverId: receiverId ? receiverId : this.userId,
|
2020-04-25 16:05:33 +02:00
|
|
|
roomId: roomId,
|
|
|
|
signal: signal
|
2020-05-15 22:04:49 +02:00
|
|
|
});
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
receiveWebrtcStart(callback: Function) {
|
|
|
|
this.socket.on(EventMessage.WEBRTC_START, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
receiveWebrtcSignal(callback: Function) {
|
2020-05-02 20:46:02 +02:00
|
|
|
return this.socket.on(EventMessage.WEBRTC_SIGNAL, callback);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errorMessage(): void {
|
|
|
|
this.socket.on(EventMessage.MESSAGE_ERROR, (message: string) => {
|
|
|
|
console.error(EventMessage.MESSAGE_ERROR, message);
|
2020-04-05 20:57:14 +02:00
|
|
|
})
|
|
|
|
}
|
2020-05-02 20:46:02 +02:00
|
|
|
|
2020-05-13 20:22:42 +02:00
|
|
|
disconnectServer(): void {
|
|
|
|
this.socket.on(EventMessage.CONNECT_ERROR, () => {
|
|
|
|
MessageUI.warningMessage("Trying to connect!");
|
|
|
|
});
|
|
|
|
|
|
|
|
this.socket.on(EventMessage.RECONNECT, () => {
|
|
|
|
MessageUI.removeMessage();
|
|
|
|
this.connectSocketServer();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-02 20:46:02 +02:00
|
|
|
disconnectMessage(callback: Function): void {
|
|
|
|
this.socket.on(EventMessage.WEBRTC_DISCONNECT, callback);
|
|
|
|
}
|
2020-05-08 00:35:36 +02:00
|
|
|
}
|