|
|
@@ -1,7 +1,92 @@
|
|
1
|
1
|
const Schmervice = require('@hapipal/schmervice')
|
|
2
|
2
|
const cosineSimilarity = require('compute-cosine-similarity')
|
|
3
|
3
|
const haversine = require('haversine')
|
|
4
|
|
-const profile = require('../plugins/profile')
|
|
|
4
|
+const { profiles } = require('../../db/mock')
|
|
|
5
|
+
|
|
|
6
|
+const rand = max => {
|
|
|
7
|
+ return Math.floor(Math.random() * max) < 1
|
|
|
8
|
+ ? 1
|
|
|
9
|
+ : Math.floor(Math.random() * max)
|
|
|
10
|
+}
|
|
|
11
|
+
|
|
|
12
|
+const runMatch = (allYins, allYangs) => {
|
|
|
13
|
+ balanceSides(allYins, allYangs)
|
|
|
14
|
+
|
|
|
15
|
+ // You only need to engage from one side
|
|
|
16
|
+ engageEveryone(allYins)
|
|
|
17
|
+
|
|
|
18
|
+ allYins.forEach(yin => yin.clearPlaceholderMatches())
|
|
|
19
|
+ allYangs.forEach(yang => yang.clearPlaceholderMatches())
|
|
|
20
|
+
|
|
|
21
|
+ console.log('---')
|
|
|
22
|
+}
|
|
|
23
|
+const engageEveryone = allYinsOrYangs => {
|
|
|
24
|
+ let done
|
|
|
25
|
+ do {
|
|
|
26
|
+ done = true
|
|
|
27
|
+ allYinsOrYangs.forEach(yinOrYang => {
|
|
|
28
|
+ if (!yinOrYang.otp) {
|
|
|
29
|
+ done = false
|
|
|
30
|
+ const yangOrYin = yinOrYang.getNextCandidate()
|
|
|
31
|
+ if (!yangOrYin.otp || yangOrYin.prefers(yinOrYang)) {
|
|
|
32
|
+ yinOrYang.engageTo(yangOrYin)
|
|
|
33
|
+ }
|
|
|
34
|
+ }
|
|
|
35
|
+ })
|
|
|
36
|
+ } while (!done)
|
|
|
37
|
+}
|
|
|
38
|
+const balanceSides = (yins, yangs) => {
|
|
|
39
|
+ let diff = Math.abs(yangs.length - yins.length)
|
|
|
40
|
+ let smallerList = yangs.length < yins.length ? yangs : yins
|
|
|
41
|
+ const totalProfiles = yangs.length + yins.length + 1
|
|
|
42
|
+ for (let i = 0; i < diff; i++) {
|
|
|
43
|
+ smallerList.push(new ProfileFacade(totalProfiles + i))
|
|
|
44
|
+ }
|
|
|
45
|
+}
|
|
|
46
|
+class ProfileFacade {
|
|
|
47
|
+ constructor(id, matchQueue) {
|
|
|
48
|
+ this.realId = id ? id : undefined
|
|
|
49
|
+
|
|
|
50
|
+ this.matchCandidateIndex = 0
|
|
|
51
|
+ this.otp = null
|
|
|
52
|
+ this.matchQueue = matchQueue?.length ? matchQueue : []
|
|
|
53
|
+ this.fOrder = []
|
|
|
54
|
+ }
|
|
|
55
|
+ clearPlaceholderMatches(){
|
|
|
56
|
+ this.matchQueue = this.matchQueue.filter(
|
|
|
57
|
+ match => match.realId == false,
|
|
|
58
|
+ )
|
|
|
59
|
+ this.fOrder = this.fOrder.filter(
|
|
|
60
|
+ potentialFiance => potentialFiance.realId == false,
|
|
|
61
|
+ )
|
|
|
62
|
+ if (this.otp && this.otp.realId) {
|
|
|
63
|
+ this.otp = null
|
|
|
64
|
+ }
|
|
|
65
|
+ }
|
|
|
66
|
+ rank(p) {
|
|
|
67
|
+ for (let i = 0; i < this.matchQueue.length; i++) {
|
|
|
68
|
+ if (this.matchQueue[i] === p) {
|
|
|
69
|
+ return i
|
|
|
70
|
+ }
|
|
|
71
|
+ }
|
|
|
72
|
+ return this.matchQueue.length + 1
|
|
|
73
|
+ }
|
|
|
74
|
+ prefers(p) {
|
|
|
75
|
+ return this.rank(p) < this.rank(this.otp)
|
|
|
76
|
+ }
|
|
|
77
|
+ getNextCandidate() {
|
|
|
78
|
+ if (this.matchCandidateIndex >= this.matchQueue.length) return null
|
|
|
79
|
+ return this.matchQueue[this.matchCandidateIndex++]
|
|
|
80
|
+ }
|
|
|
81
|
+ engageTo(p) {
|
|
|
82
|
+ if (p.otp) { p.otp.otp = null }
|
|
|
83
|
+ p.otp = this
|
|
|
84
|
+ p.fOrder.unshift(this)
|
|
|
85
|
+ if (this.otp) { this.otp.otp = null }
|
|
|
86
|
+ this.otp = p
|
|
|
87
|
+ this.fOrder.unshift(p)
|
|
|
88
|
+ }
|
|
|
89
|
+}
|
|
5
|
90
|
|
|
6
|
91
|
const magic = 1000
|
|
7
|
92
|
const scoreResponses = (seeker, potentialMatch) => {
|
|
|
@@ -202,18 +287,18 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
202
|
287
|
|
|
203
|
288
|
// Our User Profile to score for
|
|
204
|
289
|
const userProfile = await Profile.query()
|
|
205
|
|
- .findOne('profile_id', profileId)
|
|
206
|
|
- .withGraphFetched('responses')
|
|
207
|
|
- .withGraphFetched('user')
|
|
208
|
|
-
|
|
|
290
|
+ .findOne('profile_id', profileId)
|
|
|
291
|
+ .withGraphFetched('responses')
|
|
|
292
|
+ .withGraphFetched('user')
|
|
|
293
|
+
|
|
209
|
294
|
// Move unneeded responses
|
|
210
|
295
|
const userZip = getZipCodeFromProfile(userProfile)
|
|
211
|
296
|
|
|
212
|
297
|
// Find all Profiles that are NOT of our userProfile.type
|
|
213
|
298
|
// ie. If userProfile.type == seeker, then find: poster
|
|
214
|
299
|
let profileIdsOfOppositeType = await Profile.query()
|
|
215
|
|
- .withGraphFetched('responses')
|
|
216
|
|
- .withGraphFetched('user')
|
|
|
300
|
+ .withGraphFetched('responses')
|
|
|
301
|
+ .withGraphFetched('user')
|
|
217
|
302
|
|
|
218
|
303
|
// TODO: Let Objection optimize this
|
|
219
|
304
|
const isPosterOpposite = userProfile.user.is_poster == 1 ? 0 : 1
|
|
|
@@ -235,7 +320,39 @@ module.exports = class ProfileService extends Schmervice.Service {
|
|
235
|
320
|
// Order by score
|
|
236
|
321
|
return scoredProfilesWithDistance.sort((a, b) => a.score - b.score)
|
|
237
|
322
|
}
|
|
238
|
|
-
|
|
|
323
|
+ async calcMatches() {
|
|
|
324
|
+ const { Profile } = this.server.models()
|
|
|
325
|
+
|
|
|
326
|
+ // Grab all profiles with matchQueues
|
|
|
327
|
+ let allProfiles = await Profile.query()
|
|
|
328
|
+ .withGraphFetched('user')
|
|
|
329
|
+
|
|
|
330
|
+ // !:FAKE Score everyone
|
|
|
331
|
+ allProfiles = allProfiles.map(profile => {
|
|
|
332
|
+ // Copy the profile and add a queue
|
|
|
333
|
+ const profilePlusFakeScore = {
|
|
|
334
|
+ ...profile,
|
|
|
335
|
+ queue: []
|
|
|
336
|
+ }
|
|
|
337
|
+ // Generate a random order of profile_ids 1:7
|
|
|
338
|
+ while (profilePlusFakeScore.queue.length < 4) {
|
|
|
339
|
+ profilePlusFakeScore.queue.push(rand(4))
|
|
|
340
|
+ profilePlusFakeScore.queue = [...new Set(profilePlusFakeScore.queue)]
|
|
|
341
|
+ }
|
|
|
342
|
+ //Instantiate a profile facade for each id for matching
|
|
|
343
|
+ profilePlusFakeScore.queue = profilePlusFakeScore.queue.map(randId => new ProfileFacade(randId))
|
|
|
344
|
+
|
|
|
345
|
+ return profilePlusFakeScore
|
|
|
346
|
+ })
|
|
|
347
|
+ // ! FAKE -- END
|
|
|
348
|
+
|
|
|
349
|
+ const seekers = allProfiles.filter(profile => profile.user.is_poster == 0)
|
|
|
350
|
+ const posters = allProfiles.filter(profile => profile.user.is_poster == 1)
|
|
|
351
|
+ const yins = seekers.map(profile => new ProfileFacade(profile.profile_id, profile.queue))
|
|
|
352
|
+ const yangs = posters.map(profile => new ProfileFacade(profile.profile_id, profile.queue))
|
|
|
353
|
+ runMatch(yins, yangs)
|
|
|
354
|
+ return yins
|
|
|
355
|
+ }
|
|
239
|
356
|
/**
|
|
240
|
357
|
* Use the db for zipcode info
|
|
241
|
358
|
* @param {string} zipCode
|