Skip to content

Commit b09c008

Browse files
committed
logic for matching works, but state not being updated
1 parent a905e6e commit b09c008

File tree

3 files changed

+74
-14
lines changed

3 files changed

+74
-14
lines changed

MatchingService/src/matching/matching.service.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Server, Socket } from 'socket.io';
22

3+
34
// Your existing matching service logic
45
export function initializeMatchingService(io: Server) {
56
// Maintain an array to store active users seeking a match
@@ -27,7 +28,23 @@ export function initializeMatchingService(io: Server) {
2728
});
2829

2930
function tryMatchForUser(socket: Socket, preferences: any) {
30-
const { difficulty, category } = preferences;
31+
32+
// const getQuestions = async () => {
33+
// const questions = await getAllQuestions();
34+
// const filteredQuestions = questions.data.filter((q: any) =>
35+
// q.difficulty === difficulty && q.categories.includes("Strings"));
36+
// console.log(questions)
37+
// const randomIndex = Math.floor(Math.random() * filteredQuestions.length);
38+
// const selectedQuestion = filteredQuestions[randomIndex];
39+
// console.log(selectedQuestion)
40+
// if (!selectedQuestion) {
41+
// return 1
42+
// }
43+
// const selectedId = selectedQuestion.id;
44+
// return selectedId
45+
46+
47+
const { difficulty, category} = preferences;
3148

3249
// Iterate through active users to find a match
3350
const matchedUser = activeUsers.find((user) => {
@@ -42,10 +59,10 @@ export function initializeMatchingService(io: Server) {
4259
// Remove both users from the active list
4360
removeUserFromActiveList(socket);
4461
removeUserFromActiveList(matchedUser.socket);
45-
62+
const randomSeed = Date.now();
4663
// Emit "matchFound" to both users
47-
socket.emit('matchFound', matchedUser.preferences);
48-
matchedUser.socket.emit('matchFound', preferences);
64+
socket.emit('matchFound', {matchedUserPreferences: matchedUser.preferences, seed: randomSeed});
65+
matchedUser.socket.emit('matchFound', {matchedUserPreferences: preferences, seed: randomSeed});
4966
} else {
5067
// Handle the case when no match is found for the user
5168
// You can emit a "noMatchFound" event or handle it differently

QuestionService/javascript/question/question.service.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ function addQuestion(question) {
4949
return __awaiter(this, void 0, void 0, function* () {
5050
try {
5151
let questionDoc = question;
52-
const docRef = (0, firestore_1.doc)(exports.db, "questions", question.title);
53-
yield (0, firestore_1.setDoc)(docRef, questionDoc);
52+
const docRef = yield (0, firestore_1.addDoc)((0, firestore_1.collection)(exports.db, "questions"), questionDoc);
5453
for (let i = 0; i < question.examples.length; i++) {
5554
const add = (0, firestore_1.setDoc)((0, firestore_1.doc)(docRef, "examples", (i + 1).toString()), question.examples[i]);
5655
}

frontend/src/components/MatchingService/MatchingForm.tsx

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import FormControl from '@mui/material/FormControl';
77
import Select, { SelectChangeEvent } from '@mui/material/Select';
88
import { useNavigate } from 'react-router-dom'; // Import useNavigate for redirection
99
import socket from './socket';
10+
import { getAllQuestions } from '../../api/questions/data';
11+
import { useAuth } from '../../auth/auth.context'
12+
import { sha256 } from "js-sha256";
13+
1014

1115
const style = {
1216
position: 'absolute' as 'absolute',
@@ -26,21 +30,27 @@ const style = {
2630
};
2731

2832
const MatchingForm = React.forwardRef(function MatchingForm() {
29-
const [difficulty, setDifficulty] = React.useState('');
30-
const [category, setCategory] = React.useState('');
33+
const [difficulty, setDifficulty] = React.useState('Easy');
34+
const [category, setCategory] = React.useState('Strings');
3135
const [isMatching, setIsMatching] = React.useState(false); // Track matching state
3236
const navigate = useNavigate(); // Get navigate for redirection
33-
37+
const { user } = useAuth();
38+
const userEmail = user?.email
3439
const handleDiffChange = (event: SelectChangeEvent) => {
40+
3541
setDifficulty(event.target.value);
42+
3643
};
3744

3845
const handleCatChange = (event: SelectChangeEvent) => {
46+
3947
setCategory(event.target.value);
48+
4049
};
4150

4251
const handleConnect = () => {
4352
const preferences = {
53+
userEmail,
4454
difficulty,
4555
category,
4656
};
@@ -52,21 +62,54 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
5262
setIsMatching(true);
5363
};
5464

65+
66+
const generateConsistentRandomIndex = (seed: any, arrayLength: number) => {
67+
// This is a simple example. In real-world scenarios, consider a more complex and predictable pseudo-random function
68+
return seed % arrayLength;
69+
}
5570
// Handle the "matchFound" event from the server
71+
const getQuestions = async (seed: any) => {
72+
const questions = await getAllQuestions();
73+
console.log(difficulty)
74+
console.log(category)
75+
const filteredQuestions = questions.data.filter((q: any) => {
76+
// console.log(difficulty);
77+
// console.log(q.difficulty);
78+
return q.categories.includes(category) && q.difficulty === difficulty
79+
80+
});
81+
82+
const randomIndex = generateConsistentRandomIndex(seed, filteredQuestions.length)
83+
console.log(randomIndex)
84+
const selectedQuestion = filteredQuestions[randomIndex];
85+
console.log(selectedQuestion)
86+
if (!selectedQuestion) {
87+
return 1
88+
}
89+
const selectedId = selectedQuestion.id;
90+
return selectedId
91+
}
5692
React.useEffect(() => {
57-
socket.on('matchFound', (matchedUserPreferences) => {
93+
socket.on('matchFound', async (matchedUserPreferences) => {
5894
// Handle the matched user's preferences here
5995
console.log('Match Found:', matchedUserPreferences);
96+
const seed = matchedUserPreferences.seed;
97+
const matchedUser = matchedUserPreferences.matchedUserPreferences;
98+
console.log(seed)
6099
setIsMatching(false); // Set matching state to false
61100
// Redirect to the question page with the code editor
62-
navigate('/question'); // Update the route as needed
101+
const qId = await getQuestions(seed);
102+
const hashedEmailOne = sha256(userEmail || "");
103+
const hashedEmailTwo = sha256(matchedUser.userEmail)
104+
navigate(`/collab/question/${qId}/${hashedEmailOne}/${hashedEmailTwo}`); // Update the route as needed
105+
63106
});
64107

65108
// Clean up the event listener when the component unmounts
66109
return () => {
67110
socket.off('matchFound');
68111
};
69-
}, [navigate]);
112+
}, []);
70113

71114
return (
72115
<Box sx={style}>
@@ -79,6 +122,7 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
79122
id="demo-simple-select-helper"
80123
value={difficulty}
81124
label="Difficulty"
125+
82126
onChange={handleDiffChange}
83127
>
84128
<MenuItem value="">
@@ -101,8 +145,8 @@ const MatchingForm = React.forwardRef(function MatchingForm() {
101145
<MenuItem value="">
102146
<em>None</em>
103147
</MenuItem>
104-
<MenuItem value={'Algo'}>Algo</MenuItem>
105-
<MenuItem value={'ML'}>ML</MenuItem>
148+
<MenuItem value={'Strings'}>Strings</MenuItem>
149+
<MenuItem value={'Algorithms'}>Algorithms</MenuItem>
106150
</Select>
107151
</FormControl>
108152
{isMatching ? (

0 commit comments

Comments
 (0)