-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
290 lines (239 loc) · 7.88 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
const express = require("express");
const request = require("request");
const tba = require("./tba");
const db = require("./my-sql");
var bodyParser = require('body-parser');
var hash = require('pbkdf2-password')()
var path = require('path');
var session = require('express-session');
const app = express();
const httpPort = 3000;
app.use(session({
resave: false, // don't save session if unmodified
saveUninitialized: false, // don't create session until something stored
secret: 'convergence'
}));
var users = {
tj: { name: 'tj' }
};
// when you create a user, generate a salt
// and hash the password ('foobar' is the pass here)
hash({ password: 'foobar' }, function (err, pass, salt, hash) {
if (err) throw err;
// store the salt & hash in the "db"
users.tj.salt = salt;
users.tj.hash = hash;
});
var eventName;
var eventKey;
var number;
var weight;
var height;
var length;
var width;
var drivetrain;
var drivetrain_motors;
var free_speed;
var element_pickup;
var element_scoring;
var hang_charge_station;
var start_position;
var auto_balence;
app.use(express.static("public/css"));
app.use(express.static("public/js"));
app.use(express.static("public/img"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.set("view engine", "ejs");
// Session-persisted message middleware
app.use(function (req, res, next) {
var err = req.session.error;
var msg = req.session.success;
delete req.session.error;
delete req.session.success;
res.locals.message = '';
if (err) res.locals.message = '<p class="msg error">' + err + '</p>';
if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';
next();
});
//Index Page
app.get("/", function (req, res) {
res.render("homepage");
})
//Pit Scouting GET
app.get("/pit_scouting", function (req, res) {
res.render("pit_scouting");
})
//Pit Scouting POST
app.post('/pit_scouting', function (req, res, next) {
var data = req.body;
console.log(data);
number = `${data.teamNumberInput}`;
weight = `${data.teamWeightInput}`;
height = `${data.teamHeightInput}`;
length = `${data.teamLengthInput}`;
width = `${data.teamWidthInput}`;
drivetrain = `${data.robotDrivetrain}`;
drivetrain_motors = `${data.robotDrivetrainMotors}`;
free_speed = `${data.robotFreespeedinput}`;
element_pickup = `${data.gameElementPickup}`;
element_scoring = `${data.gameElementScoring}`;
hang_charge_station = `${data.hangOffChargeStation}`;
start_position = `${data.autoStartPosition}`;
auto_balence = `${data.autoBalance}`;
db.createNewEventDatabase(eventName);
db.insertTeamData(number, weight, height, length, width, drivetrain, drivetrain_motors, free_speed, element_pickup, element_scoring, hang_charge_station, start_position, auto_balence);
res.redirect('/pit_scouting');
})
app.post('/', function (req, res, next) {
console.log(req.body);
});
//Match Scout GET
app.get("/match_scouting", function (req, res) {
res.render("match_scouting");
})
//Match Scout POST
app.post('/match_scouting', function (req, res, next) {
var data = req.body;
console.log(data);
number = `${data.teamNumberInput}`;
match = `${data.matchNumberInput}`;
placement = `${data.autoPlacement}`;
mobility = `${data.autoMobility}`;
autoBalance = `${data.autoBalance}`;
coneHigh = `${data.autoConeHighScore}`;
coneLow = `${data.autoConeLowScore}`;
cubeScore = `${data.autoCubeScore}`
autoScore = `${data.autoScore}`;
teleConeHigh = `${data.teleConeHighScore}`;
teleConeLow = `${data.teleConeLowScore}`;
teleCube = `${data.teleCubeScore}`;
teleBalance = `${data.teleBalance}`;
teleScore = `${data.teleScore}`;
db.insertMatchData(number, match, placement, mobility, autoBalance, coneHigh, coneLow, cubeScore, autoScore, teleBalance, teleConeHigh, teleConeLow, teleCube, teleScore);
keep_alive = true;
res.redirect('/match_scouting');
})
//Admin Page
app.get("/admin", function (req, res) {
res.render("admin",
{
eventName: eventName,
eventKey: eventKey
});
})
//Posts Team List Table
app.get("/team_table", function (req, res) {
db.conn.query('SELECT * FROM EventTeams', function (err, result) {
if (err) {
console.error(err);
} else {
res.render('team_table', { data: result });
}
});
})
//Posts Match Scouting Table
app.get("/match_table", function (req, res) {
db.conn.query('SELECT * FROM match_info ORDER BY MatchNum ASC', function (err, result) {
if (err) {
console.error(err);
} else {
res.render('match_table', { data: result });
}
});
})
//Posts Pit Scouting Table
app.get("/pit_table", function (req, res) {
db.conn.query('SELECT * FROM team_info', function (err, result) {
if (err) {
console.error(err);
} else {
res.render('pit_table', { data: result });
}
});
})
app.post("/admin", function (req, res, next) {
var data = req.body;
eventName = `${data.eventName}`;
eventKey = `${data.eventCode}`;
db.createNewEventDatabase(eventName);
db.createAllTables();
db.insertEventData(eventName, eventKey);
tba.getTeamsByEvent(eventKey);
res.redirect('/admin');
})
app.get('/restricted', restrict, function (req, res) {
res.send('Wahoo! restricted area, click to <a href="/logout">logout</a>');
});
app.get('/logout', function (req, res) {
// destroy the user's session to log them out
// will be re-created next request
req.session.destroy(function () {
res.redirect('/');
});
});
app.get('/login', function (req, res) {
res.render('login');
});
function authenticate(name, pass, fn) {
if (!module.parent) console.log('authenticating %s:%s', name, pass);
var user = users[name];
// query the db for the given username
if (!user) return fn(null, null)
// apply the same algorithm to the POSTed password, applying
// the hash against the pass / salt, if there is a match we
// found the user
hash({ password: pass, salt: user.salt }, function (err, pass, salt, hash) {
if (err) return fn(err);
if (hash === user.hash) return fn(null, user)
fn(null, null)
});
}
function restrict(req, res, next) {
if (req.session.user) {
next();
} else {
req.session.error = 'Access denied!';
res.redirect('/login');
}
}
app.post('/login', function (req, res, next) {
authenticate(req.body.username, req.body.password, function (err, user) {
if (err) return next(err)
if (user) {
// Regenerate session when signing in
// to prevent fixation
req.session.regenerate(function () {
// Store the user's primary key
// in the session store to be retrieved,
// or in this case the entire user object
req.session.user = user;
req.session.success = 'Authenticated as ' + user.name
+ ' click to <a href="/logout">logout</a>. '
+ ' You may now access <a href="/admin">/restricted</a>.';
res.redirect('back');
});
} else {
req.session.error = 'Authentication failed, please check your '
+ ' username and password.'
+ ' (use "tj" and "foobar")';
res.redirect('/login');
}
});
});
//404 Page Error
app.get("*", function (req, res) {
res.send("404 Page Not Found!");
})
function pingdb() {
var sql_keep = `SELECT 1 + 1 AS solution`;
db.conn.query(sql_keep, function (err, result) {
if (err) throw err;
console.log("Ping DB");
});
}
setInterval(pingdb, 3600000);
//Server Start
app.listen(httpPort, function () {
console.log("Convergence Scouting Server Running on Port:" + httpPort);
})