non-functional thing
This commit is contained in:
44
hooks/useDeck.ts
Normal file
44
hooks/useDeck.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user