2021-07-29 16:42:31 +02:00
|
|
|
import { areCharacterLayersValid, isUserNameValid, LocalUser } from "./LocalUser";
|
2021-07-27 16:37:01 +02:00
|
|
|
import { v4 as uuidv4 } from "uuid";
|
2021-09-05 18:17:49 +02:00
|
|
|
import { START_ROOM_URL } from "../Enum/EnvironmentVariable";
|
2021-07-29 16:42:31 +02:00
|
|
|
|
|
|
|
const playerNameKey = "playerName";
|
|
|
|
const selectedPlayerKey = "selectedPlayer";
|
|
|
|
const customCursorPositionKey = "customCursorPosition";
|
|
|
|
const characterLayersKey = "characterLayers";
|
|
|
|
const companionKey = "companion";
|
|
|
|
const gameQualityKey = "gameQuality";
|
|
|
|
const videoQualityKey = "videoQuality";
|
|
|
|
const audioPlayerVolumeKey = "audioVolume";
|
|
|
|
const audioPlayerMuteKey = "audioMute";
|
|
|
|
const helpCameraSettingsShown = "helpCameraSettingsShown";
|
|
|
|
const fullscreenKey = "fullscreen";
|
2021-12-06 23:13:44 +01:00
|
|
|
const forceCowebsiteTriggerKey = "forceCowebsiteTrigger";
|
2021-12-15 19:47:14 +01:00
|
|
|
const ignoreFollowRequests = "ignoreFollowRequests";
|
2022-03-21 16:22:51 +01:00
|
|
|
const decreaseAudioPlayerVolumeWhileTalking = "decreaseAudioPlayerVolumeWhileTalking";
|
2021-07-29 16:42:31 +02:00
|
|
|
const lastRoomUrl = "lastRoomUrl";
|
2021-07-27 16:37:01 +02:00
|
|
|
const authToken = "authToken";
|
|
|
|
const state = "state";
|
|
|
|
const nonce = "nonce";
|
2021-08-26 12:01:07 +02:00
|
|
|
const notification = "notificationPermission";
|
2021-09-05 18:17:49 +02:00
|
|
|
const code = "code";
|
2021-09-04 21:31:36 +02:00
|
|
|
const cameraSetup = "cameraSetup";
|
2021-08-19 18:48:24 +02:00
|
|
|
const cacheAPIIndex = "workavdenture-cache";
|
2021-10-15 17:01:38 +02:00
|
|
|
const userProperties = "user-properties";
|
2022-03-07 15:43:00 +01:00
|
|
|
const privacySettings = "privacySettings";
|
2021-08-04 13:33:58 +02:00
|
|
|
|
2020-10-12 16:23:07 +02:00
|
|
|
class LocalUserStore {
|
|
|
|
saveUser(localUser: LocalUser) {
|
2021-07-29 16:42:31 +02:00
|
|
|
localStorage.setItem("localUser", JSON.stringify(localUser));
|
2020-10-12 16:23:07 +02:00
|
|
|
}
|
2021-07-29 16:42:31 +02:00
|
|
|
getLocalUser(): LocalUser | null {
|
|
|
|
const data = localStorage.getItem("localUser");
|
2020-10-12 16:23:07 +02:00
|
|
|
return data ? JSON.parse(data) : null;
|
|
|
|
}
|
2021-01-29 14:42:13 +01:00
|
|
|
|
2021-07-29 16:42:31 +02:00
|
|
|
setName(name: string): void {
|
2021-01-29 14:42:13 +01:00
|
|
|
localStorage.setItem(playerNameKey, name);
|
2020-10-15 12:05:40 +02:00
|
|
|
}
|
2021-07-29 16:42:31 +02:00
|
|
|
getName(): string | null {
|
|
|
|
const value = localStorage.getItem(playerNameKey) || "";
|
2021-04-07 14:09:45 +02:00
|
|
|
return isUserNameValid(value) ? value : null;
|
2020-10-15 12:05:40 +02:00
|
|
|
}
|
2020-10-20 13:44:57 +02:00
|
|
|
|
|
|
|
setPlayerCharacterIndex(playerCharacterIndex: number): void {
|
2021-07-29 16:42:31 +02:00
|
|
|
localStorage.setItem(selectedPlayerKey, "" + playerCharacterIndex);
|
2020-10-20 13:44:57 +02:00
|
|
|
}
|
|
|
|
getPlayerCharacterIndex(): number {
|
2021-07-29 16:42:31 +02:00
|
|
|
return parseInt(localStorage.getItem(selectedPlayerKey) || "");
|
2020-10-20 13:44:57 +02:00
|
|
|
}
|
|
|
|
|
2021-07-29 16:42:31 +02:00
|
|
|
setCustomCursorPosition(activeRow: number, selectedLayers: number[]): void {
|
|
|
|
localStorage.setItem(customCursorPositionKey, JSON.stringify({ activeRow, selectedLayers }));
|
2020-10-20 13:44:57 +02:00
|
|
|
}
|
2021-07-29 16:42:31 +02:00
|
|
|
getCustomCursorPosition(): { activeRow: number; selectedLayers: number[] } | null {
|
2021-01-29 14:42:13 +01:00
|
|
|
return JSON.parse(localStorage.getItem(customCursorPositionKey) || "null");
|
2020-10-20 13:44:57 +02:00
|
|
|
}
|
2020-12-04 11:30:35 +01:00
|
|
|
|
|
|
|
setCharacterLayers(layers: string[]): void {
|
2021-01-29 14:42:13 +01:00
|
|
|
localStorage.setItem(characterLayersKey, JSON.stringify(layers));
|
2020-12-04 11:30:35 +01:00
|
|
|
}
|
2021-07-29 16:42:31 +02:00
|
|
|
getCharacterLayers(): string[] | null {
|
2021-04-07 14:09:45 +02:00
|
|
|
const value = JSON.parse(localStorage.getItem(characterLayersKey) || "null");
|
|
|
|
return areCharacterLayersValid(value) ? value : null;
|
2020-12-04 11:30:35 +01:00
|
|
|
}
|
2021-01-29 14:42:13 +01:00
|
|
|
|
2021-07-29 16:42:31 +02:00
|
|
|
setCompanion(companion: string | null): void {
|
2021-04-02 23:00:51 +02:00
|
|
|
return localStorage.setItem(companionKey, JSON.stringify(companion));
|
2021-04-02 21:21:11 +02:00
|
|
|
}
|
2021-07-29 16:42:31 +02:00
|
|
|
getCompanion(): string | null {
|
2021-04-06 18:54:45 +02:00
|
|
|
const companion = JSON.parse(localStorage.getItem(companionKey) || "null");
|
|
|
|
|
|
|
|
if (typeof companion !== "string" || companion === "") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return companion;
|
2021-04-02 23:00:51 +02:00
|
|
|
}
|
|
|
|
wasCompanionSet(): boolean {
|
|
|
|
return localStorage.getItem(companionKey) ? true : false;
|
2021-04-02 21:21:11 +02:00
|
|
|
}
|
|
|
|
|
2020-12-04 11:30:35 +01:00
|
|
|
setGameQualityValue(value: number): void {
|
2021-07-29 16:42:31 +02:00
|
|
|
localStorage.setItem(gameQualityKey, "" + value);
|
2020-12-04 11:30:35 +01:00
|
|
|
}
|
2021-01-29 14:42:13 +01:00
|
|
|
getGameQualityValue(): number {
|
2021-07-29 16:42:31 +02:00
|
|
|
return parseInt(localStorage.getItem(gameQualityKey) || "60");
|
2020-12-04 11:30:35 +01:00
|
|
|
}
|
2021-01-29 14:42:13 +01:00
|
|
|
|
2020-12-04 11:30:35 +01:00
|
|
|
setVideoQualityValue(value: number): void {
|
2021-07-29 16:42:31 +02:00
|
|
|
localStorage.setItem(videoQualityKey, "" + value);
|
2020-12-04 11:30:35 +01:00
|
|
|
}
|
2021-01-29 14:42:13 +01:00
|
|
|
getVideoQualityValue(): number {
|
2021-07-29 16:42:31 +02:00
|
|
|
return parseInt(localStorage.getItem(videoQualityKey) || "20");
|
2021-01-29 14:42:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setAudioPlayerVolume(value: number): void {
|
2021-07-29 16:42:31 +02:00
|
|
|
localStorage.setItem(audioPlayerVolumeKey, "" + value);
|
2021-01-29 14:42:13 +01:00
|
|
|
}
|
|
|
|
getAudioPlayerVolume(): number {
|
2021-07-29 16:42:31 +02:00
|
|
|
return parseFloat(localStorage.getItem(audioPlayerVolumeKey) || "1");
|
2021-01-29 14:42:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
setAudioPlayerMuted(value: boolean): void {
|
|
|
|
localStorage.setItem(audioPlayerMuteKey, value.toString());
|
|
|
|
}
|
|
|
|
getAudioPlayerMuted(): boolean {
|
2021-07-29 16:42:31 +02:00
|
|
|
return localStorage.getItem(audioPlayerMuteKey) === "true";
|
2021-01-29 14:42:13 +01:00
|
|
|
}
|
2021-03-24 15:59:18 +01:00
|
|
|
|
|
|
|
setHelpCameraSettingsShown(): void {
|
2021-07-29 16:42:31 +02:00
|
|
|
localStorage.setItem(helpCameraSettingsShown, "1");
|
2021-03-24 15:59:18 +01:00
|
|
|
}
|
|
|
|
getHelpCameraSettingsShown(): boolean {
|
2021-07-29 16:42:31 +02:00
|
|
|
return localStorage.getItem(helpCameraSettingsShown) === "1";
|
2021-03-24 15:59:18 +01:00
|
|
|
}
|
2021-02-03 23:28:46 +01:00
|
|
|
|
2021-04-07 12:42:56 +02:00
|
|
|
setFullscreen(value: boolean): void {
|
|
|
|
localStorage.setItem(fullscreenKey, value.toString());
|
2021-02-03 23:28:46 +01:00
|
|
|
}
|
2021-04-07 12:42:56 +02:00
|
|
|
getFullscreen(): boolean {
|
2021-07-29 16:42:31 +02:00
|
|
|
return localStorage.getItem(fullscreenKey) === "true";
|
|
|
|
}
|
|
|
|
|
2021-12-06 23:13:44 +01:00
|
|
|
setForceCowebsiteTrigger(value: boolean): void {
|
|
|
|
localStorage.setItem(forceCowebsiteTriggerKey, value.toString());
|
|
|
|
}
|
|
|
|
getForceCowebsiteTrigger(): boolean {
|
|
|
|
return localStorage.getItem(forceCowebsiteTriggerKey) === "true";
|
|
|
|
}
|
|
|
|
|
2021-12-15 19:47:14 +01:00
|
|
|
setIgnoreFollowRequests(value: boolean): void {
|
|
|
|
localStorage.setItem(ignoreFollowRequests, value.toString());
|
|
|
|
}
|
|
|
|
getIgnoreFollowRequests(): boolean {
|
|
|
|
return localStorage.getItem(ignoreFollowRequests) === "true";
|
|
|
|
}
|
2022-03-21 16:22:51 +01:00
|
|
|
setDecreaseAudioPlayerVolumeWhileTalking(value: boolean): void {
|
|
|
|
localStorage.setItem(decreaseAudioPlayerVolumeWhileTalking, value.toString());
|
|
|
|
}
|
|
|
|
getDecreaseAudioPlayerVolumeWhileTalking(): boolean {
|
|
|
|
return localStorage.getItem(decreaseAudioPlayerVolumeWhileTalking) === "true";
|
|
|
|
}
|
2021-12-15 19:47:14 +01:00
|
|
|
|
2022-01-04 16:48:47 +01:00
|
|
|
async setLastRoomUrl(roomUrl: string): Promise<void> {
|
2021-07-29 16:42:31 +02:00
|
|
|
localStorage.setItem(lastRoomUrl, roomUrl.toString());
|
2021-11-24 17:02:11 +01:00
|
|
|
if ("caches" in window) {
|
2022-01-06 11:46:07 +01:00
|
|
|
try {
|
|
|
|
const cache = await caches.open(cacheAPIIndex);
|
|
|
|
const stringResponse = new Response(JSON.stringify({ roomUrl }));
|
|
|
|
await cache.put(`/${lastRoomUrl}`, stringResponse);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Could not store last room url in Browser cache. Are you using private browser mode?", e);
|
|
|
|
}
|
2021-11-23 15:42:52 +01:00
|
|
|
}
|
2021-07-29 16:42:31 +02:00
|
|
|
}
|
|
|
|
getLastRoomUrl(): string {
|
2021-09-05 18:17:49 +02:00
|
|
|
return (
|
|
|
|
localStorage.getItem(lastRoomUrl) ?? window.location.protocol + "//" + window.location.host + START_ROOM_URL
|
|
|
|
);
|
2021-02-03 23:28:46 +01:00
|
|
|
}
|
2021-08-04 13:33:58 +02:00
|
|
|
getLastRoomUrlCacheApi(): Promise<string | undefined> {
|
2021-11-24 17:02:11 +01:00
|
|
|
if (!("caches" in window)) {
|
2021-11-23 15:42:52 +01:00
|
|
|
return Promise.resolve(undefined);
|
|
|
|
}
|
2021-08-04 13:33:58 +02:00
|
|
|
return caches.open(cacheAPIIndex).then((cache) => {
|
|
|
|
return cache.match(`/${lastRoomUrl}`).then((res) => {
|
|
|
|
return res?.json().then((data) => {
|
|
|
|
return data.roomUrl;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2021-07-27 16:37:01 +02:00
|
|
|
|
|
|
|
setAuthToken(value: string | null) {
|
|
|
|
value ? localStorage.setItem(authToken, value) : localStorage.removeItem(authToken);
|
|
|
|
}
|
|
|
|
getAuthToken(): string | null {
|
|
|
|
return localStorage.getItem(authToken);
|
|
|
|
}
|
|
|
|
|
2021-08-26 12:01:07 +02:00
|
|
|
setNotification(value: string): void {
|
|
|
|
localStorage.setItem(notification, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
getNotification(): string | null {
|
|
|
|
return localStorage.getItem(notification);
|
|
|
|
}
|
|
|
|
|
2021-07-27 16:37:01 +02:00
|
|
|
generateState(): string {
|
|
|
|
const newState = uuidv4();
|
|
|
|
localStorage.setItem(state, newState);
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
|
|
|
verifyState(value: string): boolean {
|
|
|
|
const oldValue = localStorage.getItem(state);
|
2021-11-09 00:08:01 +01:00
|
|
|
if (!oldValue) {
|
|
|
|
localStorage.setItem(state, value);
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-27 16:37:01 +02:00
|
|
|
return oldValue === value;
|
|
|
|
}
|
2021-11-10 18:26:50 +01:00
|
|
|
setState(value: string) {
|
|
|
|
localStorage.setItem(state, value);
|
|
|
|
}
|
2021-09-05 18:17:49 +02:00
|
|
|
getState(): string | null {
|
|
|
|
return localStorage.getItem(state);
|
|
|
|
}
|
2021-07-27 16:37:01 +02:00
|
|
|
generateNonce(): string {
|
|
|
|
const newNonce = uuidv4();
|
|
|
|
localStorage.setItem(nonce, newNonce);
|
|
|
|
return newNonce;
|
|
|
|
}
|
|
|
|
getNonce(): string | null {
|
2021-09-05 18:17:49 +02:00
|
|
|
return localStorage.getItem(nonce);
|
|
|
|
}
|
|
|
|
setCode(value: string): void {
|
|
|
|
localStorage.setItem(code, value);
|
|
|
|
}
|
|
|
|
getCode(): string | null {
|
|
|
|
return localStorage.getItem(code);
|
2021-07-27 16:37:01 +02:00
|
|
|
}
|
2021-09-04 21:31:36 +02:00
|
|
|
|
|
|
|
setCameraSetup(cameraId: string) {
|
|
|
|
localStorage.setItem(cameraSetup, cameraId);
|
|
|
|
}
|
|
|
|
getCameraSetup(): { video: unknown; audio: unknown } | undefined {
|
|
|
|
const cameraSetupValues = localStorage.getItem(cameraSetup);
|
|
|
|
return cameraSetupValues != undefined ? JSON.parse(cameraSetupValues) : undefined;
|
|
|
|
}
|
2021-10-25 14:43:36 +02:00
|
|
|
|
2022-03-07 15:43:00 +01:00
|
|
|
setPrivacySettings(option: string) {
|
|
|
|
localStorage.setItem(privacySettings, option)
|
|
|
|
}
|
|
|
|
|
|
|
|
getPrivacySettings() {
|
|
|
|
return localStorage.getItem(privacySettings);
|
|
|
|
}
|
|
|
|
|
2021-10-25 14:43:36 +02:00
|
|
|
getAllUserProperties(): Map<string, unknown> {
|
|
|
|
const result = new Map<string, string>();
|
|
|
|
for (let i = 0; i < localStorage.length; i++) {
|
|
|
|
const key = localStorage.key(i);
|
|
|
|
if (key) {
|
|
|
|
if (key.startsWith(userProperties + "_")) {
|
|
|
|
const value = localStorage.getItem(key);
|
|
|
|
if (value) {
|
|
|
|
const userKey = key.substr((userProperties + "_").length);
|
|
|
|
result.set(userKey, JSON.parse(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2021-10-15 17:01:38 +02:00
|
|
|
}
|
|
|
|
|
2021-10-25 14:43:36 +02:00
|
|
|
setUserProperty(name: string, value: unknown): void {
|
|
|
|
localStorage.setItem(userProperties + "_" + name, JSON.stringify(value));
|
2021-10-15 17:01:38 +02:00
|
|
|
}
|
2020-10-12 16:23:07 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 14:42:13 +01:00
|
|
|
export const localUserStore = new LocalUserStore();
|