File size: 6,823 Bytes
6710512
 
565e57b
1f2c086
35d027c
00d1c08
b4531e8
565e57b
 
 
 
6710512
b4531e8
565e57b
b524411
6c2bc5a
 
565e57b
 
 
 
 
 
 
 
 
 
 
 
 
 
b524411
565e57b
6c2bc5a
565e57b
 
 
 
 
 
 
b524411
 
565e57b
 
 
 
 
 
6c2bc5a
565e57b
 
 
 
 
6f79ea2
 
 
565e57b
 
 
 
 
 
 
 
 
 
6f79ea2
565e57b
 
 
 
 
 
 
 
 
b524411
 
ce29375
565e57b
6710512
565e57b
 
 
 
 
 
2e6926b
565e57b
 
6710512
 
 
ce29375
565e57b
 
b4531e8
00d1c08
565e57b
 
 
ce29375
565e57b
 
 
ce29375
6710512
 
565e57b
6c2bc5a
565e57b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8636b2
565e57b
 
 
 
 
00d1c08
 
565e57b
 
 
b4531e8
565e57b
 
 
 
 
 
 
 
 
b4531e8
6710512
 
 
 
 
b524411
565e57b
 
 
 
 
 
 
6710512
565e57b
 
 
 
 
 
 
6710512
565e57b
 
00d1c08
565e57b
6710512
565e57b
 
 
 
 
 
 
 
00d1c08
565e57b
00d1c08
565e57b
 
 
 
 
 
 
 
 
 
00d1c08
565e57b
 
00d1c08
565e57b
 
 
 
 
00d1c08
565e57b
 
 
 
 
 
 
 
00d1c08
565e57b
 
 
00d1c08
565e57b
 
 
 
 
6710512
565e57b
 
b29f3f6
565e57b
 
b29f3f6
565e57b
 
 
1fd2c31
565e57b
1fd2c31
565e57b
 
 
1fd2c31
565e57b
 
 
 
 
 
 
 
 
 
 
 
 
1fd2c31
565e57b
 
 
 
 
 
 
 
 
 
 
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
<script lang="ts">
  import { onMount } from 'svelte';
  import { getCollectedPiclets, getCanonicalPiclets } from '$lib/db/piclets';
  import type { PicletInstance } from '$lib/db/schema';
  import { db } from '$lib/db';
  import PicletCard from '../Piclets/PicletCard.svelte';
  import PicletDetail from '../Piclets/PicletDetail.svelte';
  import { CanonicalService } from '$lib/services/canonicalService';

  let collectedPiclets: PicletInstance[] = $state([]);
  let canonicalPiclets: PicletInstance[] = $state([]);
  let isLoading = $state(true);
  let selectedPiclet: PicletInstance | null = $state(null);
  let searchQuery = $state('');

  // Filter piclets based on search
  let filteredPiclets = $derived.by(() => {
    let piclets = collectedPiclets;

    // Filter by search query
    if (searchQuery.trim()) {
      const query = searchQuery.toLowerCase();
      piclets = piclets.filter(p =>
        p.objectName?.toLowerCase().includes(query) ||
        p.nickname?.toLowerCase().includes(query) ||
        p.typeId.toLowerCase().includes(query)
      );
    }

    return piclets;
  });

  // Group piclets by object for display
  let groupedPiclets = $derived.by(() => {
    const groups = new Map<string, PicletInstance[]>();

    filteredPiclets.forEach(piclet => {
      const key = piclet.objectName || 'unknown';
      const group = groups.get(key) || [];
      group.push(piclet);
      groups.set(key, group);
    });

    return Array.from(groups.entries()).sort((a, b) =>
      a[0].localeCompare(b[0])
    );
  });

  // Stats calculation
  let stats = $derived.by(() => {
    const totalDiscovered = collectedPiclets.length;
    const uniqueObjects = new Set(collectedPiclets.map(p => p.objectName)).size;
    const canonicalCount = collectedPiclets.filter(p => p.isCanonical).length;
    const variationCount = collectedPiclets.filter(p => !p.isCanonical).length;

    // Calculate total scans
    const totalScans = collectedPiclets.reduce((sum, p) => sum + (p.scanCount || 1), 0);

    // Calculate total rarity score
    const rarityScore = collectedPiclets.reduce((sum, p) => {
      const points = CanonicalService.calculateRarity(p.scanCount);
      const multiplier = points === 'legendary' ? 100 : points === 'epic' ? 50 :
                        points === 'rare' ? 20 : points === 'uncommon' ? 10 : 5;
      return sum + multiplier;
    }, 0);

    return {
      totalDiscovered,
      totalScans,
      uniqueObjects,
      canonicalCount,
      variationCount,
      rarityScore
    };
  });

  onMount(async () => {
    await loadPiclets();
  });

  async function loadPiclets() {
    isLoading = true;
    try {
      collectedPiclets = await getCollectedPiclets();
      canonicalPiclets = await getCanonicalPiclets();

      // Sort by discovery date (most recent first)
      collectedPiclets.sort((a, b) =>
        (b.discoveredAt?.getTime() || 0) - (a.discoveredAt?.getTime() || 0)
      );
    } catch (error) {
      console.error('Failed to load piclets:', error);
    } finally {
      isLoading = false;
    }
  }

  function handlePicletClick(piclet: PicletInstance) {
    selectedPiclet = piclet;
  }

  function handleDetailClose() {
    selectedPiclet = null;
  }

  async function handlePicletDeleted() {
    await loadPiclets();
  }
