File size: 4,321 Bytes
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
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<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);
  
  // 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.rarity > 80 ? 'legendary' : stats.rarity > 60 ? 'rare' : stats.rarity > 40 ? 'uncommon' : 'common',
    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,
    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[]> {
  return await db.picletInstances
    .where('isInRoster')
    .equals(1)
    .sortBy('rosterPosition');
}

// Update roster position
export async function updateRosterPosition(id: number, position: number | undefined): Promise<void> {
  await db.picletInstances.update(id, {
    isInRoster: position !== undefined,
    rosterPosition: position
  });
}

// Delete a PicletInstance
export async function deletePicletInstance(id: number): Promise<void> {
  await db.picletInstances.delete(id);
}