|
1 | 1 | <script lang="ts"> |
2 | 2 | import Head from '$lib/components/Head.svelte'; |
| 3 | + import MarketItem from './MarketItem.svelte'; |
3 | 4 | import MarketTimer from './MarketTimer.svelte'; |
4 | 5 |
|
5 | 6 | let { data } = $props(); |
6 | | -
|
7 | | - function computeUserPrice(item: any) { |
8 | | - if (typeof item.computedPrice === 'number') return item.computedPrice; |
9 | | -
|
10 | | - const max = Number(item.maxPrice || 0); |
11 | | - const min = Number(item.minPrice || 0); |
12 | | - const diff = Math.max(0, max - min); |
13 | | - const shopScore = Number(data?.user?.shopScore || 0); |
14 | | -
|
15 | | - const minShop = Number(item.minShopScore || 0); |
16 | | - const maxShop = Number(item.maxShopScore || 0); |
17 | | - let discountPercent = 0; |
18 | | - if (maxShop > minShop) { |
19 | | - discountPercent = (shopScore - minShop) / (maxShop - minShop); |
20 | | - discountPercent = Math.max(0, Math.min(1, discountPercent)); |
21 | | - } else { |
22 | | - discountPercent = 0; |
23 | | - } |
24 | | -
|
25 | | - const discountAmount = diff * discountPercent; |
26 | | - const rawPrice = Math.ceil(max - discountAmount); |
27 | | - const price = Math.max(rawPrice, min); |
28 | | - return price; |
29 | | - } |
30 | | -
|
31 | | - function getPriceInfo(item: any) { |
32 | | - const max = Number(item.maxPrice || 0); |
33 | | - const price = computeUserPrice(item); |
34 | | - const save = Math.max(0, max - price); |
35 | | - const hasDiscount = save > 0 && max > 0; |
36 | | - const percentOff = hasDiscount ? Math.ceil((save / max) * 100) : 0; |
37 | | - return { price, save, hasDiscount, percentOff }; |
38 | | - } |
39 | | -
|
40 | | - function formatClays(value: number | string) { |
41 | | - const n = Number(value || 0); |
42 | | - return `${n.toLocaleString()} clays`; |
43 | | - } |
44 | 7 | </script> |
45 | 8 |
|
46 | 9 | <Head title="Market" /> |
47 | 10 |
|
48 | | -<h1 class="mt-5 mb-3 font-hero text-3xl font-medium">Market</h1> |
| 11 | +<h1 class="mt-5 mb-2 font-hero text-3xl font-medium">Market</h1> |
49 | 12 |
|
50 | 13 | {#if data.marketItems.length === 0} |
51 | 14 | <MarketTimer /> |
52 | 15 | {:else} |
53 | | - <div class="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3 mb-5"> |
| 16 | + <p class="mb-2"> |
| 17 | + Market score: <span class="rounded-xl bg-primary-800 px-1">{data.user.shopScore}</span> |
| 18 | + <span class="opacity-50">(allows you to get stuff for cheaper and unlock more items!)</span> |
| 19 | + </p> |
| 20 | + |
| 21 | + <div class="mb-5 grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3"> |
54 | 22 | {#each data.marketItems as item (item.id)} |
55 | | - <div class="themed-box p-4 flex flex-col gap-3"> |
56 | | - <div class="aspect-square rounded-lg overflow-hidden mb-3 bg-primary-800/10"> |
57 | | - <img src={item.image} alt={item.name} class="w-full h-full object-contain object-center" /> |
58 | | - </div> |
59 | | - <div> |
60 | | - <h3 class="text-lg font-bold mb-1">{item.name}</h3> |
61 | | - <p class="text-sm text-primary-300 leading-snug">{item.description}</p> |
62 | | - {#if getPriceInfo(item).hasDiscount} |
63 | | - <div class="flex items-center gap-3 mt-3"> |
64 | | - <div class="text-sm text-primary-300 line-through">{formatClays(item.maxPrice)}</div> |
65 | | - <div class="ml-auto text-lg font-bold text-emerald-500">{formatClays(getPriceInfo(item).price)}</div> |
66 | | - </div> |
67 | | - <div class="flex items-center justify-between mt-1 text-sm text-primary-300"> |
68 | | - <div class="inline-flex items-center gap-2 px-2 py-0.5 bg-red-100 text-red-600 rounded font-semibold">{getPriceInfo(item).percentOff}% off</div> |
69 | | - <div class="text-sm">You save {formatClays(getPriceInfo(item).save)}</div> |
70 | | - </div> |
71 | | - {:else} |
72 | | - <div class="mt-3 text-lg font-bold">{formatClays(getPriceInfo(item).price)}</div> |
73 | | - {/if} |
74 | | - </div> |
75 | | - </div> |
| 23 | + <MarketItem {item} /> |
76 | 24 | {/each} |
77 | 25 | </div> |
78 | 26 | {/if} |
79 | | - |
80 | | -<!-- <p>Market score: {data.user.shopScore}</p> --> |
0 commit comments