Skip to content

Commit

Permalink
remove redundant matching details
Browse files Browse the repository at this point in the history
  • Loading branch information
ong6 committed Nov 2, 2023
1 parent b2e18f0 commit 8afd057
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 179 deletions.
170 changes: 0 additions & 170 deletions services/matching-service/src/controllers/matchingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 1 addition & 9 deletions services/matching-service/src/routes/matchingRoutes.ts
Original file line number Diff line number Diff line change
@@ -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"));

Expand Down

0 comments on commit 8afd057

Please sign in to comment.