ddstats-server/libs/database/tasks.js

188 lines
5.6 KiB
JavaScript
Raw Normal View History

2021-10-31 23:43:36 +01:00
import msgpack from '@msgpack/msgpack'
import fs from 'fs'
import { execMany } from './helper.js'
import { sqlite } from './init.js'
/**
* This module parses the msgpack provided by DDNet...
* @module db/decodeMsgpack
*/
export function decodeMsgpack() {
2021-11-01 22:15:13 +01:00
const data = fs.readFileSync(process.env.MSGPACK_PATH ?? 'data/players.msgpack')
2021-10-31 23:43:36 +01:00
const decoded = msgpack.decodeMulti(data, { wrap: true })
const order = ['categories', 'maps', 'totalPoints', 'pointsRanks', 'pointsThisWeek', 'pointsThisMonth', 'teamRankPoints', 'rankPoints', 'serverRanks']
let final = {}
let i = 0
for (const part of decoded) {
final[order[i]] = part
++i
}
return final
}
/**
* This generates rankings for each map...
* @module db/processRankings
*/
export function processRankings() {
const maps = sqlite.prepare(`SELECT map FROM maps`);
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
`)
.run(map.Map)
}
/**
* This generates teamrankings for each map...
* @module db/processTeamRankings
*/
export function processTeamRankings() {
const maps = sqlite.prepare(`SELECT map FROM maps`);
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() {
let currentFinish
let currentBest = 0;
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
let currentBest = 0;
2021-11-01 21:52:48 +01:00
for (const record of finishes.iterate(map.map)) {
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(
map.Map,
2021-11-01 21:52:48 +01:00
record.name,
record.time,
record.timestamp,
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
}