1
0
forked from noxious/client
This commit is contained in:
2025-01-07 22:20:46 +01:00
parent c2db9b5469
commit 574777da80
19 changed files with 385 additions and 416 deletions

View File

@ -0,0 +1,60 @@
import Dexie from 'dexie'
export class BaseStorage<T extends { id: string }> {
protected dexie: Dexie
protected tableName: string
constructor(tableName: string, schema: string) {
this.tableName = tableName
this.dexie = new Dexie(tableName)
this.dexie.version(1).stores({
[tableName]: schema
})
}
async add(item: T, overwrite = false) {
try {
const existing = await this.get(item.id)
if (existing && !overwrite) return
await this.dexie.table(this.tableName).put({ ...item })
} catch (error) {
console.error(`Failed to add ${this.tableName} ${item.id}:`, error)
}
}
async get(id: string): Promise<T | null> {
try {
const item = await this.dexie.table(this.tableName).get(id)
return item || null
} catch (error) {
console.error(`Failed to retrieve ${this.tableName} ${id}:`, error)
return null
}
}
async getAll(): Promise<T[]> {
try {
return await this.dexie.table(this.tableName).toArray()
} catch (error) {
console.error(`Failed to retrieve all ${this.tableName}:`, error)
return []
}
}
async reset() {
try {
await this.dexie.table(this.tableName).clear()
} catch (error) {
console.error(`Failed to clear ${this.tableName}:`, error)
}
}
async destroy() {
try {
await this.dexie.delete()
} catch (error) {
console.error(`Failed to destroy ${this.tableName}:`, error)
}
}
}

39
src/storage/storages.ts Normal file
View File

@ -0,0 +1,39 @@
import type { Tile, Map, MapObject } from '@/application/types'
import { BaseStorage } from '@/storage/baseStorage'
export class MapStorage extends BaseStorage<Map> {
constructor() {
super('maps', 'id, name, createdAt, updatedAt')
}
}
export class TileStorage extends BaseStorage<Tile> {
constructor() {
super('tiles', 'id, name, createdAt, updatedAt')
}
}
export class MapObjectStorage extends BaseStorage<MapObject> {
constructor() {
super('mapObjects', 'id, name, createdAt, updatedAt')
}
}
export class SpriteStorage extends BaseStorage<any> {
constructor() {
super('sprites', 'id, name, createdAt, updatedAt')
}
}
export class CharacterTypeStorage extends BaseStorage<any> {
constructor() {
super('characterTypes', 'id, name, createdAt, updatedAt')
}
}
export class CharacterHairStorage extends BaseStorage<any> {
constructor() {
super('characterHairs', 'id, name, createdAt, updatedAt')
}
}

View File

@ -0,0 +1,97 @@
import config from '@/application/config'
import type { TextureData } from '@/application/types'
import Dexie from 'dexie'
export class TextureStorage {
private dexie: Dexie
constructor() {
this.dexie = new Dexie('textures')
this.dexie.version(1).stores({
textures: 'key, group'
})
}
async download(texture: TextureData) {
try {
// Check if the texture already exists, then check if updatedAt is newer
const _texture = await this.dexie.table('textures').get(texture.key)
if (_texture && _texture.updatedAt > texture.updatedAt) {
return
}
// Download the texture
const response = await fetch(config.server_endpoint + texture.data)
const blob = await response.blob()
// Store the texture in the database
await this.dexie.table('textures').put({
key: texture.key,
data: blob,
group: texture.group,
updatedAt: texture.updatedAt,
originX: texture.originX,
originY: texture.originY,
isAnimated: texture.isAnimated,
frameRate: texture.frameRate,
frameWidth: texture.frameWidth,
frameHeight: texture.frameHeight,
frameCount: texture.frameCount
})
} catch (error) {
console.error(`Failed to add texture ${texture.key}:`, error)
}
}
async get(key: string) {
try {
const texture = await this.dexie.table('textures').get(key)
if (texture) {
return {
...texture,
data: URL.createObjectURL(texture.data) // Convert blob to data URL
}
}
} catch (error) {
console.error(`Failed to retrieve texture ${key}:`, error)
}
return null
}
async getByGroup(group: string) {
try {
const textures = await this.dexie.table('textures').where('group').equals(group).toArray()
return textures.map((texture) => ({
...texture,
data: URL.createObjectURL(texture.data) // Convert blob to data URL
}))
} catch (error) {
console.error(`Failed to retrieve textures for group ${group}:`, error)
return []
}
}
async delete(key: string) {
try {
await this.dexie.table('textures').delete(key)
} catch (error) {
console.error(`Failed to delete texture ${key}:`, error)
}
}
async reset() {
try {
await this.dexie.table('textures').clear()
} catch (error) {
console.error(`Failed to clear textures:`, error)
}
}
async destroy() {
try {
await this.dexie.delete()
} catch (error) {
console.error(`Failed to destroy textures:`, error)
}
}
}