File size: 6,320 Bytes
b66ef35 a46ce65 ac7c05c b66ef35 987557b b66ef35 987557b 4c208f2 987557b 4c208f2 b66ef35 987557b b66ef35 a46ce65 b66ef35 987557b b66ef35 987557b b66ef35 4c208f2 b66ef35 2e6926b b66ef35 21805fd 2e6926b 21805fd b66ef35 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
import { db } from './index';
import type { PicletInstance, Monster, BattleMove } from './schema';
import { PicletType, AttackType, getTypeFromConcept } from '../types/picletTypes';
import type { PicletStats } from '../types';
// Convert a generated Monster to a PicletInstance
export async function monsterToPicletInstance(monster: Monster, level: number = 5): Promise<Omit<PicletInstance, 'id'>> {
if (!monster.stats) {
throw new Error('Monster must have stats to create PicletInstance');
}
// All monsters must now have the battle-ready format
const stats = monster.stats as PicletStats;
if (!stats.baseStats || !stats.specialAbility || !stats.movepool) {
throw new Error('Monster stats must be in battle-ready format with baseStats, specialAbility, and movepool');
}
// Calculate base stats from battle-ready format
const baseHp = Math.floor(stats.baseStats.hp * 2 + 50);
const baseAttack = Math.floor(stats.baseStats.attack * 1.5 + 30);
const baseDefense = Math.floor(stats.baseStats.defense * 1.5 + 30);
const baseSpeed = Math.floor(stats.baseStats.speed * 1.5 + 30);
// Determine primary type from battle stats
const normalizedType = stats.primaryType.toLowerCase();
const validType = Object.values(PicletType).find(type => type === normalizedType);
const primaryType = validType || getTypeFromConcept(monster.concept, monster.imageCaption);
if (!validType) {
console.warn(`Invalid primaryType "${stats.primaryType}" from stats, falling back to concept detection`);
}
// Create moves from battle-ready format
const moves: BattleMove[] = stats.movepool.map(move => ({
name: move.name,
type: move.type as unknown as AttackType,
power: move.power,
accuracy: move.accuracy,
pp: move.pp,
currentPp: move.pp,
description: `${move.effects?.length || 0} effects` // Show number of effects
}));
// 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);
const bst = baseHp + baseAttack + baseDefense + baseFieldAttack + baseFieldDefense + baseSpeed;
return {
// Type Info
typeId: monster.name.toLowerCase().replace(/\s+/g, '-'),
nickname: monster.name,
primaryType: primaryType,
secondaryType: 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: stats.nature,
specialAbility: stats.specialAbility,
// Roster
isInRoster: false,
rosterPosition: undefined,
// Metadata
caughtAt: new Date(),
bst,
tier: stats.tier, // Use tier from stats
role: 'balanced', // Could be enhanced based on stat distribution
variance: 0,
// Original generation data
imageUrl: monster.imageUrl,
imageData: monster.imageData,
imageCaption: monster.imageCaption,
concept: monster.concept, // Use the original concept
imagePrompt: monster.imagePrompt
};
}
// Save a new PicletInstance
export async function savePicletInstance(piclet: Omit<PicletInstance, 'id'>): Promise<number> {
return await db.picletInstances.add(piclet);
}
// Get all PicletInstances
export async function getAllPicletInstances(): Promise<PicletInstance[]> {
return await db.picletInstances.toArray();
}
// Get roster PicletInstances
export async function getRosterPiclets(): Promise<PicletInstance[]> {
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<void> {
await db.picletInstances.update(id, {
isInRoster: position !== undefined,
rosterPosition: position
});
}
// Move piclet to roster
export async function moveToRoster(id: number, position: number): Promise<void> {
// 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<void> {
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<void> {
await db.picletInstances.update(id, {
isInRoster: false,
rosterPosition: undefined
});
}
// Get storage piclets
export async function getStoragePiclets(): Promise<PicletInstance[]> {
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<void> {
await db.picletInstances.delete(id);
} |