This repository has been archived by the owner on Sep 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (57 loc) · 1.97 KB
/
index.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
const PORT = 3260;
// imports
const fs = require('fs');
const csv = require('fast-csv');
// vars
let users = require('./leaderboard.json');
let logger = fs.createWriteStream('output.log', { flags: 'a' });
let to_check = []
let next_question_index = 0;
// setup
const data_contents = fs.readFileSync(__dirname + '/data.tsv', { encoding: 'utf8' });
to_check = data_contents.split('\n').map(l => l.split(' '))
.map(([id, context, question, output, answers]) => ({
id: id, context: context, question: question, output: output, answers: answers
}));
// util
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
function save_to_leaderboard(users) {
fs.writeFile('leaderboard.json', JSON.stringify(users), e => { if (e) console.err(e); });
}
// express
const express = require('express');
const app = express();
app.use(express.json());
app.use('/static', express.static(__dirname + '/public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
app.get('/instructions', (req, res) => {
res.sendFile(__dirname + '/public/instructions.html');
})
app.get('/api/v1/next_item', (req, res) => {
//res.json(to_check[Math.floor(Math.random() * to_check.length)]);
res.json(to_check[next_question_index % to_check.length]);
next_question_index += 1;
if (next_question_index % to_check.length == 0)
shuffleArray(to_check);
});
app.get('/api/v1/leaderboard', (req, res) => {
res.json(users);
});
app.post('/api/v1/submit_item', (req, res) => {
const user = req.body.username;
if (!(user in users)) users[user] = 0;
req.body.date = (new Date()).toISOString();
console.log(req.body)
logger.write(JSON.stringify(req.body) + '\n');
users[user] += 1;
save_to_leaderboard(users);
res.end('ok');
});
app.listen(PORT, () => console.log(`serving on port ${PORT}`));