Skip to content

Commit 8399216

Browse files
committed
Add hand sorting
Close #4
1 parent b89410c commit 8399216

6 files changed

Lines changed: 66 additions & 10 deletions

File tree

package-lock.json

Lines changed: 19 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"clsx": "^1.2.1",
1616
"framer-motion": "^7.2.0",
1717
"immer": "^9.0.15",
18+
"lucide-react": "^0.88.0",
1819
"react": "^18.0.0",
1920
"react-dom": "^18.0.0",
2021
"xstate": "^4.32.1"

src/components/App.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Pile from "./Pile";
88
import ShownHand from "./ShownHand";
99
import Switcher from "./Switcher";
1010
import { isChoosingFaceUpCardsStor, isPlayingStor } from "../state/selectors";
11+
import SortButton from "./SortButton";
1112

1213
export default function App() {
1314
const { zhitheadService } = useContext(GlobalStateContext);
@@ -62,7 +63,8 @@ export default function App() {
6263

6364
<div className="relative pt-4">
6465
{isPlaying && (
65-
<div className="absolute bottom-2 z-10 mx-auto w-full">
66+
<div className="absolute -left-6 bottom-2 z-10 mx-auto flex w-full items-center justify-center gap-4">
67+
<SortButton />
6668
<Switcher player="human" />
6769
</div>
6870
)}

src/components/SortButton.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useSelector } from "@xstate/react";
2+
import { motion } from "framer-motion";
3+
import { SortAsc } from "lucide-react";
4+
import { useContext } from "react";
5+
import { GlobalStateContext } from "./providers/GlobalStateProvider";
6+
7+
export default function SortButton() {
8+
const { zhitheadService } = useContext(GlobalStateContext);
9+
const { send } = zhitheadService;
10+
const handLength = useSelector(
11+
zhitheadService,
12+
(state) => state.context.human.hand.length
13+
);
14+
15+
return (
16+
<motion.button
17+
onClick={() => send("SORT_HAND")}
18+
className="flex items-center justify-center rounded-full bg-black p-1.5 sm:p-2"
19+
title="Sort your hand"
20+
initial={{ y: 100 }}
21+
animate={{ y: !handLength ? 100 : 0 }}
22+
whileHover={{ scale: 1.15 }}
23+
whileTap={{ scale: 0.9 }}
24+
>
25+
<SortAsc className="h-4 w-4 -rotate-90 stroke-zinc-200 sm:h-6 sm:w-6" />
26+
</motion.button>
27+
);
28+
}

src/lib.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export function cardToString(card: Readonly<Card>) {
4646
return `${Suite[getSuite(card)]}-${[Rank[getRank(card)]]}`;
4747
}
4848

49+
export function compareCards(a: Readonly<Card>, b: Readonly<Card>) {
50+
return getRank(a) - getRank(b);
51+
}
52+
4953
function createRanks(): Rank[] {
5054
return [
5155
Rank.Ace,

src/state/machines/zhithead.machine.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
canPlay as _canPlay,
77
Card,
88
Cards,
9+
compareCards,
910
createDeck,
1011
dealCardsFor,
1112
getRank,
@@ -60,6 +61,7 @@ export const zhitheadModel = createModel(createInitialContext(), {
6061
}),
6162
TAKE_CARD: () => ({}),
6263
TAKE_PILE: () => ({}),
64+
SORT_HAND: () => ({}),
6365
...barePlayerEvent("CARD_CHOSEN"),
6466
},
6567
});
@@ -207,6 +209,15 @@ export const zhitheadMachine = zhitheadModel.createMachine(
207209
},
208210
},
209211
},
212+
sorter: {
213+
on: {
214+
SORT_HAND: {
215+
actions: assign((context) => {
216+
context.human.hand.sort(compareCards);
217+
}),
218+
},
219+
},
220+
},
210221
},
211222
},
212223
},

0 commit comments

Comments
 (0)