1 import {GameManager} from "./Phaser/Game/GameManager";
3 const SocketIo = require('socket.io-client');
4 import Axios from "axios";
5 import {API_URL} from "./Enum/EnvironmentVariable";
6 import {getMapKeyByUrl} from "./Phaser/Login/LogincScene";
7 import {MessageUI} from "./Logger/MessageUI";
10 WEBRTC_SIGNAL = "webrtc-signal",
11 WEBRTC_START = "webrtc-start",
12 WEBRTC_JOIN_ROOM = "webrtc-join-room",
13 JOIN_ROOM = "join-room",
14 USER_POSITION = "user-position",
15 MESSAGE_ERROR = "message-error",
16 WEBRTC_DISCONNECT = "webrtc-disconect",
17 GROUP_CREATE_UPDATE = "group-create-update",
18 GROUP_DELETE = "group-delete",
20 CONNECT_ERROR = "connect_error",
21 RECONNECT = "reconnect",
22 ATTRIBUTE_USER_ID = "attribute-user-id" // Sent from server to client just after the connexion is established to give the client its unique id.
31 constructor(userId : string, roomId : string, name: string, character: string) {
35 this.character = character;
39 export interface PointInterface {
45 class Point implements PointInterface{
50 constructor(x : number, y : number, direction : string = "none") {
51 if(x === null || y === null){
52 throw Error("position x and y cannot be null");
56 this.direction = direction;
60 export interface MessageUserPositionInterface {
65 position: PointInterface;
68 class MessageUserPosition extends Message implements MessageUserPositionInterface{
69 position: PointInterface;
71 constructor(userId : string, roomId : string, point : Point, name: string, character: string) {
72 super(userId, roomId, name, character);
73 this.position = point;
77 export interface ListMessageUserPositionInterface {
79 listUsersPosition: Array<MessageUserPosition>;
82 class ListMessageUserPosition {
84 listUsersPosition: Array<MessageUserPosition>;
86 constructor(roomId: string, data: any) {
88 this.listUsersPosition = new Array<MessageUserPosition>();
89 data.forEach((userPosition: any) => {
90 this.listUsersPosition.push(new MessageUserPosition(
94 userPosition.position.x,
95 userPosition.position.y,
96 userPosition.position.direction
99 userPosition.character
105 export interface PositionInterface {
110 export interface GroupCreatedUpdatedMessageInterface {
111 position: PositionInterface,
115 export interface ConnexionInterface {
121 createConnexion(characterSelected: string): Promise<any>;
123 loadMaps(): Promise<any>;
125 joinARoom(roomId: string, character: string): void;
127 sharePosition(x: number, y: number, direction: string, roomId: string, character: string): void;
129 positionOfAllUser(): void;
132 sendWebrtcSignal(signal: any, roomId: string, userId?: string, receiverId?: string): void;
134 receiveWebrtcSignal(callBack: Function): void;
136 receiveWebrtcStart(callBack: Function): void;
138 disconnectMessage(callBack: Function): void;
141 export class Connexion implements ConnexionInterface {
147 GameManager: GameManager;
149 lastPositionShared: MessageUserPosition = null;
151 constructor(email : string, GameManager: GameManager) {
153 this.GameManager = GameManager;
157 * @param characterSelected
159 createConnexion(characterSelected: string): Promise<ConnexionInterface> {
160 /*return Axios.post(`${API_URL}/login`, {email: this.email})
162 this.token = res.data.token;
163 this.userId = res.data.userId;*/
165 this.socket = SocketIo(`${API_URL}`, {
171 this.connectSocketServer();
173 // TODO: maybe trigger promise only when connexion is established?
174 let promise = new Promise<ConnexionInterface>((resolve, reject) => {
175 /*console.log('PROMISE CREATED')
176 this.socket.on('connection', () => {
177 console.log('CONNECTED');
197 connectSocketServer(character : string = null){
198 //if try to reconnect with last position
199 if(this.lastPositionShared) {
202 this.lastPositionShared.roomId,
203 this.lastPositionShared.character
206 //share your first position
208 this.lastPositionShared ? this.lastPositionShared.position.x : 0,
209 this.lastPositionShared ? this.lastPositionShared.position.y : 0,
210 this.lastPositionShared.character,
211 this.lastPositionShared.roomId,
212 this.lastPositionShared.position.direction
217 this.attributeUserId();
218 this.positionOfAllUser();
219 this.disconnectServer();
221 this.groupUpdatedOrCreated();
225 //TODO add middleware with access token to secure api
226 loadMaps() : Promise<any> {
227 return Axios.get(`${API_URL}/maps`)
241 joinARoom(roomId: string, character: string): void {
242 let messageUserPosition = new MessageUserPosition(
249 this.socket.emit(EventMessage.JOIN_ROOM, messageUserPosition);
260 sharePosition(x : number, y : number, character : string, roomId : string, direction : string = "none") : void{
264 let messageUserPosition = new MessageUserPosition(
267 new Point(x, y, direction),
271 this.lastPositionShared = messageUserPosition;
272 this.socket.emit(EventMessage.USER_POSITION, messageUserPosition);
275 attributeUserId(): void {
276 // This event is received as soon as the connexion is established.
277 // It allows informing the browser of its own user id.
278 this.socket.on(EventMessage.ATTRIBUTE_USER_ID, (userId: string) => {
279 console.log('Received my user id: ', userId);
280 this.userId = userId;
285 * The data sent is an array with information for each user :
293 * direction: <string>
299 positionOfAllUser(): void {
300 this.socket.on(EventMessage.USER_POSITION, (message: string) => {
301 let dataList = message;
302 let UserPositions : Array<any> = Object.values(dataList);
303 let listMessageUserPosition = new ListMessageUserPosition(UserPositions[0], UserPositions[1]);
304 this.GameManager.shareUserPosition(listMessageUserPosition);
308 private groupUpdatedOrCreated(): void {
309 this.socket.on(EventMessage.GROUP_CREATE_UPDATE, (groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface) => {
310 //console.log('Group ', groupCreateUpdateMessage.groupId, " position :", groupCreateUpdateMessage.position.x, groupCreateUpdateMessage.position.y)
311 this.GameManager.shareGroupPosition(groupCreateUpdateMessage);
315 private groupDeleted(): void {
316 this.socket.on(EventMessage.GROUP_DELETE, (groupId: string) => {
317 this.GameManager.deleteGroup(groupId);
321 sendWebrtcSignal(signal: any, roomId: string, userId? : string, receiverId? : string) {
322 return this.socket.emit(EventMessage.WEBRTC_SIGNAL, {
323 userId: userId ? userId : this.userId,
324 receiverId: receiverId ? receiverId : this.userId,
330 receiveWebrtcStart(callback: Function) {
331 this.socket.on(EventMessage.WEBRTC_START, callback);
334 receiveWebrtcSignal(callback: Function) {
335 return this.socket.on(EventMessage.WEBRTC_SIGNAL, callback);
338 errorMessage(): void {
339 this.socket.on(EventMessage.MESSAGE_ERROR, (message: string) => {
340 console.error(EventMessage.MESSAGE_ERROR, message);
344 disconnectServer(): void {
345 this.socket.on(EventMessage.CONNECT_ERROR, () => {
346 MessageUI.warningMessage("Trying to connect!");
349 this.socket.on(EventMessage.RECONNECT, () => {
350 MessageUI.removeMessage();
351 this.connectSocketServer();
355 disconnectMessage(callback: Function): void {
356 this.socket.on(EventMessage.WEBRTC_DISCONNECT, callback);