</script>

<div class="pictuary-page">
  <!-- Search -->
  <div class="controls">
    <input
      type="text"
      placeholder="Search piclets..."
      bind:value={searchQuery}
      class="search-input"
    />
  </div>

  <!-- Collection Grid -->
  <div class="collection-container">
    {#if isLoading}
      <div class="loading">
        <div class="spinner"></div>
        <p>Loading collection...</p>
      </div>
    {:else if filteredPiclets.length === 0}
      <div class="empty-state">
        {#if collectedPiclets.length === 0}
          <h2>No Piclets Discovered</h2>
          <p>Start scanning objects to build your collection!</p>
        {:else}
          <h2>No Results</h2>
          <p>Try adjusting your filters or search query</p>
        {/if}
      </div>
    {:else}
      <div class="piclet-grid">
        {#each filteredPiclets as piclet (piclet.id)}
          <div class="piclet-item" onclick={() => handlePicletClick(piclet)}>
            <PicletCard piclet={piclet} />
            {#if piclet.isCanonical}
              <div class="canonical-badge">✨</div>
            {/if}
            {#if piclet.scanCount > 1}
              <div class="scan-badge">{piclet.scanCount}×</div>
            {/if}
          </div>
        {/each}
      </div>
    {/if}
  </div>
</div>

<!-- Detail View -->
{#if selectedPiclet}
  <PicletDetail
    instance={selectedPiclet}
    onClose={handleDetailClose}
    onDeleted={handlePicletDeleted}
  />
{/if}

<style>
  .pictuary-page {
    height: 100%;
    overflow-y: auto;
  }

  .controls {
    padding: 1rem;
    background: rgba(255, 255, 255, 0.9);
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }

  .search-input {
    width: 100%;
    padding: 0.75rem;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    font-size: 1rem;
  }

  .view-selector {
    display: flex;
    gap: 0.5rem;
  }

  .view-btn {
    flex: 1;
    padding: 0.5rem;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    background: white;
    color: #666;
    cursor: pointer;
    transition: all 0.2s;
  }

  .view-btn.active {
    background: #667eea;
    color: white;
    border-color: #667eea;
  }

  .collection-container {
    padding: 1rem;
    min-height: 400px;
  }

  .loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem;
    color: white;
  }

  .spinner {
    width: 48px;
    height: 48px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 1s linear infinite;
  }

  @keyframes spin {
    to { transform: rotate(360deg); }
  }

  .empty-state {
    text-align: center;
    padding: 3rem;
    color: white;
  }

  .piclet-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 1rem;
  }

  .piclet-item {
    position: relative;
    cursor: pointer;
    transition: transform 0.2s;
  }

  .piclet-item:hover {
    transform: scale(1.05);
  }

  .canonical-badge {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    background: gold;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
  }

  .scan-badge {
    position: absolute;
    bottom: 0.5rem;
    right: 0.5rem;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    padding: 0.125rem 0.5rem;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: bold;
  }
</style>