forked from GarbageHumanStudio/Typing-Me
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
144 lines (135 loc) · 4.15 KB
/
server.js
File metadata and controls
144 lines (135 loc) · 4.15 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
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
// Max Cheat time (ms)
const CheatTime = 5999;
const fastify = require('fastify')({ logger: true });
const path = require('path');
const knex = require('knex')({
client: 'sqlite3',
connection: {
filename: path.join(__dirname, './db.sqlite'),
},
pool: {
min: 0,
max: 7,
},
useNullAsDefault: true,
});
async function initDatabase() {
console.log('Database init');
if (!(await knex.schema.hasTable('ranks'))) {
console.log('Create ranks table');
await knex.schema.createTable('ranks', (builder) => {
builder.increments('id');
builder.string('username');
builder.integer('wpm');
builder.string('text');
builder.integer('upload_time').defaultTo(knex.fn.now());
});
}
if (!(await knex.schema.hasTable('cheats'))) {
console.log('Create cheats table');
await knex.schema.createTable('cheats', (builder) => {
builder.increments('id');
builder.string('username');
builder.integer('wpm');
builder.integer('upload_time').defaultTo(knex.fn.now());
});
}
}
function initWeb() {
fastify.register(require('@fastify/static'), {
root: path.join(__dirname, '/static'),
});
fastify.post('/', (request, reply) => {
return { ok: true };
});
fastify.post(
'/upload',
{
schema: {
body: {
type: 'object',
required: ['name', 'time', 'text'],
properties: {
name: {
type: 'string',
minLength: 1,
maxLength: 24,
},
time: {
type: 'number',
},
text: {
type: 'string',
minLength: 5,
},
},
},
},
},
async (request, reply) => {
const { name: username, time, text } = request.body;
const wpm = Math.round((text.length / time) * 12000);
if (request.body.time <= CheatTime) {
await knex
.insert({
username,
wpm,
upload_time: new Date().getTime(),
})
.into('cheats');
} else {
await knex
.insert({
username,
wpm,
text,
upload_time: new Date().getTime(),
})
.into('ranks');
}
return { ok: true };
}
);
fastify.get(
'/ranks',
{
schema: {
querystring: {
type: 'object',
properties: {
limit: {
type: 'number',
maximum: 30,
},
offset: {
type: 'number',
},
orderAsc: {
type: 'boolean',
},
cheat: {
type: 'boolean',
},
},
},
},
},
async (request, reply) => {
var query = knex
.select('*')
.from(request.query.cheat ? 'cheats' : 'ranks')
.orderBy('wpm', request.query.orderAsc ? 'asc' : 'desc');
if (request.query.limit) query.limit(request.query.limit);
if (request.query.offset) query.offset(request.query.offset);
return { data: await query };
}
);
fastify.listen({ port: 1293, host: '0.0.0.0' }, (err) => {
if (err) {
fastify.log.error(err);
process.exit(1);
}
});
}
initDatabase();
initWeb();