Compare commits

..

No commits in common. "main" and "lobby-page-layout" have entirely different histories.

25 changed files with 212 additions and 417 deletions

View File

@ -14,7 +14,6 @@ export * from "./types/PlacedPiece";
export * from "./BoardBuilder"; export * from "./BoardBuilder";
export * from "./server-events/ServerError"; export * from "./server-events/ServerError";
export * from "./server-events/ServerEvent"; export * from "./server-events/ServerEvent";
export * from "./server-events/StartRoundEvent";
export * from "./server-events/UpdateLobbyEvent"; export * from "./server-events/UpdateLobbyEvent";
export * from "./client-events/ClientEvent"; export * from "./client-events/ClientEvent";
export * from "./client-events/CreateLobbyEvent"; export * from "./client-events/CreateLobbyEvent";

View File

@ -1,22 +0,0 @@
import { Socket as ServerSocket } from "socket.io";
import { Socket as ClientSocket } from "socket.io-client";
import { ServerEvent } from "./ServerEvent";
import { PieceId } from "../constants/Pieces";
export type StartRoundEvent = {
pieceIds: PieceId[];
};
export const emitStartRoundEvent = (
socket: ServerSocket,
payload: StartRoundEvent,
) => {
socket.emit(ServerEvent.START_ROUND, payload);
};
export function attachHandlerToStartRoundEvent(
socket: ClientSocket,
handler: (event: StartRoundEvent) => void,
): void {
socket.once(ServerEvent.START_ROUND, handler);
}

View File

@ -1,5 +1,4 @@
import { Socket as ServerSocket } from "socket.io"; import { Socket } from "socket.io-client";
import { Socket as ClientSocket } from "socket.io-client";
import { ServerEvent } from "./ServerEvent"; import { ServerEvent } from "./ServerEvent";
export type UpdateLobbyEvent = { export type UpdateLobbyEvent = {
@ -7,15 +6,8 @@ export type UpdateLobbyEvent = {
gameCode: string; gameCode: string;
}; };
export const emitUpdateLobbyEvent = (
socket: ServerSocket,
payload: UpdateLobbyEvent,
) => {
socket.emit(ServerEvent.LOBBY_UPDATE, payload);
};
export function attachHandlerToUpdateLobbyEvent( export function attachHandlerToUpdateLobbyEvent(
socket: ClientSocket, socket: Socket,
handler: (event: UpdateLobbyEvent) => void, handler: (event: UpdateLobbyEvent) => void,
): void { ): void {
socket.once(ServerEvent.LOBBY_UPDATE, handler); socket.once(ServerEvent.LOBBY_UPDATE, handler);

View File

@ -63,7 +63,7 @@ export class Cell {
trackType: track.type, trackType: track.type,
}; };
}) })
.reduce((isConnected, { trackExternalNodes, trackType }) => { .some(({ trackExternalNodes, trackType }) => {
let isTrackConnected: boolean = false; let isTrackConnected: boolean = false;
trackExternalNodes trackExternalNodes
.filter((node) => node.traverseBorder() instanceof Exit) .filter((node) => node.traverseBorder() instanceof Exit)
@ -98,15 +98,11 @@ export class Cell {
} }
}); });
return isConnected || isTrackConnected; return isTrackConnected;
}, false); });
if (!hasAnyConnection) { if (!hasAnyConnection) {
throw Error("No adjacent exit or piece available to connect to"); throw Error("No adjacent exit or piece available to connect to");
} }
} }
public removePiece() {
this.placedPiece = undefined;
}
} }

8
web/package-lock.json generated
View File

