import { db } from './index'; import type { PicletInstance, Monster, BattleMove } from './schema'; // Convert a generated Monster to a PicletInstance export async function monsterToPicletInstance(monster: Monster, level: number = 5): Promise> { if (!monster.stats) { throw new Error('Monster must have stats to create PicletInstance'); } const stats = monster.stats; // Calculate base stats from the 0-100 scale const baseHp = Math.floor(stats.HP * 2 + 50); const baseAttack = Math.floor(stats.attack * 1.5 + 30); const baseDefense = Math.floor(stats.defence * 1.5 + 30); const baseSpeed = Math.floor(stats.speed * 1.5 + 30); // Field stats are variations of regular stats const baseFieldAttack = Math.floor(baseAttack * 0.8); const baseFieldDefense = Math.floor(baseDefense * 0.8); // Calculate current stats based on level const calculateStat = (base: number, level: number) => Math.floor((base * level) / 50 + 5); const calculateHp = (base: number, level: number) => Math.floor((base * level) / 50 + level + 10); const maxHp = calculateHp(baseHp, level); // Create moves based on monster's abilities const moves: BattleMove[] = [ { name: stats.attackActionName, type: 'normal', power: 50, accuracy: 95, pp: 20, currentPp: 20, description: stats.attackActionDescription }, { name: stats.buffActionName, type: 'status', power: 0, accuracy: 100, pp: 15, currentPp: 15, description: stats.buffActionDescription }, { name: stats.debuffActionName, type: 'status', power: 0, accuracy: 85, pp: 15, currentPp: 15, description: stats.debuffActionDescription }, { name: stats.specialActionName, type: 'special', power: 80, accuracy: 90, pp: 5, currentPp: 5, description: stats.specialActionDescription } ]; const bst = baseHp + baseAttack + baseDefense + baseFieldAttack + baseFieldDefense + baseSpeed; return { // Type Info typeId: monster.name.toLowerCase().replace(/\s+/g, '-'), nickname: monster.name, primaryTypeString: 'normal', // Default type, could be enhanced based on concept secondaryTypeString: undefined, // Current Stats currentHp: maxHp, maxHp, level, xp: 0, attack: calculateStat(baseAttack, level), defense: calculateStat(baseDefense, level), fieldAttack: calculateStat(baseFieldAttack, level), fieldDefense: calculateStat(baseFieldDefense, level), speed: calculateStat(baseSpeed, level), // Base Stats baseHp, baseAttack, baseDefense, baseFieldAttack, baseFieldDefense, baseSpeed, // Battle moves, nature: 'hardy', // Default nature // Roster isInRoster: false, rosterPosition: undefined, // Metadata caughtAt: new Date(), bst, tier: (stats as any).tier || 'medium', // Use tier from stats, default to medium role: 'balanced', // Could be enhanced based on stat distribution variance: 0, // Original generation data imageUrl: monster.imageUrl, imageData: monster.imageData, imageCaption: monster.imageCaption, concept: stats.description || monster.concept, // Use the Monster Lore description imagePrompt: monster.imagePrompt }; } // Save a new PicletInstance export async function savePicletInstance(piclet: Omit): Promise { return await db.picletInstances.add(piclet); } // Get all PicletInstances export async function getAllPicletInstances(): Promise { return await db.picletInstances.toArray(); } // Get roster PicletInstances export async function getRosterPiclets(): Promise { const allPiclets = await db.picletInstances.toArray(); return allPiclets .filter(p => p.rosterPosition !== undefined && p.rosterPosition !== null && p.rosterPosition >= 0 && p.rosterPosition <= 5 ) .sort((a, b) => (a.rosterPosition ?? 0) - (b.rosterPosition ?? 0)); } // Update roster position export async function updateRosterPosition(id: number, position: number | undefined): Promise { await db.picletInstances.update(id, { isInRoster: position !== undefined, rosterPosition: position }); } // Move piclet to roster export async function moveToRoster(id: number, position: number): Promise { // Check if position is already occupied const existingPiclet = await db.picletInstances .where('rosterPosition') .equals(position) .and(item => item.isInRoster) .first(); if (existingPiclet) { // Move existing piclet to storage await db.picletInstances.update(existingPiclet.id!, { isInRoster: false, rosterPosition: undefined }); } // Move new piclet to roster await db.picletInstances.update(id, { isInRoster: true, rosterPosition: position }); } // Swap roster positions export async function swapRosterPositions(id1: number, position1: number, id2: number, position2: number): Promise { await db.transaction('rw', db.picletInstances, async () => { await db.picletInstances.update(id1, { rosterPosition: position2 }); await db.picletInstances.update(id2, { rosterPosition: position1 }); }); } // Move piclet to storage export async function moveToStorage(id: number): Promise { await db.picletInstances.update(id, { isInRoster: false, rosterPosition: undefined }); } // Get storage piclets export async function getStoragePiclets(): Promise { const allPiclets = await db.picletInstances.toArray(); return allPiclets.filter(p => p.rosterPosition === undefined || p.rosterPosition === null || p.rosterPosition < 0 || p.rosterPosition > 5 ); } // Delete a PicletInstance export async function deletePicletInstance(id: number): Promise { await db.picletInstances.delete(id); }