瀏覽代碼

:recycle: more mock responses | semi-broken stable marriage | must score BEFORE match duh

tags/0.0.1
j 4 年之前
父節點
當前提交
13a6203628
共有 4 個文件被更改,包括 755 次插入125 次删除
  1. 576
    0
      backend/db/mock.js
  2. 17
    14
      backend/lib/routes/profile/match.js
  3. 27
    21
      backend/lib/routes/profile/respond.js
  4. 135
    90
      backend/lib/services/profile.js

+ 576
- 0
backend/db/mock.js 查看文件

737
         { response_id: 97, profile_id: 13, response_key_id: 16, val: '96701' },
737
         { response_id: 97, profile_id: 13, response_key_id: 16, val: '96701' },
738
         { response_id: 98, profile_id: 14, response_key_id: 16, val: '90452' },
738
         { response_id: 98, profile_id: 14, response_key_id: 16, val: '90452' },
739
         { response_id: 99, profile_id: 15, response_key_id: 16, val: '90025' },
739
         { response_id: 99, profile_id: 15, response_key_id: 16, val: '90025' },
740
+        {
741
+            response_id: 100,
742
+            profile_id: 8,
743
+            response_key_id: 0,
744
+            val: '160',
745
+        },
746
+        {
747
+            response_id: 101,
748
+            profile_id: 8,
749
+            response_key_id: 1,
750
+            val: '140',
751
+        },
752
+        {
753
+            response_id: 102,
754
+            profile_id: 8,
755
+            response_key_id: 2,
756
+            val: '180',
757
+        },
758
+        {
759
+            response_id: 103,
760
+            profile_id: 8,
761
+            response_key_id: 3,
762
+            val: '120',
763
+        },
764
+        {
765
+            response_id: 104,
766
+            profile_id: 8,
767
+            response_key_id: 4,
768
+            val: '140',
769
+        },
770
+        {
771
+            response_id: 105,
772
+            profile_id: 8,
773
+            response_key_id: 5,
774
+            val: '120',
775
+        },
776
+        {
777
+            response_id: 106,
778
+            profile_id: 8,
779
+            response_key_id: 6,
780
+            val: '120',
781
+        },
782
+        {
783
+            response_id: 107,
784
+            profile_id: 8,
785
+            response_key_id: 7,
786
+            val: '120',
787
+        },
788
+        {
789
+            response_id: 108,
790
+            profile_id: 8,
791
+            response_key_id: 8,
792
+            val: '140',
793
+        },
794
+        {
795
+            response_id: 109,
796
+            profile_id: 8,
797
+            response_key_id: 9,
798
+            val: '200',
799
+        },
800
+        {
801
+            response_id: 110,
802
+            profile_id: 8,
803
+            response_key_id: 10,
804
+            val: '200',
805
+        },
806
+        {
807
+            response_id: 111,
808
+            profile_id: 8,
809
+            response_key_id: 11,
810
+            val: '160',
811
+        },
812
+        {
813
+            response_id: 112,
814
+            profile_id: 9,
815
+            response_key_id: 0,
816
+            val: '180',
817
+        },
818
+        {
819
+            response_id: 113,
820
+            profile_id: 9,
821
+            response_key_id: 1,
822
+            val: '200',
823
+        },
824
+        {
825
+            response_id: 114,
826
+            profile_id: 9,
827
+            response_key_id: 2,
828
+            val: '120',
829
+        },
830
+        {
831
+            response_id: 115,
832
+            profile_id: 9,
833
+            response_key_id: 3,
834
+            val: '140',
835
+        },
836
+        {
837
+            response_id: 116,
838
+            profile_id: 9,
839
+            response_key_id: 4,
840
+            val: '180',
841
+        },
842
+        {
843
+            response_id: 117,
844
+            profile_id: 9,
845
+            response_key_id: 5,
846
+            val: '180',
847
+        },
848
+        {
849
+            response_id: 118,
850
+            profile_id: 9,
851
+            response_key_id: 6,
852
+            val: '120',
853
+        },
854
+        {
855
+            response_id: 119,
856
+            profile_id: 9,
857
+            response_key_id: 7,
858
+            val: '140',
859
+        },
860
+        {
861
+            response_id: 120,
862
+            profile_id: 9,
863
+            response_key_id: 8,
864
+            val: '180',
865
+        },
866
+        {
867
+            response_id: 121,
868
+            profile_id: 9,
869
+            response_key_id: 9,
870
+            val: '140',
871
+        },
872
+        {
873
+            response_id: 122,
874
+            profile_id: 9,
875
+            response_key_id: 10,
876
+            val: '160',
877
+        },
878
+        {
879
+            response_id: 123,
880
+            profile_id: 9,
881
+            response_key_id: 11,
882
+            val: '180',
883
+        },
884
+        {
885
+            response_id: 124,
886
+            profile_id: 10,
887
+            response_key_id: 0,
888
+            val: '160',
889
+        },
890
+        {
891
+            response_id: 125,
892
+            profile_id: 10,
893
+            response_key_id: 1,
894
+            val: '140',
895
+        },
896
+        {
897
+            response_id: 126,
898
+            profile_id: 10,
899
+            response_key_id: 2,
900
+            val: '140',
901
+        },
902
+        {
903
+            response_id: 127,
904
+            profile_id: 10,
905
+            response_key_id: 3,
906
+            val: '200',
907
+        },
908
+        {
909
+            response_id: 128,
910
+            profile_id: 10,
911
+            response_key_id: 4,
912
+            val: '180',
913
+        },
914
+        {
915
+            response_id: 129,
916
+            profile_id: 10,
917
+            response_key_id: 5,
918
+            val: '200',
919
+        },
920
+        {
921
+            response_id: 130,
922
+            profile_id: 10,
923
+            response_key_id: 6,
924
+            val: '160',
925
+        },
926
+        {
927
+            response_id: 131,
928
+            profile_id: 10,
929
+            response_key_id: 7,
930
+            val: '200',
931
+        },
932
+        {
933
+            response_id: 132,
934
+            profile_id: 10,
935
+            response_key_id: 8,
936
+            val: '120',
937
+        },
938
+        {
939
+            response_id: 133,
940
+            profile_id: 10,
941
+            response_key_id: 9,
942
+            val: '180',
943
+        },
944
+        {
945
+            response_id: 134,
946
+            profile_id: 10,
947
+            response_key_id: 10,
948
+            val: '120',
949
+        },
950
+        {
951
+            response_id: 135,
952
+            profile_id: 10,
953
+            response_key_id: 11,
954
+            val: '200',
955
+        },
956
+        {
957
+            response_id: 136,
958
+            profile_id: 11,
959
+            response_key_id: 0,
960
+            val: '140',
961
+        },
962
+        {
963
+            response_id: 137,
964
+            profile_id: 11,
965
+            response_key_id: 1,
966
+            val: '180',
967
+        },
968
+        {
969
+            response_id: 138,
970
+            profile_id: 11,
971
+            response_key_id: 2,
972
+            val: '160',
973
+        },
974
+        {
975
+            response_id: 139,
976
+            profile_id: 11,
977
+            response_key_id: 3,
978
+            val: '160',
979
+        },
980
+        {
981
+            response_id: 140,
982
+            profile_id: 11,
983
+            response_key_id: 4,
984
+            val: '140',
985
+        },
986
+        {
987
+            response_id: 141,
988
+            profile_id: 11,
989
+            response_key_id: 5,
990
+            val: '140',
991
+        },
992
+        {
993
+            response_id: 142,
994
+            profile_id: 11,
995
+            response_key_id: 6,
996
+            val: '120',
997
+        },
998
+        {
999
+            response_id: 143,
1000
+            profile_id: 11,
1001
+            response_key_id: 7,
1002
+            val: '140',
1003
+        },
1004
+        {
1005
+            response_id: 144,
1006
+            profile_id: 11,
1007
+            response_key_id: 8,
1008
+            val: '120',
1009
+        },
1010
+        {
1011
+            response_id: 145,
1012
+            profile_id: 11,
1013
+            response_key_id: 9,
1014
+            val: '120',
1015
+        },
1016
+        {
1017
+            response_id: 146,
1018
+            profile_id: 11,
1019
+            response_key_id: 10,
1020
+            val: '200',
1021
+        },
1022
+        {
1023
+            response_id: 147,
1024
+            profile_id: 11,
1025
+            response_key_id: 11,
1026
+            val: '140',
1027
+        },
1028
+        {
1029
+            response_id: 148,
1030
+            profile_id: 12,
1031
+            response_key_id: 0,
1032
+            val: '200',
1033
+        },
1034
+        {
1035
+            response_id: 149,
1036
+            profile_id: 12,
1037
+            response_key_id: 1,
1038
+            val: '160',
1039
+        },
1040
+        {
1041
+            response_id: 150,
1042
+            profile_id: 12,
1043
+            response_key_id: 2,
1044
+            val: '200',
1045
+        },
1046
+        {
1047
+            response_id: 151,
1048
+            profile_id: 12,
1049
+            response_key_id: 3,
1050
+            val: '140',
1051
+        },
1052
+        {
1053
+            response_id: 152,
1054
+            profile_id: 12,
1055
+            response_key_id: 4,
1056
+            val: '180',
1057
+        },
1058
+        {
1059
+            response_id: 153,
1060
+            profile_id: 12,
1061
+            response_key_id: 5,
1062
+            val: '200',
1063
+        },
1064
+        {
1065
+            response_id: 154,
1066
+            profile_id: 12,
1067
+            response_key_id: 6,
1068
+            val: '200',
1069
+        },
1070
+        {
1071
+            response_id: 155,
1072
+            profile_id: 12,
1073
+            response_key_id: 7,
1074
+            val: '180',
1075
+        },
1076
+        {
1077
+            response_id: 156,
1078
+            profile_id: 12,
1079
+            response_key_id: 8,
1080
+            val: '140',
1081
+        },
1082
+        {
1083
+            response_id: 157,
1084
+            profile_id: 12,
1085
+            response_key_id: 9,
1086
+            val: '140',
1087
+        },
1088
+        {
1089
+            response_id: 158,
1090
+            profile_id: 12,
1091
+            response_key_id: 10,
1092
+            val: '180',
1093
+        },
1094
+        {
1095
+            response_id: 159,
1096
+            profile_id: 12,
1097
+            response_key_id: 11,
1098
+            val: '160',
1099
+        },
1100
+        {
1101
+            response_id: 160,
1102
+            profile_id: 13,
1103
+            response_key_id: 0,
1104
+            val: '120',
1105
+        },
1106
+        {
1107
+            response_id: 161,
1108
+            profile_id: 13,
1109
+            response_key_id: 1,
1110
+            val: '180',
1111
+        },
1112
+        {
1113
+            response_id: 162,
1114
+            profile_id: 13,
1115
+            response_key_id: 2,
1116
+            val: '160',
1117
+        },
1118
+        {
1119
+            response_id: 163,
1120
+            profile_id: 13,
1121
+            response_key_id: 3,
1122
+            val: '120',
1123
+        },
1124
+        {
1125
+            response_id: 164,
1126
+            profile_id: 13,
1127
+            response_key_id: 4,
1128
+            val: '140',
1129
+        },
1130
+        {
1131
+            response_id: 165,
1132
+            profile_id: 13,
1133
+            response_key_id: 5,
1134
+            val: '140',
1135
+        },
1136
+        {
1137
+            response_id: 166,
1138
+            profile_id: 13,
1139
+            response_key_id: 6,
1140
+            val: '140',
1141
+        },
1142
+        {
1143
+            response_id: 167,
1144
+            profile_id: 13,
1145
+            response_key_id: 7,
1146
+            val: '160',
1147
+        },
1148
+        {
1149
+            response_id: 168,
1150
+            profile_id: 13,
1151
+            response_key_id: 8,
1152
+            val: '180',
1153
+        },
1154
+        {
1155
+            response_id: 169,
1156
+            profile_id: 13,
1157
+            response_key_id: 9,
1158
+            val: '200',
1159
+        },
1160
+        {
1161
+            response_id: 170,
1162
+            profile_id: 13,
1163
+            response_key_id: 10,
1164
+            val: '180',
1165
+        },
1166
+        {
1167
+            response_id: 171,
1168
+            profile_id: 13,
1169
+            response_key_id: 11,
1170
+            val: '160',
1171
+        },
1172
+        {
1173
+            response_id: 172,
1174
+            profile_id: 14,
1175
+            response_key_id: 0,
1176
+            val: '160',
1177
+        },
1178
+        {
1179
+            response_id: 173,
1180
+            profile_id: 14,
1181
+            response_key_id: 1,
1182
+            val: '140',
1183
+        },
1184
+        {
1185
+            response_id: 174,
1186
+            profile_id: 14,
1187
+            response_key_id: 2,
1188
+            val: '140',
1189
+        },
1190
+        {
1191
+            response_id: 175,
1192
+            profile_id: 14,
1193
+            response_key_id: 3,
1194
+            val: '160',
1195
+        },
1196
+        {
1197
+            response_id: 176,
1198
+            profile_id: 14,
1199
+            response_key_id: 4,
1200
+            val: '200',
1201
+        },
1202
+        {
1203
+            response_id: 177,
1204
+            profile_id: 14,
1205
+            response_key_id: 5,
1206
+            val: '120',
1207
+        },
1208
+        {
1209
+            response_id: 178,
1210
+            profile_id: 14,
1211
+            response_key_id: 6,
1212
+            val: '160',
1213
+        },
1214
+        {
1215
+            response_id: 179,
1216
+            profile_id: 14,
1217
+            response_key_id: 7,
1218
+            val: '160',
1219
+        },
1220
+        {
1221
+            response_id: 180,
1222
+            profile_id: 14,
1223
+            response_key_id: 8,
1224
+            val: '160',
1225
+        },
1226
+        {
1227
+            response_id: 181,
1228
+            profile_id: 14,
1229
+            response_key_id: 9,
1230
+            val: '120',
1231
+        },
1232
+        {
1233
+            response_id: 182,
1234
+            profile_id: 14,
1235
+            response_key_id: 10,
1236
+            val: '180',
1237
+        },
1238
+        {
1239
+            response_id: 183,
1240
+            profile_id: 14,
1241
+            response_key_id: 11,
1242
+            val: '180',
1243
+        },
1244
+        {
1245
+            response_id: 184,
1246
+            profile_id: 15,
1247
+            response_key_id: 0,
1248
+            val: '160',
1249
+        },
1250
+        {
1251
+            response_id: 185,
1252
+            profile_id: 15,
1253
+            response_key_id: 1,
1254
+            val: '140',
1255
+        },
1256
+        {
1257
+            response_id: 186,
1258
+            profile_id: 15,
1259
+            response_key_id: 2,
1260
+            val: '140',
1261
+        },
1262
+        {
1263
+            response_id: 187,
1264
+            profile_id: 15,
1265
+            response_key_id: 3,
1266
+            val: '160',
1267
+        },
1268
+        {
1269
+            response_id: 188,
1270
+            profile_id: 15,
1271
+            response_key_id: 4,
1272
+            val: '200',
1273
+        },
1274
+        {
1275
+            response_id: 189,
1276
+            profile_id: 15,
1277
+            response_key_id: 5,
1278
+            val: '120',
1279
+        },
1280
+        {
1281
+            response_id: 190,
1282
+            profile_id: 15,
1283
+            response_key_id: 6,
1284
+            val: '160',
1285
+        },
1286
+        {
1287
+            response_id: 191,
1288
+            profile_id: 15,
1289
+            response_key_id: 7,
1290
+            val: '160',
1291
+        },
1292
+        {
1293
+            response_id: 192,
1294
+            profile_id: 15,
1295
+            response_key_id: 8,
1296
+            val: '160',
1297
+        },
1298
+        {
1299
+            response_id: 193,
1300
+            profile_id: 15,
1301
+            response_key_id: 9,
1302
+            val: '120',
1303
+        },
1304
+        {
1305
+            response_id: 194,
1306
+            profile_id: 15,
1307
+            response_key_id: 10,
1308
+            val: '180',
1309
+        },
1310
+        {
1311
+            response_id: 195,
1312
+            profile_id: 15,
1313
+            response_key_id: 11,
1314
+            val: '180',
1315
+        },
740
     ],