@ -13,7 +13,7 @@
"@testing-library/react": "^13.4.0", "@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0", "@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.5.2", "@types/jest": "^27.5.2",
"@types/node": "^17.0.29", "@types/node": "^16.18.119",
"@types/react": "^18.3.12", "@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1", "@types/react-dom": "^18.3.1",
"interface": "file:../interface", "interface": "file:../interface",
@ -4202,9 +4202,9 @@
"license": "MIT" "license": "MIT"
}, },
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "17.0.29", "version": "16.18.119",
"resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.29.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.119.tgz",
"integrity": "sha512-tx5jMmMFwx7wBwq/V7OohKDVb/JwJU5qCVkeLMh1//xycAJ/ESuw9aJ9SEtlCZDYi2pBfe4JkisSoAtbOsBNAA==", "integrity": "sha512-ia7V9a2FnhUFfetng4/sRPBMTwHZUkPFY736rb1cg9AgG7MZdR97q7/nLR9om+sq5f1la9C857E0l/nrI0RiFQ==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/@types/node-forge": { "node_modules/@types/node-forge": {

View File

@ -8,7 +8,7 @@
"@testing-library/react": "^13.4.0", "@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0", "@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.5.2", "@types/jest": "^27.5.2",
"@types/node": "^17.0.29", "@types/node": "^16.18.119",
"@types/react": "^18.3.12", "@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1", "@types/react-dom": "^18.3.1",
"interface": "file:../interface", "interface": "file:../interface",

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

View File

@ -3,11 +3,7 @@ import { Socket } from "socket.io-client";
import LandingPage from "./pages/landing/LandingPage"; import LandingPage from "./pages/landing/LandingPage";
import GamePage from "./pages/game/GamePage"; import GamePage from "./pages/game/GamePage";
import LobbyPage from "./pages/lobby/LobbyPage"; import LobbyPage from "./pages/lobby/LobbyPage";
import { import { attachHandlerToUpdateLobbyEvent } from "interface";
attachHandlerToStartRoundEvent,
attachHandlerToUpdateLobbyEvent,
PieceId,
} from "interface";
export interface AppProps { export interface AppProps {
socket: Socket; socket: Socket;
@ -17,9 +13,8 @@ const App = (props: AppProps) => {
const { socket } = props; const { socket } = props;
const [renderedPage, setRenderedPage] = useState("LANDING"); const [renderedPage, setRenderedPage] = useState("LANDING");
const [initialPlayerNames, setInitialPlayerNames] = useState([] as string[]); const [initialPlayerNames, setInitialPlayerNames] = useState([""]);
const [gameCode, setGameCode] = useState(""); const [gameCode, setGameCode] = useState("");
const [initialPieceIds, setInitialPieceIds] = useState([] as PieceId[]);
const setupNextPageTransition = useCallback(() => { const setupNextPageTransition = useCallback(() => {
if (renderedPage === "LANDING") { if (renderedPage === "LANDING") {
@ -28,15 +23,11 @@ const App = (props: AppProps) => {
setGameCode(event.gameCode); setGameCode(event.gameCode);
setRenderedPage("LOBBY"); setRenderedPage("LOBBY");
}); });
} else if (renderedPage === "LOBBY") {
attachHandlerToStartRoundEvent(socket, (event) => {
setInitialPieceIds(event.pieceIds);
setRenderedPage("GAME");
});
} }
}, [renderedPage]); }, [renderedPage]);
setupNextPageTransition(); setupNextPageTransition();
return ( return (
<div className="app"> <div className="app">
{renderedPage === "LANDING" && <LandingPage {...props} />} {renderedPage === "LANDING" && <LandingPage {...props} />}
@ -47,9 +38,7 @@ const App = (props: AppProps) => {
gameCode={gameCode} gameCode={gameCode}
/> />
)} )}
{renderedPage === "GAME" && ( {renderedPage === "GAME" && <GamePage />}
<GamePage initialPieceIds={initialPieceIds} />
)}
</div> </div>
); );
}; };

View File

@ -12,6 +12,5 @@ h1 {
.right-panel { .right-panel {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
padding: 50px;
} }
} }

View File

