You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const Schmervice = require('@hapipal/schmervice')
  2. const engageEveryone = allYins => {
  3. let done
  4. do {
  5. console.log('rerunning...')
  6. done = true
  7. allYins.forEach(yin => {
  8. // Keep matching if no true pairing is found
  9. console.log(yin.realId, yin.otp?.realId)
  10. if (!yin.otp) {
  11. done = false
  12. const yang = yin.getNextCandidate()
  13. if (!yang.otp || yang.prefers(yin)) {
  14. yin.engageTo(yang)
  15. }
  16. } else {
  17. console.log(yin.otp.realId)
  18. }
  19. })
  20. } while (!done)
  21. }
  22. class ProfileFacade {
  23. constructor(id, matchQueue) {
  24. this.realId = id ? id : undefined
  25. this.matchCandidateIndex = 0
  26. this.otp = null
  27. this.matchQueue = matchQueue?.length ? matchQueue : []
  28. this.fOrder = []
  29. }
  30. clearPlaceholderMatches() {
  31. this.matchQueue = this.matchQueue.filter(match => match.realId == false)
  32. this.fOrder = this.fOrder.filter(
  33. potentialFiance => potentialFiance.realId == false,
  34. )
  35. if (this.otp && this.otp.realId) {
  36. this.otp = null
  37. }
  38. }
  39. rank(id) {
  40. const idQueue = this.matchQueue.map(
  41. profileFacade => profileFacade.realId,
  42. )
  43. return idQueue.includes(id)
  44. ? idQueue.indexOf(id)
  45. : this.matchQueue.length + 1
  46. }
  47. prefers(p) {
  48. return this.rank(p.realId) < this.rank(this.otp.realId)
  49. }
  50. getNextCandidate() {
  51. if (this.matchCandidateIndex >= this.matchQueue.length) return null
  52. this.matchCandidateIndex = this.matchCandidateIndex + 1
  53. return this.matchQueue[this.matchCandidateIndex - 1]
  54. }
  55. engageTo(p) {
  56. if (p.otp) {
  57. p.otp.otp = null
  58. }
  59. if (this.otp) {
  60. this.otp.otp = null
  61. }
  62. p.otp = this
  63. p.fOrder.unshift(this)
  64. this.otp = p
  65. this.fOrder.unshift(p)
  66. console.log(
  67. 'partners pref: ',
  68. this.otp.matchQueue.map(f => f.realId).indexOf(this.realId) + 1,
  69. 'choice',
  70. )
  71. }
  72. }
  73. module.exports = class MatchService extends Schmervice.Service {
  74. constructor(...args) {
  75. super(...args)
  76. }
  77. async calcMatches(allQueuesByType) {
  78. const seekerIds = Object.keys(allQueuesByType['seeker']).map(id => ({
  79. profile_id: parseInt(id),
  80. queue: allQueuesByType['seeker'][id],
  81. }))
  82. const posterIds = Object.keys(allQueuesByType['poster']).map(id => ({
  83. profile_id: parseInt(id),
  84. queue: allQueuesByType['poster'][id],
  85. }))
  86. const diff = Math.abs(posterIds.length - seekerIds.length)
  87. const smallerList =
  88. posterIds.length < seekerIds.length ? posterIds : seekerIds
  89. // ADD DUMMY IDS TO THE SMALLER LIST
  90. for (let d = 0; d < diff; d++) {
  91. smallerList.push({ profile_id: 'dummy', queue: [] })
  92. }
  93. console.log(seekerIds)
  94. console.log(posterIds)
  95. // const yins = seekerIds.map(id => allProfileFacadesWithQueue[id])
  96. // const yangs = posterIds.map(id => allProfileFacadesWithQueue[id])
  97. // You only need to engage from one side
  98. // engageEveryone(yins)
  99. return []
  100. }
  101. }