1316
     ],
741
     memberships: [
1317
     memberships: [
742
         {
1318
         {

+ 17
- 14
backend/lib/routes/profile/match.js 查看文件

31
         handler: async function (request, h) {
31
         handler: async function (request, h) {
32
             const { profileService } = request.services()
32
             const { profileService } = request.services()
33
             const postMatchYins = await profileService.calcMatches()
33
             const postMatchYins = await profileService.calcMatches()
34
-            console.log(postMatchYins)
35
             try {
34
             try {
36
-                if(!postMatchYins){
35
+                if (!postMatchYins) {
37
                     throw new RangeError('Unable to match profiles')
36
                     throw new RangeError('Unable to match profiles')
38
                 }
37
                 }
39
-                
40
-                return h.response({
41
-                    ok: true,
42
-                    handler: pluginConfig.handlerType,
43
-                    data: postMatchYins,
44
-                }).code(200)
38
+
39
+                return h
40
+                    .response({
41
+                        ok: true,
42
+                        handler: pluginConfig.handlerType,
43
+                        data: postMatchYins,
44
+                    })
45
+                    .code(200)
45
             } catch (err) {
46
             } catch (err) {
46
-                return h.response({
47
-                    ok: false,
48
-                    handler: pluginConfig.handlerType,
49
-                    data: { error: `${err}` },
50
-                }).code(409)
47
+                return h
48
+                    .response({
49
+                        ok: false,
50
+                        handler: pluginConfig.handlerType,
51
+                        data: { error: `${err}` },
52
+                    })
53
+                    .code(409)
51
             }
54
             }
52
         },
55
         },
53
 
56
 
70
                     handler: Joi.string(),
73
                     handler: Joi.string(),
71
                     data: responseSchemas.error,
74
                     data: responseSchemas.error,
72
                 }),
75
                 }),
