workadventure/front/src/Connexion.ts

348 lines
9.9 KiB
TypeScript
Raw Normal View History

import {GameManager} from "./Phaser/Game/GameManager";
import Axios from "axios";
import {API_URL} from "./Enum/EnvironmentVariable";
2020-05-13 20:22:42 +02:00
import {MessageUI} from "./Logger/MessageUI";
import {SetPlayerDetailsMessage} from "./Messages/SetPlayerDetailsMessage";
const SocketIo = require('socket.io-client');
import Socket = SocketIOClient.Socket;
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",
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",
RECONNECT = "reconnect",
SET_PLAYER_DETAILS = "set-player-details" // Send the name and character to the server (on connect), receive back the id.
2020-04-25 16:05:33 +02:00
}
class Message {
userId: string;
roomId: string;
name: string;
character: string;
constructor(userId : string, roomId : string, name: string, character: string) {
this.userId = userId;
this.roomId = roomId;
this.name = name;
this.character = character;
}
}
export interface PointInterface {
x: number;
y: number;
direction : string;
}
class Point implements PointInterface{
x: number;
y: number;
2020-04-07 21:02:23 +02:00
direction : string;
2020-04-07 21:02:23 +02:00
constructor(x : number, y : number, direction : string = "none") {
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;
}
}
export interface MessageUserPositionInterface {
userId: string;
roomId: string;
name: string;
character: string;
position: PointInterface;
}
2020-04-25 16:05:33 +02:00
class MessageUserPosition extends Message implements MessageUserPositionInterface{
position: PointInterface;
constructor(userId : string, roomId : string, point : Point, name: string, character: string) {
super(userId, roomId, name, character);
this.position = point;
}
}
export interface ListMessageUserPositionInterface {
2020-04-25 16:05:33 +02:00
roomId: string;
listUsersPosition: Array<MessageUserPosition>;
}
2020-04-25 16:05:33 +02:00
class ListMessageUserPosition {
roomId: string;
listUsersPosition: Array<MessageUserPosition>;
2020-04-25 16:05:33 +02:00
constructor(roomId: string, data: any) {
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
),
userPosition.name,
userPosition.character
));
});
}
}
2020-04-25 16:05:33 +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;
name: string;
2020-04-12 13:57:00 +02:00
userId: string;
2020-04-25 16:05:33 +02:00
createConnexion(name: string, characterSelected: string): Promise<any>;
2020-04-25 16:05:33 +02:00
loadMaps(): Promise<any>;
joinARoom(roomId: string, character: string): void;
2020-04-25 16:05:33 +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*/
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: Socket;
2020-04-25 16:05:33 +02:00
token: string;
name: string; // TODO: drop "name" storage here
character: string;
userId: string;
GameManager: GameManager;
2020-04-07 20:41:35 +02:00
2020-05-13 20:22:42 +02:00
lastPositionShared: MessageUserPosition = null;
constructor(GameManager: GameManager) {
2020-04-07 20:41:35 +02:00
this.GameManager = GameManager;
}
createConnexion(name: string, characterSelected: string): Promise<ConnexionInterface> {
this.name = name;
this.character = characterSelected;
/*return Axios.post(`${API_URL}/login`, {email: this.email})
.then((res) => {
this.token = res.data.token;
this.userId = res.data.userId;*/
this.socket = SocketIo(`${API_URL}`, {
/*query: {
token: this.token
}*/
});
return this.connectSocketServer();
/* return res.data;
})
.catch((err) => {
console.error(err);
throw err;
});*/
}
2020-05-13 20:22:42 +02:00
/**
*
* @param character
*/
connectSocketServer(): Promise<ConnexionInterface>{
2020-05-13 20:22:42 +02:00
//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
this.positionOfAllUser();
this.disconnectServer();
this.errorMessage();
this.groupUpdatedOrCreated();
this.groupDeleted();
return new Promise<ConnexionInterface>((resolve, reject) => {
this.socket.emit(EventMessage.SET_PLAYER_DETAILS, {
name: this.name,
character: this.character
} as SetPlayerDetailsMessage, (id: string) => {
this.userId = id;
});
resolve(this);
});
2020-05-13 20:22:42 +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;
});
}
/**
*
* @param roomId
* @param character
*/
joinARoom(roomId: string, character: string): void {
let messageUserPosition = new MessageUserPosition(
this.userId,
roomId,
new Point(0, 0),
this.name,
character
);
this.socket.emit(EventMessage.JOIN_ROOM, messageUserPosition);
}
/**
2020-04-07 21:02:23 +02:00
*
* @param x
* @param y
* @param character
* @param roomId
2020-04-07 21:02:23 +02:00
* @param direction
*/
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;
}
let messageUserPosition = new MessageUserPosition(
this.userId,
roomId,
new Point(x, y, direction),
this.name,
character
);
2020-05-13 20:22:42 +02:00
this.lastPositionShared = messageUserPosition;
this.socket.emit(EventMessage.USER_POSITION, messageUserPosition);
}
/**
* The data sent is an array with information for each user :
* [
* {
* userId: <string>,
* roomId: <string>,
* position: {
* x : <number>,
* y : <number>,
* direction: <string>
* }
* },
* ...
* ]
**/
2020-04-25 16:05:33 +02:00
positionOfAllUser(): void {
this.socket.on(EventMessage.USER_POSITION, (message: string) => {
let dataList = message;
let UserPositions : Array<any> = Object.values(dataList);
let listMessageUserPosition = new ListMessageUserPosition(UserPositions[0], UserPositions[1]);
this.GameManager.shareUserPosition(listMessageUserPosition);
});
}
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);
})
}
sendWebrtcSignal(signal: any, roomId: string, userId? : string, receiverId? : string) {
return this.socket.emit(EventMessage.WEBRTC_SIGNAL, {
2020-04-26 17:43:21 +02:00
userId: userId ? userId : this.userId,
receiverId: receiverId ? receiverId : this.userId,
2020-04-25 16:05:33 +02:00
roomId: roomId,
signal: signal
});
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-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);
}
}