-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
168 lines (152 loc) · 5.45 KB
/
app.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const express = require('express');
const bodyParser = require('body-parser');
const { spawn } = require('child_process');
//var indexRouter = require('./index');
const authRouter = require('./auth');
var logger = require('morgan');
var session = require('express-session');
var SQLiteStore = require('connect-sqlite3')(session);
var path = require('path')
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('./data.sql');
// db.all("SELECT * FROM bars", [], (err, rows) => {
// console.log(rows)
// });
//function Add
const app = express();
app.use(express.static('client'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
store: new SQLiteStore({ db: 'sessions.db', dir: './' })
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
//app.use('/', indexRouter);
app.use('/', authRouter);
let d = require('./distances.js');
// const { array } = require('assert-plus');
app.get('/user/get', (req, res) => {
let returnData = {};
if (req.session.passport && req.session.passport.user) {
returnData.loggedIn = true;
returnData.shareid = req.session.passport.user.share;
} else {
returnData.loggedIn = false;
}
res.send(JSON.stringify(returnData))
});
app.get('/bar/get', (req, res) => {
db.all("SELECT * FROM bars WHERE name=?", [req.query.bar], (err, rows) => {
if (err) {console.log(err)}
res.end(JSON.stringify(rows))
});
});
app.get('/bar/getAll', (req, res) => {
if (req.session.passport && req.session.passport.user) {
db.all("SELECT name, coords FROM bars WHERE userid=? OR userid IS NULL", [String(req.session.passport.user.id)], (err, rows) => {
if (err) {console.log(err)}
res.end(JSON.stringify(rows))
});
}
});
let inUseShares = [];
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const shareLength = 8;
function createShareId() {
let inUse = true;
let id = '';
while (inUse) {
for (let i=0; i<shareLength; i++) {
id += characters.charAt(Math.floor(Math.random() * characters.length))
}
if (!inUseShares.includes(id)) {
inUseShares.push(id);
inUse = false;
} else {
id = '';
}
}
return id
}
let shares = {}
app.get('/share/get', (req, res) => {
if (shares[req.query.shareid]) {
res.end(JSON.stringify(shares[req.query.shareid]))
} else {
res.end(JSON.stringify({type: "Unknown"}))
}
});
app.post('/share/location', (req, res) => {
let shareid = createShareId();
shares[shareid] = {
type: "location",
location: req.body.location
}
res.end(JSON.stringify({shareid}));
});
app.post('/share/route', (req, res) => {
let shareid = createShareId();
shares[shareid] = {
type: "route",
route: req.body.route
}
res.end(JSON.stringify({shareid}));
});
app.post('/bar/order', (req, res) => {
let matrix = [];
for (let i=0; i<req.body.bars.length; i++) {
matrix.push(new Array(req.body.bars.length).fill(0))
}
db.all("SELECT * FROM distances", [], (err, rows) => {
if (err) {console.log(err)}
for (let i=0;i<req.body.bars.length;i++) {
for (let j=0; j<rows.length; j++) {
if (rows[j].bar1 === req.body.bars[i] && req.body.bars.indexOf(rows[j].bar2) !== -1) {
matrix[req.body.bars.indexOf(rows[j].bar2)][i] = rows[j].duration;
} else if (rows[j].bar2 === req.body.bars[i] && req.body.bars.indexOf(rows[j].bar1) !== -1) {
matrix[req.body.bars.indexOf(rows[j].bar1)][i] = rows[j].duration;
}
}
}
//run python
let shortestBars;
const python = spawn('python', ['solver.py', JSON.stringify(matrix)]);
python.stdout.on('data', data => {
shortestBars = JSON.parse(data.toString());
});
python.on('close', (code) => {
let orderedBars = []
for (let i=0; i<shortestBars.length; i++) {
db.all("SELECT coords FROM bars WHERE name=?", [req.body.bars[i]], (err, coords) => {
db.all("SELECT duration FROM distances WHERE (bar1=? AND bar2=?) OR (bar2=? AND bar1=?)", [req.body.bars[i], req.body.bars[i+1], req.body.bars[i], req.body.bars[i+1]], (err, duration) => {
if (duration[0]) {
orderedBars.push({name: req.body.bars[i], coords: coords[0].coords, time: duration[0].duration})
} else {
orderedBars.push({name: req.body.bars[i], coords: coords[0].coords, time: null})
res.end(JSON.stringify(orderedBars))
}
})
})
}
})
});
});
// this is used when adding vote to a poll.
app.post('/bar/new', function (req, res) {
let new_bar = req.body.new_bar;
// this adds the new bar to the table.
db.run(`INSERT INTO bars(name, coords, userid) VALUES(?,?,?)`, [new_bar.name, new_bar.coords, String(req.session.passport.user.id)], function(err) {
if (err) {
return console.log(err.message);
}
// get the last insert id
db.all("SELECT * FROM bars", [], (err, rows) => {
d.work_out_all(new_bar, rows);
res.end("success")
});
});
});
app.listen(3000);