-
Notifications
You must be signed in to change notification settings - Fork 0
/
matchReferencer2.js
74 lines (61 loc) · 2.23 KB
/
matchReferencer2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var fuzzy = require('fuzzy');
var request = require('request');
var IDs = [];
var findId = [];
var searchId = [];
var idToPush = [];
var getIDforPut = [];
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log("successfully created");
} else {
console.log("failed " + error);
}
};
//request all matches that are in the database and push them into a search array
request('http://localhost:2403/matches', function(error, response, body) {
var data = JSON.parse(body);
for(var i = 0; i < data.length; i++) {
searchId.push(data[i].homeTeamName + data[i].awayTeamName)
idToPush.push(data[i].id)
};
console.log(searchId);
console.log(idToPush)
//request all expertpredictions in the database and push them into a find array
request('http://localhost:2403/expertpredictions', function(error, response, body) {
var data = JSON.parse(body);
for(var i = 0; i < data.length; i++) {
IDs.push({teamNames: data[i].homeTeam + data[i].awayTeam, id: data[i].id})
findId.push(data[i].homeTeam + data[i].awayTeam)
getIDforPut.push(data[i].id)
};
console.log(IDs);
console.log(findId);
//lookup all matches for matching phrases in the expertpredictions collection. If any found, print the results
for(var i = 0; i < searchId.length; i++) {
var options = {extract: function(el) { return el.teamNames; }
};
var results = fuzzy.filter(searchId[i], IDs, options);
var matches = results.map(function(el) { return el.string; });
console.log(searchId[i]);
console.log(idToPush[i]);
console.log(matches.length);
for(var x = 0; x < matches.length; x++) {
if(matches != []) {
var indices = [];
var indicesFinal = [];
var idx = findId.indexOf(matches[x]);
console.log(getIDforPut[idx]);
request({ url: 'http://localhost:2403/expertpredictions?id=' + getIDforPut[idx], method: 'PUT', json: {matchReference: idToPush[i]}}, callback);
//console.log(indicesFinal);
while (idx != -1) {
//indices.push(idx);
idx = findId.indexOf(matches[x], idx + 1)
console.log(getIDforPut[idx]);
request({ url: 'http://localhost:2403/expertpredictions?id=' + getIDforPut[idx], method: 'PUT', json: {matchReference: idToPush[i]}}, callback)
}
}
};
}
});
});