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
|
node_modules
|
||||||
|
_test_
|
||||||
.env
|
.env
|
||||||
|
|||||||
+28
-8
@@ -3,6 +3,7 @@ const bodyParser = require("body-parser");
|
|||||||
const cors = require("cors");
|
const cors = require("cors");
|
||||||
const authRoutes = require("./routes/auth");
|
const authRoutes = require("./routes/auth");
|
||||||
const userRoutes = require("./routes/user");
|
const userRoutes = require("./routes/user");
|
||||||
|
const roomRoutes = require('./routes/room')
|
||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
|
|
||||||
@@ -25,13 +26,22 @@ const pendingChallenges = new Map();
|
|||||||
io.on("connection", (socket) => {
|
io.on("connection", (socket) => {
|
||||||
console.log("Client connected:", socket.id);
|
console.log("Client connected:", socket.id);
|
||||||
|
|
||||||
|
socket.on("disconnect", () => {
|
||||||
|
console.log("Disconnecting");
|
||||||
|
});
|
||||||
|
|
||||||
// Handle join event
|
// Handle join event
|
||||||
socket.on("join-room", async (roomID, playerUsername, challengedPlayerUsername) => {
|
socket.on("join-room", async (roomID, playerUsername, challengedPlayerUsername, gameData) => {
|
||||||
// room exists
|
// room exists
|
||||||
if (activeRooms.has(roomID)) {
|
console.log(roomID, playerUsername, challengedPlayerUsername);
|
||||||
socket.emit("room-full");
|
console.log("activeRooms", activeRooms);
|
||||||
return;
|
console.log("pendingChallenges", pendingChallenges);
|
||||||
} else if (pendingChallenges.has(roomID)) {
|
// if (activeRooms.has(roomID)) {
|
||||||
|
// socket.emit("room-full");
|
||||||
|
// console.log('Room full');
|
||||||
|
// return;
|
||||||
|
// } else
|
||||||
|
if (pendingChallenges.has(roomID)) {
|
||||||
const challenge = pendingChallenges.get(roomID);
|
const challenge = pendingChallenges.get(roomID);
|
||||||
pendingChallenges.delete(roomID);
|
pendingChallenges.delete(roomID);
|
||||||
let newRoom = {
|
let newRoom = {
|
||||||
@@ -47,8 +57,14 @@ io.on("connection", (socket) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
console.log("New room created", roomID);
|
||||||
|
socket.join(roomID);
|
||||||
activeRooms.set(roomID, newRoom);
|
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 {
|
} else {
|
||||||
// no room on pending challenges found
|
// no room on pending challenges found
|
||||||
const challenge = {
|
const challenge = {
|
||||||
@@ -56,6 +72,8 @@ io.on("connection", (socket) => {
|
|||||||
challengerID: socket.id,
|
challengerID: socket.id,
|
||||||
challengerUsername: playerUsername,
|
challengerUsername: playerUsername,
|
||||||
challengedUsername: challengedPlayerUsername,
|
challengedUsername: challengedPlayerUsername,
|
||||||
|
challengerColor: gameData.color,
|
||||||
|
timeLimit: gameData.timeLimit,
|
||||||
};
|
};
|
||||||
pendingChallenges.set(roomID, challenge);
|
pendingChallenges.set(roomID, challenge);
|
||||||
|
|
||||||
@@ -65,10 +83,11 @@ io.on("connection", (socket) => {
|
|||||||
sendEmail(
|
sendEmail(
|
||||||
email,
|
email,
|
||||||
`Challenge from ${playerUsername}`,
|
`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("/", (req, res, next) => res.send('Hello'));
|
||||||
app.use("/api/auth", authRoutes);
|
app.use("/api/auth", authRoutes);
|
||||||
app.use("/api/user", userRoutes);
|
app.use("/api/user", userRoutes);
|
||||||
|
app.use("/api/room", roomRoutes)
|
||||||
|
|
||||||
app.use((error, req, res, next) => {
|
app.use((error, req, res, next) => {
|
||||||
const status = error.status || 500;
|
const status = error.status || 500;
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ router.post("/login", async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const token = createJSONToken(user.id);
|
const token = createJSONToken(user.id);
|
||||||
return res.json({ token });
|
return res.json({ token, user });
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = router;
|
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