diff --git a/src/application/utilities.ts b/src/application/utilities.ts index b298e58..f2484f9 100644 --- a/src/application/utilities.ts +++ b/src/application/utilities.ts @@ -1,3 +1,7 @@ +import type { BaseStorage } from '@/storage/baseStorage' +import config from '@/application/config' +import type { HttpResponse } from '@/application/types' + export function uuidv4() { return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, (c) => (+c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (+c / 4)))).toString(16)) } @@ -23,3 +27,18 @@ export function getDomain() { return window.location.hostname.split('.').slice(-2).join('.') } + +export async function downloadCache(endpoint: string, storage: BaseStorage) { + const request = await fetch(`${config.server_endpoint}/cache/${endpoint}`) + const response = (await request.json()) as HttpResponse + + if (!response.success) { + console.error(`Failed to download ${endpoint}:`, response.message) + return + } + + const items = response.data ?? [] + for (const item of items) { + await storage.add(item) + } +} \ No newline at end of file diff --git a/src/components/screens/Loading.vue b/src/components/screens/Loading.vue index caa7109..91a82bb 100644 --- a/src/components/screens/Loading.vue +++ b/src/components/screens/Loading.vue @@ -22,38 +22,24 @@ import { CharacterHairStorage, CharacterTypeStorage, MapObjectStorage, MapStorag // import type { Map } from '@/application/types' import { useGameStore } from '@/stores/gameStore' import { ref } from 'vue' +import { downloadCache } from '@/application/utilities' const gameStore = useGameStore() const totalItems = ref(0) const currentItem = ref(0) -async function downloadAndStore(endpoint: string, storage: BaseStorage) { - const request = await fetch(`${config.server_endpoint}/cache/${endpoint}`) - const response = (await request.json()) as HttpResponse - - if (!response.success) { - console.error(`Failed to download ${endpoint}:`, response.message) - return - } - - const items = response.data ?? [] - for (const item of items) { - await storage.add(item) - } -} - const tileStorage = new TileStorage() const mapStorage = new MapStorage() const mapObjectStorage = new MapObjectStorage() Promise.all([ - downloadAndStore('tiles', tileStorage), - downloadAndStore('maps', mapStorage), - downloadAndStore('map_objects', mapObjectStorage), - downloadAndStore('sprites', new SpriteStorage()), - downloadAndStore('character_types', new CharacterTypeStorage()), - downloadAndStore('character_hair', new CharacterHairStorage()) + downloadCache('tiles', tileStorage), + downloadCache('maps', mapStorage), + downloadCache('map_objects', mapObjectStorage), + downloadCache('sprites', new SpriteStorage()), + downloadCache('character_types', new CharacterTypeStorage()), + downloadCache('character_hair', new CharacterHairStorage()) ]).then(() => { gameStore.game.isLoaded = true }) diff --git a/src/stores/mapEditorStore.ts b/src/stores/mapEditorStore.ts index f36dddf..49215d5 100644 --- a/src/stores/mapEditorStore.ts +++ b/src/stores/mapEditorStore.ts @@ -11,7 +11,7 @@ export type TeleportSettings = { export const useMapEditorStore = defineStore('mapEditor', { state: () => { return { - active: true, + active: false, mapId: '', tool: 'move', drawMode: 'tile',