mirror of
https://github.com/YouHaveTrouble/GuildMaster.git
synced 2026-05-12 06:26:59 +00:00
centralized save data
This commit is contained in:
+28
-9
@@ -33,7 +33,7 @@ import {version} from "../package.json"
|
||||
<script lang="ts">
|
||||
import {defineComponent} from "vue";
|
||||
import AdventurerIdentity from "@/models/AdventurerIdentity.ts";
|
||||
import Adventurer from "@/models/Adventurer.ts";
|
||||
import SaveData from "@/models/SaveData.ts";
|
||||
|
||||
export default defineComponent({
|
||||
name: "GuildMaster",
|
||||
@@ -41,7 +41,7 @@ export default defineComponent({
|
||||
loading: true,
|
||||
screenWakeLock: null as null | WakeLockSentinel,
|
||||
possibleRecruits: {} as { [key: string]: AdventurerIdentity },
|
||||
adventurers: {} as { [key: string]: Adventurer },
|
||||
saveData: null as null | SaveData,
|
||||
}),
|
||||
methods: {
|
||||
async acquireScreenWakeLock(): Promise<void> {
|
||||
@@ -59,12 +59,11 @@ export default defineComponent({
|
||||
}, 1000);
|
||||
}
|
||||
},
|
||||
async loadAdventurers(): Promise<void> {
|
||||
async loadAdventurerData(): Promise<void> {
|
||||
await fetch("/data/adventurers.json")
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (!Array.isArray(data)) throw new Error("Data was expected to be an array");
|
||||
|
||||
for (const rawData of data) {
|
||||
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");
|
||||
@@ -76,25 +75,45 @@ export default defineComponent({
|
||||
rawData.portrait,
|
||||
);
|
||||
}
|
||||
|
||||
// TODO load adventurers from save file and remove their data from possible recruits
|
||||
|
||||
})
|
||||
.catch(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() {
|
||||
|
||||
await this.acquireScreenWakeLock();
|
||||
|
||||
await this.loadAdventurers();
|
||||
await this.loadAdventurerData();
|
||||
|
||||
this.loadGame();
|
||||
|
||||
this.loading = false;
|
||||
console.debug("Game loaded");
|
||||
|
||||
setInterval(() => {
|
||||
this.saveGame();
|
||||
}, 10 * 1000);
|
||||
|
||||
},
|
||||
})
|
||||
</script>
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user