73
-            }
76
+            },
74
         },
77
         },
75
     },
78
     },
76
 }
79
 }

+ 27
- 21
backend/lib/routes/profile/respond.js 查看文件

1
 'use strict'
1
 'use strict'
2
 
2
 
3
 const Joi = require('joi')
3
 const Joi = require('joi')
4
-const profile = require('../../plugins/profile')
5
 
4
 
6
 const pluginConfig = {
5
 const pluginConfig = {
7
     handlerType: 'profile',
6
     handlerType: 'profile',
30
     // headers: true,
29
     // headers: true,
31
 
30
 
32
     /** Validate the route params (/active/{thing}) */
31
     /** Validate the route params (/active/{thing}) */
33
-    params: Joi.object({ 
34
-        profile_id: Joi.number()
32
+    params: Joi.object({
33
+        profile_id: Joi.number(),
35
     }),
34
     }),
36
 
35
 
37
     /** Validate the route query (/active/{thing}?limit=10&offset=10) */
36
     /** Validate the route query (/active/{thing}?limit=10&offset=10) */
38
-    query: Joi.object({ 
37
+    query: Joi.object({
39
         response_key_id: Joi.number(),
38
         response_key_id: Joi.number(),
40
-        val: Joi.string()
41
-    })
39
+        val: Joi.string(),
40
+    }),
42
     /** Validate the incoming payload (POST method) */
41
     /** Validate the incoming payload (POST method) */
43
     // payload: responseSchemas.responses,
42
     // payload: responseSchemas.responses,
44
 }
43
 }
