File size: 836 Bytes
9470e9f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
class MatchingQueue {
constructor() {
this.queue = []; // Array of { socketId, user }
}
add(socketId, user) {
this.queue.push({ socketId, user, joinedAt: Date.now() });
}
remove(socketId) {
this.queue = this.queue.filter(client => client.socketId !== socketId);
}
findMatch(currentUser) {
// 1. Filter out self and blocked users (omitted for MVP basics)
const candidates = this.queue.filter(c => c.user._id !== currentUser._id);
if (candidates.length === 0) return null;
// 2. Priority: Opposite Gender
const oppositeGender = candidates.find(
c => c.user.profile.gender !== currentUser.profile.gender
);
if (oppositeGender) return oppositeGender;
// 3. Fallback: Anyone (First in, First out)
return candidates[0];
}
}
export default new MatchingQueue();
|