Fraser commited on
Commit
99ecf7d
·
1 Parent(s): cf7c859
Files changed (1) hide show
  1. src/lib/db/encounterService.ts +46 -27
src/lib/db/encounterService.ts CHANGED
@@ -77,20 +77,38 @@ export class EncounterService {
77
  static async generateEncounters(): Promise<Encounter[]> {
78
  const encounters: Omit<Encounter, 'id'>[] = [];
79
 
80
- // Check if player has caught any piclets first
81
- const playerPiclets = await db.picletInstances.toArray();
 
82
 
83
- if (playerPiclets.length === 0) {
84
- // No piclets caught yet - this shouldn't happen in normal flow since we use picletInstances
85
- // But just in case, return empty encounters
86
- console.log('No piclet instances found - returning empty encounters');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  await db.encounters.clear();
88
  await markEncountersRefreshed();
89
  return [];
90
  }
91
 
92
- // Player has piclets - generate normal encounters
93
- console.log('Generating encounters for player with piclets');
94
 
95
  // Generate wild piclet encounters FIRST to ensure they're included
96
  const wildEncounters = await this.generateWildEncounters();
@@ -143,17 +161,17 @@ export class EncounterService {
143
  // Get player's average level
144
  const avgLevel = await this.getPlayerAverageLevel();
145
 
146
- // Get all piclet instances (these represent "discovered" piclets)
147
- const allPiclets = await db.picletInstances.toArray();
148
- console.log('Total piclet instances for wild encounters:', allPiclets.length);
149
 
150
- if (allPiclets.length === 0) {
151
- console.log('No piclet instances - returning empty wild encounters');
152
  return encounters;
153
  }
154
 
155
- // Get unique piclet types for encounters (allow duplicates)
156
- const availablePiclets = allPiclets;
157
  console.log('Available piclets for encounters:', availablePiclets.map(p => p.typeId));
158
 
159
  const encounterCount = MIN_WILD_ENCOUNTERS + Math.floor(Math.random() * (MAX_WILD_ENCOUNTERS - MIN_WILD_ENCOUNTERS + 1));
@@ -194,11 +212,11 @@ export class EncounterService {
194
  .toArray();
195
 
196
  if (rosterPiclets.length === 0) {
197
- const allPiclets = await db.picletInstances.toArray();
198
- if (allPiclets.length === 0) return 5; // Default starting level
199
 
200
- const totalLevel = allPiclets.reduce((sum, p) => sum + p.level, 0);
201
- return Math.round(totalLevel / allPiclets.length);
202
  }
203
 
204
  const totalLevel = rosterPiclets.reduce((sum, p) => sum + p.level, 0);
@@ -243,11 +261,9 @@ export class EncounterService {
243
  static async catchWildPiclet(encounter: Encounter): Promise<PicletInstance> {
244
  if (!encounter.picletTypeId) throw new Error('No piclet type specified');
245
 
246
- // Find an existing piclet instance with this typeId to use as a template
247
- const templatePiclet = await db.picletInstances
248
- .where('typeId')
249
- .equals(encounter.picletTypeId)
250
- .first();
251
 
252
  if (!templatePiclet) {
253
  throw new Error(`Piclet type not found: ${encounter.picletTypeId}`);
@@ -284,9 +300,12 @@ export class EncounterService {
284
  caughtAt: new Date()
285
  };
286
 
287
- // Set roster position 0 if this is the first piclet
288
- const existingPiclets = await db.picletInstances.toArray();
289
- if (existingPiclets.length === 1) { // Only the template exists
 
 
 
290
  newPiclet.rosterPosition = 0;
291
  newPiclet.isInRoster = true;
292
  }
 
77
  static async generateEncounters(): Promise<Encounter[]> {
78
  const encounters: Omit<Encounter, 'id'>[] = [];
79
 
80
+ // Check for "Your First Piclet" scenario first
81
+ const caughtPiclets = await getCaughtPiclets();
82
+ const uncaughtPiclets = await getUncaughtPiclets();
83
 
84
+ if (caughtPiclets.length === 0 && uncaughtPiclets.length > 0) {
85
+ // Player has no caught piclets but has uncaught ones - return ONLY first piclet encounters
86
+ console.log('Player has uncaught piclets but no caught ones - checking for first piclet encounters');
87
+
88
+ // Check if there are already first piclet encounters
89
+ const existingEncounters = await this.getCurrentEncounters();
90
+ const firstPicletEncounters = existingEncounters.filter(e => e.type === EncounterType.FIRST_PICLET);
91
+
92
+ if (firstPicletEncounters.length > 0) {
93
+ console.log('First piclet encounters already exist:', firstPicletEncounters.length);
94
+ return existingEncounters; // Return existing encounters without clearing
95
+ } else {
96
+ console.log('No first piclet encounters found - this should have been created during generation');
97
+ // Don't generate normal encounters - return existing (should be empty)
98
+ return existingEncounters;
99
+ }
100
+ }
101
+
102
+ if (caughtPiclets.length === 0 && uncaughtPiclets.length === 0) {
103
+ // No piclets at all - return empty encounters
104
+ console.log('No piclets found - returning empty encounters');
105
  await db.encounters.clear();
106
  await markEncountersRefreshed();
107
  return [];
108
  }
109
 
110
+ // Player has caught piclets - generate normal encounters
111
+ console.log('Generating normal encounters for player with caught piclets');
112
 
113
  // Generate wild piclet encounters FIRST to ensure they're included
114
  const wildEncounters = await this.generateWildEncounters();
 
161
  // Get player's average level
162
  const avgLevel = await this.getPlayerAverageLevel();
163
 
164
+ // Get caught piclets only (these can appear as wild encounters)
165
+ const caughtPiclets = await getCaughtPiclets();
166
+ console.log('Caught piclets for wild encounters:', caughtPiclets.length);
167
 
168
+ if (caughtPiclets.length === 0) {
169
+ console.log('No caught piclets - returning empty wild encounters');
170
  return encounters;
171
  }
172
 
173
+ // Use caught piclets as templates for wild encounters
174
+ const availablePiclets = caughtPiclets;
175
  console.log('Available piclets for encounters:', availablePiclets.map(p => p.typeId));
176
 
177
  const encounterCount = MIN_WILD_ENCOUNTERS + Math.floor(Math.random() * (MAX_WILD_ENCOUNTERS - MIN_WILD_ENCOUNTERS + 1));
 
212
  .toArray();
213
 
214
  if (rosterPiclets.length === 0) {
215
+ const caughtPiclets = await getCaughtPiclets();
216
+ if (caughtPiclets.length === 0) return 5; // Default starting level
217
 
218
+ const totalLevel = caughtPiclets.reduce((sum, p) => sum + p.level, 0);
219
+ return Math.round(totalLevel / caughtPiclets.length);
220
  }
221
 
222
  const totalLevel = rosterPiclets.reduce((sum, p) => sum + p.level, 0);
 
261
  static async catchWildPiclet(encounter: Encounter): Promise<PicletInstance> {
262
  if (!encounter.picletTypeId) throw new Error('No piclet type specified');
263
 
264
+ // Find a caught piclet instance with this typeId to use as a template
265
+ const caughtPiclets = await getCaughtPiclets();
266
+ const templatePiclet = caughtPiclets.find(p => p.typeId === encounter.picletTypeId);
 
 
267
 
268
  if (!templatePiclet) {
269
  throw new Error(`Piclet type not found: ${encounter.picletTypeId}`);
 
300
  caughtAt: new Date()
301
  };
302
 
303
+ // Wild piclets are always caught
304
+ newPiclet.caught = true;
305
+
306
+ // Set roster position 0 if this is the first caught piclet
307
+ const existingCaughtPiclets = await getCaughtPiclets();
308
+ if (existingCaughtPiclets.length === 1) { // Only one caught piclet exists (the template)
309
  newPiclet.rosterPosition = 0;
310
  newPiclet.isInRoster = true;
311
  }