54
 
53
 
55
         handler: async function (request, h) {
54
         handler: async function (request, h) {
56
             const { profileService } = request.services()
55
             const { profileService } = request.services()
57
-            
56
+
58
             const profileId = request.params.profile_id
57
             const profileId = request.params.profile_id
59
             const responseToSave = {
58
             const responseToSave = {
60
                 profile_id: profileId,
59
                 profile_id: profileId,
61
                 response_key_id: request.query.response_key_id,
60
                 response_key_id: request.query.response_key_id,
62
-                val: request.query.val
61
+                val: request.query.val,
63
             }
62
             }
64
-            const allResponses = await profileService.saveResponseForProfile(profileId, responseToSave)
65
-            
63
+            const allResponses = await profileService.saveResponseForProfile(
64
+                profileId,
65
+                responseToSave,
66
+            )
67
+
66
             try {
68
             try {
67
                 // profileService.saveResponseForProfile() will return null if it exists
69
                 // profileService.saveResponseForProfile() will return null if it exists
68
-                if(!allResponses){
70
+                if (!allResponses) {
69
                     throw new RangeError('Response already exists')
71
                     throw new RangeError('Response already exists')
70
                 }
72
                 }
71
 
73
 
72
-                return h.response({
73
-                    ok: true,
74
-                    handler: pluginConfig.handlerType,
75
-                    data: allResponses,
76
-                }).code(201)
74
+                return h
75
+                    .response({
76
+                        ok: true,
77
+                        handler: pluginConfig.handlerType,
78
+                        data: allResponses,
79
+                    })
80
+                    .code(201)
77
             } catch (err) {
81
             } catch (err) {
78
-                return h.response({
79
-                    ok: false,
80
-                    handler: pluginConfig.handlerType,
81
-                    data: { error: `${err}` },
82
-                }).code(409)
82
+                return h
83
+                    .response({
84
+                        ok: false,
85
+                        handler: pluginConfig.handlerType,
86
+                        data: { error: `${err}` },
87
+                    })
88
+                    .code(409)
83
             }
89
             }
84
         },
90
         },
