room creation and join logic improved
This commit is contained in:
Vendored
-21
@@ -1,21 +0,0 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"*.tcc": "cpp",
|
||||
"cctype": "cpp",
|
||||
"clocale": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"cwctype": "cpp",
|
||||
"exception": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"iostream": "cpp",
|
||||
"istream": "cpp",
|
||||
"ostream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"system_error": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"typeinfo": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
node_modules
|
||||
_test_
|
||||
.env
|
||||
|
||||
+28
-8
@@ -3,6 +3,7 @@ const bodyParser = require("body-parser");
|
||||
const cors = require("cors");
|
||||
const authRoutes = require("./routes/auth");
|
||||
const userRoutes = require("./routes/user");
|
||||
const roomRoutes = require('./routes/room')
|
||||
const mongoose = require("mongoose");
|
||||
require("dotenv").config();
|
||||
|
||||
@@ -25,13 +26,22 @@ const pendingChallenges = new Map();
|
||||
io.on("connection", (socket) => {
|
||||
console.log("Client connected:", socket.id);
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
console.log("Disconnecting");
|
||||
});
|
||||
|
||||
// Handle join event
|
||||
socket.on("join-room", async (roomID, playerUsername, challengedPlayerUsername) => {
|
||||
socket.on("join-room", async (roomID, playerUsername, challengedPlayerUsername, gameData) => {
|
||||
// room exists
|
||||
if (activeRooms.has(roomID)) {
|
||||
socket.emit("room-full");
|
||||
return;
|
||||
} else if (pendingChallenges.has(roomID)) {
|
||||
console.log(roomID, playerUsername, challengedPlayerUsername);
|
||||
console.log("activeRooms", activeRooms);
|
||||
console.log("pendingChallenges", pendingChallenges);
|
||||
// if (activeRooms.has(roomID)) {
|
||||
// socket.emit("room-full");
|
||||
// console.log('Room full');
|
||||
// return;
|
||||
// } else
|
||||
if (pendingChallenges.has(roomID)) {
|
||||
const challenge = pendingChallenges.get(roomID);
|
||||
pendingChallenges.delete(roomID);
|
||||
let newRoom = {
|
||||
@@ -47,8 +57,14 @@ io.on("connection", (socket) => {
|
||||
},
|
||||
},
|
||||
};
|
||||
console.log("New room created", roomID);
|
||||
socket.join(roomID);
|
||||
activeRooms.set(roomID, newRoom);
|
||||
io.to(roomID).emit("room-created");
|
||||
socket.emit("joined-room", {
|
||||
color: challenge.challengerColor === "w" ? "b" : "w",
|
||||
timeLimit: challenge.timeLimit,
|
||||
});
|
||||
// io.to(roomID).emit("joined-room");
|
||||
} else {
|
||||
// no room on pending challenges found
|
||||
const challenge = {
|
||||
@@ -56,6 +72,8 @@ io.on("connection", (socket) => {
|
||||
challengerID: socket.id,
|
||||
challengerUsername: playerUsername,
|
||||
challengedUsername: challengedPlayerUsername,
|
||||
challengerColor: gameData.color,
|
||||
timeLimit: gameData.timeLimit,
|
||||
};
|
||||
pendingChallenges.set(roomID, challenge);
|
||||
|
||||
@@ -65,10 +83,11 @@ io.on("connection", (socket) => {
|
||||
sendEmail(
|
||||
email,
|
||||
`Challenge from ${playerUsername}`,
|
||||
`To accept the challenge follow the link: http://localhost:5173/game/friend/${roomID}`
|
||||
`To accept the challenge follow the link: http://192.168.136.99:5173/game/challenges/${challengedPlayerUsername}/${roomID} \n login through: http://192.168.136.99:5173/login \n roomid:${roomID}`
|
||||
);
|
||||
|
||||
socket.emit("challenge-pending");
|
||||
socket.join(roomID);
|
||||
socket.emit("joined-room");
|
||||
}
|
||||
});
|
||||
|
||||
@@ -94,6 +113,7 @@ app.use((req, res, next) => {
|
||||
// app.use("/", (req, res, next) => res.send('Hello'));
|
||||
app.use("/api/auth", authRoutes);
|
||||
app.use("/api/user", userRoutes);
|
||||
app.use("/api/room", roomRoutes)
|
||||
|
||||
app.use((error, req, res, next) => {
|
||||
const status = error.status || 500;
|
||||
|
||||
@@ -74,7 +74,7 @@ router.post("/login", async (req, res) => {
|
||||
}
|
||||
|
||||
const token = createJSONToken(user.id);
|
||||
return res.json({ token });
|
||||
return res.json({ token, user });
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
const router = require("express").Router();
|
||||
const uuid = require("uuid");
|
||||
const { createRoom } = require("../socket");
|
||||
|
||||
router.post("/create", (req, res, next) => {
|
||||
const roomID = uuid.v4();
|
||||
createRoom(roomID);
|
||||
res.json({ roomID });
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user