2024-03-28 05:08:09 +01:00
|
|
|
import { SaveData } from "./types";
|
|
|
|
|
2021-09-21 22:49:38 +02:00
|
|
|
function getDB(): Promise<IDBObjectStore> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!window.indexedDB) {
|
|
|
|
reject("Indexed DB does not exists");
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* DB is called bitburnerSave
|
|
|
|
* Object store is called savestring
|
|
|
|
* key for the Object store is called save
|
|
|
|
* Version `1` is important
|
|
|
|
*/
|
|
|
|
const indexedDbRequest: IDBOpenDBRequest = window.indexedDB.open("bitburnerSave", 1);
|
|
|
|
|
|
|
|
// This is called when there's no db to begin with. It's important, don't remove it.
|
|
|
|
indexedDbRequest.onupgradeneeded = function (this: IDBRequest<IDBDatabase>) {
|
|
|
|
const db = this.result;
|
|
|
|
db.createObjectStore("savestring");
|
|
|
|
};
|
|
|
|
|
|
|
|
indexedDbRequest.onerror = function (this: IDBRequest<IDBDatabase>, ev: Event) {
|
|
|
|
reject(`Failed to get IDB ${ev}`);
|
|
|
|
};
|
|
|
|
|
2021-09-25 07:26:03 +02:00
|
|
|
indexedDbRequest.onsuccess = function (this: IDBRequest<IDBDatabase>) {
|
2021-09-21 22:49:38 +02:00
|
|
|
const db = this.result;
|
|
|
|
if (!db) {
|
2022-01-10 06:17:04 +01:00
|
|
|
reject("database loading result was undefined");
|
2021-09-21 22:49:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(db.transaction(["savestring"], "readwrite").objectStore("savestring"));
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-28 05:08:09 +01:00
|
|
|
export function load(): Promise<SaveData> {
|
2021-11-12 05:28:08 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
getDB()
|
2021-09-21 22:49:38 +02:00
|
|
|
.then((db) => {
|
2024-03-28 05:08:09 +01:00
|
|
|
return new Promise<SaveData>((resolve, reject) => {
|
|
|
|
const request: IDBRequest<SaveData> = db.get("save");
|
|
|
|
request.onerror = function (this: IDBRequest<SaveData>, ev: Event) {
|
|
|
|
reject("Error in Database request to get save data: " + ev);
|
2021-09-21 22:49:38 +02:00
|
|
|
};
|
|
|
|
|
2024-03-28 05:08:09 +01:00
|
|
|
request.onsuccess = function (this: IDBRequest<SaveData>) {
|
2021-09-21 22:49:38 +02:00
|
|
|
resolve(this.result);
|
|
|
|
};
|
2024-03-28 05:08:09 +01:00
|
|
|
}).then((saveData) => resolve(saveData));
|
2021-09-21 22:49:38 +02:00
|
|
|
})
|
|
|
|
.catch((r) => reject(r));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-03-28 05:08:09 +01:00
|
|
|
export function save(saveData: SaveData): Promise<void> {
|
2021-09-21 22:49:38 +02:00
|
|
|
return getDB().then((db) => {
|
|
|
|
return new Promise<void>((resolve, reject) => {
|
2024-03-28 05:08:09 +01:00
|
|
|
// We'll save to IndexedDB
|
|
|
|
const request = db.put(saveData, "save");
|
2021-09-21 22:49:38 +02:00
|
|
|
|
|
|
|
request.onerror = function (e) {
|
|
|
|
reject("Error saving game to IndexedDB: " + e);
|
|
|
|
};
|
|
|
|
|
|
|
|
request.onsuccess = () => resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteGame(): Promise<void> {
|
|
|
|
return getDB().then((db) => {
|
|
|
|
db.delete("save");
|
|
|
|
});
|
|
|
|
}
|