85
 
91
 

+ 135
- 90
backend/lib/services/profile.js 查看文件

1
 const Schmervice = require('@hapipal/schmervice')
1
 const Schmervice = require('@hapipal/schmervice')
2
 const cosineSimilarity = require('compute-cosine-similarity')
2
 const cosineSimilarity = require('compute-cosine-similarity')
3
 const haversine = require('haversine')
3
 const haversine = require('haversine')
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
 
4
 
12
 const runMatch = (allYins, allYangs) => {
5
 const runMatch = (allYins, allYangs) => {
13
     balanceSides(allYins, allYangs)
6
     balanceSides(allYins, allYangs)
14
-    
7
+    console.log(allYins.length, ':', allYangs.length)
15
     // You only need to engage from one side
8
     // You only need to engage from one side
16
     engageEveryone(allYins)
9
     engageEveryone(allYins)
17
 
10
 
18
-    allYins.forEach(yin => yin.clearPlaceholderMatches())
19
-    allYangs.forEach(yang => yang.clearPlaceholderMatches())
20
-
21
     console.log('---')
11
     console.log('---')
22
 }
12
 }
23
-const engageEveryone = allYinsOrYangs => {
13
+const engageEveryone = allYins => {
24
     let done
14
     let done
25
     do {
15
     do {
16
+        console.log('rerunning...')
26
         done = true
17
         done = true
27
-        allYinsOrYangs.forEach(yinOrYang => {
28
-            if (!yinOrYang.otp) {
18
+        allYins.forEach(yin => {
19
+            // Keep matching if no true pairing is found
20
+            console.log(yin.realId, yin.otp?.realId)
21
+            if (!yin.otp) {
29
                 done = false
22
                 done = false
30
-                const yangOrYin = yinOrYang.getNextCandidate()
31
-                if (!yangOrYin.otp || yangOrYin.prefers(yinOrYang)) {
32
-                    yinOrYang.engageTo(yangOrYin)
23
+                const yang = yin.getNextCandidate()
24
+                if (!yang.otp || yang.prefers(yin)) {
25
+                    yin.engageTo(yang)
33
                 }
26
                 }
27
+            } else {
28
+                console.log(yin.otp.realId)
34
             }
29
             }
35
         })
30
         })
36
     } while (!done)
31
     } while (!done)
52
         this.matchQueue = matchQueue?.length ? matchQueue : []
47
         this.matchQueue = matchQueue?.length ? matchQueue : []
53
         this.fOrder = []
48
         this.fOrder = []
54
     }
49
     }
