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-10-12 16:23:07 +02:00
|
|
|
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
|
|
|
|
2020-09-25 18:29:22 +02:00
|
|
|
class ConnectionManager {
|
2020-10-12 16:23:07 +02:00
|
|
|
private localUser!:LocalUser;
|
2020-09-29 17:12:28 +02:00
|
|
|
|
2020-10-12 16:23:07 +02:00
|
|
|
/**
|
|
|
|
* Tries to login to the node server and return the starting map url to be loaded
|
|
|
|
*/
|
|
|
|
public async initGameConnexion(): Promise<Room> {
|
2020-09-29 17:12:28 +02:00
|
|
|
|
2020-10-12 16:23:07 +02:00
|
|
|
const connexionType = urlManager.getGameConnexionType();
|
|
|
|
if(connexionType === GameConnexionTypes.register) {
|
|
|
|
const organizationMemberToken = urlManager.getOrganizationToken();
|
|
|
|
const data:any = 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 {
|
|
|
|
const data:any = 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
|
|
|
|
2020-10-12 16:23:07 +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
|
|
|
}
|
2020-10-12 16:23:07 +02:00
|
|
|
return Promise.reject('ConnexionManager initialization failed');
|
2020-09-25 18:29:22 +02:00
|
|
|
}
|
2020-09-29 17:12:28 +02:00
|
|
|
|
2020-09-30 12:12:24 +02:00
|
|
|
public initBenchmark(): void {
|
2020-10-12 16:23:07 +02:00
|
|
|
this.localUser = new LocalUser('', 'test');
|
2020-09-30 12:12:24 +02:00
|
|
|
}
|
|
|
|
|
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-12 16:23:07 +02:00
|
|
|
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
|
|
|
});
|
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-25 18:29:22 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 17:12:28 +02:00
|
|
|
export const connectionManager = new ConnectionManager();
|