@ -2,129 +2,32 @@ import React, { useState } from "react";
import GameBoard from "./components/GameBoard"; import GameBoard from "./components/GameBoard";
import DiceSet from "./components/DiceSet"; import DiceSet from "./components/DiceSet";
import "./GamePage.scss"; import "./GamePage.scss";
import { buildBoard, PieceId } from "interface"; import { PieceId } from "interface";
import { DieViewProps } from "./types/DieViewProps";
import { PlacePieceActionStack } from "./types/PlacePieceActionStack";
export interface GamePageProps { const GamePage = () => {
initialPieceIds: PieceId[]; const getRandomPieceId = () => {
} const randomId = Math.floor(Math.random() * Object.keys(PieceId).length);
return Object.values(PieceId)[randomId];
const GamePage = (props: GamePageProps) => {
const { initialPieceIds } = props;
const [pieceIds] = useState(initialPieceIds);
const getDiceSet: () => DieViewProps[] = () =>
pieceIds.map((pieceId) => {
return {
pieceId: pieceId,
isSelected: false,
isDisabled: false,
isSpecial: false,
rotation: 0,
}; };
});
const [dice, setDice] = useState(getDiceSet()); const [dice, setDice] = useState(
const [specialDice, setSpecialDice] = useState( [1, 2, 3, 4].map(() => {
[
PieceId.P03,
PieceId.P08,
PieceId.P13,
PieceId.P14,
PieceId.P15,
PieceId.P19,
].map((pieceId) => {
return { return {
pieceId: pieceId, pieceId: getRandomPieceId(),
isSelected: false, isSelected: false,
isDisabled: false, isDisabled: false,
isSpecial: true,
rotation: 0, rotation: 0,
}; };
}), }),
); );
const [specialDieUsedInRound, setSpecialDieUsedInRound] = useState(false);
const modifyDieState = (
matcher: (die: DieViewProps) => boolean,
newStateComputer: (die: DieViewProps) => Partial<DieViewProps>,
) => {
setDice(
dice.map((die) => {
if (!matcher(die)) return die;
return {
...die,
...newStateComputer(die),
};
}),
);
setSpecialDice(
specialDice.map((die) => {
if (!matcher(die)) return die;
return {
...die,
...newStateComputer(die),
};
}),
);
};
const fetchDie = (matcher: (die: DieViewProps) => boolean) => {
return dice.concat(specialDice).find((die) => matcher(die));
};
const [board, setBoard] = useState(buildBoard());
const [id, setId] = useState(1);
const refreshBoardRender = () => {
setBoard(board);
setId(id + 1);
};
const [placePieceActionStack, setPlacePieceActionStack] = useState(
new PlacePieceActionStack(),
);
const resetBoard = () => {
placePieceActionStack.resetActions(board);
setBoard(board);
modifyDieState(
() => true,
() => {
return { isSelected: false, isDisabled: false };
},
);
setId(id + 1);
};
const commitBoard = () => {
if (dice.some((die) => !die.isDisabled)) return;
placePieceActionStack.commitActions();
setDice(getDiceSet());
setSpecialDieUsedInRound(false);
};
return ( return (
<React.Fragment> <React.Fragment>
<h1>Game Page Title</h1> <h1>Game Page Title</h1>
<div className="game-panel" id={id.toString()}> <div className="game-panel">
<GameBoard <GameBoard dice={dice} setDice={setDice} />
modifyDieState={modifyDieState} <div className="rigth-panel">
fetchDie={fetchDie} <DiceSet dice={dice} setDice={setDice} />
setSpecialDieUsedInRound={setSpecialDieUsedInRound}
refreshBoardRender={refreshBoardRender}
board={board}
placePieceActionStack={placePieceActionStack}
setPlacePieceActionStack={setPlacePieceActionStack}
/>
<div className="right-panel">
<DiceSet
dice={dice}
specialDice={specialDice}
modifyDieState={modifyDieState}
specialDieUsedInRound={specialDieUsedInRound}
resetBoard={resetBoard}
commitBoard={commitBoard}
/>
</div> </div>
</div> </div>
</React.Fragment> </React.Fragment>

View File

@ -3,12 +3,18 @@
height: 100px; height: 100px;
border: 2px solid black; border: 2px solid black;
border-radius: 10%; border-radius: 10%;
position: relative;
.piece { .piece {
border-radius: 10%; border-radius: 10%;
position: absolute; position: relative;
top: 0;
&:nth-child(2) {
top: -20px;
}
&:nth-child(3) {
top: -40px;
}
&.rotate-90 { &.rotate-90 {
transform: rotate(-90deg); transform: rotate(-90deg);
@ -23,23 +29,19 @@
} }
} }
.cell-type-icon { &.house {
width: 25px; background-color: blue;
height: 25px;
padding: 2px;
margin: 2px;
position: absolute;
top: 0;
left: 0;
} }
}
.cell .exit { &.university {
position: absolute; background-color: orange;
width: 20px; }
height: 20px;
&:has(.exit-road) { &.factory {
background-color: grey;
}
.exit:has(.exit-road) {
border: solid black; border: solid black;
border-width: 0 1px; border-width: 0 1px;
@ -53,7 +55,21 @@
} }
} }
&:has(.exit-rail) { .exit:has(.exit-road) {
border: solid black;
border-width: 0 1px;
.exit-road {
margin: auto;
width: 0;
height: 100%;
border: dashed black;
border-width: 0 1px;
transform: translate(calc(50% - 1px));
}
}
.exit:has(.exit-rail) {
border-width: 0 1px; border-width: 0 1px;
.exit-rail { .exit-rail {
@ -66,7 +82,7 @@
} }
} }
&:has(.exit-ambivalent) { .exit:has(.exit-ambivalent) {
border-width: 0 1px; border-width: 0 1px;
border-style: dotted; border-style: dotted;
@ -79,30 +95,47 @@
transform: translate(calc(50%)); transform: translate(calc(50%));
} }
} }
&:has(.exit-north), &:has(.exit-south) { }
left: 50%;
} .exit {
position: relative;
width: 20px;
height: 20px;
&:has(.exit-north) { &:has(.exit-north) {
left: 50%;
transform: translate(calc(-50% - 1px), -100%); transform: translate(calc(-50% - 1px), -100%);
} }
&:has(.exit-south) { &:has(.exit-south) {
left: 50%;
top: 100%; top: 100%;
transform: translate(calc(-50% - 1px), 0%); transform: translate(calc(-50% - 1px), 0%);
} }
&:has(.exit-east), &:has(.exit-west) {
top: 50%;
}
&:has(.exit-east) { &:has(.exit-east) {
left: 100%; left: 100%;
top: 50%;
transform: rotate(90deg) translate(-50%, 0); transform: rotate(90deg) translate(-50%, 0);
} }
&:has(.exit-west) { &:has(.exit-west) {
left: 0%; left: 0%;
top: 50%;
transform: rotate(90deg) translate(-50%, 100%); transform: rotate(90deg) translate(-50%, 100%);
} }
&:has(.exit-north), &:has(.exit-south) {
+ .exit:has(.exit-east) {
left: 100%;
transform: rotate(90deg) translate(-150%, 0);
}
}
&:has(.exit-north), &:has(.exit-south) {
+ .exit:has(.exit-west) {
top: 50%;
transform: rotate(90deg) translate(-150%, 100%);
}
}
} }

