diff --git a/src/app.css b/src/app.css index 54cbea7..22c43df 100644 --- a/src/app.css +++ b/src/app.css @@ -23,6 +23,10 @@ body::before { background: radial-gradient(rgb(24, 82, 65), rgb(1, 42, 16)); } +.hand { + background: radial-gradient(rgb(24, 70, 82), rgb(1, 42, 41)); +} + .full { height: 100%; width: 100%; diff --git a/src/components/Card.tsx b/src/components/Card.tsx index 3a2cd88..2848e53 100644 --- a/src/components/Card.tsx +++ b/src/components/Card.tsx @@ -1,5 +1,6 @@ import { Component, createResource, JSX, Suspense } from "solid-js"; import { Card } from "../types/cards"; +import { Clickable, Stylable } from "./toolbox"; const cardToSvgFilename = (card: Card) => { if (card.kind == "joker") { @@ -30,6 +31,7 @@ export default ((props) => { return ( { /> ); -}) satisfies Component<{ - class?: string; - style?: JSX.CSSProperties; - card: Card; - face?: "up" | "down"; -}>; +}) satisfies Component< + { + card: Card; + face?: "up" | "down"; + } & Stylable & + Clickable +>; diff --git a/src/components/Game.tsx b/src/components/Game.tsx index 1dc4720..c836cb1 100644 --- a/src/components/Game.tsx +++ b/src/components/Game.tsx @@ -2,32 +2,35 @@ import { createContext, JSX } from "solid-js"; import Card from "./Card"; import Hand from "./Hand"; import Pile from "./Pile"; -import { newDeck, shuffle, Hand as THand } from "../types/cards"; -import { createStore, produce } from "solid-js/store"; +import { GameState, newDeck, shuffle, Hand as THand } from "../types/cards"; +import { createStore, produce, SetStoreFunction, Store } from "solid-js/store"; -const GameContext = createContext(); +export const GameContext = createContext<{ + gameState: Store; + setGameState: SetStoreFunction; +}>(); export default () => { - const [gameState, setGameState] = createStore({ - pile: shuffle(newDeck()), - hand: [] as THand, + const [gameState, setGameState] = createStore({ + deck: shuffle(newDeck()), + hand: [], }); return (
{}} - class="full column" + class="full column center" style={{ "row-gap": "20px", "font-size": "32px" }} >
setGameState( produce((state) => { - state.hand.push(state.pile.pop()!); + state.hand.push(state.deck.pop()!); }) ) } diff --git a/src/components/Hand.tsx b/src/components/Hand.tsx index 8277be1..3ef62c2 100644 --- a/src/components/Hand.tsx +++ b/src/components/Hand.tsx @@ -1,11 +1,19 @@ -import { Component, For } from "solid-js"; +import { Component, For, useContext } from "solid-js"; import Card from "./Card"; import { Hand } from "../types/cards"; +import { GameContext } from "./Game"; +import { produce } from "solid-js/store"; export default ((props) => { + const { setGameState } = useContext(GameContext)!; + return (
{ gap: "5px", }} > - {(card) => } + + {(card) => ( + + setGameState( + produce((state) => { + const index = state.hand.indexOf(card); + console.log(index); + state.deck.push( + state.hand.splice( + props.hand.indexOf(card), + 1 + )[0]! + ); + }) + ) + } + /> + )} +
); }) satisfies Component<{ hand: Hand }>; diff --git a/src/components/Pile.tsx b/src/components/Pile.tsx index 2dea2d3..41350b1 100644 --- a/src/components/Pile.tsx +++ b/src/components/Pile.tsx @@ -1,12 +1,19 @@ import { Component, For, JSX } from "solid-js"; import Card from "./Card"; import { Pile } from "../types/cards"; +import { type ComponentProps } from "solid-js"; +import { Clickable, Stylable } from "./toolbox"; export default ((props) => { return (
@@ -31,14 +38,9 @@ export default ((props) => {
); -}) satisfies Component<{ - pile: Pile; - style?: JSX.CSSProperties; - onClick?: - | JSX.EventHandlerUnion< - HTMLDivElement, - MouseEvent, - JSX.EventHandler - > - | undefined; -}>; +}) satisfies Component< + { + pile: Pile; + } & Stylable & + Clickable +>; diff --git a/src/components/toolbox.tsx b/src/components/toolbox.tsx new file mode 100644 index 0000000..ca2ae98 --- /dev/null +++ b/src/components/toolbox.tsx @@ -0,0 +1,16 @@ +import { JSX } from "solid-js"; + +export type Stylable = { + class?: string; + style?: JSX.CSSProperties; +}; + +export type Clickable = { + onClick?: + | JSX.EventHandlerUnion< + HTMLDivElement, + MouseEvent, + JSX.EventHandler + > + | undefined; +}; diff --git a/src/types/cards.ts b/src/types/cards.ts index 029680d..5b75a48 100644 --- a/src/types/cards.ts +++ b/src/types/cards.ts @@ -55,3 +55,8 @@ export const shuffle = (cards: Card[]) => { } return cards; }; + +export type GameState = { + deck: Pile; + hand: Hand; +};