ddstats-server/libs/database/tasks.js

164 lines
5.0 KiB
JavaScript
Raw Normal View History

2021-11-01 22:20:13 +01:00
import decodeMsgpack from './decodeMsgpack.js'
2021-10-31 23:43:36 +01:00
import { execMany } from './helper.js'
import { sqlite } from './init.js'
/**
* This generates rankings for each map...
2021-11-01 22:49:52 +01:00
* @module libs/database/processRankings
2021-10-31 23:43:36 +01:00
*/
export function processRankings() {
2021-11-01 22:49:52 +01:00
const maps = sqlite.prepare(`SELECT map FROM maps`)
2021-10-31 23:43:36 +01:00
for (const map of maps.iterate())
sqlite
.prepare(`
INSERT INTO rankings
(
2021-11-01 21:52:48 +01:00
map, player, time, date, rank, server
2021-10-31 23:43:36 +01:00
)
2021-11-01 21:52:48 +01:00
SELECT map, player, time, date, rank, server
2021-10-31 23:43:36 +01:00
FROM (
SELECT rank() OVER w AS rank,
map,
2021-11-01 21:52:48 +01:00
date,
player,
2021-10-31 23:43:36 +01:00
min(time) AS time,
server
FROM race
WHERE map = ?
2021-11-01 21:52:48 +01:00
GROUP BY player window w AS (ORDER BY time) ) AS a
2021-10-31 23:43:36 +01:00
ORDER BY rank
`)
2021-11-01 22:49:52 +01:00
.run(map.map)
2021-10-31 23:43:36 +01:00
}
/**
* This generates teamrankings for each map...
2021-11-01 22:49:52 +01:00
* @module libs/database/processTeamRankings
2021-10-31 23:43:36 +01:00
*/
export function processTeamRankings() {
2021-11-01 22:49:52 +01:00
const maps = sqlite.prepare(`SELECT map FROM maps`)
2021-10-31 23:43:36 +01:00
for (const map of maps.iterate())
sqlite
.prepare(`
INSERT INTO teamrankings
(
2021-11-01 21:52:48 +01:00
player, map, id, time, date, server, teamrank
2021-10-31 23:43:36 +01:00
)
2021-11-01 21:52:48 +01:00
SELECT DISTINCT(r.player),
r.map, r.id, r.time, r.date,
2021-10-31 23:43:36 +01:00
Substring(n.server, 1, 3),
dense_rank() OVER w AS rank
FROM ((
SELECT DISTINCT id
FROM teamrace
WHERE map = ?
ORDER BY time) AS l
)
LEFT JOIN teamrace AS r
ON l.id = r.id
INNER JOIN race AS n
ON r.map = n.map
2021-11-01 21:52:48 +01:00
AND r.player = n.player
2021-10-31 23:43:36 +01:00
AND r.time = n.time window w AS (ORDER BY r.time)
`)
2021-11-01 21:52:48 +01:00
.run(map.map)
2021-10-31 23:43:36 +01:00
}
/**
* This generates a cache for all the dates the top record has been improved for each map...
* @module libs/database/processTimeGraph
*/
export function processTimeGraph() {
2021-11-01 22:49:52 +01:00
const maps = sqlite.prepare(`SELECT map FROM maps`)
2021-11-01 21:52:48 +01:00
const finishes = sqlite.prepare(`SELECT * FROM race WHERE map = ? ORDER BY date`)
for (const map of maps.iterate()) {
let currentFinish
2021-11-01 22:49:52 +01:00
let currentBest = 0
2021-11-01 21:52:48 +01:00
for (const record of finishes.iterate(map.map)) {
2021-11-01 22:49:52 +01:00
currentFinish = record.time
if (currentFinish <= currentBest || currentBest == 0) {
currentBest = currentFinish
sqlite.prepare(`
INSERT INTO "graphRecordCache"
(
2021-11-01 21:52:48 +01:00
map, player, time, date, server
) VALUES (?, ?, ?, ?, ?)
`).run(
2021-11-01 22:49:52 +01:00
map.map,
record.player,
2021-11-01 21:52:48 +01:00
record.time,
2021-11-01 22:49:52 +01:00
record.date,
2021-11-01 21:52:48 +01:00
record.server
)
}
}
}
}
2021-10-31 23:43:36 +01:00
/**
* This inserts all types of points into a table...
* @module db/processAllPoints
*/
export function processAllPoints() {
const msgpack = decodeMsgpack()
const types = {
points: msgpack.pointsRanks,
pointsThisWeek: msgpack.pointsThisWeek,
pointsThisMonth: msgpack.pointsThisMonth,
pointsTeam: msgpack.teamRankPoints,
pointsRank: msgpack.rankPoints,
}
/* Generate tables */
sqlite.exec(`
CREATE TABLE IF NOT EXISTS "points"
(
"type" varchar(16) NOT NULL,
"rank" INTEGER NOT NULL,
2021-11-01 21:52:48 +01:00
"player" varchar(16) NOT NULL,
2021-10-31 23:43:36 +01:00
"points" INTEGER NOT NULL
2021-11-01 21:58:19 +01:00
)
2021-10-31 23:43:36 +01:00
`)
/* Insert data */
for (const type in types) {
2021-10-31 23:43:36 +01:00
let rank = 1
for (const entry of types[type]) {
sqlite
.prepare(`
INSERT INTO "points"
(
2021-11-01 21:52:48 +01:00
type, rank, player, points
2021-10-31 23:43:36 +01:00
) VALUES (?, ?, ?, ?)
`)
.run(
type,
rank,
entry[0],
entry[1]
)
++rank
}
}
/* Generate indexes */
execMany([
`CREATE INDEX IF NOT EXISTS "idx_points_type" ON "points" ("type")`,
`CREATE INDEX IF NOT EXISTS "Idx_points_rank" on "points" ("rank")`,
2021-11-01 21:52:48 +01:00
`CREATE INDEX IF NOT EXISTS "Idx_points_name" on "points" ("player")`
2021-10-31 23:43:36 +01:00
])
}
export default {
processAllPoints,
processRankings,
processTeamRankings,
processTimeGraph
2021-10-31 23:43:36 +01:00
}