-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
189 lines (143 loc) · 4.26 KB
/
controller.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
let fs = require('fs');
const repository = require('./repository.js');
const hash = require('md5');
const tester = require('./testing.js');
const now = require('performance-now');
var hashOfSolvedFile;
var seedCount = 0;
// to do - save old file and take only new lines
exports.seed = function () {
//let tester = require('./testing.js');
//tester.seedDataToUserScoresFile(repository, fs);
//tester.testUnixTs(repository);
//tester.testAliases();
// happens if and only if program is just starting
if (hashOfSolvedFile === undefined) {
// time and compute hash of file
t0 = now();
hashOfSolvedFile = hash(fs.readFileSync('./data/solved.txt').toString());
t1 = now();
result = (t1-t0).toFixed(3);
console.log('took ' + result + " ms to read and hash scores file to " + hashOfSolvedFile);
console.log('initial seed, hashing and constructing database..');
// seed the database
repository.readScoresFromSolvedFile();
}
else {
// tmp
let oldHash = hashOfSolvedFile;
// time and compute new hash
let t0 = now();
hashOfSolvedFile = hash(fs.readFileSync('./data/solved.txt').toString());
let t1 = now();
result = (t1-t0).toFixed(3);
console.log('took ' + result + " ms to read and hash scores file to " + hashOfSolvedFile);
// if file differs, re-parse it to update db
if (hashOfSolvedFile !== oldHash) {
console.log('hash differed! updating database...');
// re-consume solved.txt and time the process
t0 = now();
repository.readScoresFromSolvedFile();
t1 = now();
result = (t1-t0).toFixed(3);
console.log('took ' + result + " ms to read scores from file");
}
}
// used for getSeedCount(req, res)
seedCount = seedCount + 1;
}
exports.index = function(req, res) {
res.render('index', {message: "", data: JSON.stringify(repository.retrieveUserScores())});
}
exports.getScores = function (req, res) {
let data = repository.retrieveUserScores();
// error conditions
if (data === null) {
res.send('no scores found -- probably none have been submitted yet');
return;
}
if (data === undefined) {
res.send('something went wrong reading in scores from disk. consult error logs');
return;
}
res.send(JSON.stringify(data));
}
exports.getRanks = function (req, res) {
let data = repository.retrieveUserRanks();
if (data === null) {
res.send('no ranks found');
return
}
if (data === undefined) {
res.send('something went wrong reading ranks from disk');
}
res.render('ranks', {users: data});
}
exports.updateScore = function (req, res) {
let pwhash = require('./data/secret.json');
let pw = req.params['pw'];
if (pw === undefined) {
res.end('must provide a password as first query parameter');
}
let givenPwHash = hash(req.params['pw']);
if (pwhash.pw === givenPwHash) {
let username = req.params['username'];
if (username === undefined) {
res.end('no username given');
}
let score = req.params['score'];
if (score === undefined) {
res.end('no score given');
}
if (isNaN(score)) {
res.end('score must be a number');
}
repository.addScoreToUser(username, score);
res.end('success');
}
else {
res.send('invalid password token');
}
}
exports.updateFlags = function (req, res) {
try {
repository.readScoresFromSolvedFile();
res.send('success');
}
catch (e){
res.send('failure ' + e.message);
}
}
exports.getSeedCount = function(req, res) {
res.send('seed count: ' + seedCount);
}
exports.getScoresForUser = function(req, res) {
let userName = req.params['userName'];
if (userName === undefined) {
res.end('no username given - must provide as a route parameter');
}
let datapoints = repository.retrieveOneUserScore(userName);
// edge cases
if (datapoints === null) {
res.end('score file not found');
return;
}
if (datapoints === undefined) {
res.end('error reading score file');
return;
}
if (datapoints === -1) {
res.end('user not found');
return;
}
let data = {user: userName, points: datapoints};
res.send(JSON.stringify(data));
}
exports.getIndividualGraph = function(req, res) {
let userName = req.params['userName'];
userName = userName.toUpperCase();
if (userName === undefined) {
res.end('no username given - must provide as a route parameter');
}
res.render('individualgraph', {dataurl : '/getscoresforuser/' + userName});
}