diff --git a/src/client/api.ts b/src/client/api.ts index 2f9d76ee..6d5148e7 100644 --- a/src/client/api.ts +++ b/src/client/api.ts @@ -42,6 +42,7 @@ export function useSocket( onMessage?: MessageHandler, onOpen?: () => void, ): SendMessage { + // handle sending a message and opening it const { sendMessage } = useWebSocket(WEBSOCKET_URL, { onOpen: () => { console.log("Connection established"); @@ -50,6 +51,7 @@ export function useSocket( onOpen(); } }, + // handle what to do with the message onMessage: (msg: MessageEvent) => { const message = parseMessage(msg.data.toString()); console.log("Handle message: " + message.toJson()); @@ -60,6 +62,7 @@ export function useSocket( }, }); + // handle how a message is sent const sendMessageHandler = useMemo(() => { return (message: Message) => { console.log("Sending message: " + message.toJson()); @@ -81,6 +84,7 @@ export async function post( // eslint-disable-next-line @typescript-eslint/no-explicit-any ): Promise { try { + // run a post to the provided api path with the provided query let normalizedUrl = `/api${apiPath}`; if (query) { normalizedUrl += `?${new URLSearchParams(query)}`; diff --git a/src/client/chessboard/board-container.tsx b/src/client/chessboard/board-container.tsx index b298104a..f8120c77 100644 --- a/src/client/chessboard/board-container.tsx +++ b/src/client/chessboard/board-container.tsx @@ -9,6 +9,11 @@ interface BoardContainerProps extends PropsWithChildren { rotation: number; } +/** + * A container to deal with chessboard resizing + * @param props - width handler and inner elements + * @returns + */ export function BoardContainer(props: BoardContainerProps) { const [transform, setTransform] = useState(); @@ -20,6 +25,7 @@ export function BoardContainer(props: BoardContainerProps) { setTransform(transform); }; + // returns the resizable container return (
diff --git a/src/client/chessboard/board-transform.ts b/src/client/chessboard/board-transform.ts index 623b6f74..90279bff 100644 --- a/src/client/chessboard/board-transform.ts +++ b/src/client/chessboard/board-transform.ts @@ -17,7 +17,10 @@ export function computeChessboardTransform( const width = Math.min(canvasHeight, canvasWidth) * scale; const height = width; + // shifts the x and y const xShift = (canvasWidth - width) / 2; const yShift = (canvasHeight - height) / 2; + + // return the shift return { left: xShift, top: yShift, height, width }; } diff --git a/src/client/chessboard/chessboard-wrapper.tsx b/src/client/chessboard/chessboard-wrapper.tsx index 4e481c60..d09cd2c4 100644 --- a/src/client/chessboard/chessboard-wrapper.tsx +++ b/src/client/chessboard/chessboard-wrapper.tsx @@ -9,6 +9,9 @@ import { customSquareRenderer } from "./custom-square-renderer"; import { CustomSquareContext } from "./custom-square-context"; import { BoardOrientation } from "react-chessboard/dist/chessboard/types"; +/** + * an interface of relevant properties for chessboard + */ interface ChessboardWrapperProps { /** * The chess.js instance displayed by this class. @@ -28,6 +31,13 @@ interface ChessboardWrapperProps { onMove: (move: Move) => void; } +/** + * Creates a chessboard that uses our custom properties inside a board container + * + * These include width, moves, promotions, piece dragging, square highlighting, and + * @param props - chess(ChessEngine), side, onMove + * @returns JSX.Element chessboard + */ export function ChessboardWrapper(props: ChessboardWrapperProps): JSX.Element { const { chess, side, onMove } = props; @@ -40,6 +50,7 @@ export function ChessboardWrapper(props: ChessboardWrapperProps): JSX.Element { Square | undefined >(); + // promotion states const [isPromoting, setIsPromoting] = useState(false); const [manualPromotionSquare, setManualPromotionSquare] = useState< @@ -64,12 +75,17 @@ export function ChessboardWrapper(props: ChessboardWrapperProps): JSX.Element { ); }; + /** + * make to move passed in and unset lastClickedSquare + * + * @param move - the move made + */ const doMove = (move: Move): void => { onMove(move); setLastClickedSquare(undefined); }; - //set the side to their respective colors + //set the side to their respective colors and orientations switch (props.side) { case Side.WHITE: if (orientation !== "white") setOrientation("white"); @@ -95,9 +111,11 @@ export function ChessboardWrapper(props: ChessboardWrapperProps): JSX.Element { if (width !== undefined) { chessboard = ( { const promoting = chess.checkPromotion(from, to); setIsPromoting(promoting); @@ -105,6 +123,7 @@ export function ChessboardWrapper(props: ChessboardWrapperProps): JSX.Element { }} showPromotionDialog={manualPromotionSquare !== undefined} promotionToSquare={manualPromotionSquare} + // handle dragging and dropping pieces onPieceDrop={( from: Square, to: Square, @@ -130,17 +149,20 @@ export function ChessboardWrapper(props: ChessboardWrapperProps): JSX.Element { promotion: isPromoting ? piece : undefined, }); } + // Reset state setIsPromoting(false); return true; } return false; }} + // when you start dragging, unset clicked square onPieceDragBegin={(_, square: Square) => { if (square !== lastClickedSquare) { setLastClickedSquare(undefined); } }} + // handle square clicking onSquareClick={(square: Square) => { setManualPromotionSquare(undefined); @@ -149,11 +171,13 @@ export function ChessboardWrapper(props: ChessboardWrapperProps): JSX.Element { lastClickedSquare !== undefined && isLegalMove(lastClickedSquare, square); + // check if the square is legal if (isSquareLegalMove) { if (chess.checkPromotion(lastClickedSquare, square)) { // Manually show promotion dialog setManualPromotionSquare(square); } else { + // make the move normally doMove({ from: lastClickedSquare, to: square, @@ -178,6 +202,7 @@ export function ChessboardWrapper(props: ChessboardWrapperProps): JSX.Element { ); } + // return the created chessboard inside the board container return ( ); } else { - //Square is empty + // Square is empty selectElement = ; } } + // return all the highlights inside a div return (
{clickedPieceHighlight} diff --git a/src/client/chessboard/svg-components.tsx b/src/client/chessboard/svg-components.tsx index 1c7567f0..f0d3d30e 100644 --- a/src/client/chessboard/svg-components.tsx +++ b/src/client/chessboard/svg-components.tsx @@ -80,6 +80,9 @@ export function SquareHighlight(props: PieceSquareProps) { ); } +/** + * SVG of the piece clicked + */ export function ClickedPiece(props: PieceSquareProps) { return (
(); - const navigate = useNavigate(); + // helper functions + const navigate = useNavigate(); const sendMessage = useSocket(); + // get all the registered robots useEffect(() => { const fetchIds = async () => { const response = await get("/get-ids"); @@ -27,6 +29,7 @@ export function Debug() { fetchIds(); }, [setRobotIds]); + // create the select and move buttons let body: ReactNode; if (robotIds === undefined) { body = ; @@ -65,6 +68,7 @@ export function Debug() { ); } + // return the dialog with buttons for home and simulator return (