|
|
|
@ -19,24 +19,27 @@ app.use(cookieParser());
|
|
|
|
|
|
|
|
|
|
const secretKey = process.env.COOKIE_SECRET_KEY;
|
|
|
|
|
|
|
|
|
|
// #region routing and cookies
|
|
|
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
|
const token = req.cookies.authToken;
|
|
|
|
|
|
|
|
|
|
if(token) {
|
|
|
|
|
try {
|
|
|
|
|
jwt.verify(token, secretKey);
|
|
|
|
|
res.status(200)
|
|
|
|
|
res.sendFile(path.join(__dirname, '/public/pages/gameView.html'))
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
res.status(401)
|
|
|
|
|
res.sendFile(path.join(__dirname, '/public/pages/connectionView.html'))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
res.sendFile(path.join(__dirname, '/public/pages/connectionView.html'))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
app.get('/game', (req, res) => {
|
|
|
|
|
const token = req.cookies.authToken;
|
|
|
|
|
|
|
|
|
|
if (!token) {
|
|
|
|
|
res.sendFile(path.join(__dirname, '/public/pages/connectionView.html'))
|
|
|
|
|
} else {
|
|
|
|
|
res.sendFile(path.join(__dirname, '/public/pages/gameView.html'))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
app.post('/logIn', (req, res) => {
|
|
|
|
@ -120,6 +123,11 @@ app.get('/user-info', (req, res) => {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// #endregion routing and cookies
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// #region socket and game
|
|
|
|
|
|
|
|
|
|
let rooms = [];
|
|
|
|
|
let players = [];
|
|
|
|
|
|
|
|
|
@ -128,8 +136,9 @@ io.on("connection", (socket) => {
|
|
|
|
|
|
|
|
|
|
socket.on("disconnect", () => {
|
|
|
|
|
const index = players.findIndex((p) => p.id === socket.id)
|
|
|
|
|
// if there is no room this line creates error
|
|
|
|
|
const roomIndex = rooms.findIndex(room =>
|
|
|
|
|
room.players.some(player => player.id === socket.id)
|
|
|
|
|
room.players.some((player) => player.id === socket.id)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (roomIndex !== -1) {
|
|
|
|
@ -154,6 +163,7 @@ io.on("connection", (socket) => {
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
socket.on("first connection", (socketId) => {
|
|
|
|
|
// error my occurs if cookie is expired
|
|
|
|
|
const cookies = socket.request.headers.cookie;
|
|
|
|
|
const authToken = cookies.split('; ').find(cookie => cookie.startsWith('authToken=')).split('=')[1];
|
|
|
|
|
|
|
|
|
@ -256,6 +266,22 @@ io.on("connection", (socket) => {
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on("game ended", (roomId) => {
|
|
|
|
|
const room = rooms.find((r) => r.id === roomId)
|
|
|
|
|
const roomIndex = rooms.findIndex((r) => r.id === roomId)
|
|
|
|
|
|
|
|
|
|
room.players.forEach(player => {
|
|
|
|
|
player.resetGrid()
|
|
|
|
|
io.to(player.id).emit("go to menu")
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
room.players = []
|
|
|
|
|
rooms.slice(roomIndex, 1)
|
|
|
|
|
delete room
|
|
|
|
|
|
|
|
|
|
console.log("rooms list : ", rooms)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
socket.on("reset grid", (roomId) => {
|
|
|
|
|
const player = rooms.find((r) => r.id === roomId).players[0]
|
|
|
|
|
player.resetGrid();
|
|
|
|
@ -288,6 +314,9 @@ const sendMoveToPlayers = (moveData) => {
|
|
|
|
|
askToPlay(moveData.player)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// #endregion socket and game
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
http.listen(port, () => {
|
|
|
|
|
console.log(`Listening on http://localhost:${port}`);
|
|
|
|
|
});
|