Skip to content

Commit

Permalink
Resolving matching found event issue 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
MOHAMMED1MEDHAT committed Sep 18, 2023
1 parent 9450600 commit 597a169
Showing 1 changed file with 130 additions and 125 deletions.
255 changes: 130 additions & 125 deletions src/utils/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ const dyte = require('./dyte');
// // await client.set('foo', 'bar');

// //----upstash
const redisClient = createClient({
url:
'redis://default:145b15782fff4086b23126a3d07305ce@amusing-bulldog-39687.upstash.io:39687'
});
// const redisClient = createClient({
// url:
// 'redis://default:145b15782fff4086b23126a3d07305ce@amusing-bulldog-39687.upstash.io:39687'
// });

//-----localhost
// const redisClient = createClient();
const redisClient = createClient();

redisClient.on('error', err => console.log('Redis Client Error', err));

Expand Down Expand Up @@ -199,118 +199,123 @@ const listen = function(io) {
console.log(notToProvide);
console.log(`${tag}`, await getMany(tag), 'tag conten');
//start ------------------
const match = await searchForMatch(crossSkill, notToProvide);
if (match.found) {
const matchSocketId = await getMany(
`${match.MatchedUserId}`
// const match = await searchForMatch(crossSkill, notToProvide);
// if (match.found) {
// const matchSocketId = await getMany(
// `${match.MatchedUserId}`
// );
// const userSockets = await getMany(`${user._id}`);
// const matchedUser = await User.findById(
// match.MatchedUserId
// );
// io.to(socketId)
// .to(userSockets)
// .to(matchSocketId)
// .emit('match-found', {
// user1: matchedUser,
// user2: userInner
// });
// await removeFromSet(tag, `${user._id}`);
// await removeFromSet(crossSkill, `${match.MatchedUserId}`);
// console.log(`${user._id}/${match.MatchedUserId}`, 'buggg');
// await setOne(
// `${user._id}/${match.MatchedUserId}`,
// 'started'
// );
// } else {
// // while (match.found === false) {
while (true) {
const match = await searchForMatch(
crossSkill,
notToProvide
);
const userSockets = await getMany(`${user._id}`);
const matchedUser = await User.findById(
match.MatchedUserId
);
io.to(socketId)
.to(userSockets)
.to(matchSocketId)
.emit('match-found', {
user1: matchedUser,
user2: userInner
});
await removeFromSet(tag, `${user._id}`);
await removeFromSet(crossSkill, `${match.MatchedUserId}`);
console.log(`${user._id}/${match.MatchedUserId}`, 'buggg');
await setOne(
`${user._id}/${match.MatchedUserId}`,
'started'
// console.log(
// await getOne(`${user._id}/${match.MatchedUserId}`),
// 'res of status'
// );

// if (!match.MatchedUserId) {
// await setOne(`${user._id}/state`, 'stopped');
// }

const state = await getOne(
`${user._id}/${match.MatchedUserId}`
);
} else {
// while (match.found === false ) {
while (true) {
const match = await searchForMatch(
crossSkill,
notToProvide
);
console.log(
await getOne(`${user._id}/${match.MatchedUserId}`),
'res of status'
);

// if (!match.MatchedUserId) {
// await setOne(`${user._id}/state`, 'stopped');
// }
const singleUserState = await getOne(`${user._id}/state`);

const state = await getOne(
`${user._id}/${match.MatchedUserId}`
);
console.log(singleUserState, 'singleUserState');
console.log(state, 'state');

const singleUserState = await getOne(
`${user._id}/state`
if (
singleUserState === 'stopped' ||
singleUserState === 'found'
) {
console.log('hi from stopped or found');
break;
} else if (match.found && state === 'started') {
console.log(
`${user._id}/${match.MatchedUserId}`,
'buggg'
);

if (
match.found ||
state === 'started' ||
singleUserState === 'stopped'
) {
console.log(
`${user._id}/${match.MatchedUserId}`,
'buggg'
);
const matchSocketId = await getMany(
`${match.MatchedUserId}`
);
const userSockets = await getMany(`${user._id}`);
const matchedUser = await User.findById(
match.MatchedUserId
);
io.to(socketId)
.to(userSockets)
.to(matchSocketId)
.emit('match-found', {
user1: matchedUser,
user2: userInner
});
await removeFromSet(tag, `${user._id}`);
await removeFromSet(
crossSkill,
`${match.MatchedUserId}`
);
await setOne(
`${user._id}/${match.MatchedUserId}`,
'started'
);
break;
}
const matchSocketId = await getMany(
`${match.MatchedUserId}`
);
const userSockets = await getMany(`${user._id}`);
const matchedUser = await User.findById(
match.MatchedUserId
);
io.to(socketId)
.to(userSockets)
.to(matchSocketId)
.emit('match-found', {
user1: matchedUser,
user2: userInner
});
await removeFromSet(tag, `${user._id}`);
await removeFromSet(
crossSkill,
`${match.MatchedUserId}`
);
await setOne(
`${user._id}/${match.MatchedUserId}`,
'started'
);
await setOne(`${userInner._id}/state`, 'found');
await setOne(`${matchedUser._id}/state`, 'found');
break;
}

// if (
// match.found &&
// (await getOne(`${user._id}/${match.MatchedUserId}`)) !==
// 'started'
// ) {
// const matchSocketId = await getMany(
// `${match.MatchedUserId}`
// );
// const userSockets = await getMany(`${user._id}`);
// const matchedUser = await User.findById(
// match.MatchedUserId
// );
// io.to(socketId)
// .to(userSockets)
// .to(matchSocketId)
// .emit('match-found', {
// user1: matchedUser,
// user2: userInner
// });
// await removeFromSet(tag, `${user._id}`);
// await removeFromSet(
// crossSkill,
// `${match.MatchedUserId}`
// );
// await setOne(
// `${user._id}/${match.MatchedUserId}`,
// 'started'
// );
}

// if (
// match.found &&
// (await getOne(`${user._id}/${match.MatchedUserId}`)) !==
// 'started'
// ) {
// const matchSocketId = await getMany(
// `${match.MatchedUserId}`
// );
// const userSockets = await getMany(`${user._id}`);
// const matchedUser = await User.findById(
// match.MatchedUserId
// );
// io.to(socketId)
// .to(userSockets)
// .to(matchSocketId)
// .emit('match-found', {
// user1: matchedUser,
// user2: userInner
// });
// await removeFromSet(tag, `${user._id}`);
// await removeFromSet(
// crossSkill,
// `${match.MatchedUserId}`
// );
// await setOne(
// `${user._id}/${match.MatchedUserId}`,
// 'started'
// );
// }
// }
// io.to(socketId).emit('dude', { info: { isFuckable: false } });
} catch (err) {
Expand Down Expand Up @@ -465,22 +470,22 @@ const listen = function(io) {

console.log(tags);

if (state === 'started') {
await setOne(`${userId}/state`, 'stopped');
tags.forEach(async tag => {
console.log(
`${tag}`,
await getMany(tag),
'this tag content'
);
await removeFromSet(tag, `${user._id}`);
console.log(
`${tag}`,
await getMany(tag),
'this tag content'
);
});
}
// if (state === 'started') {
await setOne(`${userId}/state`, 'stopped');
tags.forEach(async tag => {
console.log(
`${tag}`,
await getMany(tag),
'this tag content'
);
await removeFromSet(tag, `${userInner._id}`);
console.log(
`${tag}`,
await getMany(tag),
'this tag content'
);
});
// }
} catch (err) {
console.log(err.message);
}
Expand Down

0 comments on commit 597a169

Please sign in to comment.