44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import { Card, Deck } from "@/Deck";
|
|
import { useCallback, useRef, useState } from "react"
|
|
|
|
export const useDeck = <T>() => {
|
|
const deck = useRef(new Deck<T>());
|
|
|
|
const shuffle = useCallback(() => {
|
|
deck.current.shuffle();
|
|
}, [])
|
|
|
|
const reshuffle = useCallback(() => {
|
|
deck.current.reshuffle();
|
|
}, [])
|
|
|
|
const createHand = useCallback(() => deck.current.createHand(), [])
|
|
|
|
const drawToHand = useCallback((index: number, count?: number, reshuffle?: boolean) => {
|
|
deck.current.drawToHand(index, count, reshuffle);
|
|
}, [])
|
|
|
|
const addCard = useCallback((...cards: Card<T>[]) => {
|
|
deck.current.addCard(...cards)
|
|
}, [])
|
|
|
|
const getDeck = useCallback(() => deck.current.getDeck(), []);
|
|
const getDiscard = useCallback(() => deck.current.getDiscard(), []);
|
|
const getHands = useCallback(() => deck.current.hands, []);
|
|
|
|
const createDeck = useCallback((cards: Card<T>[]) => {
|
|
deck.current = new Deck(cards);
|
|
}, [])
|
|
|
|
return {
|
|
deck: getDeck(),
|
|
discard: getDiscard(),
|
|
hands: getHands(),
|
|
shuffle,
|
|
reshuffle,
|
|
createHand,
|
|
drawToHand,
|
|
addCard,
|
|
createDeck
|
|
}
|
|
} |