Skip to content

Commit

Permalink
Merge pull request #15 from JollyGrin/feat/add-discard-actions
Browse files Browse the repository at this point in the history
Feat/add discard actions
  • Loading branch information
JollyGrin authored Mar 12, 2024
2 parents e68e6ed + ed29734 commit ed8df12
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 13 deletions.
8 changes: 5 additions & 3 deletions components/DeckPool/PoolFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const newPool = (deckData: DeckImportType): PoolType => {
// */
export const makeDeck = (pool: PoolType): PoolType => {
const newDeck: DeckImportCardType[] = pool.cards.flatMap(
({ quantity, ...rest }) => Array(quantity).fill({ quantity, ...rest })
({ quantity, ...rest }) => Array(quantity).fill({ quantity, ...rest }),
);
return {
...pool,
Expand Down Expand Up @@ -99,7 +99,7 @@ export const draw = (pool: PoolType): PoolType => {
export const adjustHp = (
pool: PoolType,
selectedPawn: "hero" | "sidekick",
adjustAmount: number
adjustAmount: number,
): PoolType => {
if (adjustAmount === 0) return pool;
if (!pool[selectedPawn] || !pool[selectedPawn]?.hp === null) return pool;
Expand All @@ -110,7 +110,7 @@ export const adjustHp = (

export const adjustSidekickQuantity = (
pool: PoolType,
adjustAmount: number
adjustAmount: number,
): PoolType => {
if (adjustAmount === 0) return pool;
if (!pool || !pool.sidekick.quantity) return pool;
Expand All @@ -128,6 +128,7 @@ export const drawDeck = (pool: PoolType, cardIndex: number) => {
if (!pool.deck) return;
pool.hand.push(pool.deck[cardIndex]);
pool.deck.splice(cardIndex, 1);
return pool;
};

// /**
Expand Down Expand Up @@ -170,6 +171,7 @@ export const discardRandomCard = (pool: PoolType, cardIndex: number) => {
export const drawDiscard = (pool: PoolType, cardIndex: number) => {
pool.hand.push(pool.discard[cardIndex]);
pool.discard.splice(cardIndex, 1);
return pool;
};

export const commitCard = (pool: PoolType, cardIndex: number): PoolType => {
Expand Down
34 changes: 32 additions & 2 deletions components/Game/Header/header.components.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Grid, Flex, Box, Text, HStack, useDisclosure } from "@chakra-ui/react";
import {
Grid,
Flex,
Box,
Text,
HStack,
useDisclosure,
Tooltip,
Divider,
} from "@chakra-ui/react";
import { IconType } from "react-icons";
import {
MoveStatContainer,
Expand Down Expand Up @@ -55,7 +64,9 @@ export const PlayerBox: React.FC<{
<MapModal {...mapDisclosure} />
<StatContainer>
<PlayerTitleBar>
<Text>{name}</Text>
<Tooltip label={<TipBody pool={playerState?.pool} />}>
<Text>{name}</Text>
</Tooltip>
<HStack gap="1rem">
<FaMap
style={{
Expand Down Expand Up @@ -196,6 +207,25 @@ export const Stat: React.FC<{
);
};

const TipBody = (props: { pool: PoolType }) => {
const { deckName, hero, sidekick } = props.pool;
return (
<Box minW="300px">
<Text fontWeight="bold">{deckName}</Text>
<Divider />
<Text>
{hero.name}: {hero.isRanged ? "Ranged" : "Meele"}
</Text>
<Text>{hero.specialAbility}</Text>
<Divider />
<Text>
{sidekick.name}: {sidekick.isRanged ? "Ranged" : "Meele"}
</Text>
<Text>{sidekick.quote}</Text>
</Box>
);
};

const AdjustButton = styled(Flex)`
background-color: ${(props) => props.bg};
padding: 0.18rem 0.35rem;
Expand Down
14 changes: 9 additions & 5 deletions components/Game/game.modal-body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { useState } from "react";

export const DeckModalContent = ({
cards,
add,
}: {
cards: DeckImportCardType[];
add: (index: number) => () => void;
}) => {
const [isHover, setIsHover] = useState<number>();
const onEnter = (num: number) => setIsHover(num);
const onLeave = () => setIsHover(undefined);

return (
<Grid gridTemplateColumns={"repeat(auto-fit, minmax(250px,1fr))"} gap={1}>
{cards?.map((card, index) => (
Expand All @@ -24,11 +27,12 @@ export const DeckModalContent = ({
>
{isHover === index && (
<Flex position="absolute">
<Tag fontSize="1.25rem" userSelect="none">
+
</Tag>

<Tag fontSize="1.25rem" userSelect="none">
<Tag
fontSize="1.25rem"
userSelect="none"
cursor="pointer"
onClick={() => add(index)()}
>
+
</Tag>
</Flex>
Expand Down
30 changes: 29 additions & 1 deletion components/Game/game.modal-template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ import {
PoolType,
cancelCommit,
discardCommit,
drawDeck,
drawDiscard,
revealCommit,
shuffleDeck,
} from "../DeckPool/PoolFns";
import { useRouter } from "next/router";
import { DeckImportCardType } from "../DeckPool/deck-import.type";
import { flow } from "lodash";
import { toast } from "react-hot-toast";

type ModalTemplateType = {
isOpen: boolean;
Expand All @@ -46,7 +50,19 @@ export const ModalContainer: React.FC<ModalTemplateType> = ({
setPlayerState()({ pool: poolInput });
};

const onClose = () => setModalType(false);
const gShuffleDeck = flow(
() => playerState?.pool && shuffleDeck(playerState?.pool),
setGameState,
);

const onClose = () => {
if (modalType === "deck") {
gShuffleDeck();
toast.success("Deck successfully shuffled after closing");
}
setModalType(false);
};

const gCancelCommit = flow(cancelCommit, setGameState);
const onCommitCancelClose = () => {
if (!playerState?.pool) return;
Expand All @@ -62,6 +78,17 @@ export const ModalContainer: React.FC<ModalTemplateType> = ({
() => playerState?.pool && discardCommit(playerState?.pool),
setGameState,
);
const gDrawDiscard = (discardIndex: number) =>
flow(
() => playerState?.pool && drawDiscard(playerState?.pool, discardIndex),
setGameState,
);

const gDrawDeck = (discardIndex: number) =>
flow(
() => playerState?.pool && drawDeck(playerState?.pool, discardIndex),
setGameState,
);

useEffect(() => {
const playerCommit = playerState?.pool?.commit;
Expand Down Expand Up @@ -101,6 +128,7 @@ export const ModalContainer: React.FC<ModalTemplateType> = ({
{playerState?.pool && modalType && !isCommit && (
<DeckModalContent
cards={playerState.pool[modalType] as DeckImportCardType[]}
add={modalType === "discard" ? gDrawDiscard : gDrawDeck}
/>
)}
{playerState?.pool && isCommit && commits.length > 0 && (
Expand Down
2 changes: 0 additions & 2 deletions pages/game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ const BoardContainer = ({ self }: { self: string }) => {
setGamePosition(defData);
}, [positions, self, gameState, setGamePosition]);

console.log({ mapUrl });

return (
<Box h={"100%"}>
<BoardCanvas
Expand Down

0 comments on commit ed8df12

Please sign in to comment.