Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (77 loc) · 3.23 KB
/
Copy pathserver.js
File metadata and controls
92 lines (77 loc) · 3.23 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.static(`${__dirname}/client`));
app.get('/search/:radius/:lat/:lon/:budget', (req, res) => {
findPossibilities(req.params.lat, req.params.lon, req.params.radius, req.params.budget, results => {
maximizeBudget(results, req.params.budget)
.then(results => res.status(300).send(results));
});
})
app.listen(PORT, () => console.log(`listening on port ${PORT}`));
function maximizeBudget(results, budget) {
return new Promise((resolve, reject) => {
const maxValuesAtCapacities = [];
for(let i = 0; i <= budget; i+=100) {
maxValuesAtCapacities.push([0, []])
};
for (let currentCapacity = 0; currentCapacity <= budget; currentCapacity+=100) {
let currentMaxValue = [0,[]];
for (let j = 0; j < results.length; j++) {
const team = results[j];
if (team.price <= currentCapacity) {
var maxValueUsingTeam = [0, []];
if(maxValuesAtCapacities[(currentCapacity - parseInt(team.price))/100][1] && maxValuesAtCapacities[(currentCapacity - parseInt(team.price))/100][1].indexOf(team.name) === -1 ) {
maxValueUsingTeam[0] = parseInt(team.price)
+ parseInt(maxValuesAtCapacities[(currentCapacity - team.price)/100][0])
maxValueUsingTeam[1] = maxValuesAtCapacities[(currentCapacity - parseInt(team.price))/100][1].concat(team.name);
} else {
maxValueUsingTeam[0] = parseInt(team.price)
maxValueUsingTeam[1] = [team.name];
}
if(currentMaxValue[0] > maxValueUsingTeam[0]) {
currentMaxValue = currentMaxValue;
} else if(currentMaxValue[0] === maxValueUsingTeam[0]) {
currentMaxValue = currentMaxValue[1].length > maxValueUsingTeam[1].length ? currentMaxValue : maxValueUsingTeam;
} else {
currentMaxValue = maxValueUsingTeam;
}
}
}
maxValuesAtCapacities[currentCapacity/100] = currentMaxValue;
}
resolve(maxValuesAtCapacities[budget/100][1]);
})
}
//==================================================================================
//DATABASE HELPERS
//Typically these would be seperated out into a different file
const mysql = require('mysql');
if(!process.env.JAWSDB_URL) require('./env.js');
const connection = mysql.createConnection(process.env.JAWSDB_URL);
connection.connect(function(err) {
if (err) throw err;
console.log('connected to database');
});
// See "Equirectangular approximation" (better performance for small distances)
// http://www.movable-type.co.uk/scripts/latlong.html
function findPossibilities(lat, lon, radius, budget, cb) {
connection.query(
`SELECT id, name, price,
( 3959 *
SQRT(
(RADIANS(longitude) - RADIANS(${lon})) * COS((RADIANS(${lat}) + RADIANS(latitude))/2)
*
(RADIANS(longitude) - RADIANS(${lon})) * COS((RADIANS(${lat}) + RADIANS(latitude))/2)
+
(RADIANS(latitude) - RADIANS(${lat})) * (RADIANS(latitude) - RADIANS(${lat}))
)
) AS distance FROM teams HAVING price <= ${budget} AND distance <= ${radius} ORDER BY distance;`,
(error, results) => {
if(error) {
throw new Error(error);
} else {
cb(results);
}
})
}