55
-    clearPlaceholderMatches(){
56
-        this.matchQueue = this.matchQueue.filter(
57
-            match => match.realId == false,
58
-        )
50
+    clearPlaceholderMatches() {
51
+        this.matchQueue = this.matchQueue.filter(match => match.realId == false)
59
         this.fOrder = this.fOrder.filter(
52
         this.fOrder = this.fOrder.filter(
60
             potentialFiance => potentialFiance.realId == false,
53
             potentialFiance => potentialFiance.realId == false,
61
         )
54
         )
63
             this.otp = null
56
             this.otp = null
64
         }
57
         }
65
     }
58
     }
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
59
+    rank(id) {
60
+        const idQueue = this.matchQueue.map(
61
+            profileFacade => profileFacade.realId,
62
+        )
63
+        return idQueue.includes(id)
64
+            ? idQueue.indexOf(id)
65
+            : this.matchQueue.length + 1
73
     }
66
     }
74
     prefers(p) {
67
     prefers(p) {
75
-        return this.rank(p) < this.rank(this.otp)
68
+        return this.rank(p.realId) < this.rank(this.otp.realId)
76
     }
69
     }
77
     getNextCandidate() {
70
     getNextCandidate() {
78
         if (this.matchCandidateIndex >= this.matchQueue.length) return null
71
         if (this.matchCandidateIndex >= this.matchQueue.length) return null
79
-        return this.matchQueue[this.matchCandidateIndex++]
72
+        this.matchCandidateIndex = this.matchCandidateIndex + 1
73
+        return this.matchQueue[this.matchCandidateIndex - 1]
80
     }
74
     }
81
     engageTo(p) {
75
     engageTo(p) {
82
-        if (p.otp) { p.otp.otp = null }
76
+        if (p.otp) {
77
+            p.otp.otp = null
78
+        }
79
+        if (this.otp) {
80
+            this.otp.otp = null
81
+        }
82
+
83
         p.otp = this
83
         p.otp = this
84
         p.fOrder.unshift(this)
84
         p.fOrder.unshift(this)
85
-        if (this.otp) { this.otp.otp = null }
85
+
86
         this.otp = p
86
         this.otp = p
87
         this.fOrder.unshift(p)
87
         this.fOrder.unshift(p)
88
+        console.log(
89
+            'partners pref: ',
90
+            this.otp.matchQueue.map(f => f.realId).indexOf(this.realId) + 1,
91
+            'choice',
92
+        )
88
     }
93
     }
89
 }
94
 }
90
 
95
 
121
             // ...profile,
126
             // ...profile,
122
             profile_id: profile.profile_id,
127
             profile_id: profile.profile_id,
123
             score: scoreResponses(userProfile, profile),
128
             score: scoreResponses(userProfile, profile),
124
-            distance: profile.distance
129
+            distance: profile.distance,
125
         }
130
         }
126
     })
131
     })
127
 }
132
 }
