|
import { db } from './index'; |
|
import type { PicletInstance } from './schema'; |
|
import { PicletType } from '../types/picletTypes'; |
|
import type { PicletStats } from '../types'; |
|
|
|
|
|
interface GeneratedPicletData { |
|
name: string; |
|
imageUrl: string; |
|
imageData?: string; |
|
imageCaption: string; |
|
concept: string; |
|
imagePrompt: string; |
|
stats: PicletStats; |
|
createdAt: Date; |
|
} |
|
|
|
|
|
export async function generatedDataToPicletInstance(data: GeneratedPicletData): Promise<Omit<PicletInstance, 'id'>> { |
|
if (!data.stats) { |
|
throw new Error('Generated data must have stats to create PicletInstance'); |
|
} |
|
|
|
const stats = data.stats as PicletStats; |
|
|
|
|
|
let tier: string = stats.tier || 'medium'; |
|
|
|
|
|
const existingPiclets = await db.picletInstances.count(); |
|
const isFirstPiclet = existingPiclets === 0; |
|
|
|
return { |
|
|
|
typeId: stats.name || data.name, |
|
nickname: stats.name || data.name, |
|
primaryType: stats.primaryType as PicletType, |
|
tier: tier, |
|
|
|
|
|
isInRoster: isFirstPiclet, |
|
rosterPosition: isFirstPiclet ? 0 : undefined, |
|
|
|
|
|
caught: isFirstPiclet, |
|
caughtAt: isFirstPiclet ? new Date() : undefined, |
|
|
|
|
|
imageUrl: data.imageUrl, |
|
imageData: data.imageData, |
|
imageCaption: data.imageCaption, |
|
concept: data.concept, |
|
description: stats.description, |
|
imagePrompt: data.imagePrompt |
|
}; |
|
} |
|
|
|
|
|
export async function savePicletInstance(picletInstance: Omit<PicletInstance, 'id'>): Promise<number> { |
|
const id = await db.picletInstances.add(picletInstance as any); |
|
return id; |
|
} |
|
|
|
|
|
export async function updatePicletInstance(id: number, updates: Partial<PicletInstance>): Promise<void> { |
|
await db.picletInstances.update(id, updates); |
|
} |
|
|
|
|
|
export async function deletePicletInstance(id: number): Promise<void> { |
|
await db.picletInstances.delete(id); |
|
} |
|
|
|
|
|
export async function getAllPicletInstances(): Promise<PicletInstance[]> { |
|
return await db.picletInstances.toArray(); |
|
} |
|
|
|
|
|
export async function getPicletInstance(id: number): Promise<PicletInstance | undefined> { |
|
return await db.picletInstances.get(id); |
|
} |
|
|
|
|
|
export async function getRosterPiclets(): Promise<PicletInstance[]> { |
|
return await db.picletInstances.where('isInRoster').equals(1).toArray(); |
|
} |
|
|
|
|
|
export async function getCaughtPiclets(): Promise<PicletInstance[]> { |
|
return await db.picletInstances.where('caught').equals(1).toArray(); |
|
} |
|
|
|
|
|
export async function moveToRoster(picletId: number, position: number): Promise<void> { |
|
await db.picletInstances.update(picletId, { |
|
isInRoster: true, |
|
rosterPosition: position |
|
}); |
|
} |
|
|
|
|
|
export async function getUncaughtPiclets(): Promise<PicletInstance[]> { |
|
return await db.picletInstances.where('caught').equals(0).toArray(); |
|
} |
|
|
|
|
|
export async function swapRosterPositions(id1: number, position1: number, id2: number, position2: number): Promise<void> { |
|
await db.picletInstances.update(id1, { rosterPosition: position2 }); |
|
await db.picletInstances.update(id2, { rosterPosition: position1 }); |
|
} |
|
|
|
|
|
export async function moveToStorage(picletId: number): Promise<void> { |
|
await db.picletInstances.update(picletId, { |
|
isInRoster: false, |
|
rosterPosition: undefined |
|
}); |
|
} |