File size: 17,245 Bytes
6710512
465b043
 
7428b13
465b043
 
 
c703ea3
15c1c68
7428b13
657d1cf
5fe1a3d
465b043
 
 
 
2652bf8
465b043
7428b13
 
 
 
 
c61508c
7428b13
5fe1a3d
 
 
 
465b043
 
 
 
 
 
 
23a36f6
a21678d
465b043
23a36f6
 
a21678d
 
 
 
9fd8794
23a36f6
 
 
 
2652bf8
a21678d
 
23a36f6
 
465b043
 
 
 
 
 
23a36f6
5816acb
 
2652bf8
 
5816acb
2652bf8
 
23a36f6
 
 
 
2652bf8
 
23a36f6
 
2652bf8
 
 
 
 
 
657d1cf
 
 
 
 
 
 
23a36f6
 
2652bf8
657d1cf
 
 
 
 
 
 
 
 
 
 
465b043
5816acb
 
 
 
5fe1a3d
 
465b043
 
 
5816acb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f711bf
 
5816acb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
465b043
 
 
 
 
 
23a36f6
465b043
 
 
 
 
 
 
 
5816acb
 
465b043
 
 
 
 
 
 
 
 
 
5816acb
 
465b043
 
 
 
7428b13
 
 
2e6926b
 
7428b13
2e6926b
 
 
 
 
 
 
 
 
 
 
c703ea3
 
7428b13
 
 
2e6926b
 
 
 
 
 
 
7428b13
 
 
 
 
c703ea3
7428b13
 
c61508c
7428b13
15c1c68
7428b13
 
 
 
 
 
 
 
23a36f6
 
 
 
90a88fc
 
23a36f6
 
90a88fc
 
 
23a36f6
90a88fc
 
c703ea3
7428b13
23a36f6
7428b13
 
 
23a36f6
7428b13
 
 
 
80baaa7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afb1e97
007132e
 
afb1e97
 
007132e
 
afb1e97
 
 
 
 
80baaa7
 
 
c703ea3
afb1e97
80baaa7
afb1e97
 
 
80baaa7
afb1e97
 
 
007132e
afb1e97
 
 
80baaa7
5730391
afb1e97
5730391
afb1e97
c703ea3
afb1e97
 
 
 
 
007132e
80baaa7
 
 
 
 
7428b13
5730391
7428b13
15c1c68
7428b13
 
 
 
5730391
 
7428b13
 
 
5730391
 
7428b13
80baaa7
7428b13
5730391
7428b13
 
5730391
 
7428b13
 
6710512
 
7428b13
 
 
 
 
c61508c
7428b13
 
 
6710512
657d1cf
 
 
 
 
738cc29
657d1cf
 
 
 
 
 
 
 
 
 
 
 
465b043
 
 
 
 
 
 
 
 
5816acb
5fe1a3d
 
 
5816acb
5fe1a3d
465b043
5fe1a3d
465b043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
657d1cf
6710512
7428b13
6710512
5fe1a3d
 
 
 
 
 
 
5730391
 
5fe1a3d
 
 
 
6710512
 
 
657d1cf
465b043
 
 
 
 
 
 
 
6710512
657d1cf
6710512
 
465b043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6710512
 
 
465b043
6710512
465b043
6710512
 
 
465b043
 
 
 
 
 
6710512
 
 
657d1cf
 
6710512
 
465b043
 
 
 
 
 
6710512
465b043
 
 
 
 
6710512
 
 
465b043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7428b13
 
465b043
 
 
 
 
 
 
 
 
 
 
6710512
465b043
 
6710512
 
465b043
6710512
465b043
6710512
 
 
465b043
 
 
 
 
738cc29
 
 
 
 
 
465b043
 