128
-/** 
129
-     * Grab the zip code string
130
-    */
131
- const getZipCodeFromProfile = (profile) => {
133
+/**
134
+ * Grab the zip code string
135
+ */
136
+const getZipCodeFromProfile = profile => {
132
     // There should only be one zip code entry per profile
137
     // There should only be one zip code entry per profile
133
-    let zip = profile.responses.filter(response => response.response_key_id == 16)[0]
138
+    let zip = profile.responses.filter(
139
+        response => response.response_key_id == 16,
140
+    )[0]
134
     const responseIndexForZip = profile.responses.indexOf(zip)
141
     const responseIndexForZip = profile.responses.indexOf(zip)
135
-    if(responseIndexForZip >= 0) {
142
+    if (responseIndexForZip >= 0) {
136
         profile.responses.splice(responseIndexForZip, 1)
143
         profile.responses.splice(responseIndexForZip, 1)
137
     }
144
     }
138
     return zip.val
145
     return zip.val
251
         let allResponses = await Response.query().where({
258
         let allResponses = await Response.query().where({
252
             profile_id: profileId,
259
             profile_id: profileId,
253
         })
260
         })
254
-        const matchingResponses = allResponses.filter(response => response.response_key_id == responseToSave.response_key_id)
255
-        
261
+        const matchingResponses = allResponses.filter(
262
+            response =>
263
+                response.response_key_id == responseToSave.response_key_id,
264
+        )
265
+
256
         // ?:Maybe bad idea
266
         // ?:Maybe bad idea
257
-        if(matchingResponses.length > 0) { return null }
267
+        if (matchingResponses.length > 0) {
268
+            return null
269
+        }
258
 
270
 
259
         await await Response.query().insert(responseToSave)
271
         await await Response.query().insert(responseToSave)
260
         return allResponses
272
         return allResponses
276
 
288
 
277
         return await Profile.query().delete().where('profile_id', profileId)
289
         return await Profile.query().delete().where('profile_id', profileId)
278
     }
290
     }
279
-    
291
+
280
     /**
292
     /**
281
      * Score a profile
293
      * Score a profile
282
      * @param {number} profileId
294
      * @param {number} profileId
284
      */
296
      */
285
     async scoreProfilesFor(profileId, maxDistance, distanceUnit) {
297
     async scoreProfilesFor(profileId, maxDistance, distanceUnit) {
286
         const { Profile } = this.server.models()
298
         const { Profile } = this.server.models()
287
-        
299
+
288
         // Our User Profile to score for
300
         // Our User Profile to score for
289
         const userProfile = await Profile.query()
301
         const userProfile = await Profile.query()
290
             .findOne('profile_id', profileId)
302
             .findOne('profile_id', profileId)
291
             .withGraphFetched('responses')
303
             .withGraphFetched('responses')
292
             .withGraphFetched('user')
304
             .withGraphFetched('user')
293
-            
305
+
294
         // Move unneeded responses
306
         // Move unneeded responses
295
         const userZip = getZipCodeFromProfile(userProfile)
307
         const userZip = getZipCodeFromProfile(userProfile)
296
 
308
 
299
         let profileIdsOfOppositeType = await Profile.query()
311
         let profileIdsOfOppositeType = await Profile.query()
300
             .withGraphFetched('responses')
312
             .withGraphFetched('responses')
301
             .withGraphFetched('user')
313
             .withGraphFetched('user')
302
-        
314
+
303
         // TODO: Let Objection optimize this
315
         // TODO: Let Objection optimize this
304
         const isPosterOpposite = userProfile.user.is_poster == 1 ? 0 : 1
316
         const isPosterOpposite = userProfile.user.is_poster == 1 ? 0 : 1
305
-        profileIdsOfOppositeType = profileIdsOfOppositeType.filter(profile => profile.user.is_poster == isPosterOpposite)
306
-        
307
-        const profilePlusDistance = await Promise.all(profileIdsOfOppositeType.map(async profile => {
308
-            const targetZip = getZipCodeFromProfile(profile)
309
-            const distance = await this._compareDistance(userZip, targetZip, distanceUnit)
310
-            return {
311
-                ...profile,
312
-                distance: [distance.toFixed(2), distanceUnit]
313
-            }
314
-        }))
317
+        profileIdsOfOppositeType = profileIdsOfOppositeType.filter(
318
+            profile => profile.user.is_poster == isPosterOpposite,
319
+        )
320
+
321
+        const profilePlusDistance = await Promise.all(
322
+            profileIdsOfOppositeType.map(async profile => {
323
+                const targetZip = getZipCodeFromProfile(profile)
324
+                const distance = await this._compareDistance(
325
+                    userZip,
326
+                    targetZip,
327
+                    distanceUnit,
328
+                )
329
+                return {
330
+                    ...profile,
331
+                    distance: [distance.toFixed(2), distanceUnit],
332
+                }
333
+            }),
334
+        )
315
 
335
 
316
-        const distanceFilteredProfiles = filterByDistance(profilePlusDistance, maxDistance)
336
+        const distanceFilteredProfiles = filterByDistance(
337
+            profilePlusDistance,
338
+            maxDistance,
339
+        )
317
 
340
 
318
-        const scoredProfilesWithDistance = scoreAll(distanceFilteredProfiles, userProfile)
341
+        const scoredProfilesWithDistance = scoreAll(
342
+            distanceFilteredProfiles,
343
+            userProfile,
344
+        )
319
 
345
 
320
         // Order by score
346
         // Order by score
321
         return scoredProfilesWithDistance.sort((a, b) => a.score - b.score)
347
         return scoredProfilesWithDistance.sort((a, b) => a.score - b.score)
322
     }
348
     }
