|
|
import { db } from './index'; |
|
|
import type { PicletInstance } from './schema'; |
|
|
import { PicletType } from '../types/picletTypes'; |
|
|
import type { PicletStats } from '../types'; |
|
|
import { generatePicletId } from '../utils/picletId'; |
|
|
import type { UserInfo } from '../stores/auth'; |
|
|
|
|
|
|
|
|
interface GeneratedPicletData { |
|
|
name: string; |
|
|
imageUrl: string; |
|
|
imageData?: string; |
|
|
imageCaption: string; |
|
|
concept: string; |
|
|
imagePrompt: string; |
|
|
stats: PicletStats; |
|
|
createdAt: Date; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function generatedDataToPicletInstance( |
|
|
data: GeneratedPicletData, |
|
|
objectName?: string, |
|
|
attributes?: string[], |
|
|
visualDetails?: string, |
|
|
userInfo?: UserInfo | null |
|
|
): 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 || 'common'; |
|
|
|
|
|
|
|
|
|
|
|
const normalizedObjectName = objectName || stats.name; |
|
|
const variation = attributes && attributes.length > 0 ? attributes[0] : undefined; |
|
|
const typeId = generatePicletId(normalizedObjectName, variation && variation !== 'canonical' ? variation : undefined); |
|
|
|
|
|
return { |
|
|
|
|
|
typeId: typeId, |
|
|
objectName: objectName || stats.name || data.name, |
|
|
nickname: stats.name || data.name, |
|
|
primaryType: stats.primaryType as PicletType, |
|
|
tier: tier, |
|
|
|
|
|
|
|
|
isCanonical: false, |
|
|
canonicalId: undefined, |
|
|
variationAttributes: attributes || [], |
|
|
|
|
|
discoveredBy: userInfo?.preferred_username || 'Player', |
|
|
discovererSub: userInfo?.sub, |
|
|
discovererUsername: userInfo?.preferred_username, |
|
|
discovererName: userInfo?.name, |
|
|
discovererPicture: userInfo?.picture, |
|
|
discoveredAt: new Date(), |
|
|
scanCount: 1, |
|
|
|
|
|
|
|
|
isInCollection: true, |
|
|
collectedAt: new Date(), |
|
|
|
|
|
|
|
|
imageUrl: data.imageUrl, |
|
|
imageData: data.imageData, |
|
|
imageCaption: data.imageCaption, |
|
|
visualDetails: visualDetails || '', |
|
|
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 getCollectedPiclets(): Promise<PicletInstance[]> { |
|
|
const allRecords = await db.picletInstances.toArray(); |
|
|
return allRecords.filter(p => p.isInCollection === true); |
|
|
} |
|
|
|
|
|
|
|
|
export async function getCanonicalPiclets(): Promise<PicletInstance[]> { |
|
|
|
|
|
const allRecords = await db.picletInstances.toArray(); |
|
|
return allRecords.filter(p => p.isCanonical === true); |
|
|
} |
|
|
|
|
|
|
|
|
export async function getPicletsByObjectName(objectName: string): Promise<PicletInstance[]> { |
|
|
return await db.picletInstances.where('objectName').equals(objectName).toArray(); |
|
|
} |
|
|
|
|
|
|
|
|
export async function getVariations(canonicalId: string): Promise<PicletInstance[]> { |
|
|
return await db.picletInstances.where('canonicalId').equals(canonicalId).toArray(); |
|
|
} |
|
|
|
|
|
|
|
|
export async function updateScanCount(picletId: number): Promise<void> { |
|
|
const piclet = await db.picletInstances.get(picletId); |
|
|
if (piclet) { |
|
|
await db.picletInstances.update(picletId, { |
|
|
scanCount: (piclet.scanCount || 0) + 1 |
|
|
}); |
|
|
} |
|
|
} |