aboutsummaryrefslogtreecommitdiff
path: root/src/routes/auctionprices/+page.svelte
blob: 844af04defc8e2a124642e9c7814cabe9a09e947 (plain)
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
<script lang="ts">
	import Header from '$lib/Header.svelte'
	import Head from '$lib/Head.svelte'
	import { cleanId, removeFormattingCode, toTitleCase, type PreviewedAuctionData } from '$lib/utils'
	import type { ItemAuctionsSchema } from '$lib/APITypes'
	import AuctionPriceScatterplot from '$lib/AuctionPriceScatterplot.svelte'
	import AuctionPreviewTooltip from '$lib/AuctionPreviewTooltip.svelte'
	import { browser } from '$app/environment'
	import Item from '$lib/minecraft/Item.svelte'
	import furfskyReborn from 'skyblock-assets/matchers/furfsky_reborn.json'
	import type { PageData } from './$types'
	import { fetchApi } from '$lib/api'

	export let data: PageData
	let auctionPrices: ItemAuctionsSchema[] = data.prices
	let auctionItems: Record<string, { display: { name: string }; vanillaId?: string }> = data.items

	let currentlyPreviewedAuction: PreviewedAuctionData | null = null

	let query: string = ''

	$: queryNormalized = query.toLowerCase()

	let allMatchingItemIds: string[]
	$: {
		pageNumber = 0
		allMatchingItemIds = Object.entries(auctionItems)
			.filter(a => removeFormattingCode(a[1]?.display.name.toLowerCase()).includes(queryNormalized))
			.map(a => a[0])
	}
	$: {
		if (browser) fetchAndSetItems(allMatchingItemIds.slice(0, 100))
	}

	async function fetchAndSetItems(itemIds: string[]) {
		const localQuery = query
		const localData = await fetchItems(query.length > 0 ? itemIds : null)
		// if the query hasn't changed, update the data
		if (query === localQuery) data = localData
	}
	async function fetchItems(itemIds: null | string[]): Promise<ItemAuctionsSchema[]> {
		let url = `auctionprices`
		if (itemIds !== null) {
			if (itemIds.length === 0) return []
			url += `?items=${itemIds.join(',')}`
		}
		return await fetchApi(url, fetch).then(r => r.json())
	}

	let pageHeight = 0
	$: {
		pageHeight = 0
	}

	// 0 indexed
	let pageNumber = 0
	let loadingPage = false

	async function checkScroll() {
		if (loadingPage) return

		let pageHeightTemp = window.scrollY + window.innerHeight
		if (pageHeightTemp <= pageHeight) return
		pageHeight = pageHeightTemp
		if (pageHeight >= document.body.scrollHeight - 1000) {
			loadingPage = true
			pageNumber++
			const itemIds = allMatchingItemIds.slice(pageNumber * 100, (pageNumber + 1) * 100)
			if (itemIds.length > 0) {
				const shownIds = data.map(d => d.id)
				const items = (await fetchItems(itemIds)).filter(i => !shownIds.includes(i.id))
				if (items.length > 0) data = [...data, ...items]
			}
			loadingPage = false
		}
	}

	$: {
		if (browser && !currentlyPreviewedAuction) {
			for (const el of document.getElementsByClassName('selected-auction'))
				el.classList.remove('selected-auction')
		}
	}
</script>

<Head
	title="SkyBlock Auction Prices"
	description="View how many coins every SkyBlock item costs in the auction house."
/>
<Header />

<svelte:window on:scroll={checkScroll} />

<AuctionPreviewTooltip bind:preview={currentlyPreviewedAuction} />

<main>
	<h1>SkyBlock Auction Prices</h1>
	<div class="filter-items-settings">
		<input type="text" id="filter-items-tier" placeholder="Search..." bind:value={query} />
	</div>
	<div class="item-list">
		{#each data as item (item.id)}
			{@const binAuctions = item.auctions.filter(i => i.bin)}
			{@const normalAuctions = item.auctions.filter(i => !i.bin)}
			{@const itemData = auctionItems[item.sbId]}
			<div class="item-container">
				{#if itemData && itemData.vanillaId}
					<div class="item-slot-container">
						<Item item={{ ...itemData, id: item.id }} pack={furfskyReborn} headSize={50} isslot />
					</div>
				{/if}
				<h2>
					{removeFormattingCode(
						auctionItems[item.id]?.display.name ?? toTitleCase(cleanId(item.id.toLowerCase()))
					)}
				</h2>
				<div class="auctions-info-text">
					{#if binAuctions.length > 0}
						<p>
							Cheapest recent BIN: <b>
								{binAuctions.reduce((a, b) => (a.coins < b.coins ? a : b)).coins.toLocaleString()} coins
							</b>
						</p>
					{/if}
					{#if normalAuctions.length > 0}
						<p>
							Cheapest recent auction: <b>
								{normalAuctions
									.reduce((a, b) => (a.coins < b.coins ? a : b))
									.coins.toLocaleString()} coins
							</b>
						</p>
					{/if}
					{#if item.auctions.length >= 2}
						<p>
							Median:
							<b>
								{[...item.auctions]
									.sort((a, b) => a.coins - b.coins)
									[Math.floor(item.auctions.length / 2)].coins.toLocaleString()} coins
							</b>
						</p>
						<p>
							Volume:
							<b>
								{parseFloat(
									(
										(24 * 60 * 60) /
										((Date.now() / 1000 - item.auctions[0].ts) / item.auctions.length)
									).toPrecision(2)
								).toLocaleString()}/day
							</b>
						</p>
					{/if}
				</div>
				<div class="item-scatterplot">
					<AuctionPriceScatterplot {item} bind:currentlyPreviewedAuction />
				</div>
			</div>
		{/each}
		{#if data.length === 0}
			No results
		{/if}
	</div>
</main>

<style>
	p {
		margin: 0;
	}
	.item-list {
		display: grid;
		grid-template-columns: repeat(auto-fill, minmax(16em, 1fr));
		grid-gap: 1em;
		margin-top: 1em;
	}
	.item-container {
		border: 1px solid rgba(255, 255, 255, 0.1);
		background: rgba(0, 0, 0, 0.1);
		padding: 0.75em;
		border-radius: 1em;
	}

	.item-scatterplot {
		margin-top: 1em;
	}

	.auctions-info-text {
		color: var(--theme-darker-text);
	}
	.auctions-info-text b {
		color: var(--theme-main-text);
	}

	.item-slot-container {
		float: right;
	}
</style>