This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overview.js
88 lines (78 loc) · 2.93 KB
/
overview.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
const Table = require('cli-table2')
function _getPlaceholder () {
return {
name: '-',
floors: '-',
distance_km: '-'
}
}
async function onOverviewLeaderboard (message, state) {
const stairsRows = await state.db.query(`
WITH recent_users
AS (
SELECT users.*
FROM users
JOIN runs
ON runs.user_id = users.id
GROUP BY users.id
HAVING max(runs.created_at) > now() - interval '2 months'
)
SELECT recent_users.name,
sum(floors) AS floors
FROM runs
JOIN recent_users
ON recent_users.id = runs.user_id
WHERE created_at > date_trunc('year', now())
GROUP BY user_id, recent_users.name
ORDER BY sum(floors) DESC
`)
const bikeRows = await state.db.query(`
WITH recent_users
AS (
SELECT users.*
FROM users
JOIN bike_runs
ON bike_runs.user_id = users.id
GROUP BY users.id
HAVING max(bike_runs.created_at) > now() - interval '2 months'
)
SELECT recent_users.name,
sum(distance)::float / 1000 AS distance_km
FROM bike_runs
JOIN recent_users
ON recent_users.id = bike_runs.user_id
WHERE created_at > date_trunc('year', now())
GROUP BY user_id, recent_users.name
ORDER BY sum(distance) DESC
`)
const table = new Table({
head: ['Stairs Rank', 'Name', 'Floors', ' ', 'Bike Rank', 'Name', 'Distance (km)'],
style: { head: [], border: [] }
})
const maxRows = Math.max(stairsRows.length, bikeRows.length)
for (let i = 0; i < maxRows; i++) {
const stairsRow = stairsRows[i] || _getPlaceholder()
const bikeRow = bikeRows[i] || _getPlaceholder()
table.push([i + 1, stairsRow.name, stairsRow.floors, ' ', i + 1, bikeRow.name, bikeRow.distance_km])
}
await message.send('```\n' + table.toString() + '\n```')
}
async function onHelp (message, state) {
const helpText = `Here's all the fitness commands:
* \`#stairs\`: records stairs session (${state.config.floors} floors by default)
* \`#stairs <floors>\`: records stairs session of specific amount of floors (e.g. \`#stairs 5\`)
* \`#stairs-lead\`: shows stairs leaderboard
* \`#stairs-achievements\`: shows stairs achievements
* \`#btw\`: records bike session, stands for "biked to work"
* \`#btw <distance>\`: records bike session of specific distance (e.g. \`#btw 6.2\`), the first time you do it will set as default
* \`#btw <distance> #save\`: records bike session and saves over previous default
* \`#bike-lead\`: shows bike leaderboard
* \`#bike-achievements\`: shows bike achievements
* \`#lead\`: shows company leaderboard
* \`#help\`: show this message`
await message.send(helpText)
}
module.exports = {
onOverviewLeaderboard,
onHelp
}