|
|
@@ -1,77 +1,62 @@
|
|
1
|
1
|
const Schmervice = require('@hapipal/schmervice')
|
|
2
|
2
|
|
|
3
|
|
-const engageEveryone = allYins => {
|
|
|
3
|
+function engageEveryone(guys) {
|
|
4
|
4
|
let done
|
|
5
|
5
|
do {
|
|
6
|
|
- console.log('rerunning...')
|
|
7
|
6
|
done = true
|
|
8
|
|
- allYins.forEach(yin => {
|
|
9
|
|
- // Keep matching if no true pairing is found
|
|
10
|
|
- console.log(yin.realId, yin.otp?.realId)
|
|
11
|
|
- if (!yin.otp) {
|
|
|
7
|
+ for (let i = 0; i < guys.length; i++) {
|
|
|
8
|
+ const guy = guys[i]
|
|
|
9
|
+ if (!guy.fiance) {
|
|
12
|
10
|
done = false
|
|
13
|
|
- const yang = yin.getNextCandidate()
|
|
14
|
|
- if (!yang.otp || yang.prefers(yin)) {
|
|
15
|
|
- yin.engageTo(yang)
|
|
|
11
|
+ const gal = guy.nextCandidate()
|
|
|
12
|
+ if (!gal.fiance || gal.prefers(guy)) {
|
|
|
13
|
+ guy.engageTo(gal)
|
|
16
|
14
|
}
|
|
17
|
|
- } else {
|
|
18
|
|
- console.log(yin.otp.realId)
|
|
19
|
15
|
}
|
|
20
|
|
- })
|
|
|
16
|
+ }
|
|
21
|
17
|
} while (!done)
|
|
22
|
18
|
}
|
|
23
|
|
-class ProfileFacade {
|
|
24
|
|
- constructor(id, matchQueue) {
|
|
25
|
|
- this.realId = id ? id : undefined
|
|
26
|
19
|
|
|
27
|
|
- this.matchCandidateIndex = 0
|
|
28
|
|
- this.otp = null
|
|
29
|
|
- this.matchQueue = matchQueue?.length ? matchQueue : []
|
|
30
|
|
- this.fOrder = []
|
|
31
|
|
- }
|
|
32
|
|
- clearPlaceholderMatches() {
|
|
33
|
|
- this.matchQueue = this.matchQueue.filter(match => match.realId == false)
|
|
34
|
|
- this.fOrder = this.fOrder.filter(
|
|
35
|
|
- potentialFiance => potentialFiance.realId == false,
|
|
36
|
|
- )
|
|
37
|
|
- if (this.otp && this.otp.realId) {
|
|
38
|
|
- this.otp = null
|
|
|
20
|
+class Person {
|
|
|
21
|
+ constructor(name) {
|
|
|
22
|
+ let candidateIndex = 0
|
|
|
23
|
+ this.name = name
|
|
|
24
|
+ this.fiance = null
|
|
|
25
|
+ this.candidates = []
|
|
|
26
|
+ this.rank = p => {
|
|
|
27
|
+ for (let i = 0; i < this.candidates.length; i++) {
|
|
|
28
|
+ if (this.candidates[i] === p) return i
|
|
|
29
|
+ }
|
|
|
30
|
+ return this.candidates.length + 1
|
|
39
|
31
|
}
|
|
40
|
|
- }
|
|
41
|
|
- rank(id) {
|
|
42
|
|
- const idQueue = this.matchQueue.map(
|
|
43
|
|
- profileFacade => profileFacade.realId,
|
|
44
|
|
- )
|
|
45
|
|
- return idQueue.includes(id)
|
|
46
|
|
- ? idQueue.indexOf(id)
|
|
47
|
|
- : this.matchQueue.length + 1
|
|
48
|
|
- }
|
|
49
|
|
- prefers(p) {
|
|
50
|
|
- return this.rank(p.realId) < this.rank(this.otp.realId)
|
|
51
|
|
- }
|
|
52
|
|
- getNextCandidate() {
|
|
53
|
|
- if (this.matchCandidateIndex >= this.matchQueue.length) return null
|
|
54
|
|
- this.matchCandidateIndex = this.matchCandidateIndex + 1
|
|
55
|
|
- return this.matchQueue[this.matchCandidateIndex - 1]
|
|
56
|
|
- }
|
|
57
|
|
- engageTo(p) {
|
|
58
|
|
- if (p.otp) {
|
|
59
|
|
- p.otp.otp = null
|
|
|
32
|
+ this.prefers = p => {
|
|
|
33
|
+ return this.rank(p) < this.rank(this.fiance)
|
|
60
|
34
|
}
|
|
61
|
|
- if (this.otp) {
|
|
62
|
|
- this.otp.otp = null
|
|
|
35
|
+ this.nextCandidate = () => {
|
|
|
36
|
+ if (candidateIndex >= this.candidates.length) {
|
|
|
37
|
+ return null
|
|
|
38
|
+ }
|
|
|
39
|
+ return this.candidates[candidateIndex++]
|
|
63
|
40
|
}
|
|
64
|
41
|
|
|
65
|
|
- p.otp = this
|
|
66
|
|
- p.fOrder.unshift(this)
|
|
|
42
|
+ this.engageTo = p => {
|
|
|
43
|
+ if (p.fiance) {
|
|
|
44
|
+ p.fiance.fiance = null
|
|
|
45
|
+ }
|
|
|
46
|
+ p.fiance = this
|
|
|
47
|
+ if (this.fiance) {
|
|
|
48
|
+ this.fiance.fiance = null
|
|
|
49
|
+ }
|
|
|
50
|
+ this.fiance = p
|
|
|
51
|
+ }
|
|
67
|
52
|
|
|
68
|
|
- this.otp = p
|
|
69
|
|
- this.fOrder.unshift(p)
|
|
70
|
|
- console.log(
|
|
71
|
|
- 'partners pref: ',
|
|
72
|
|
- this.otp.matchQueue.map(f => f.realId).indexOf(this.realId) + 1,
|
|
73
|
|
- 'choice',
|
|
74
|
|
- )
|
|
|
53
|
+ this.swapWith = p => {
|
|
|
54
|
+ console.log('%s & %s swap partners', this.name, p.name)
|
|
|
55
|
+ const thisFiance = this.fiance
|
|
|
56
|
+ const pFiance = p.fiance
|
|
|
57
|
+ this.engageTo(pFiance)
|
|
|
58
|
+ p.engageTo(thisFiance)
|
|
|
59
|
+ }
|
|
75
|
60
|
}
|
|
76
|
61
|
}
|
|
77
|
62
|
|
|
|
@@ -80,29 +65,61 @@ module.exports = class MatchService extends Schmervice.Service {
|
|
80
|
65
|
super(...args)
|
|
81
|
66
|
}
|
|
82
|
67
|
async calcMatches(allQueuesByType) {
|
|
83
|
|
- const seekerIds = Object.keys(allQueuesByType['seeker']).map(id => ({
|
|
|
68
|
+ const seekers = Object.keys(allQueuesByType['seeker']).map(id => ({
|
|
84
|
69
|
profile_id: parseInt(id),
|
|
85
|
70
|
queue: allQueuesByType['seeker'][id],
|
|
86
|
71
|
}))
|
|
87
|
|
- const posterIds = Object.keys(allQueuesByType['poster']).map(id => ({
|
|
|
72
|
+ const posters = Object.keys(allQueuesByType['poster']).map(id => ({
|
|
88
|
73
|
profile_id: parseInt(id),
|
|
89
|
74
|
queue: allQueuesByType['poster'][id],
|
|
90
|
75
|
}))
|
|
91
|
76
|
|
|
92
|
|
- const diff = Math.abs(posterIds.length - seekerIds.length)
|
|
93
|
|
- const smallerList =
|
|
94
|
|
- posterIds.length < seekerIds.length ? posterIds : seekerIds
|
|
|
77
|
+ const diff = Math.abs(posters.length - seekers.length)
|
|
|
78
|
+ const smallerList = posters.length < seekers.length ? posters : seekers
|
|
95
|
79
|
// ADD DUMMY IDS TO THE SMALLER LIST
|
|
96
|
80
|
for (let d = 0; d < diff; d++) {
|
|
97
|
|
- smallerList.push({ profile_id: 'dummy', queue: [] })
|
|
|
81
|
+ smallerList.push({ profile_id: `${d}-dummy`, queue: [] })
|
|
98
|
82
|
}
|
|
99
|
|
- console.log(seekerIds)
|
|
100
|
|
- console.log(posterIds)
|
|
101
|
|
- // const yins = seekerIds.map(id => allProfileFacadesWithQueue[id])
|
|
102
|
|
- // const yangs = posterIds.map(id => allProfileFacadesWithQueue[id])
|
|
103
|
|
-
|
|
|
83
|
+ console.log('---')
|
|
|
84
|
+ let allPeople = [...seekers, ...posters]
|
|
|
85
|
+ const queuesById = allPeople.reduce((queuesById, profile) => {
|
|
|
86
|
+ queuesById[profile.profile_id] = profile.queue
|
|
|
87
|
+ return queuesById
|
|
|
88
|
+ }, {})
|
|
|
89
|
+ let allIds = allPeople.map(profile => profile.profile_id)
|
|
|
90
|
+ seekers.forEach(profile => {
|
|
|
91
|
+ profile.queue.forEach(id => allIds.push(id))
|
|
|
92
|
+ })
|
|
|
93
|
+ posters.forEach(profile => {
|
|
|
94
|
+ profile.queue.forEach(id => allIds.push(id))
|
|
|
95
|
+ })
|
|
|
96
|
+ allIds = [...new Set(allIds)]
|
|
|
97
|
+ const peopleById = allIds.reduce((peopleById, id) => {
|
|
|
98
|
+ peopleById[id] = new Person(id)
|
|
|
99
|
+ if (queuesById[id]) {
|
|
|
100
|
+ peopleById[id].candidates = queuesById[id].map(
|
|
|
101
|
+ queueId => new Person(queueId),
|
|
|
102
|
+ )
|
|
|
103
|
+ }
|
|
|
104
|
+ return peopleById
|
|
|
105
|
+ }, {})
|
|
|
106
|
+ const seekerPeople = seekers.map(
|
|
|
107
|
+ profile => peopleById[profile.profile_id],
|
|
|
108
|
+ )
|
|
|
109
|
+ const posterPeople = posters.map(
|
|
|
110
|
+ profile => peopleById[profile.profile_id],
|
|
|
111
|
+ )
|
|
104
|
112
|
// You only need to engage from one side
|
|
105
|
|
- // engageEveryone(yins)
|
|
106
|
|
- return []
|
|
|
113
|
+ engageEveryone(seekerPeople)
|
|
|
114
|
+ engageEveryone(posterPeople)
|
|
|
115
|
+ Object.values(peopleById).forEach(person => {
|
|
|
116
|
+ if (person.fiance) {
|
|
|
117
|
+ person.fiance.fiance = null
|
|
|
118
|
+ }
|
|
|
119
|
+ })
|
|
|
120
|
+ return Object.values(peopleById).map(person => ({
|
|
|
121
|
+ id: person.name,
|
|
|
122
|
+ otp: person?.fiance?.name ?? null,
|
|
|
123
|
+ }))
|
|
107
|
124
|
}
|
|
108
|
125
|
}
|