File size: 7,056 Bytes
b66ef35 a46ce65 b66ef35 fd1786e 55b1a24 fd1786e 55b1a24 fd1786e a46ce65 b66ef35 a46ce65 b66ef35 a46ce65 b66ef35 a46ce65 b66ef35 a46ce65 b66ef35 a46ce65 b66ef35 381170a b66ef35 381170a 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
import { db } from './index';
import type { PicletInstance, Monster, BattleMove } from './schema';
import { PicletType, AttackType, getTypeFromConcept } from '../types/picletTypes';
// 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');
}
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);
// Determine primary type - use picletType from stats if available, otherwise auto-detect
let primaryType: PicletType;
if (monster.stats && (monster.stats as any).picletType) {
// Use the type determined during stat generation, but validate it
const statsType = (monster.stats as any).picletType as string;
const normalizedType = statsType.toLowerCase();
// Validate that the type exists in our enum
const validType = Object.values(PicletType).find(type => type === normalizedType);
if (validType) {
primaryType = validType;
} else {
console.warn(`Invalid picletType "${statsType}" from stats, falling back to concept detection`);
primaryType = getTypeFromConcept(monster.concept, monster.imageCaption);
}
} else {
// Fallback to concept-based detection
primaryType = getTypeFromConcept(monster.concept, monster.imageCaption);
}
// Create moves based on monster's abilities
const moves: BattleMove[] = [
{
name: stats.attackActionName,
type: primaryType as unknown as AttackType,
power: 50,
accuracy: 95,
pp: 20,
currentPp: 20,
description: stats.attackActionDescription
},
{
name: stats.buffActionName,
type: AttackType.NORMAL,
power: 0,
accuracy: 100,
pp: 15,
currentPp: 15,
description: stats.buffActionDescription
},
{
name: stats.debuffActionName,
type: AttackType.NORMAL,
power: 0,
accuracy: 85,
pp: 15,
currentPp: 15,
description: stats.debuffActionDescription
},
{
name: stats.specialActionName,
type: primaryType as unknown as AttackType,
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,
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: '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<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);
} |