use gzipped card data

This commit is contained in:
Hamcha 2024-10-27 17:38:00 +01:00
parent 73cc1d1ac8
commit 1ec10b4c12
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
6 changed files with 74 additions and 21 deletions

View file

@ -3,7 +3,8 @@
"private": true, "private": true,
"version": "0.1.0", "version": "0.1.0",
"scripts": { "scripts": {
"start": "vite" "start": "vite",
"build": "vite build"
}, },
"devDependencies": { "devDependencies": {
"@biomejs/biome": "^1.9.4", "@biomejs/biome": "^1.9.4",

File diff suppressed because one or more lines are too long

BIN
src/card-data.jsonz Normal file

Binary file not shown.

View file

@ -1,22 +1,12 @@
import { $el } from '@hamcha/domutil'; import { $el } from '@hamcha/domutil';
import { convert_to_hiragana } from '@hamcha/weeblib'; import { convert_to_hiragana } from '@hamcha/weeblib';
import cardData from './card-data.json'; import { loadCardData } from './read-data';
import './index.css'; import './index.css';
interface Card { const scryfallURL = (scryfallId: string) =>
manaCost: string; `https://cards.scryfall.io/large/front/${scryfallId?.charAt(0)}/${scryfallId?.charAt(1)}/${scryfallId}.jpg`;
name: string;
type: string;
text: string;
flavorText: string;
power?: number;
toughness?: number;
italianImage: string;
image: string;
}
const cards = cardData as Card[];
const parseMC = (mc: string) => const parseMC = (mc: string) =>
mc mc
@ -24,11 +14,17 @@ const parseMC = (mc: string) =>
.split('}{') .split('}{')
.map((v) => $el('span.manapip', v)); .map((v) => $el('span.manapip', v));
let main = $el('main'); let main = $el(
'main',
$el('h1', convert_to_hiragana('Caricamento in corso...')),
);
document.body.appendChild(main);
const cards = await loadCardData();
const render = () => { const render = () => {
const randomCard = cards[Math.floor(Math.random() * cards.length)]; const randomCard = cards[Math.floor(Math.random() * cards.length)];
const cardImage = $el('img.bg', { src: randomCard.italianImage }); const cardImage = $el('img.bg', { src: scryfallURL(randomCard.id) });
main.remove(); main.remove();
main = $el( main = $el(
@ -48,9 +44,7 @@ const render = () => {
randomCard.flavorText randomCard.flavorText
? $el('p.flavor', convert_to_hiragana(randomCard.flavorText)) ? $el('p.flavor', convert_to_hiragana(randomCard.flavorText))
: '', : '',
randomCard.power randomCard.power ? $el('p.power', `${randomCard.power}`) : '',
? $el('p.power', `${randomCard.power} / ${randomCard.toughness}`)
: '',
), ),
$el( $el(
'section.actions', 'section.actions',

54
src/read-data.ts Normal file
View file

@ -0,0 +1,54 @@
//@ts-expect-error asset
import cardDataURL from './card-data.jsonz';
interface Card {
manaCost: string;
name: string;
type: string;
text: string;
flavorText: string;
power?: string;
id: string;
}
const nextFrame = async () =>
new Promise((resolve) => requestAnimationFrame(resolve));
export async function loadCardData(): Promise<Card[]> {
// Fetch the gzipped JSON file
const response = await fetch(cardDataURL);
if (!response.ok) {
throw new Error(
`HTTP request failed: ${response.status} ${response.statusText}`,
);
}
if (!response.body) {
throw new Error('No body found');
}
// Create a DecompressionStream
const decompressionStream = new DecompressionStream('gzip');
const textReader = new TextDecoderStream();
const reader = response.body
.pipeThrough(decompressionStream)
.pipeThrough(textReader)
.getReader();
// Read the decompressed text
let text = '';
while (true) {
// I like my browser not being stuck, unlike VScode...
await nextFrame();
// Read next chunk
const { done, value } = await reader.read();
text += value ?? '';
if (done) {
break;
}
}
return JSON.parse(text);
}

View file

@ -2,4 +2,9 @@ import wasm from 'vite-plugin-wasm';
export default { export default {
plugins: [wasm()], plugins: [wasm()],
build: {
target: 'esnext',
},
assetsInclude: ['**/*.jsonz'],
base: './',
}; };