Skip to content

Commit 345d4c5

Browse files
committed
implement backgammon
1 parent f0330f4 commit 345d4c5

File tree

11 files changed

+124
-37
lines changed

11 files changed

+124
-37
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/Components/Backgammon

.eslintrc

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@
2727
"react/no-unknown-property": "off",
2828
"no-unused-expressions": "off",
2929
"react/jsx-props-no-spreading": "off",
30-
"consistent-return": "off"
30+
"consistent-return": "off",
31+
"no-lonely-if": "off",
32+
"no-restricted-syntax": "off",
33+
"prefer-destructuring": "off",
34+
"no-multi-assign": "off",
35+
"class-methods-use-this": "off",
36+
"react/no-access-state-in-setstate": "off",
37+
"array-callback-return": "off"
3138
},
3239
"env": {
3340
"browser": true

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/Components/Backgammon

package-lock.json

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"react-merge-refs": "^2.0.1",
2727
"react-router-dom": "^6.9.0",
2828
"react-scripts": "^5.0.1",
29+
"react-serialize": "^0.2.1",
2930
"react-spring": "^9.7.1",
3031
"three": "^0.150.1",
3132
"vite": "^4.2.0",

src/Components/Backgammon/components/Menu/Menu.jsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Menu extends Component {
1616
footer: content.footer,
1717
menuWidth: content.width,
1818
canClose: content.canClose,
19-
playerNames: { p1: this.props.players.p1, p2: this.props.players.p2 },
19+
playerNames: { p1: this.props.players.p1.name, p2: this.props.players.p2.name },
2020
playerStarts: 1,
2121
}
2222
}
@@ -68,7 +68,7 @@ class Menu extends Component {
6868

6969
content.body = <React.Fragment>
7070
<img src="./assets/congratulation.png" alt="congratulation" />
71-
<p className="modal-body-centralized">{gameStatus === 60 ? players.p1 : players.p2} wins!</p>
71+
<p className="modal-body-centralized">{gameStatus === 60 ? players.p1.name : players.p2.name} wins!</p>
7272
</React.Fragment>
7373

7474
content.footer = this.getRegularFooter();
@@ -96,6 +96,7 @@ class Menu extends Component {
9696

9797
//Get a new game
9898
getNewGame = (gameStatus, players, playerStarts) => {
99+
console.log('players: ', players);
99100
const content = {};
100101

101102
const p1Starts = playerStarts === 1 ? 'btn-info' : 'btn-default';
@@ -112,7 +113,8 @@ class Menu extends Component {
112113
<input type="text"
113114
className="form-control modal-body-t2"
114115
value={players.p1}
115-
onChange={this.changePlayerName.bind(this, 1)}
116+
// onChange={this.changePlayerName.bind(this, 1)}
117+
disabled
116118
/>
117119
<div className="input-group-btn">
118120
<button className={'btn ' + p1Starts}
@@ -126,7 +128,8 @@ class Menu extends Component {
126128
<input type="text"
127129
className="form-control modal-body-t2"
128130
value={players.p2}
129-
onChange={this.changePlayerName.bind(this, 2)}
131+
// onChange={this.changePlayerName.bind(this, 2)}
132+
disabled
130133
/>
131134
<div className="input-group-btn">
132135
<button className={'btn ' + p2Starts}

src/Components/Backgammon/components/Status/Status.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const Status = (props) => {
4545
<div id="game-score">
4646
<div className="player-score-container">
4747
<div className="player-score-name">
48-
<p>{props.players.p1}</p>
48+
<p>{props.players.p1.name}</p>
4949
</div>
5050
<div className="player-score-checker">
5151
<Checker player={1} count={1} />
@@ -56,7 +56,7 @@ const Status = (props) => {
5656
</div>
5757
<div className="player-score-container">
5858
<div className="player-score-name">
59-
<p>{props.players.p2}</p>
59+
<p>{props.players.p2.name}</p>
6060
</div>
6161
<div className="player-score-checker">
6262
<Checker player={2} count={1} />

src/Components/Backgammon/containers/App.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212

1313
#viewbox {
14-
width: 1200px;
14+
width: 95%;
1515
}
1616

1717
#game {

src/Components/Backgammon/containers/App.jsx

+71-29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,68 @@ import Board from "../components/Board/Board";
77
import Status from "../components/Status/Status";
88
import Menu from "../components/Menu/Menu";
99
import _ from "lodash";
10+
import { useAuthStore } from "../../../hooks/useStore";
11+
import { useMap } from "@joebobmiles/y-react";
12+
13+
function withHooksAndPlayers(Component) {
14+
return function WrappedComponent(props) {
15+
const [currentUser] = useAuthStore((state) => [state.currentUser]);
16+
const ymap = useMap("tictactoe-state");
17+
const state = ymap.get("game-state");
18+
const setState = (value) => ymap.set("game-state", value);
19+
let users = ymap.get("users");
20+
const setUsers = (value) => {
21+
ymap.set("users", value);
22+
};
23+
24+
if (users === undefined) {
25+
users = { p1: null, p2: null };
26+
setUsers(users);
27+
}
28+
if (!users.p1 || !users.p2) {
29+
return (
30+
<div>
31+
{users.p1 ? (
32+
<span>{users.p1.name}</span>
33+
) : (
34+
<button
35+
onClick={() => {
36+
if (users.p2?.id !== currentUser.id) {
37+
setUsers({ ...users, p1: currentUser });
38+
}
39+
}}
40+
>
41+
Play as Player One
42+
</button>
43+
)}{" "}
44+
VS{" "}
45+
<button
46+
onClick={() => {
47+
if (users.p1?.id !== currentUser.id) {
48+
setUsers({ ...users, p2: currentUser });
49+
}
50+
}}
51+
>
52+
Play as Player Two
53+
</button>
54+
</div>
55+
);
56+
}
57+
58+
return (<>
59+
<button onClick={()=>{
60+
setUsers({p1:null,p2:null})
61+
}}>Reset Players</button>
62+
<Component
63+
{...props}
64+
currentUser={currentUser}
65+
state={state}
66+
setState={setState}
67+
users={users}
68+
/></>
69+
);
70+
};
71+
}
1072

1173
class App extends Component {
1274
constructor(props) {
@@ -22,19 +84,14 @@ class App extends Component {
2284
grayBar: { checkersP1: 0, checkersP2: 0 },
2385
outSideBar: { checkersP1: 15, checkersP2: 15 },
2486
movingChecker: false,
25-
players: { p1: "Player 1", p2: "Player 2" },
87+
players: { p1: this.props.users.p1, p2: this.props.users.p2 },
2688
showMenu: true,
2789
};
28-
const handleData = (data) => {
29-
if (data.type === "backgammon") {
30-
console.log("backgammon: ", data);
31-
this.setState(data.data);
32-
}
33-
};
34-
props.setDataListeners((prevDataListeners) => [
35-
...prevDataListeners,
36-
handleData,
37-
]);
90+
}
91+
componentDidUpdate(prevProps) {
92+
if (this.props.state !== prevProps.state) {
93+
this.setState(this.props.state);
94+
}
3895
}
3996

4097
//Toggle menu
@@ -46,16 +103,7 @@ class App extends Component {
46103

47104
setSharedState = (newState) => {
48105
this.setState(newState);
49-
function customizer(value) {
50-
if (_.isFunction(value)) {
51-
return false;
52-
}
53-
}
54-
const withoutFunc = _.cloneDeepWith(newState, customizer);
55-
this.props.sendData({
56-
type: "backgammon",
57-
data: withoutFunc,
58-
});
106+
this.props.setState(newState);
59107
};
60108

61109
//set up new game
@@ -71,11 +119,6 @@ class App extends Component {
71119
const movingChecker = false;
72120
const showMenu = false;
73121

74-
const players = {
75-
p1: this.props.id,
76-
p2: this.props.players.find((p) => p.id !== this.props.id).id,
77-
};
78-
79122
history.push(this.setHistory(p1IsNext, dice, points, grayBar, outSideBar));
80123

81124
//set points
@@ -100,7 +143,6 @@ class App extends Component {
100143
outSideBar: outSideBar,
101144
movingChecker: movingChecker,
102145
showMenu: showMenu,
103-
players: players,
104146
});
105147
};
106148

@@ -662,7 +704,7 @@ class App extends Component {
662704
dice={this.state.dice}
663705
points={this.state.points}
664706
p1IsNext={this.state.p1IsNext}
665-
isP1={this.state.players.p1 === this.props.id}
707+
isP1={this.state.players.p1.id === this.props.currentUser.id}
666708
gameStatus={this.state.gameStatus}
667709
>
668710
<Graybar checkers={this.state.grayBar} />
@@ -680,4 +722,4 @@ class App extends Component {
680722
}
681723
}
682724

683-
export default App;
725+
export default withHooksAndPlayers(App);

src/Components/Credits.jsx

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ import { useLocation, useMatch } from "react-router-dom";
33
function Credits() {
44
const isTicTacToe = useMatch("/:roomId/tictactoe");
55
const isMinecraft = useMatch("/:roomId/minecraft");
6+
const isBackgammon = useMatch("/:roomId/backgammon");
7+
if (isBackgammon) {
8+
return (
9+
<div style={{ flexDirection: "column" }}>
10+
<div>Credits for this example:</div>
11+
<a
12+
href="https://github.com/bnunesc/react-backgammon"
13+
target="_blank"
14+
rel="noreferrer"
15+
>
16+
Bruno Nunes
17+
</a>
18+
</div>
19+
);
20+
}
621
if (isTicTacToe) {
722
return (
823
<div style={{ flexDirection: "column" }}>

src/main.jsx

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { RequireAuth } from "./routes/RequireAuth";
1616
import TicTacToe from "./Components/TicTacToe";
1717
import Minecraft from "./Components/Minecraft/App";
1818
import Chat from "./Components/Chat";
19+
import Backgammon from "./Components/Backgammon/containers/App";
1920

2021
const router = createBrowserRouter(
2122
createRoutesFromElements(
@@ -35,6 +36,7 @@ const router = createBrowserRouter(
3536
<Route path="tictactoe" element={<TicTacToe />} />
3637
<Route path="minecraft" element={<Minecraft />} />
3738
<Route path="chat" element={<Chat />} />
39+
<Route path="backgammon" element={<Backgammon />} />
3840
</Route>
3941
</Route>
4042
</Route>

0 commit comments

Comments
 (0)