View File

@ -1,58 +1,63 @@
import { Cell, CellType, directions, Exit, PieceId } from "interface"; import { Cell, directions, Exit, PieceId } from "interface";
import "./BoardCell.scss"; import "./BoardCell.scss";
import { useState } from "react"; import { useState } from "react";
import { DieViewProps } from "../types/DieViewProps";
export interface BoardCellProps { export interface BoardCellProps {
cell: Cell; cell: Cell;
dice: {
pieceId: PieceId;
isSelected: boolean;
isDisabled: boolean;
rotation: number;
}[];
setDice: React.Dispatch<
React.SetStateAction<
{
pieceId: PieceId;
isSelected: boolean;
isDisabled: boolean;
rotation: number;
}[]
>
>;
refreshBoardRender: () => void; refreshBoardRender: () => void;
modifyDieState: (
matcher: (die: DieViewProps) => boolean,
newStateComputer: (die: DieViewProps) => Partial<DieViewProps>,
) => void;
fetchDie: (
matcher: (die: DieViewProps) => boolean,
) => DieViewProps | undefined;
setSpecialDieUsedInRound: React.Dispatch<React.SetStateAction<boolean>>;
placePieceActionHandler: (pieceId: PieceId, rotation: number) => void;
} }
const BoardCell = (props: BoardCellProps) => { const BoardCell = (props: BoardCellProps) => {
const { const { cell, dice, setDice, refreshBoardRender } = props;
cell,
refreshBoardRender,
modifyDieState,
fetchDie,
setSpecialDieUsedInRound,
placePieceActionHandler,
} = props;
const [pieceRotationAngle, setPieceRotationAngle] = useState(0); const [pieceRotationAngle, setPieceRotationAngle] = useState(0);
const handleBoardCellClick = () => { const handleBoardCellClick = () => {
const selectedDie = fetchDie((die) => die.isSelected); const selectedDie = dice.find((die) => die.isSelected);
if (!selectedDie) return; if (!selectedDie) return;
try { try {
placePieceActionHandler(selectedDie.pieceId, selectedDie.rotation); cell.placePiece(
selectedDie.pieceId,
selectedDie.rotation as 0 | 90 | 180 | 270,
);
} catch (error) { } catch (error) {
console.log(error); console.log(error);
return; return;
} }
modifyDieState( setDice(
(die) => die === selectedDie, dice.map((die) => {
() => { return die !== selectedDie
return { ? die
: {
...selectedDie,
isSelected: false, isSelected: false,
isDisabled: true, isDisabled: true,
}; };
}, }),
); );
if (selectedDie.isSpecial) setSpecialDieUsedInRound(true);
// Set rotation to the piece in the board, not the die
setPieceRotationAngle(selectedDie.rotation); setPieceRotationAngle(selectedDie.rotation);
refreshBoardRender(); refreshBoardRender();
}; };
return ( return (
<div className={"cell"} onClick={handleBoardCellClick}> <div
className={"cell " + cell.cellType.toLowerCase()}
onClick={handleBoardCellClick}
>
{directions.map((direction) => { {directions.map((direction) => {
const traversedNode = cell.getNodeAt(direction).traverseBorder(); const traversedNode = cell.getNodeAt(direction).traverseBorder();
const isExit = traversedNode instanceof Exit; const isExit = traversedNode instanceof Exit;
@ -72,12 +77,6 @@ const BoardCell = (props: BoardCellProps) => {
src={`pieces/${cell.placedPiece.id}.jpeg`} src={`pieces/${cell.placedPiece.id}.jpeg`}
></img> ></img>
)} )}
{cell.cellType !== CellType.NORMAL && (
<img
className="cell-type-icon"
src={`./${cell.cellType.toLowerCase()}-icon.png`}
></img>
)}
</div> </div>
); );
}; };

View File

@ -1,25 +1,22 @@
.dice-set-actions { .dice-set-actions {
display: flex; display: flex;
flex-direction: row; flex-direction: row;
padding: 50px 50px 0;
width: max-content; width: max-content;
margin: auto auto 50px auto; margin: auto;
img { img {
border: 3px solid green; border: 3px solid green;
box-shadow: 0 0 10px green; box-shadow: 0 0 10px green;
border-radius: 20%; border-radius: 20%;
padding: 5px; padding: 5px;
width: 70px; width: 80px;
height: 70px; height: 80px;
margin-right: 10px; margin-right: 10px;
&.icon-inverted { &.icon-inverted {
transform: scaleX(-1); transform: scaleX(-1);
} }
&:last-child {
margin-right: 0;
}
} }
} }
@ -27,14 +24,5 @@
display: grid; display: grid;
grid-template-columns: repeat(2, auto); grid-template-columns: repeat(2, auto);
gap: 20px; gap: 20px;
margin: 0 0 50px 0; padding: 50px;
}
.special-dice-set {
display: grid;
grid-template-columns: repeat(3, auto);
margin: 0 0 50px 0;
column-gap: 12%;
row-gap: 20px;
width: 100%;
} }

View File

@ -1,51 +1,55 @@
import { PieceId } from "interface";
import "./DiceSet.scss"; import "./DiceSet.scss";
import Die from "./Die"; import Die from "./Die";
import React from "react"; import React from "react";
import { DieViewProps } from "../types/DieViewProps";
export interface DiceSetProps { export interface DiceSetProps {
dice: DieViewProps[]; dice: {
specialDice: DieViewProps[]; pieceId: PieceId;
modifyDieState: ( isSelected: boolean;
matcher: (die: DieViewProps) => boolean, isDisabled: boolean;
newStateComputer: (die: DieViewProps) => Partial<DieViewProps>, rotation: number;
) => void; }[];
specialDieUsedInRound: boolean; setDice: React.Dispatch<
resetBoard: () => void; React.SetStateAction<
commitBoard: () => void; {
pieceId: PieceId;
isSelected: boolean;
isDisabled: boolean;
rotation: number;
}[]
>
>;
} }
const DiceSet = (props: DiceSetProps) => { const DiceSet = (props: DiceSetProps) => {
const { const { dice, setDice } = props;
dice, const handleDieClick = (die: {
specialDice, pieceId: PieceId;
modifyDieState, isSelected: boolean;
specialDieUsedInRound, isDisabled: boolean;
resetBoard, rotation: number;
commitBoard, }) => {
} = props; if (die.isDisabled) return;
const handleDieClick = (clickedDie: DieViewProps) => { const newDiceState = dice.map((oldDie) => {
if (clickedDie.isDisabled) return; const isSelected = die === oldDie;
const isSpecialDie = clickedDie.isSpecial;
if (isSpecialDie && specialDieUsedInRound) return;
modifyDieState(
() => true,
(die) => {
return { return {
isSelected: die === clickedDie, ...oldDie,
isSelected: isSelected,
}; };
}, });
); setDice(newDiceState);
}; };
const handleRotateButton = (rotation: number) => { const handleRotateButton = (rotation: number) => {
modifyDieState( const newDiceState = dice.map((die) => {
(die) => die.isSelected, if (!die.isSelected) return die;
(die) => { const rotationAngle = (die.rotation + rotation + 360) % 360;
return { return {
rotation: (die.rotation + rotation + 360) % 360, ...die,
rotation: rotationAngle,
}; };
}, });
); setDice(newDiceState);
}; };
return ( return (
@ -60,19 +64,12 @@ const DiceSet = (props: DiceSetProps) => {
className="icon-inverted" className="icon-inverted"
onClick={() => handleRotateButton(90)} onClick={() => handleRotateButton(90)}
></img> ></img>
<img src="./clean-icon.png" onClick={resetBoard}></img>
<img src="./commit-icon.png" onClick={commitBoard}></img>
</div> </div>
<div className="dice-set"> <div className="dice-set">
{dice.map((die) => ( {dice.map((die) => (
<Die die={die} handleDieClick={handleDieClick} /> <Die die={die} handleDieClick={handleDieClick} />
))} ))}
</div> </div>
<div className="special-dice-set">
{specialDice.map((die) => (
<Die die={die} handleDieClick={handleDieClick} />
))}
</div>
</React.Fragment> </React.Fragment>
); );
}; };

View File

@ -30,8 +30,3 @@
transition: transform 0.5s cubic-bezier(.47,1.64,.41,.8); transition: transform 0.5s cubic-bezier(.47,1.64,.41,.8);
} }
.special-dice-set .dice {
height: 80px;
width: 80px;
}

