-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
101 lines (86 loc) · 2.33 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import "./App.css";
import { useState, useEffect } from "react";
import io from "socket.io-client";
import Board from "./components/board";
// ! ! ! ! IMPORTANT ! ! ! !
// Change the host here
const HOST = "192.168.1.105";
// Change the host here
// ! ! ! ! IMPORTANT ! ! ! !
const socket = io.connect(`http://${HOST}:3001`);
function App() {
// Setting up the state
const [turn, setTurn] = useState("X");
const [winner, setWinner] = useState(false);
const [player, setPlayer] = useState("X");
const [table, setTable] = useState([
null,
null,
null,
null,
null,
null,
null,
null,
null,
]);
const [error, setError] = useState(false);
const [message, setMessage] = useState();
// Handling each turn by emitting a "turn" event - sending the server a copy of
// the current table (game board) and the player who did the last move
const handleTurn = (e) => {
e.preventDefault();
const index = e.currentTarget.id;
if (table[index]) {
alert("[ERROR]: a player already used this spot");
} else {
const tableCopy = [...table];
tableCopy[index] = player;
socket.emit("turn", { table: tableCopy, player });
}
};
// Real-time changes of the interface
useEffect(() => {
// Reset the UI when the game starts
socket.on("newConnection", (payload) => {
setTable(payload.table);
setPlayer(payload.player);
setError(payload.error);
setWinner(false);
setTurn("X");
});
// Dispaly an error message (if there's an error)
socket.on("err", (payload) => {
setError(payload.error);
setMessage(payload.message);
});
// Update the table and the player who's turn is after each turn
socket.on("turn", (payload) => {
setTable(payload.table);
payload.player === "X" ? setTurn("O") : setTurn("X");
});
// Display a win / tie message
socket.on("win", (payload) => {
setWinner(payload);
});
});
return (
<div className="App">
<header className="App-header">
<h1>Tic Tac Toe</h1>
{error ? (
<p>{message}</p>
) : (
<Board
onTurn={handleTurn}
table={table}
player={player}
winner={winner}
turn={turn}
/>
)}
</header>
</div>
);
}
export default App;