Renamed downloadAndStore to downloadCache

This commit is contained in:
Dennis Postma 2025-01-23 19:46:19 +01:00
parent 5202251ac7
commit 9744083dea
3 changed files with 27 additions and 22 deletions

View File

@ -1,3 +1,7 @@
import type { BaseStorage } from '@/storage/baseStorage'
import config from '@/application/config'
import type { HttpResponse } from '@/application/types'
export function uuidv4() { 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)) 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('.') return window.location.hostname.split('.').slice(-2).join('.')
} }
export async function downloadCache<T extends { id: string }>(endpoint: string, storage: BaseStorage<T>) {
const request = await fetch(`${config.server_endpoint}/cache/${endpoint}`)
const response = (await request.json()) as HttpResponse<T[]>
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)
}
}

View File

@ -22,38 +22,24 @@ import { CharacterHairStorage, CharacterTypeStorage, MapObjectStorage, MapStorag
// import type { Map } from '@/application/types' // import type { Map } from '@/application/types'
import { useGameStore } from '@/stores/gameStore' import { useGameStore } from '@/stores/gameStore'
import { ref } from 'vue' import { ref } from 'vue'
import { downloadCache } from '@/application/utilities'
const gameStore = useGameStore() const gameStore = useGameStore()
const totalItems = ref(0) const totalItems = ref(0)
const currentItem = ref(0) const currentItem = ref(0)
async function downloadAndStore<T extends { id: string }>(endpoint: string, storage: BaseStorage<T>) {
const request = await fetch(`${config.server_endpoint}/cache/${endpoint}`)
const response = (await request.json()) as HttpResponse<T[]>
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 tileStorage = new TileStorage()
const mapStorage = new MapStorage() const mapStorage = new MapStorage()
const mapObjectStorage = new MapObjectStorage() const mapObjectStorage = new MapObjectStorage()
Promise.all([ Promise.all([
downloadAndStore('tiles', tileStorage), downloadCache('tiles', tileStorage),
downloadAndStore('maps', mapStorage), downloadCache('maps', mapStorage),
downloadAndStore('map_objects', mapObjectStorage), downloadCache('map_objects', mapObjectStorage),
downloadAndStore('sprites', new SpriteStorage()), downloadCache('sprites', new SpriteStorage()),
downloadAndStore('character_types', new CharacterTypeStorage()), downloadCache('character_types', new CharacterTypeStorage()),
downloadAndStore('character_hair', new CharacterHairStorage()) downloadCache('character_hair', new CharacterHairStorage())
]).then(() => { ]).then(() => {
gameStore.game.isLoaded = true gameStore.game.isLoaded = true
}) })

View File

@ -11,7 +11,7 @@ export type TeleportSettings = {
export const useMapEditorStore = defineStore('mapEditor', { export const useMapEditorStore = defineStore('mapEditor', {
state: () => { state: () => {
return { return {
active: true, active: false,
mapId: '', mapId: '',
tool: 'move', tool: 'move',
drawMode: 'tile', drawMode: 'tile',