323
     async calcMatches() {
349
     async calcMatches() {
324
         const { Profile } = this.server.models()
350
         const { Profile } = this.server.models()
325
-        
351
+
326
         // Grab all profiles with matchQueues
352
         // Grab all profiles with matchQueues
327
-        let allProfiles = await Profile.query()
328
-            .withGraphFetched('user')
329
-        
353
+        let allProfiles = await Profile.query().withGraphFetched('user')
354
+
330
         // !:FAKE Score everyone
355
         // !: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
356
+        // const profileIds = allProfiles.map(profile => profile.profile_id)
357
+        const scoredProfileQueuesById = {}
358
+        for (let profile of allProfiles) {
359
+            const profileQueue = await this.scoreProfilesFor(
360
+                profile.profile_id,
361
+                10000,
362
+                'mile',
363
+            )
364
+            scoredProfileQueuesById[profile.profile_id] = profileQueue.map(
365
+                profile => profile.profile_id,
366
+            )
367
+        }
368
+        const allProfileFacadesWithQueue = allProfiles.map(profile => {
369
+            const profileFacadeQueue = scoredProfileQueuesById[
370
+                profile.profile_id
371
+            ].map(id => {
372
+                const subQueue = scoredProfileQueuesById[id].map(
373
+                    id => new ProfileFacade(id),
374
+                )
375
+                return new ProfileFacade(id, subQueue)
376
+            })
377
+            return new ProfileFacade(profile.profile_id, profileFacadeQueue)
346
         })
378
         })
347
-        // ! FAKE -- END
379
+        // // ! FAKE -- END
380
+
381
+        const seekerIds = allProfiles
382
+            .filter(profile => profile.user.is_poster == 0)
383
+            .map(profile => profile.profile_id)
384
+        const posterIds = allProfiles
385
+            .filter(profile => profile.user.is_poster == 1)
386
+            .map(profile => profile.profile_id)
387
+
388
+        const yins = seekerIds.map(id => allProfileFacadesWithQueue[id])
389
+        const yangs = posterIds.map(id => allProfileFacadesWithQueue[id])
348
 
390
 
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)
391
         runMatch(yins, yangs)
354
         return yins
392
         return yins
355
     }
393
     }
361
     async _latLonForZip(zipCode) {
399
     async _latLonForZip(zipCode) {
362
         const { ZipCode } = this.server.models()
400
         const { ZipCode } = this.server.models()
363
 
401
 
364
-        const zipInfo = await ZipCode.query().findOne('zip_code_id', parseInt(zipCode))
365
-        const latitude = parseFloat(zipInfo.latitude)
366
-        const longitude =  parseFloat(zipInfo.longitude)
367
-        
368
-        return { latitude, longitude }
402
+        const zipInfo = await ZipCode.query().findOne(
403
+            'zip_code_id',
404
+            parseInt(zipCode),
405
+        )
406
+        if (!zipInfo) {
407
+            console.log(zipCode)
408
+        }
409
+
410
+        return {
411
+            latitude: parseFloat(zipInfo.latitude),
412
+            longitude: parseFloat(zipInfo.longitude),
413
+        }
369
     }
414
     }
370
     /**
415
     /**
371
      * Get the distance between two zipcodes
416
      * Get the distance between two zipcodes
375
      * @param {number} distance in miles
420
      * @param {number} distance in miles
376
      */
421
      */
377
     async _compareDistance(start_zip, end_zip, distanceUnit) {
422
     async _compareDistance(start_zip, end_zip, distanceUnit) {
378
-        if(!start_zip || !end_zip) return
379
-        
423
+        if (!start_zip || !end_zip) return
424
+
380
         const start = await this._latLonForZip(start_zip)
425
         const start = await this._latLonForZip(start_zip)
381
         const end = await this._latLonForZip(end_zip)
426
         const end = await this._latLonForZip(end_zip)
382
-        
427
+
383
         return haversine(start, end, { unit: distanceUnit })
428
         return haversine(start, end, { unit: distanceUnit })
384
     }
429
     }
385
 }
430
 }

Loading…
取消
儲存