From 8afd0575bc7661e6d500fce2d3111ff2f4054ca5 Mon Sep 17 00:00:00 2001 From: Ong Jun Xiong Date: Thu, 2 Nov 2023 22:07:43 +0800 Subject: [PATCH] remove redundant matching details --- .../src/controllers/matchingController.ts | 170 ------------------ .../src/routes/matchingRoutes.ts | 10 +- 2 files changed, 1 insertion(+), 179 deletions(-) diff --git a/services/matching-service/src/controllers/matchingController.ts b/services/matching-service/src/controllers/matchingController.ts index 0f39b063..0151ca91 100644 --- a/services/matching-service/src/controllers/matchingController.ts +++ b/services/matching-service/src/controllers/matchingController.ts @@ -390,176 +390,6 @@ export function handleSendMessage( }; } -export const findMatch = async (req: Request, res: Response) => { - const io: Server = req.app.get("io"); - - const userId = req.params.userId; - const difficulties = req.body.difficulties || ["easy", "medium", "hard"]; - const programming_language = req.body.programming_language || "python"; - - const user = await prisma.user.findUnique({ - where: { id: userId }, - }); - - if (!user) { - return res.status(404).json({ error: "User not found" }); - } - - // Check if the user is already matched with another user - if (user.matchedUserId) { - const matchedUser = await prisma.user.findUnique({ - where: { id: user.matchedUserId }, - }); - - if (matchedUser) { - // Check for timeout or if the matched user has left - const now = new Date(); - const sixtySecondsAgo = new Date(now.getTime() - 60 * 1000); - - if ( - !user.lastConnected || - user.lastConnected < sixtySecondsAgo || - !matchedUser.matchedUserId - ) { - // Break the match and update both users' status - await prisma.user.update({ - where: { id: userId }, - data: { matchedUserId: null, lastConnected: null }, - }); - - if (matchedUser.matchedUserId) { - await prisma.user.update({ - where: { id: matchedUser.id }, - data: { matchedUserId: null, lastConnected: null }, - }); - } - } else { - // Update the lastConnected timestamp and reconnect the users - const now = new Date(); - await prisma.user.update({ - where: { id: userId }, - data: { - lastConnected: now, - }, - }); - - await prisma.user.update({ - where: { id: matchedUser.id }, - data: { - lastConnected: now, - }, - }); - - // Emit match found event to both users - io.to(userId.toString()).emit("matchFound", matchedUser); - io.to(matchedUser.id.toString()).emit("matchFound", user); - return res.json({ match: matchedUser }); - } - } - } - - if (user.isLookingForMatch) { - return res - .status(400) - .json({ error: "User is already looking for a match" }); - } - - // Update user status to looking for a match - await prisma.user.update({ - where: { id: userId }, - data: { isLookingForMatch: true }, - }); - - // Try to find a match - const match = await prisma.user.findFirst({ - where: { isLookingForMatch: true, id: { not: userId } }, - }); - - if (match) { - // Both users are matched - await prisma.user.update({ - where: { id: userId }, - data: { - isLookingForMatch: false, - matchedUserId: match.id, - lastConnected: new Date(), - }, - }); - - await prisma.user.update({ - where: { id: match.id }, - data: { - isLookingForMatch: false, - matchedUserId: userId, - lastConnected: new Date(), - }, - }); - - // This function and REST API seems to be not in use - const questionId = await getRandomQuestionOfDifficulty( - difficulties[0] - ).then( - // difficulties???? need to intersect difficulties or not - (questionId) => { - return questionId; - } - ); - - // Emit match found event to both users - io.to(userId.toString()).emit("matchFound", { match, questionId }); - io.to(match.id.toString()).emit("matchFound", { user, questionId }); - - return res.json({ match, questionId }); - } - - // If no immediate match is found, keep the user in the queue - return res.status(202).json({ message: "Looking for a match, please wait." }); -}; - -export const leaveMatch = async (req: Request, res: Response) => { - const userId = req.params.userId; - const user = await prisma.user.findUnique({ - where: { id: userId }, - }); - - if (!user) { - return res.status(404).json({ error: "User not found" }); - } - - if (!user.matchedUserId) { - return res.status(400).json({ error: "User is not in a match" }); - } - - // Update both users' status - await prisma.user.update({ - where: { id: userId }, - data: { matchedUserId: null, lastConnected: null }, - }); - - await prisma.user.update({ - where: { id: user.matchedUserId }, - data: { matchedUserId: null, lastConnected: null }, - }); - - res.status(200).json({ message: "Successfully left the match" }); -}; - -export async function getMatch(req: Request, res: Response) { - const room_id = req.params.room_id as string; - - const match = await prisma.match.findUnique({ where: { roomId: room_id } }); - - if (!match) { - return res.status(404).json({ error: "Match not found" }); - } - - return res.status(200).json({ - message: "Match exists", - room_id: room_id, - info: match, - }); -} - export async function updateMatchQuestion(req: Request, res: Response) { const room_id = req.params.room_id as string; diff --git a/services/matching-service/src/routes/matchingRoutes.ts b/services/matching-service/src/routes/matchingRoutes.ts index bda0ebcc..9707c5f0 100644 --- a/services/matching-service/src/routes/matchingRoutes.ts +++ b/services/matching-service/src/routes/matchingRoutes.ts @@ -1,16 +1,8 @@ import express from "express"; -import { - findMatch, - getMatch, - leaveMatch, - updateMatchQuestion, -} from "../controllers/matchingController"; +import { updateMatchQuestion } from "../controllers/matchingController"; const router = express.Router(); -router.get("/:userId/findMatch", findMatch); -router.post("/:userId/leave", leaveMatch); -router.get("/match/:room_id", getMatch); router.patch("/match/:room_id", updateMatchQuestion); router.get("/demo", (req, res) => res.sendFile(__dirname + "/index.html"));