164 lines
5.0 KiB
JavaScript
164 lines
5.0 KiB
JavaScript
import decodeMsgpack from './decodeMsgpack.js'
|
|
import { execMany } from './helper.js'
|
|
import { sqlite } from './init.js'
|
|
|
|
/**
|
|
* This generates rankings for each map...
|
|
* @module libs/database/processRankings
|
|
*/
|
|
export function processRankings() {
|
|
const maps = sqlite.prepare(`SELECT map FROM maps`)
|
|
|
|
for (const map of maps.iterate())
|
|
sqlite
|
|
.prepare(`
|
|
INSERT INTO rankings
|
|
(
|
|
map, player, time, date, rank, server
|
|
)
|
|
SELECT map, player, time, date, rank, server
|
|
FROM (
|
|
SELECT rank() OVER w AS rank,
|
|
map,
|
|
date,
|
|
player,
|
|
min(time) AS time,
|
|
server
|
|
FROM race
|
|
WHERE map = ?
|
|
GROUP BY player window w AS (ORDER BY time) ) AS a
|
|
ORDER BY rank
|
|
`)
|
|
.run(map.map)
|
|
}
|
|
|
|
/**
|
|
* This generates teamrankings for each map...
|
|
* @module libs/database/processTeamRankings
|
|
*/
|
|
export function processTeamRankings() {
|
|
const maps = sqlite.prepare(`SELECT map FROM maps`)
|
|
|
|
for (const map of maps.iterate())
|
|
sqlite
|
|
.prepare(`
|
|
INSERT INTO teamrankings
|
|
(
|
|
player, map, id, time, date, server, teamrank
|
|
)
|
|
SELECT DISTINCT(r.player),
|
|
r.map, r.id, r.time, r.date,
|
|
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
|
|
AND r.player = n.player
|
|
AND r.time = n.time window w AS (ORDER BY r.time)
|
|
`)
|
|
.run(map.map)
|
|
}
|
|
|
|
/**
|
|
* This generates a cache for all the dates the top record has been improved for each map...
|
|
* @module libs/database/processTimeGraph
|
|
*/
|
|
export function processTimeGraph() {
|
|
const maps = sqlite.prepare(`SELECT map FROM maps`)
|
|
const finishes = sqlite.prepare(`SELECT * FROM race WHERE map = ? ORDER BY date`)
|
|
for (const map of maps.iterate()) {
|
|
let currentFinish
|
|
let currentBest = 0
|
|
|
|
for (const record of finishes.iterate(map.map)) {
|
|
currentFinish = record.time
|
|
if (currentFinish <= currentBest || currentBest == 0) {
|
|
currentBest = currentFinish
|
|
|
|
sqlite.prepare(`
|
|
INSERT INTO "graphRecordCache"
|
|
(
|
|
map, player, time, date, server
|
|
) VALUES (?, ?, ?, ?, ?)
|
|
`).run(
|
|
map.map,
|
|
record.player,
|
|
record.time,
|
|
record.date,
|
|
record.server
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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,
|
|
"player" varchar(16) NOT NULL,
|
|
"points" INTEGER NOT NULL
|
|
)
|
|
`)
|
|
|
|
/* Insert data */
|
|
for (const type in types) {
|
|
let rank = 1
|
|
|
|
for (const entry of types[type]) {
|
|
sqlite
|
|
.prepare(`
|
|
INSERT INTO "points"
|
|
(
|
|
type, rank, player, points
|
|
) 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")`,
|
|
`CREATE INDEX IF NOT EXISTS "Idx_points_name" on "points" ("player")`
|
|
])
|
|
}
|
|
|
|
export default {
|
|
processAllPoints,
|
|
processRankings,
|
|
processTeamRankings,
|
|
processTimeGraph
|
|
} |