2024-09-08 22:35:32 +01:00
|
|
|
import Dexie, { EntityTable } from "dexie";
|
2024-09-09 08:47:41 +01:00
|
|
|
import { setPlayerIdCookie } from "../website-utils";
|
2024-09-08 22:35:32 +01:00
|
|
|
import Settings from "./types/settings";
|
|
|
|
|
|
|
|
const SETTINGS_ID = "SSR"; // DO NOT CHANGE
|
|
|
|
|
|
|
|
export default class Database extends Dexie {
|
|
|
|
settings!: EntityTable<Settings, "id">;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super("ScoreSaberReloaded");
|
|
|
|
|
|
|
|
// Stores
|
|
|
|
this.version(1).stores({
|
|
|
|
settings: "id",
|
|
|
|
});
|
|
|
|
|
|
|
|
// Mapped tables
|
|
|
|
this.settings.mapToClass(Settings);
|
|
|
|
|
|
|
|
// Populate default settings if the table is empty
|
|
|
|
this.on("populate", () => this.populateDefaults());
|
2024-09-09 08:47:41 +01:00
|
|
|
|
|
|
|
this.on("ready", async () => {
|
|
|
|
const settings = await this.getSettings();
|
|
|
|
// If the settings are not found, return
|
|
|
|
if (settings == undefined || settings.playerId == undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setPlayerIdCookie(settings.playerId);
|
|
|
|
});
|
2024-09-08 22:35:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async populateDefaults() {
|
|
|
|
await this.settings.add({
|
|
|
|
id: SETTINGS_ID, // Fixed ID for the single settings object
|
|
|
|
backgroundImage: "/assets/background.jpg",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the settings from the database
|
|
|
|
*
|
|
|
|
* @returns the settings
|
|
|
|
*/
|
|
|
|
async getSettings(): Promise<Settings | undefined> {
|
|
|
|
return await this.settings.get(SETTINGS_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the settings in the database
|
|
|
|
*
|
|
|
|
* @param settings the settings to set
|
|
|
|
* @returns the settings
|
|
|
|
*/
|
|
|
|
async setSettings(settings: Settings) {
|
|
|
|
return await this.settings.update(SETTINGS_ID, settings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const db = new Database();
|