Fraser commited on
Commit
afb1e97
·
1 Parent(s): f6bbd39

fix db issue

Browse files
src/lib/components/Pages/Encounters.svelte CHANGED
@@ -346,39 +346,50 @@
346
  nextPosition = 5;
347
  }
348
 
349
- console.log('About to update captured Piclet in database:', {
350
  id: capturedPiclet.id,
351
  nickname: capturedPiclet.nickname,
352
- currentCaught: capturedPiclet.caught,
353
- currentInRoster: capturedPiclet.isInRoster
354
  });
355
 
356
- // Update the captured piclet to be in roster
357
- const updateResult = await db.picletInstances.update(capturedPiclet.id, {
 
 
 
358
  caught: true,
359
  caughtAt: new Date(),
360
  isInRoster: true,
361
- rosterPosition: nextPosition
362
- });
 
 
363
 
364
- console.log('Database update result:', updateResult);
365
- console.log(`Added captured Piclet ${capturedPiclet.nickname} to roster position ${nextPosition}`);
 
366
 
367
- // Get the updated piclet instance and show detail page
368
- console.log('Attempting to retrieve updated piclet with ID:', capturedPiclet.id);
369
- const updatedPiclet = await db.picletInstances.get(capturedPiclet.id);
370
- console.log('Retrieved piclet from database:', updatedPiclet);
371
 
372
- if (updatedPiclet) {
373
- console.log('Setting newlyCaughtPiclet and showing detail page:', updatedPiclet.nickname);
374
- newlyCaughtPiclet = updatedPiclet;
375
  showNewlyCaught = true;
376
  console.log('showNewlyCaught is now:', showNewlyCaught);
 
377
  } else {
378
- console.error('Could not retrieve updated piclet from database - ID:', capturedPiclet.id);
379
- // Let's try to show the original piclet data as fallback
380
- console.log('Trying fallback with original piclet data');
381
- newlyCaughtPiclet = capturedPiclet;
 
 
 
 
 
382
  showNewlyCaught = true;
383
  }
384
  } catch (error) {
 
346
  nextPosition = 5;
347
  }
348
 
349
+ console.log('About to create captured Piclet in database:', {
350
  id: capturedPiclet.id,
351
  nickname: capturedPiclet.nickname,
352
+ typeId: capturedPiclet.typeId,
353
+ level: capturedPiclet.level
354
  });
355
 
356
+ // Create a new database record for the captured Piclet (enemy has temporary ID -1)
357
+ const newPicletData = {
358
+ ...capturedPiclet,
359
+ // Remove the temporary ID so database auto-generates a real one
360
+ id: undefined as any,
361
  caught: true,
362
  caughtAt: new Date(),
363
  isInRoster: true,
364
+ rosterPosition: nextPosition,
365
+ // Ensure level is valid (battle enemies might have invalid levels)
366
+ level: Math.max(1, Math.min(100, capturedPiclet.level || 5))
367
+ };
368
 
369
+ // Add the captured piclet to the database
370
+ const newPicletId = await db.picletInstances.add(newPicletData);
371
+ console.log('Created new database record with ID:', newPicletId);
372
 
373
+ // Get the newly created piclet instance and show detail page
374
+ const createdPiclet = await db.picletInstances.get(newPicletId);
375
+ console.log('Retrieved newly created piclet from database:', createdPiclet);
 
376
 
377
+ if (createdPiclet) {
378
+ console.log('Setting newlyCaughtPiclet and showing detail page:', createdPiclet.nickname);
379
+ newlyCaughtPiclet = createdPiclet;
380
  showNewlyCaught = true;
381
  console.log('showNewlyCaught is now:', showNewlyCaught);
382
+ console.log(`Added captured Piclet ${createdPiclet.nickname} to roster position ${nextPosition}`);
383
  } else {
384
+ console.error('Could not retrieve newly created piclet from database');
385
+ // Fix the level issue and use fallback data
386
+ const fallbackPiclet = {
387
+ ...capturedPiclet,
388
+ level: Math.max(1, Math.min(100, capturedPiclet.level || 5)),
389
+ id: newPicletId
390
+ };
391
+ console.log('Using fallback with fixed level:', fallbackPiclet.level);
392
+ newlyCaughtPiclet = fallbackPiclet;
393
  showNewlyCaught = true;
394
  }
395
  } catch (error) {