Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New pitch grid #68

Merged
merged 4 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 29 additions & 37 deletions web/src/components/playing/BatterView2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const FullcountABI = FullcountABIImported as unknown as AbiItem[];

const BatterView2 = ({ sessionStatus }: { sessionStatus: SessionStatus }) => {
const [kind, setKind] = useState(0);
const [gridIndex, setGridIndex] = useState(12);
const [gridIndex, setGridIndex] = useState(-1);
const [isRevealed, setIsRevealed] = useState(false);
const [nonce, setNonce] = useState("0");
const web3ctx = useContext(Web3Context);
const { selectedSession, contractAddress, selectedToken } = useGameContext();
Expand Down Expand Up @@ -155,6 +156,7 @@ const BatterView2 = ({ sessionStatus }: { sessionStatus: SessionStatus }) => {
},
{
onSuccess: () => {
setIsRevealed(true);
queryClient.invalidateQueries("sessions");
queryClient.refetchQueries("session");
},
Expand Down Expand Up @@ -200,6 +202,7 @@ const BatterView2 = ({ sessionStatus }: { sessionStatus: SessionStatus }) => {
</Text>
<GridComponent
selectedIndex={gridIndex}
isPitcher={false}
setSelectedIndex={sessionStatus.didBatterCommit ? undefined : setGridIndex}
/>
<Text className={globalStyles.gradientText} fontSize={"18px"} fontWeight={"700"}>
Expand All @@ -219,54 +222,43 @@ const BatterView2 = ({ sessionStatus }: { sessionStatus: SessionStatus }) => {
Generate
</button>
)}
{seed && (
<Flex
w="180px"
h="27px"
alignItems={"center"}
justifyContent={"center"}
bg="#4D4D4D"
onClick={handleGenerate}
border={"#767676"}
gap={"10px"}
>
Generated
</Flex>
)}
{seed && <Flex className={styles.completedAction}>Generated</Flex>}
{movements.length > 0 && sessionStatus.progress === 3 && !sessionStatus.didBatterCommit && (
<Flex
onClick={() => window.removeEventListener("mousemove", handleMouseMove)}
w={"180px"}
h={"31px"}
border={"1px solid white"}
position={"relative"}
>
<Box w={`${(movements.length / 500) * 100}%`} bg={"green"} />
<Box bg={"gray"} />
<Text className={styles.moveMouseTip}>move mouse</Text>
</Flex>
)}
<button
className={globalStyles.commitButton}
onClick={handleCommit}
disabled={!seed || sessionStatus.didBatterCommit}
>
{commitSwing.isLoading ? (
<Spinner h={"14px"} w={"14px"} />
) : (
<Text>{sessionStatus.didBatterCommit ? "Committed" : "Commit"}</Text>
)}
</button>
{!sessionStatus.didBatterCommit ? (
<button
className={globalStyles.commitButton}
onClick={handleCommit}
disabled={!seed || sessionStatus.didBatterCommit}
>
{commitSwing.isLoading ? <Spinner h={"14px"} w={"14px"} /> : <Text>Commit</Text>}
</button>
) : (
<Flex className={styles.completedAction}>Committed</Flex>
)}

<button
className={globalStyles.commitButton}
onClick={handleReveal}
disabled={sessionStatus.progress !== 4 || sessionStatus.didBatterReveal}
>
{revealSwing.isLoading ? (
<Spinner h={"14px"} w={"14px"} />
) : (
<Text>{sessionStatus.didBatterReveal ? "Revealed" : "Reveal"}</Text>
)}
</button>
{sessionStatus.didBatterReveal || isRevealed ? (
<Flex className={styles.completedAction}>Revealed</Flex>
) : (
<button
className={globalStyles.commitButton}
onClick={handleReveal}
disabled={sessionStatus.progress !== 4 || sessionStatus.didBatterReveal}
>
{revealSwing.isLoading ? <Spinner h={"14px"} w={"14px"} /> : <Text>Reveal</Text>}
</button>
)}
<Text className={styles.text}>
Once both players have committed their moves, press{" "}
<span className={styles.textBold}> Reveal</span> to see the outcome
Expand Down
89 changes: 73 additions & 16 deletions web/src/components/playing/GridComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,106 @@
import React, { useState } from "react";
import { Box, Grid, Text } from "@chakra-ui/react";
import React, { useEffect, useState } from "react";
import { Box, Flex, Grid, Image } from "@chakra-ui/react";
import { FULLCOUNT_ASSETS_PATH } from "../../constants";
import styles from "./PlayView.module.css";

const assets = FULLCOUNT_ASSETS_PATH;

const GridComponent = ({
selectedIndex,
setSelectedIndex,
isPitcher,
}: {
selectedIndex: number;
setSelectedIndex?: (value: number) => void;
isPitcher: boolean;
}) => {
const handleClick = (index: number) => {
if (setSelectedIndex) {
const [isDragging, setIsDragging] = useState(false);

const handleMouseDown = (index: number) => {
if (!setSelectedIndex) {
return;
}
if (selectedIndex === -1) {
setSelectedIndex(index);
}
setIsDragging(true);
};

const handleMouseUp = (index: number) => {
if (!setSelectedIndex) {
return;
}
if (isDragging) {
setIsDragging(false);
setSelectedIndex(index);
}
};

const numbers = [
10, 11, 12, 13, 14, 15, 1, 2, 3, 16, 17, 4, 5, 6, 18, 19, 7, 8, 9, 20, 21, 22, 23, 24, 25,
];
const leftBorder = [6, 11, 16];
const topBorder = [6, 7, 8];
const rightBorder = [8, 13, 18];
const bottomBorder = [16, 17, 18];
useEffect(() => {
console.log(isPitcher);
}, [isPitcher]);

// Generate cell with click handler and style based on index
const generateCell = (index: number) => (
<Box
key={index}
height="50px" // Set your desired height
width="50px" // Set your desired width
bg={numbers[index] < 10 ? "#00441b" : "transparent"}
height="50px"
width="50px"
color={index === selectedIndex ? "#e6482b" : numbers[index] < 10 ? "white" : "#b0b0b0"}
display="flex"
alignItems="center"
justifyContent="center"
border="1px"
borderColor={numbers[index] < 10 ? "white" : "#b0b0b0"}
cursor={setSelectedIndex ? "pointer" : "default"}
onClick={() => handleClick(index)}
border={"1px solid #333333"}
borderLeftColor={leftBorder.includes(index) ? "#AAA" : "#333333"}
borderRightColor={rightBorder.includes(index) ? "#AAA" : "#33333"}
borderTopColor={topBorder.includes(index) ? "#AAA" : "#333333"}
borderBottomColor={bottomBorder.includes(index) ? "#AAA" : "#333333"}
cursor={
selectedIndex === index && !isDragging && setSelectedIndex
? "pointer"
: selectedIndex === -1 || isDragging
? "inherit"
: "default"
}
fontSize={index === selectedIndex ? "22px" : "16px"}
bg={"#111111"}
onMouseUp={() => handleMouseUp(index)}
onMouseDown={() => handleMouseDown(index)}
>
<Text>{numbers[index]}</Text>
{index === selectedIndex && !isDragging && (
<Image
h={"32px"}
w={"32px"}
src={isPitcher ? `${assets}/ball2.png` : `${assets}/bat2.png`}
alt={"ball"}
draggable={false}
userSelect={"none"}
/>
)}
</Box>
);

return (
<Grid templateColumns="repeat(5, 1fr)" w={"fit-content"}>
{/* Generate cells for the grid */}
{Array.from({ length: 25 }).map((_, i) => generateCell(i))}
</Grid>
<Flex
className={
selectedIndex !== -1 && !isDragging
? styles.pitcherGridSelected
: isPitcher
? styles.pitcherGrid
: styles.batterGrid
}
>
<Grid templateColumns="repeat(5, 1fr)" w={"fit-content"}>
{Array.from({ length: 25 }).map((_, i) => generateCell(i))}
</Grid>
</Flex>
);
};

Expand Down
68 changes: 29 additions & 39 deletions web/src/components/playing/PitcherView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const FullcountABI = FullcountABIImported as unknown as AbiItem[];

const PitcherView = ({ sessionStatus }: { sessionStatus: SessionStatus }) => {
const [speed, setSpeed] = useState(0);
const [gridIndex, setGridIndex] = useState(12);
const [gridIndex, setGridIndex] = useState(-1);
const [isRevealed, setIsRevealed] = useState(false);
const [nonce, setNonce] = useState("0");
const web3ctx = useContext(Web3Context);
const { selectedSession, contractAddress, selectedToken } = useGameContext();
Expand Down Expand Up @@ -156,6 +157,7 @@ const PitcherView = ({ sessionStatus }: { sessionStatus: SessionStatus }) => {
},
{
onSuccess: () => {
setIsRevealed(true);
queryClient.invalidateQueries("sessions");
queryClient.refetchQueries("session");
},
Expand Down Expand Up @@ -194,6 +196,7 @@ const PitcherView = ({ sessionStatus }: { sessionStatus: SessionStatus }) => {
</Text>
<GridComponent
selectedIndex={gridIndex}
isPitcher={true}
setSelectedIndex={sessionStatus.didPitcherCommit ? undefined : setGridIndex}
/>
<Text className={globalStyles.gradientText} fontSize={"18px"} fontWeight={"700"}>
Expand All @@ -213,55 +216,42 @@ const PitcherView = ({ sessionStatus }: { sessionStatus: SessionStatus }) => {
Generate
</button>
)}
{seed && (
<Flex
w="180px"
h="27px"
alignItems={"center"}
justifyContent={"center"}
bg="#4D4D4D"
onClick={handleGenerate}
border={"#767676"}
gap={"10px"}
>
Generated
</Flex>
)}
{seed && <Flex className={styles.completedAction}>Generated</Flex>}
{movements.length > 0 && sessionStatus.progress === 3 && !sessionStatus.didPitcherCommit && (
<Flex
onClick={() => window.removeEventListener("mousemove", handleMouseMove)}
w={"180px"}
h={"31px"}
border={"1px solid white"}
position={"relative"}
>
<Box w={`${(movements.length / 500) * 100}%`} bg={"green"} />
<Box bg={"gray"} />
<Text className={styles.moveMouseTip}>move mouse</Text>
</Flex>
)}

<button
className={globalStyles.commitButton}
onClick={handleCommit}
disabled={!seed || sessionStatus.didPitcherCommit}
>
{commitPitch.isLoading ? (
<Spinner h={"14px"} w={"14px"} />
) : (
<Text>{sessionStatus.didPitcherCommit ? "Committed" : "Commit"}</Text>
)}
</button>

<button
className={globalStyles.commitButton}
onClick={handleReveal}
disabled={sessionStatus.progress !== 4 || sessionStatus.didPitcherReveal}
>
{revealPitch.isLoading ? (
<Spinner h={"14px"} w={"14px"} />
) : (
<Text>{sessionStatus.didPitcherReveal ? "Revealed" : "Reveal"}</Text>
)}
</button>
{!sessionStatus.didPitcherCommit ? (
<button
className={globalStyles.commitButton}
onClick={handleCommit}
disabled={!seed || sessionStatus.didPitcherCommit}
>
{commitPitch.isLoading ? <Spinner h={"14px"} w={"14px"} /> : <Text>Commit</Text>}
</button>
) : (
<Flex className={styles.completedAction}>Committed</Flex>
)}
{sessionStatus.didPitcherReveal || isRevealed ? (
<Flex className={styles.completedAction}>Revealed</Flex>
) : (
<button
className={globalStyles.commitButton}
onClick={handleReveal}
disabled={sessionStatus.progress !== 4 || sessionStatus.didPitcherReveal}
>
{revealPitch.isLoading ? <Spinner h={"14px"} w={"14px"} /> : <Text>Reveal</Text>}
</button>
)}
<Text className={styles.text}>
Once both players have committed their moves, press{" "}
<span className={styles.textBold}> Reveal</span> to see the outcome
Expand Down
34 changes: 34 additions & 0 deletions web/src/components/playing/PlayView.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,37 @@
font-weight: 700;
line-height: normal;
}

.pitcherGrid {
cursor: url("https://static.simiotics.com/fullcount/ball4.png"), pointer;
}
.pitcherGridSelected {
cursor: pointer;
}
.batterGrid {
cursor: url("https://static.simiotics.com/fullcount/bat-cursor.png"), pointer;
}

.moveMouseTip {
white-space: nowrap;
font-size: 12px;
position: absolute;
color: #CCCCCC;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

}

.completedAction {
display: flex;
height: 31px;
width: 180px;
padding: 6px 20px;
justify-content: center;
align-items: center;
gap: 10px;
border: 1px solid #767676;
background: #4D4D4D;
line-height: normal;
}
Loading