feat(Cards Picker): enhancements (packs, jokers, ui)

Fix randomness with use jokers options and number of packs
Better UI and randomness
Fix #39
This commit is contained in:
ShareVB committed 2025-03-30 13:26:07 +02:00
1 parent b870e0e99b
commit f120be9227
3 files changed
+34 -8

No files matched your search

+1
View File
@@ -243,6 +243,7 @@
"ua-parser-js": "^2.0.1", "ua-parser-js": "^2.0.1",
"ulid": "^2.3.0", "ulid": "^2.3.0",
"unicode-emoji-json": "^0.4.0", "unicode-emoji-json": "^0.4.0",
"unique-random": "^4.0.0",
"unplugin-auto-import": "^0.16.4", "unplugin-auto-import": "^0.16.4",
"utf8": "^3.0.0", "utf8": "^3.0.0",
"uuid": "^11.0.5", "uuid": "^11.0.5",
+8
View File
@@ -623,6 +623,9 @@ dependencies:
unicode-emoji-json: unicode-emoji-json:
specifier: ^0.4.0 specifier: ^0.4.0
version: 0.4.0 version: 0.4.0
unique-random:
specifier: ^4.0.0
version: 4.0.0
unplugin-auto-import: unplugin-auto-import:
specifier: ^0.16.4 specifier: ^0.16.4
version: 0.16.4(@vueuse/core@10.11.1)(rollup@2.79.2) version: 0.16.4(@vueuse/core@10.11.1)(rollup@2.79.2)
@@ -16666,6 +16669,11 @@ packages:
- rollup - rollup
dev: false dev: false
/unique-random@4.0.0:
resolution: {integrity: sha512-lrIvMDF5M9DOCy6j1YdNkMkAZkQAd0Bk7PWazA/Kr3cy2L0NhanxdeFmBFg+tDUirNHrIcQf3K7oIeBTCXpIFQ==}
engines: {node: '>=18'}
dev: false
/unique-string@2.0.0: /unique-string@2.0.0:
resolution: {integrity: sha512-uNaeirEPvpZWSgzwsPGtU2zVSTrn/8L5q/IexZmH0eH6SA73CmAA5U4GwORTxQAZs95TAXLNqeLoPPNO5gZfWg==} resolution: {integrity: sha512-uNaeirEPvpZWSgzwsPGtU2zVSTrn/8L5q/IexZmH0eH6SA73CmAA5U4GwORTxQAZs95TAXLNqeLoPPNO5gZfWg==}
engines: {node: '>=8'} engines: {node: '>=8'}
+25 -8
View File
@@ -1,17 +1,26 @@
<script setup lang="ts"> <script setup lang="ts">
import cards from '@younestouati/playing-cards-standard-deck'; import cards from '@younestouati/playing-cards-standard-deck';
import { exhaustiveUniqueRandom } from 'unique-random';
import { computedRefreshable } from '@/composable/computedRefreshable'; import { computedRefreshable } from '@/composable/computedRefreshable';
import { useCopy } from '@/composable/copy'; import { useCopy } from '@/composable/copy';
import { useQueryParamOrStorage } from '@/composable/queryParams'; import { useQueryParamOrStorage } from '@/composable/queryParams';
import { randIntFromInterval } from '@/utils/random';
const cardKeys = Object.keys(cards); const cardKeys = [...Object.keys(cards).filter(k => k !== 'joker'), 'joker', 'joker'];
type CardNames = keyof typeof cards; type CardNames = keyof typeof cards;
const numberOfCards = useQueryParamOrStorage({ name: 'cards', storageName: 'card-picker:n', defaultValue: 5 }); const numberOfCards = useQueryParamOrStorage({ name: 'cards', storageName: 'card-picker:n', defaultValue: 5 });
const [cardPicked, refreshCardPicked] = computedRefreshable(() => const numberOfPacks = useQueryParamOrStorage({ name: 'packs', storageName: 'card-picker:p', defaultValue: 1 });
Array.from({ length: numberOfCards.value }, const useJokers = useQueryParamOrStorage({ name: 'jokers', storageName: 'card-picker:j', defaultValue: true });
() => cardKeys[randIntFromInterval(0, 51)])); const [cardPicked, refreshCardPicked] = computedRefreshable(() => {
let cardNumbers: number[] = [];
for (let pack = 0; pack < numberOfPacks.value; pack++) {
cardNumbers = [...cardNumbers, ...Array.from({ length: useJokers.value ? 54 : 52 }, (_, num) => num)];
}
const random = exhaustiveUniqueRandom(0, cardNumbers.length - 1);
return Array.from({ length: numberOfCards.value }, () => cardKeys[cardNumbers[random()]]);
});
const maxCards = computed(() => (useJokers.value ? 54 : 52) * numberOfPacks.value);
const suitNames = { const suitNames = {
c: 'clubs (♣)', c: 'clubs (♣)',
@@ -40,16 +49,24 @@ const { copy } = useCopy({ source: cardPickedString, text: 'Cards Picked copied
<template> <template>
<c-card> <c-card>
<div mb-2 flex justify-center> <n-space justify="space-around" mb-1>
<img v-for="(card, index) in cardPicked" :key="index" style="width:90px" mr-1 :src="`data:image/svg+xml;base64,${cards[card as CardNames]}`"> <img v-for="(card, index) in cardPicked" :key="index" style="width:90px" mr-1 :src="`data:image/svg+xml;base64,${cards[card as CardNames]}`">
</div> </n-space>
<div mb-2> <div mb-2>
<textarea-copyable :value="cardPickedString" readonly mb-1 /> <textarea-copyable :value="cardPickedString" readonly mb-1 />
</div> </div>
<div flex justify-center gap-3> <div flex justify-center gap-3>
<n-form-item label="Number of cards:" label-placement="left"> <n-form-item label="Number of cards:" label-placement="left">
<n-input-number v-model:value="numberOfCards" min="1" placeholder="Width of the text" /> <n-input-number v-model:value="numberOfCards" min="1" :max="maxCards" placeholder="Number of cards to pick" />
</n-form-item> </n-form-item>
<n-form-item label="Number of packs:" label-placement="left">
<n-input-number v-model:value="numberOfPacks" min="1" placeholder="Number of card packs to pick in" />
</n-form-item>
</div>
<div mb-4 flex justify-center>
<n-checkbox v-model:checked="useJokers">
Use Jokers
</n-checkbox>
</div> </div>
<div flex justify-center gap-3> <div flex justify-center gap-3>
<c-button @click="copy()"> <c-button @click="copy()">