738cc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6710512
5fe1a3d
6710512
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
<script lang="ts">
  import { onMount } from 'svelte';
  import { fade, fly } from 'svelte/transition';
  import type { Encounter, GameState, PicletInstance } from '$lib/db/schema';
  import { EncounterType } from '$lib/db/schema';
  import { EncounterService } from '$lib/db/encounterService';
  import { getOrCreateGameState, incrementCounter, addProgressPoints } from '$lib/db/gameState';
    import { db } from '$lib/db';
  import { uiStore } from '$lib/stores/ui';
  import Battle from './Battle.svelte';
  import PullToRefresh from '../UI/PullToRefresh.svelte';
  import NewlyCaughtPicletDetail from '../Piclets/NewlyCaughtPicletDetail.svelte';
  
  let encounters: Encounter[] = [];
  let isLoading = true;
  let isRefreshing = false;
  let monsterImages: Map<string, string> = new Map();
  
  // Battle state
  let showBattle = false;
  let battlePlayerPiclet: PicletInstance | null = null;
  let battleEnemyPiclet: PicletInstance | null = null;
  let battleIsWild = true;
  let battleRosterPiclets: PicletInstance[] = [];
  
  // Newly caught Piclet state
  let showNewlyCaught = false;
  let newlyCaughtPiclet: PicletInstance | null = null;
  
  onMount(async () => {
    await loadEncounters();
  });
  
  async function loadEncounters() {
    isLoading = true;
    try {
      // Check if we have any piclet instances
      const playerPiclets = await db.picletInstances.toArray();
      
      if (playerPiclets.length === 0) {
        // No piclets discovered/caught - show empty state
        encounters = [];
        isLoading = false;
        return;
      }
      
      // Player has piclets - always generate fresh encounters with wild piclets
      console.log('Player has piclets - generating fresh encounters with wild piclets');
      await EncounterService.forceEncounterRefresh();
      encounters = await EncounterService.generateEncounters();
      
      console.log('Final encounters:', encounters.map(e => ({ type: e.type, title: e.title })));
      
      // Load piclet images for wild encounters
      await loadPicletImages();
    } catch (error) {
      console.error('Error loading encounters:', error);
    }
    isLoading = false;
  }
  
  async function loadPicletImages() {
    const picletEncounters = encounters.filter(e => 
      (e.type === EncounterType.WILD_PICLET || e.type === EncounterType.FIRST_PICLET) && e.picletTypeId
    );
    
    for (const encounter of picletEncounters) {
      if (!encounter.picletTypeId) continue;
      
      // Find a piclet instance with this typeId
      const piclet = await db.picletInstances
        .where('typeId')
        .equals(encounter.picletTypeId)
        .first();
      
      if (piclet && piclet.imageData) {
        monsterImages.set(encounter.picletTypeId, piclet.imageData);
      }
    }
    // Trigger reactive update
    monsterImages = monsterImages;
  }
  
  async function handleRefresh() {
    isRefreshing = true;
    try {
      // Force refresh encounters
      console.log('Force refreshing encounters...');
      encounters = await EncounterService.generateEncounters();
      
      // Load piclet images for new encounters
      await loadPicletImages();
      
      // Update game state with new refresh time
      const gameState = await getOrCreateGameState();
      await db.gameState.update(gameState.id!, {
        lastEncounterRefresh: new Date()
      });
    } catch (error) {
      console.error('Error refreshing encounters:', error);
    }
    isRefreshing = false;
  }
  
  async function handleEncounterTap(encounter: Encounter) {
    if (encounter.type === EncounterType.FIRST_PICLET) {
      // First piclet encounter - direct catch
      await handleFirstPicletEncounter(encounter);
    } else if (encounter.type === EncounterType.WILD_PICLET && encounter.picletTypeId) {
      // Regular wild encounter - start battle
      await startBattle(encounter);
    }
  }
  

  async function handleFirstPicletEncounter(encounter: Encounter) {
    try {
      if (!encounter.picletInstanceId) {
        throw new Error('No piclet instance specified for first piclet encounter');
      }

      // Get the specific piclet instance
      const picletInstance = await db.picletInstances.get(encounter.picletInstanceId);
      if (!picletInstance) {
        throw new Error('Piclet instance not found');
      }

      // Mark the piclet as caught and set it as the first roster piclet
      await db.picletInstances.update(encounter.picletInstanceId, {
        caught: true,
        caughtAt: new Date(),
        isInRoster: true,
        rosterPosition: 0,
        level: 3
      });

      // Get the updated piclet instance
      const caughtPiclet = await db.picletInstances.get(encounter.picletInstanceId);
      
      // Show the newly caught piclet detail page
      newlyCaughtPiclet = caughtPiclet!;
      showNewlyCaught = true;
      
      // Update counters and progress
      incrementCounter('picletsCapured');
      addProgressPoints(100);
      
      // Force refresh encounters to remove the first piclet encounter
      await forceEncounterRefresh();
    } catch (error) {
      console.error('Error handling first piclet encounter:', error);
      alert('Something went wrong. Please try again.');
    }
  }
  
  async function forceEncounterRefresh() {
    isRefreshing = true;
    try {
      await EncounterService.forceEncounterRefresh();
      encounters = await EncounterService.generateEncounters();
      await loadPicletImages();
    } catch (error) {
      console.error('Error refreshing encounters:', error);
    }
    isRefreshing = false;
  }
  
  function getEncounterIcon(encounter: Encounter): string {
    switch (encounter.type) {
      case EncounterType.FIRST_PICLET:
        return '✨';
      case EncounterType.WILD_PICLET:
      default:
        return '⚔️';
    }
  }
  
  function getEncounterColor(encounter: Encounter): string {
    switch (encounter.type) {
      case EncounterType.WILD_PICLET:
        return '#4caf50';
      case EncounterType.FIRST_PICLET:
        return '#ffd700';
      default:
        return '#607d8b';
    }
  }
  
  async function startBattle(encounter: Encounter) {
    try {
      // Get all piclet instances
      const allPiclets = await db.picletInstances.toArray();
      
      // Filter piclets that have a roster position (0-5)
      const rosterPiclets = allPiclets.filter(p => 
        p.rosterPosition !== undefined && 
        p.rosterPosition !== null && 
        p.rosterPosition >= 0 && 
        p.rosterPosition <= 5
      );
      
      // Sort by roster position
      rosterPiclets.sort((a, b) => (a.rosterPosition ?? 0) - (b.rosterPosition ?? 0));
      
      if (rosterPiclets.length === 0) {
        alert('You need at least one piclet in your roster to battle!');
        return;
      }
      
      // Check if there's at least one piclet in position 0
      const hasPosition0 = rosterPiclets.some(p => p.rosterPosition === 0);
      if (!hasPosition0) {
        alert('You need a piclet in the first roster slot (position 0) to battle!');
        return;
      }
      
      // Generate enemy piclet for battle
      const enemyPiclet = await generateEnemyPiclet(encounter);
      if (!enemyPiclet) return;
      
      // Set up battle
      battlePlayerPiclet = rosterPiclets[0];
      battleEnemyPiclet = enemyPiclet;
      battleIsWild = true;
      battleRosterPiclets = rosterPiclets; // Pass all roster piclets
      showBattle = true;
      uiStore.enterBattle();
    } catch (error) {
      console.error('Error starting battle:', error);
    }
  }
  
  async function generateEnemyPiclet(encounter: Encounter): Promise<PicletInstance | null> {
    if (!encounter.picletTypeId || !encounter.enemyLevel) return null;
    
    // Get a piclet instance with this typeId to use as a template
    const templatePiclet = await db.picletInstances
      .where('typeId')
      .equals(encounter.picletTypeId)
      .first();
    
    if (!templatePiclet) {
      console.error('Piclet template not found for typeId:', encounter.picletTypeId);
      return null;
    }
    
    // Calculate stats based on template's base stats and encounter level
    const level = encounter.enemyLevel;
    
    // Create enemy piclet instance based on template (simplified)
    const enemyPiclet: PicletInstance = {
      ...templatePiclet,
      id: -1, // Temporary ID for enemy
      
      isInRoster: false,
      caughtAt: new Date()
    };
    
    return enemyPiclet;
  }

  async function addCapturedPicletToRoster(capturedPiclet: PicletInstance) {
    try {
      // Get all roster piclets to find the next available position
      const allPiclets = await db.picletInstances.toArray();
      const rosterPiclets = allPiclets.filter(p => 
        p.rosterPosition !== undefined && 
        p.rosterPosition !== null && 
        p.rosterPosition >= 0 && 
        p.rosterPosition <= 5
      );
      
      // Find next available roster position (0-5)
      let nextPosition = 0;
      const occupiedPositions = new Set(rosterPiclets.map(p => p.rosterPosition));
      while (occupiedPositions.has(nextPosition) && nextPosition <= 5) {
        nextPosition++;
      }
      
      if (nextPosition > 5) {
        // Roster is full - for now just add to position 5 (could implement storage system later)
        console.warn('Roster is full, overriding position 5');
        nextPosition = 5;
      }
      
      console.log('About to create captured Piclet in database:', {
        id: capturedPiclet.id,
        nickname: capturedPiclet.nickname,
        typeId: capturedPiclet.typeId,
        level: capturedPiclet.level
      });
      
      // Create a new database record for the captured Piclet (enemy has temporary ID -1)
      const newPicletData = {
        ...capturedPiclet,
        // Remove the temporary ID so database auto-generates a real one
        id: undefined as any,
        caught: true,
        caughtAt: new Date(),
        isInRoster: true,
        rosterPosition: nextPosition
      };
      
      // Add the captured piclet to the database
      const newPicletId = await db.picletInstances.add(newPicletData);
      console.log('Created new database record with ID:', newPicletId);
      
      // Get the newly created piclet instance and show detail page
      const createdPiclet = await db.picletInstances.get(newPicletId);
      console.log('Retrieved newly created piclet from database:', createdPiclet);
      
      if (createdPiclet) {
        console.log('Setting newlyCaughtPiclet and showing detail page:', createdPiclet.nickname);
        newlyCaughtPiclet = createdPiclet;
        showNewlyCaught = true;
        console.log('showNewlyCaught is now:', showNewlyCaught);
        console.log(`Added captured Piclet ${createdPiclet.nickname} to roster position ${nextPosition}`);
      } else {
        console.error('Could not retrieve newly created piclet from database');
        // Use fallback data
        const fallbackPiclet = {
          ...capturedPiclet,
          id: newPicletId
        };
        newlyCaughtPiclet = fallbackPiclet;
        showNewlyCaught = true;
      }
    } catch (error) {
      console.error('Error adding captured Piclet to roster:', error);
    }
  }
  
  async function handleBattleEnd(result: any) {
    showBattle = false;
    uiStore.exitBattle();
    
    if (result === true) {
      // Victory
      console.log('Battle won!');
      // Force refresh encounters after battle
      forceEncounterRefresh();
    } else if (result === false) {
      // Defeat or ran away
      console.log('Battle lost or fled');
      // Force refresh encounters after battle
      forceEncounterRefresh();
    } else if (result && result.id) {
      // Caught a piclet - add to roster
      console.log('Piclet caught!', result);
      await addCapturedPicletToRoster(result);
      incrementCounter('picletsCapured');
      addProgressPoints(100);
      // Don't refresh encounters immediately - let user view the caught piclet first
      // Refresh will happen when they close the detail dialog
    }
  }
