2020-09-25 18:29:22 +02:00
|
|
|
import Axios from "axios";
|
|
|
|
import {API_URL} from "../Enum/EnvironmentVariable";
|
|
|
|
import {RoomConnection} from "./RoomConnection";
|
2020-10-06 18:09:23 +02:00
|
|
|
import {PositionInterface, ViewportInterface} from "./ConnexionModels";
|
2020-09-25 18:29:22 +02:00
|
|
|
|
2020-09-28 15:02:37 +02:00
|
|
|
interface LoginApiData {
|
|
|
|
authToken: string
|
|
|
|
userUuid: string
|
|
|
|
mapUrlStart: string
|
|
|
|
newUrl: string
|
|
|
|
}
|
|
|
|
|
2020-09-25 18:29:22 +02:00
|
|
|
class ConnectionManager {
|
2020-10-01 17:15:33 +02:00
|
|
|
private initPromise!: Promise<LoginApiData>;
|
2020-09-25 18:29:22 +02:00
|
|
|
private mapUrlStart: string|null = null;
|
2020-09-29 17:12:28 +02:00
|
|
|
|
2020-09-25 18:29:22 +02:00
|
|
|
private authToken:string|null = null;
|
|
|
|
private userUuid: string|null = null;
|
2020-09-29 17:12:28 +02:00
|
|
|
|
2020-10-08 18:51:24 +02:00
|
|
|
//todo: get map infos from url in anonym case
|
2020-09-25 18:29:22 +02:00
|
|
|
public async init(): Promise<void> {
|
2020-10-08 18:51:24 +02:00
|
|
|
let organizationMemberToken = null;
|
|
|
|
let teamSlug = null;
|
|
|
|
let mapSlug = null;
|
2020-09-25 18:29:22 +02:00
|
|
|
const match = /\/register\/(.+)/.exec(window.location.toString());
|
2020-10-08 18:51:24 +02:00
|
|
|
if (match) {
|
|
|
|
organizationMemberToken = match[1];
|
|
|
|
} else {
|
|
|
|
const match = /\/_\/(.+)\/(.+)/.exec(window.location.toString());
|
|
|
|
teamSlug = match ? match[1] : null;
|
|
|
|
mapSlug = match ? match[2] : null;
|
|
|
|
}
|
|
|
|
this.initPromise = Axios.post(`${API_URL}/login`, {organizationMemberToken, teamSlug, mapSlug}).then(res => res.data);
|
2020-09-28 15:02:37 +02:00
|
|
|
const data = await this.initPromise
|
|
|
|
this.authToken = data.authToken;
|
|
|
|
this.userUuid = data.userUuid;
|
|
|
|
this.mapUrlStart = data.mapUrlStart;
|
|
|
|
const newUrl = data.newUrl;
|
2020-10-09 16:18:25 +02:00
|
|
|
console.log('u', this.userUuid)
|
2020-09-25 18:29:22 +02:00
|
|
|
|
|
|
|
if (newUrl) {
|
|
|
|
history.pushState({}, '', newUrl);
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 17:12:28 +02:00
|
|
|
|
2020-09-30 12:12:24 +02:00
|
|
|
public initBenchmark(): void {
|
|
|
|
this.authToken = 'test';
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:09:23 +02:00
|
|
|
public connectToRoomSocket(roomId: string, name: string, characterLayers: string[], position: PositionInterface, viewport: ViewportInterface): Promise<RoomConnection> {
|
2020-09-28 15:02:37 +02:00
|
|
|
return new Promise<RoomConnection>((resolve, reject) => {
|
2020-10-09 14:53:18 +02:00
|
|
|
const connection = new RoomConnection(this.authToken, roomId, name, characterLayers, position, viewport);
|
2020-09-28 15:02:37 +02:00
|
|
|
connection.onConnectError((error: object) => {
|
|
|
|
console.log('An error occurred while connecting to socket server. Retrying');
|
|
|
|
reject(error);
|
2020-09-25 18:29:22 +02:00
|
|
|
});
|
2020-09-29 17:12:28 +02:00
|
|
|
connection.onConnect(() => {
|
|
|
|
resolve(connection);
|
|
|
|
})
|
2020-09-28 15:02:37 +02:00
|
|
|
}).catch((err) => {
|
|
|
|
// Let's retry in 4-6 seconds
|
|
|
|
return new Promise<RoomConnection>((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
//todo: allow a way to break recurrsion?
|
2020-10-06 18:09:23 +02:00
|
|
|
this.connectToRoomSocket(roomId, name, characterLayers, position, viewport).then((connection) => resolve(connection));
|
2020-09-28 15:02:37 +02:00
|
|
|
}, 4000 + Math.floor(Math.random() * 2000) );
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-09-29 17:12:28 +02:00
|
|
|
|
2020-09-28 15:02:37 +02:00
|
|
|
public getMapUrlStart(): Promise<string> {
|
|
|
|
return this.initPromise.then(() => {
|
|
|
|
if (!this.mapUrlStart) {
|
|
|
|
throw new Error('No map url set!');
|
|
|
|
}
|
|
|
|
return this.mapUrlStart;
|
|
|
|
})
|
2020-09-25 18:29:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 17:12:28 +02:00
|
|
|
export const connectionManager = new ConnectionManager();
|