centralized save data

This commit is contained in:
2024-11-22 22:53:11 +01:00
parent dd92009cba
commit bfd30c935b
2 changed files with 64 additions and 9 deletions
+28 -9
View File
@@ -33,7 +33,7 @@ import {version} from "../package.json"
<script lang="ts"> <script lang="ts">
import {defineComponent} from "vue"; import {defineComponent} from "vue";
import AdventurerIdentity from "@/models/AdventurerIdentity.ts"; import AdventurerIdentity from "@/models/AdventurerIdentity.ts";
import Adventurer from "@/models/Adventurer.ts"; import SaveData from "@/models/SaveData.ts";
export default defineComponent({ export default defineComponent({
name: "GuildMaster", name: "GuildMaster",
@@ -41,7 +41,7 @@ export default defineComponent({
loading: true, loading: true,
screenWakeLock: null as null | WakeLockSentinel, screenWakeLock: null as null | WakeLockSentinel,
possibleRecruits: {} as { [key: string]: AdventurerIdentity }, possibleRecruits: {} as { [key: string]: AdventurerIdentity },
adventurers: {} as { [key: string]: Adventurer }, saveData: null as null | SaveData,
}), }),
methods: { methods: {
async acquireScreenWakeLock(): Promise<void> { async acquireScreenWakeLock(): Promise<void> {
@@ -59,12 +59,11 @@ export default defineComponent({
}, 1000); }, 1000);
} }
}, },
async loadAdventurers(): Promise<void> { async loadAdventurerData(): Promise<void> {
await fetch("/data/adventurers.json") await fetch("/data/adventurers.json")
.then(response => response.json()) .then(response => response.json())
.then(data => { .then(data => {
if (!Array.isArray(data)) throw new Error("Data was expected to be an array"); if (!Array.isArray(data)) throw new Error("Data was expected to be an array");
for (const rawData of data) { for (const rawData of data) {
if (typeof rawData.id !== "string" || typeof rawData.name !== "string" || typeof rawData.portrait !== "string") { if (typeof rawData.id !== "string" || typeof rawData.name !== "string" || typeof rawData.portrait !== "string") {
console.error("Failed to load one of the adventurer's data: invalid data"); console.error("Failed to load one of the adventurer's data: invalid data");
@@ -76,25 +75,45 @@ export default defineComponent({
rawData.portrait, rawData.portrait,
); );
} }
// TODO load adventurers from save file and remove their data from possible recruits
}) })
.catch(error => { .catch(error => {
console.error("Failed to load adventurer data:", error); console.error("Failed to load adventurer data:", error);
}); });
}, },
saveGame(): void {
window.localStorage.setItem("saveData", JSON.stringify(this.saveData));
console.debug("Game saved");
},
loadGame(): void {
const saveData = window.localStorage.getItem("saveData");
if (saveData === null) {
this.saveData = new SaveData({}, this.possibleRecruits);
return;
}
const jsonData = JSON.parse(saveData);
if (jsonData === null) return;
this.saveData = new SaveData(jsonData, this.possibleRecruits);
for (const adventurer of this.saveData.adventurers) {
delete this.possibleRecruits[adventurer?.identity?.id];
}
}
}, },
async mounted() { async mounted() {
await this.acquireScreenWakeLock(); await this.acquireScreenWakeLock();
await this.loadAdventurers(); await this.loadAdventurerData();
this.loadGame();
this.loading = false; this.loading = false;
console.debug("Game loaded"); console.debug("Game loaded");
setInterval(() => {
this.saveGame();
}, 10 * 1000);
}, },
}) })
</script> </script>
+36
View File
@@ -0,0 +1,36 @@
import Adventurer from "@/models/Adventurer.ts";
import type AdventurerIdentity from "@/models/AdventurerIdentity.ts";
import AdventurerInventory from "@/models/AdventurerInventory.ts";
export default class SaveData {
adventurers: Array<Adventurer> = [];
constructor(
data: {[key:string]: unknown} = {},
adventurerIdentities: {[key:string]: AdventurerIdentity} = {}
) {
if (Array.isArray(data?.adventurers)) {
for (const adventurerData of data.adventurers) {
const identity = adventurerIdentities[adventurerData?.identity?.id];
if (!identity) {
console.error("Adventurer identity not found for adventurer data", adventurerData);
continue;
}
this.adventurers.push(new Adventurer(
identity,
adventurerData?.experience ?? 0,
adventurerData?.basePower ?? 0,
adventurerData?.baseDefense ?? 0,
new AdventurerInventory(
adventurerData?.inventory?.helmet ?? null,
adventurerData?.inventory?.armor ?? null,
adventurerData?.inventory?.boots ?? null,
adventurerData?.inventory?.weapon ?? null,
)
));
}
}
}
}