2021-07-27 16:37:01 +02:00
|
|
|
import type { Room } from "../Connexion/Room";
|
2021-09-06 18:36:31 +02:00
|
|
|
import { localUserStore } from "../Connexion/LocalUserStore";
|
2020-10-12 16:23:07 +02:00
|
|
|
|
|
|
|
export enum GameConnexionTypes {
|
2021-07-27 16:37:01 +02:00
|
|
|
anonymous = 1,
|
2020-10-12 16:23:07 +02:00
|
|
|
organization,
|
|
|
|
register,
|
2020-10-13 17:20:20 +02:00
|
|
|
empty,
|
2020-10-12 16:23:07 +02:00
|
|
|
unknown,
|
2021-07-27 16:37:01 +02:00
|
|
|
jwt,
|
2021-09-06 18:36:31 +02:00
|
|
|
login,
|
2020-10-12 16:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//this class is responsible with analysing and editing the game's url
|
|
|
|
class UrlManager {
|
|
|
|
public getGameConnexionType(): GameConnexionTypes {
|
|
|
|
const url = window.location.pathname.toString();
|
2021-09-06 18:36:31 +02:00
|
|
|
if (url === "/login") {
|
|
|
|
return GameConnexionTypes.login;
|
|
|
|
} else if (url === "/jwt") {
|
2021-07-27 16:37:01 +02:00
|
|
|
return GameConnexionTypes.jwt;
|
|
|
|
} else if (url.includes("_/")) {
|
2020-10-12 16:23:07 +02:00
|
|
|
return GameConnexionTypes.anonymous;
|
2021-07-27 16:37:01 +02:00
|
|
|
} else if (url.includes("@/")) {
|
2020-10-12 16:23:07 +02:00
|
|
|
return GameConnexionTypes.organization;
|
2021-07-27 16:37:01 +02:00
|
|
|
} else if (url.includes("register/")) {
|
2020-10-13 17:20:20 +02:00
|
|
|
return GameConnexionTypes.register;
|
2021-07-27 16:37:01 +02:00
|
|
|
} else if (url === "/") {
|
2020-10-13 17:20:20 +02:00
|
|
|
return GameConnexionTypes.empty;
|
2020-10-12 16:23:07 +02:00
|
|
|
} else {
|
2020-10-13 17:20:20 +02:00
|
|
|
return GameConnexionTypes.unknown;
|
2020-10-12 16:23:07 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 18:59:49 +02:00
|
|
|
|
2021-07-27 16:37:01 +02:00
|
|
|
public getOrganizationToken(): string | null {
|
2020-10-12 16:23:07 +02:00
|
|
|
const match = /\/register\/(.+)/.exec(window.location.pathname.toString());
|
2021-07-27 16:37:01 +02:00
|
|
|
return match ? match[1] : null;
|
2020-10-12 16:23:07 +02:00
|
|
|
}
|
|
|
|
|
2021-07-27 16:37:01 +02:00
|
|
|
public pushRoomIdToUrl(room: Room): void {
|
2021-01-21 09:40:11 +01:00
|
|
|
if (window.location.pathname === room.id) return;
|
2021-09-06 18:36:31 +02:00
|
|
|
//Set last room visited! (connected or nor, must to be saved in localstorage and cache API)
|
|
|
|
localUserStore.setLastRoomUrl(room.key);
|
2020-11-25 17:17:48 +01:00
|
|
|
const hash = window.location.hash;
|
2021-03-30 16:08:49 +02:00
|
|
|
const search = room.search.toString();
|
2021-07-27 16:37:01 +02:00
|
|
|
history.pushState({}, "WorkAdventure", room.id + (search ? "?" + search : "") + hash);
|
2020-11-25 17:17:48 +01:00
|
|
|
}
|
2021-01-21 09:40:11 +01:00
|
|
|
|
2021-07-27 16:37:01 +02:00
|
|
|
public getStartLayerNameFromUrl(): string | null {
|
2020-11-19 17:06:28 +01:00
|
|
|
const hash = window.location.hash;
|
2020-11-19 14:32:18 +01:00
|
|
|
return hash.length > 1 ? hash.substring(1) : null;
|
2020-11-18 18:15:57 +01:00
|
|
|
}
|
2020-10-12 18:59:49 +02:00
|
|
|
|
2020-11-19 14:32:18 +01:00
|
|
|
pushStartLayerNameToUrl(startLayerName: string): void {
|
|
|
|
window.location.hash = startLayerName;
|
|
|
|
}
|
2020-10-12 16:23:07 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 18:59:49 +02:00
|
|
|
export const urlManager = new UrlManager();
|