View File

@ -1,9 +1,19 @@
import { PieceId } from "interface";
import "./Die.scss"; import "./Die.scss";
import { DieViewProps } from "../types/DieViewProps";
interface DieProps { interface DieProps {
die: DieViewProps; die: {
handleDieClick: (die: DieViewProps) => void; pieceId: PieceId;
isSelected: boolean;
isDisabled: boolean;
rotation: number;
};
handleDieClick: (die: {
pieceId: PieceId;
isSelected: boolean;
isDisabled: boolean;
rotation: number;
}) => void;
} }
const Die = (props: DieProps) => { const Die = (props: DieProps) => {

View File

@ -1,44 +1,43 @@
import { Cell } from "interface"; import { buildBoard, PieceId } from "interface";
import "./GameBoard.scss"; import "./GameBoard.scss";
import { useState } from "react";
import BoardCell from "./BoardCell"; import BoardCell from "./BoardCell";
import { DieViewProps } from "../types/DieViewProps";
import { PlacePieceActionStack } from "../types/PlacePieceActionStack";
import { PlacePieceAction } from "../types/PlacePieceAction";
export interface GameBoardProps { export interface GameBoardProps {
modifyDieState: ( dice: {
matcher: (die: DieViewProps) => boolean, pieceId: PieceId;
newStateComputer: (die: DieViewProps) => Partial<DieViewProps>, isSelected: boolean;
) => void; isDisabled: boolean;
fetchDie: ( rotation: number;
matcher: (die: DieViewProps) => boolean, }[];
) => DieViewProps | undefined; setDice: React.Dispatch<
setSpecialDieUsedInRound: React.Dispatch<React.SetStateAction<boolean>>; React.SetStateAction<
refreshBoardRender: () => void; {
board: Cell[][]; pieceId: PieceId;
placePieceActionStack: PlacePieceActionStack; isSelected: boolean;
setPlacePieceActionStack: React.Dispatch< isDisabled: boolean;
React.SetStateAction<PlacePieceActionStack> rotation: number;
}[]
>
>; >;
} }
const GameBoard = (props: GameBoardProps) => { const GameBoard = (props: GameBoardProps) => {
const { board, placePieceActionStack, setPlacePieceActionStack } = props; const [board, setBoard] = useState(buildBoard());
const [id, setId] = useState(1);
const refreshBoardRender = () => {
setBoard(board);
setId(id + 1);
};
return ( return (
<div className="game-board"> <div className="game-board" id={id.toString()}>
{board.flatMap((row, rowIndex) => {board.flatMap((row) =>
row.map((cell, colIndex) => ( row.map((cell) => (
<BoardCell <BoardCell
{...props} {...props}
cell={cell} cell={cell}
placePieceActionHandler={(pieceId, rotation) => { refreshBoardRender={refreshBoardRender}
placePieceActionStack.executeAction(
new PlacePieceAction(pieceId, rotation, rowIndex, colIndex),
board,
);
setPlacePieceActionStack(placePieceActionStack);
}}
/> />
)), )),
)} )}

View File

@ -1,9 +0,0 @@
import { PieceId } from "interface";
export interface DieViewProps {
pieceId: PieceId;
isSelected: boolean;
isDisabled: boolean;
readonly isSpecial: boolean;
rotation: number;
}

View File

@ -1,32 +0,0 @@
import { Cell, PieceId } from "interface";
export class PlacePieceAction {
pieceId: PieceId;
rotation: number;
cell: {
row: number;
col: number;
};
constructor(pieceId: PieceId, rotation: number, row: number, col: number) {
this.pieceId = pieceId;
this.rotation = rotation;
this.cell = {
row: row,
col: col,
};
}
do(board: Cell[][]) {
const cell = board[this.cell.row][this.cell.col];
cell.placePiece(this.pieceId, this.rotation as 0 | 90 | 180 | 270);
}
undo(board: Cell[][]) {
const cell = board[this.cell.row][this.cell.col];
if (!cell.placedPiece || cell.placedPiece.id !== this.pieceId) {
throw Error("Un-doing action error");
}
cell.removePiece();
}
}

View File

@ -1,40 +0,0 @@
import { Cell } from "interface";
import { PlacePieceAction } from "./PlacePieceAction";
export class PlacePieceActionStack {
readonly committedActions: PlacePieceAction[] = [];
inProgressActions: PlacePieceAction[] = [];
executeAction(action: PlacePieceAction, board: Cell[][]) {
action.do(board);
this.inProgressActions.push(action);
}
undoLastAction(board: Cell[][]) {
const lastAction = this.inProgressActions.pop();
if (!lastAction) {
throw Error("No in progress action to undo");
}
lastAction.undo(board);
}
resetActions(board: Cell[][]) {
try {
while (true) {
this.undoLastAction(board);
}
} catch (error) {
if (
!(error instanceof Error) ||
error.message !== "No in progress action to undo"
) {
throw error;
}
}
}
commitActions() {
this.committedActions.push(...this.inProgressActions);
this.inProgressActions = [];
}
}

View File

@ -1,7 +1,7 @@
import { useState } from "react"; import { useState } from "react";
import "./LobbyPage.scss"; import "./LobbyPage.scss";
import { Socket } from "socket.io-client"; import { Socket } from "socket.io-client";
import { attachHandlerToUpdateLobbyEvent, ClientEvent } from "interface"; import { attachHandlerToUpdateLobbyEvent } from "interface";
export interface LobbyPageProps { export interface LobbyPageProps {
initialPlayerNames: string[]; initialPlayerNames: string[];
@ -16,7 +16,6 @@ const LobbyPage = (props: LobbyPageProps) => {
attachHandlerToUpdateLobbyEvent(socket, (event) => { attachHandlerToUpdateLobbyEvent(socket, (event) => {
setPlayerNames(event.playerNames); setPlayerNames(event.playerNames);
}); });
const startGame = () => socket.emit(ClientEvent.START_GAME);
return ( return (
<div className="lobby-page"> <div className="lobby-page">
@ -28,7 +27,7 @@ const LobbyPage = (props: LobbyPageProps) => {
<article className="lobby-user-name">{name}</article> <article className="lobby-user-name">{name}</article>
))} ))}
<div className="start-game-button"> <div className="start-game-button">
<button onClick={startGame}>Start Game</button> <button>Start Game</button>
</div> </div>
<article className="game-code">Game code: {gameCode}</article> <article className="game-code">Game code: {gameCode}</article>
</div> </div>