This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
Files
scoresaber-reloadedv3/projects/website/src/common/database/database.ts

138 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-09-08 22:35:32 +01:00
import Dexie, { EntityTable } from "dexie";
2024-09-11 23:10:16 +01:00
import BeatSaverMap from "./types/beatsaver-map";
2024-09-08 22:35:32 +01:00
import Settings from "./types/settings";
2024-10-16 11:23:28 +01:00
import { Friend } from "@/common/database/types/friends";
import ScoreSaberPlayerToken from "@ssr/common/types/token/scoresaber/score-saber-player-token";
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
import { setCookieValue } from "@ssr/common/utils/cookie-utils";
2024-09-08 22:35:32 +01:00
const SETTINGS_ID = "SSR"; // DO NOT CHANGE
export default class Database extends Dexie {
2024-09-11 20:10:27 +01:00
/**
* The settings for the website.
*/
2024-09-08 22:35:32 +01:00
settings!: EntityTable<Settings, "id">;
2024-09-11 23:10:16 +01:00
/**
2024-10-04 18:25:37 +01:00
* Cached BeatSaver maps
2024-09-11 23:10:16 +01:00
*/
beatSaverMaps!: EntityTable<BeatSaverMap, "hash">;
2024-10-16 11:23:28 +01:00
/**
* The added friends
*/
friends!: EntityTable<Friend, "id">;
2024-09-08 22:35:32 +01:00
constructor() {
super("ScoreSaberReloaded");
// Stores
this.version(1).stores({
settings: "id",
2024-09-11 23:10:16 +01:00
beatSaverMaps: "hash",
2024-10-16 11:23:28 +01:00
friends: "id",
2024-09-08 22:35:32 +01:00
});
// Mapped tables
this.settings.mapToClass(Settings);
2024-09-11 23:10:16 +01:00
this.beatSaverMaps.mapToClass(BeatSaverMap);
2024-09-08 22:35:32 +01:00
// Populate default settings if the table is empty
this.on("populate", () => this.populateDefaults());
this.on("ready", async () => {
const settings = await this.getSettings();
// If the settings are not found, return
if (settings == undefined || settings.playerId == undefined) {
return;
}
2024-10-13 03:49:33 +01:00
await setCookieValue("playerId", settings.playerId);
});
2024-09-08 22:35:32 +01:00
}
2024-09-11 20:10:27 +01:00
/**
* Populates the default settings
*/
2024-09-08 22:35:32 +01:00
async populateDefaults() {
2024-10-04 18:25:37 +01:00
await this.resetSettings();
2024-09-08 22:35:32 +01:00
}
/**
* Gets the settings from the database
*
* @returns the settings
*/
async getSettings(): Promise<Settings | undefined> {
2024-10-04 18:25:37 +01:00
return this.settings.get(SETTINGS_ID);
2024-09-08 22:35:32 +01:00
}
/**
* Sets the settings in the database
*
* @param settings the settings to set
* @returns the settings
*/
async setSettings(settings: Settings) {
2024-10-04 18:25:37 +01:00
return this.settings.update(SETTINGS_ID, settings);
}
2024-10-16 11:23:28 +01:00
/**
* Adds a friend
*
* @param id the id of the friend
*/
public async addFriend(id: string) {
await this.friends.add({ id });
}
/**
* Removes a friend
*
* @param id the id of the friend
*/
public async removeFriend(id: string) {
await this.friends.delete(id);
}
/**
* Checks if this player is a friend
*
* @param id the id of the player
*/
public async isFriend(id: string): Promise<boolean> {
const friend = await this.friends.get(id);
return friend != undefined;
}
/**
* Gets all friends as {@link ScoreSaberPlayerToken}'s
*
* @returns the friends
*/
public async getFriends(): Promise<ScoreSaberPlayerToken[]> {
const friends = await this.friends.toArray();
const players = await Promise.all(
friends.map(async ({ id }) => {
return await scoresaberService.lookupPlayer(id, true);
})
);
return players.filter(player => player !== undefined) as ScoreSaberPlayerToken[];
}
2024-10-04 18:25:37 +01:00
/**
* Resets the settings in the database
*/
async resetSettings() {
this.settings.delete(SETTINGS_ID);
this.settings.add({
id: SETTINGS_ID, // Fixed ID for the single settings object
backgroundCover: "/assets/background.jpg",
});
return this.getSettings();
2024-10-01 21:36:46 +01:00
}
2024-09-08 22:35:32 +01:00
}
export const db = new Database();