cragy-api / src /services /MatchingQueue.js
ShieldX's picture
Upload 18 files
9470e9f verified
raw
history blame contribute delete
836 Bytes
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();