</script>

{#if showBattle && battlePlayerPiclet && battleEnemyPiclet}
  <Battle 
    playerPiclet={battlePlayerPiclet}
    enemyPiclet={battleEnemyPiclet}
    isWildBattle={battleIsWild}
    rosterPiclets={battleRosterPiclets}
    onBattleEnd={handleBattleEnd}
  />
{:else}
<div class="encounters-page">
  <PullToRefresh onRefresh={handleRefresh}>
    {#if isLoading}
      <div class="loading">
        <div class="spinner"></div>
        <p>Loading encounters...</p>
      </div>
    {:else if encounters.length === 0}
      <div class="empty-state">
        <div class="empty-icon">📸</div>
        <h2>No Piclets Discovered</h2>
        <p>To start your adventure, select the Snap logo image:</p>
        <div class="logo-instruction">
          <img src="/assets/snap_logo.png" alt="Snap Logo" class="snap-logo-preview" />
          <p class="instruction-text">↑ Select this image in the scanner</p>
        </div>
      </div>
    {:else}
      <div class="encounters-list">
      {#each encounters as encounter, index (encounter.id)}
        <button
          class="encounter-card"
          style="border-color: {getEncounterColor(encounter)}30"
          on:click={() => handleEncounterTap(encounter)}
          in:fly={{ y: 20, delay: index * 50 }}
          disabled={isRefreshing}
        >
          <div class="encounter-icon">
            {#if (encounter.type === EncounterType.WILD_PICLET || encounter.type === EncounterType.FIRST_PICLET) && encounter.picletTypeId}
              {#if monsterImages.has(encounter.picletTypeId)}
                <img 
                  src={monsterImages.get(encounter.picletTypeId)}
                  alt="Piclet"
                />
              {:else}
                <div class="fallback-icon">{getEncounterIcon(encounter)}</div>
              {/if}
            {:else}
              <span class="type-icon">{getEncounterIcon(encounter)}</span>
            {/if}
          </div>
          
          <div class="encounter-info">
            <h3>{encounter.title}</h3>
            <p>{encounter.description}</p>
          </div>
          
          <div class="encounter-arrow"></div>
        </button>
      {/each}
    </div>
  {/if}
  </PullToRefresh>
</div>
{/if}

<!-- Newly Caught Piclet Dialog -->
{#if showNewlyCaught && newlyCaughtPiclet}
  <NewlyCaughtPicletDetail 
    instance={newlyCaughtPiclet}
    onClose={() => {
      showNewlyCaught = false;
      newlyCaughtPiclet = null;
      // Refresh encounters after user closes the detail dialog
      forceEncounterRefresh();
    }}
  />
{/if}

<style>
  .encounters-page {
    height: 100%;
    overflow: hidden; /* PullToRefresh handles scrolling */
  }
  
  .loading, .empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 60vh;
    text-align: center;
    padding: 1rem;
  }
  
  .spinner {
    width: 48px;
    height: 48px;
    border: 4px solid #f0f0f0;
    border-top-color: #4caf50;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 1rem;
  }
  
  @keyframes spin {
    to { transform: rotate(360deg); }
  }
  
  .empty-icon {
    font-size: 4rem;
    margin-bottom: 1rem;
  }
  
  .empty-state h2 {
    margin: 0 0 0.5rem;
    font-size: 1.25rem;
    color: #333;
  }
  
  .empty-state p {
    color: #666;
    font-size: 0.9rem;
  }
  
  .encounters-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding: 1rem;
    padding-bottom: 5rem;
  }
  
  .encounter-card {
    display: flex;
    align-items: center;
    gap: 1rem;
    background: #fff;
    border: 2px solid;
    border-radius: 12px;
    padding: 1rem;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    transition: all 0.2s ease;
    cursor: pointer;
    width: 100%;
    text-align: left;
  }
  
  .encounter-card:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.12);
  }
  
  .encounter-card:disabled {
    opacity: 0.6;
    cursor: not-allowed;
  }
  
  .encounter-icon {
    width: 60px;
    height: 60px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .encounter-icon img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 8px;
  }
  
  
  
  
  .type-icon, .fallback-icon {
    font-size: 2rem;
  }
  
  .encounter-info {
    flex: 1;
  }
  
  .encounter-info h3 {
    margin: 0 0 0.25rem;
    font-size: 1.1rem;
    font-weight: 600;
    color: #1a1a1a;
  }
  
  .encounter-info p {
    margin: 0;
    font-size: 0.875rem;
    color: #666;
  }
  
  .encounter-arrow {
    font-size: 1.5rem;
    color: #999;
  }
  
  .logo-instruction {
    margin-top: 1.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
  }
  
  .snap-logo-preview {
    width: 120px;
    height: 120px;
    object-fit: contain;
    border: 2px dashed #007bff;
    border-radius: 12px;
    padding: 1rem;
    background: #f0f7ff;
  }
  
  .instruction-text {
    font-size: 0.875rem;
    color: #007bff;
    font-weight: 500;
    margin: 0;
  }

</style>