Fraser commited on
Commit
f9de116
·
1 Parent(s): f6cee99

try fix encounter start

Browse files
Files changed (1) hide show
  1. src/lib/db/encounterService.ts +38 -12
src/lib/db/encounterService.ts CHANGED
@@ -47,11 +47,27 @@ export class EncounterService {
47
  const uncaughtPiclets = await getUncaughtPiclets();
48
 
49
  if (caughtPiclets.length === 0) {
50
- // Player has no caught piclets - return empty encounters (no shop/heal until first piclet is caught)
51
- console.log('Player has no caught piclets - returning empty encounters');
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  await db.encounters.clear();
 
 
 
53
  await markEncountersRefreshed();
54
- return [];
55
  }
56
 
57
  // Player has caught piclets - generate normal encounters
@@ -88,15 +104,19 @@ export class EncounterService {
88
  }
89
 
90
  // Create first catch encounter
91
- private static async createFirstCatchEncounter(): Promise<Omit<Encounter, 'id'>> {
92
- // TODO: Replace with actual piclet data when available
93
- // For now, using placeholder data
 
 
 
 
94
  return {
95
  type: EncounterType.WILD_PICLET,
96
  title: 'Your First Piclet!',
97
- description: 'A friendly piclet appears! This one seems easy to catch.',
98
- picletTypeId: 'starter-001', // Placeholder ID
99
- enemyLevel: 5,
100
  createdAt: new Date()
101
  };
102
  }
@@ -175,9 +195,15 @@ export class EncounterService {
175
  static async catchWildPiclet(encounter: Encounter): Promise<PicletInstance> {
176
  if (!encounter.picletTypeId) throw new Error('No piclet type specified');
177
 
178
- // Find a caught piclet instance with this typeId to use as a template
179
  const caughtPiclets = await getCaughtPiclets();
180
- const templatePiclet = caughtPiclets.find(p => p.typeId === encounter.picletTypeId);
 
 
 
 
 
 
181
 
182
  if (!templatePiclet) {
183
  throw new Error(`Piclet type not found: ${encounter.picletTypeId}`);
@@ -219,7 +245,7 @@ export class EncounterService {
219
 
220
  // Set roster position 0 if this is the first caught piclet
221
  const existingCaughtPiclets = await getCaughtPiclets();
222
- if (existingCaughtPiclets.length === 1) { // Only one caught piclet exists (the template)
223
  newPiclet.rosterPosition = 0;
224
  newPiclet.isInRoster = true;
225
  }
 
47
  const uncaughtPiclets = await getUncaughtPiclets();
48
 
49
  if (caughtPiclets.length === 0) {
50
+ // Player has no caught piclets
51
+ if (uncaughtPiclets.length > 0) {
52
+ // Player has scanned piclets but hasn't caught any - create first piclet encounter
53
+ console.log('Player has scanned piclets but no caught ones - creating first piclet encounter');
54
+ const firstPicletEncounter = await this.createFirstCatchEncounter(uncaughtPiclets);
55
+ encounters.push(firstPicletEncounter);
56
+ } else {
57
+ // Player has no piclets at all - return empty encounters
58
+ console.log('Player has no piclets at all - returning empty encounters');
59
+ await db.encounters.clear();
60
+ await markEncountersRefreshed();
61
+ return [];
62
+ }
63
+
64
+ // Save the first piclet encounter and return
65
  await db.encounters.clear();
66
+ for (const encounter of encounters) {
67
+ await db.encounters.add(encounter);
68
+ }
69
  await markEncountersRefreshed();
70
+ return await this.getCurrentEncounters();
71
  }
72
 
73
  // Player has caught piclets - generate normal encounters
 
104
  }
105
 
106
  // Create first catch encounter
107
+ private static async createFirstCatchEncounter(uncaughtPiclets: PicletInstance[]): Promise<Omit<Encounter, 'id'>> {
108
+ // Use the most recently scanned (last in array) uncaught piclet
109
+ const latestPiclet = uncaughtPiclets[uncaughtPiclets.length - 1];
110
+
111
+ // Use the piclet's nickname or typeId for display
112
+ const displayName = latestPiclet.nickname || latestPiclet.typeId.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
113
+
114
  return {
115
  type: EncounterType.WILD_PICLET,
116
  title: 'Your First Piclet!',
117
+ description: `A friendly ${displayName} appears! This one seems easy to catch.`,
118
+ picletTypeId: latestPiclet.typeId,
119
+ enemyLevel: 5, // Easy level for first encounter
120
  createdAt: new Date()
121
  };
122
  }
 
195
  static async catchWildPiclet(encounter: Encounter): Promise<PicletInstance> {
196
  if (!encounter.picletTypeId) throw new Error('No piclet type specified');
197
 
198
+ // First try to find a caught piclet instance with this typeId to use as a template
199
  const caughtPiclets = await getCaughtPiclets();
200
+ let templatePiclet = caughtPiclets.find(p => p.typeId === encounter.picletTypeId);
201
+
202
+ // If no caught template found, try uncaught piclets (for first catch scenario)
203
+ if (!templatePiclet) {
204
+ const uncaughtPiclets = await getUncaughtPiclets();
205
+ templatePiclet = uncaughtPiclets.find(p => p.typeId === encounter.picletTypeId);
206
+ }
207
 
208
  if (!templatePiclet) {
209
  throw new Error(`Piclet type not found: ${encounter.picletTypeId}`);
 
245
 
246
  // Set roster position 0 if this is the first caught piclet
247
  const existingCaughtPiclets = await getCaughtPiclets();
248
+ if (existingCaughtPiclets.length === 0) { // This will be the first caught piclet
249
  newPiclet.rosterPosition = 0;
250
  newPiclet.isInRoster = true;
251
  }