partey_workadventure/front/src/Connexion/ConnectionManager.ts

92 lines
4.2 KiB
TypeScript
Raw Normal View History

2020-09-25 18:29:22 +02:00
import Axios from "axios";
import {API_URL} from "../Enum/EnvironmentVariable";
import {RoomConnection} from "./RoomConnection";
import {PositionInterface, ViewportInterface} from "./ConnexionModels";
import {GameConnexionTypes, urlManager} from "../Url/UrlManager";
import {localUserStore} from "./LocalUserStore";
import {LocalUser} from "./LocalUser";
import {Room} from "./Room";
2020-09-28 15:02:37 +02:00
const URL_ROOM_STARTED = '/Floor0/floor0.json';
2020-09-25 18:29:22 +02:00
class ConnectionManager {
private localUser!:LocalUser;
/**
* Tries to login to the node server and return the starting map url to be loaded
*/
public async initGameConnexion(): Promise<Room> {
const connexionType = urlManager.getGameConnexionType();
if(connexionType === GameConnexionTypes.register) {
const organizationMemberToken = urlManager.getOrganizationToken();
2020-10-13 15:55:30 +02:00
const data = await Axios.post(`${API_URL}/register`, {organizationMemberToken}).then(res => res.data);
this.localUser = new LocalUser(data.userUuid, data.authToken);
localUserStore.saveUser(this.localUser);
const organizationSlug = data.organizationSlug;
const worldSlug = data.worldSlug;
const roomSlug = data.roomSlug;
urlManager.editUrlForRoom(roomSlug, organizationSlug, worldSlug);
const room = new Room(window.location.pathname, data.mapUrlStart)
return Promise.resolve(room);
} else if (connexionType === GameConnexionTypes.anonymous) {
const localUser = localUserStore.getLocalUser();
if (localUser) {
this.localUser = localUser
} else {
2020-10-13 15:55:30 +02:00
const data = await Axios.post(`${API_URL}/anonymLogin`).then(res => res.data);
this.localUser = new LocalUser(data.userUuid, data.authToken);
localUserStore.saveUser(this.localUser);
}
const room = new Room(window.location.pathname, urlManager.getAnonymousMapUrlStart())
return Promise.resolve(room);
} else if (connexionType == GameConnexionTypes.organization) {
const localUser = localUserStore.getLocalUser();
2020-09-25 18:29:22 +02:00
if (localUser) {
this.localUser = localUser
//todo: ask the node api for the correct starting map Url from its slug
return Promise.reject('Case not handled: need to get the map\'s url from its slug');
} else {
//todo: find some kind of fallback?
return Promise.reject('Could not find a user in localstorage');
}
2020-09-25 18:29:22 +02:00
}
//todo: cleaner way to handle the default case
const defaultMapUrl = window.location.host.replace('api.', 'maps.') + URL_ROOM_STARTED;
const defaultRoomId = urlManager.editUrlForRoom(URL_ROOM_STARTED, null, null);
return Promise.resolve(new Room(defaultRoomId, defaultMapUrl));
2020-09-25 18:29:22 +02:00
}
2020-09-30 12:12:24 +02:00
public initBenchmark(): void {
this.localUser = new LocalUser('', 'test');
2020-09-30 12:12:24 +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) => {
const connection = new RoomConnection(this.localUser.jwtToken, 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
});
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?
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-25 18:29:22 +02:00
}
export const connectionManager = new ConnectionManager();