const Schmervice = require('@hapipal/schmervice') const engageEveryone = allYins => { let done do { console.log('rerunning...') done = true allYins.forEach(yin => { // Keep matching if no true pairing is found console.log(yin.realId, yin.otp?.realId) if (!yin.otp) { done = false const yang = yin.getNextCandidate() if (!yang.otp || yang.prefers(yin)) { yin.engageTo(yang) } } else { console.log(yin.otp.realId) } }) } while (!done) } class ProfileFacade { constructor(id, matchQueue) { this.realId = id ? id : undefined this.matchCandidateIndex = 0 this.otp = null this.matchQueue = matchQueue?.length ? matchQueue : [] this.fOrder = [] } clearPlaceholderMatches() { this.matchQueue = this.matchQueue.filter(match => match.realId == false) this.fOrder = this.fOrder.filter( potentialFiance => potentialFiance.realId == false, ) if (this.otp && this.otp.realId) { this.otp = null } } rank(id) { const idQueue = this.matchQueue.map( profileFacade => profileFacade.realId, ) return idQueue.includes(id) ? idQueue.indexOf(id) : this.matchQueue.length + 1 } prefers(p) { return this.rank(p.realId) < this.rank(this.otp.realId) } getNextCandidate() { if (this.matchCandidateIndex >= this.matchQueue.length) return null this.matchCandidateIndex = this.matchCandidateIndex + 1 return this.matchQueue[this.matchCandidateIndex - 1] } engageTo(p) { if (p.otp) { p.otp.otp = null } if (this.otp) { this.otp.otp = null } p.otp = this p.fOrder.unshift(this) this.otp = p this.fOrder.unshift(p) console.log( 'partners pref: ', this.otp.matchQueue.map(f => f.realId).indexOf(this.realId) + 1, 'choice', ) } } module.exports = class MatchService extends Schmervice.Service { constructor(...args) { super(...args) } async calcMatches(allQueuesByType) { const seekerIds = Object.keys(allQueuesByType['seeker']).map(id => ({ profile_id: parseInt(id), queue: allQueuesByType['seeker'][id], })) const posterIds = Object.keys(allQueuesByType['poster']).map(id => ({ profile_id: parseInt(id), queue: allQueuesByType['poster'][id], })) const diff = Math.abs(posterIds.length - seekerIds.length) const smallerList = posterIds.length < seekerIds.length ? posterIds : seekerIds // ADD DUMMY IDS TO THE SMALLER LIST for (let d = 0; d < diff; d++) { smallerList.push({ profile_id: 'dummy', queue: [] }) } console.log(seekerIds) console.log(posterIds) // const yins = seekerIds.map(id => allProfileFacadesWithQueue[id]) // const yangs = posterIds.map(id => allProfileFacadesWithQueue[id]) // You only need to engage from one side // engageEveryone(yins) return [] } }