Files
ChessHub/backend/routes/room.js
T
Moon Patel 5dbf0409f6 new API endpoint added
- DELETE /api/user/:username/challenges/:challengeID -> when user accepts or declines a challenge
2023-07-04 23:49:09 +05:30

52 lines
2.0 KiB
JavaScript

const router = require("express").Router();
const uuid = require("uuid");
const { createRoom } = require("../socket");
const { checkAuth } = require("../util/auth");
const { Challenge } = require("../models/challenge");
async function fetchChallenge({ challenger, challenged }) {}
// rooms can only be created through HTTP requests and destroyed only by socket.io server
// and vice versa is not true
router.post("/create", checkAuth, async (req, res, next) => {
console.log(req.body);
// challenger and challenged are username, color is the color played by challenger, timeLimit is the timeLimit for one player
const { challenger, challenged, color, timeLimit } = req.body;
let challenge = await Challenge.findOne({ challenger });
// a user can create only one challenge at a time
if (challenge) {
return res.status(405).json({ success: false, error: { message: "Cannot create new challenge" } });
}
// get email of the challenged person
// const challengedEmail = (await User.findOne({ username: challenged })).email;
// console.log(challengedEmail);
const roomID = uuid.v4();
createRoom(roomID, timeLimit);
challenge = await Challenge.create({ challenger, challenged, color, timeLimit, roomID });
// if (pendingChallenges.has(challenged)) {
// let challenges = pendingChallenges.get(challenged);
// challenges.push({ challenger, roomID, color, timeLimit });
// } else {
// // color is the choosed by the challenger
// pendingChallenges.set(challenged, [{ challenger, roomID, color, timeLimit }]);
// }
console.log("Pending challenge:", challenge);
// STOP SENDING EMAILS FOR NOW
// sendEmail(
// challengedEmail,
// `Challenge from ${challenger}`,
// `To accept the challenge follow the link: http://192.168.136.99:5173/game/challenges/${challenged}/${roomID} \n login through: http://192.168.136.99:5173/login \n roomid:${roomID}`
// );
res.json({ roomID });
});
module.exports = router;