ddstats-server/libs/database/generate.js

122 lines
4.4 KiB
JavaScript
Raw Normal View History

2021-10-31 21:24:55 +01:00
import { sqlite, skinDB } from './init.js'
import tasks from './tasks.js'
import { execMany } from './helper.js'
import initLog from '../utils/log.js'
const log = initLog("DB Generation")
2021-10-30 20:26:37 +02:00
/**
* This constructs the DB with indexes and rankings...
* @module db/generateDB
*/
export function generateDB() {
/* TODO: Clean this up as it is a mess */
2021-10-31 23:43:36 +01:00
/* TODO: Remove useless ones */
log("Generating map index...")
execMany([
`CREATE INDEX IF NOT EXISTS "idx_maps_map" ON "maps" ("map")`,
`CREATE INDEX IF NOT EXISTS "idx_maps_category" ON "maps" ("server");`
])
2021-10-31 21:24:55 +01:00
log("Generating race index...")
execMany([
`CREATE INDEX IF NOT EXISTS "idx_race_player" ON "race" ("Name")`,
`CREATE INDEX IF NOT EXISTS "idx_race_name" ON "race" ("Name","Timestamp")`,
`CREATE INDEX IF NOT EXISTS "idx_race_server" ON "race" ("Server")`,
`CREATE INDEX IF NOT EXISTS "idx_race_mapTimestamp" ON "race" ("Map","Timestamp")`,
`CREATE INDEX IF NOT EXISTS "idx_race_timestamp" ON "race" ("Timestamp")`,
`CREATE INDEX IF NOT EXISTS "idx_race_mapNameTime" ON "race" ("Map", "Name", "Time")`
2021-10-31 21:24:55 +01:00
])
2021-10-30 20:26:37 +02:00
2021-10-31 21:24:55 +01:00
log("Creating rankings table...")
2021-10-30 20:26:37 +02:00
sqlite.exec(`
CREATE TABLE IF NOT EXISTS "rankings" (
"Map" varchar(128) NOT NULL,
"Name" varchar(16) NOT NULL,
"Time" float NOT NULL DEFAULT 0,
"Timestamp" timestamp NOT NULL DEFAULT current_timestamp,
"Server" char(4) NOT NULL DEFAULT '',
"rank" INTEGER NOT NULL);
`)
2021-10-31 21:24:55 +01:00
log("Calculating rankings for each map...")
2021-10-30 20:26:37 +02:00
tasks.processRankings()
2021-10-31 21:24:55 +01:00
log("Generating rankings index...")
2021-10-31 23:43:36 +01:00
execMany([
`CREATE INDEX IF NOT EXISTS "idx_rankings_map" ON "rankings" ("Map")`,
`CREATE INDEX IF NOT EXISTS "idx_rankings_rank" ON "rankings" ("rank")`,
`CREATE INDEX IF NOT EXISTS "idx_rankings_player" ON "rankings" ("Name")`
])
2021-10-30 20:26:37 +02:00
2021-10-31 21:24:55 +01:00
log("Generating teamrace index...")
2021-10-31 23:43:36 +01:00
execMany([
`CREATE INDEX IF NOT EXISTS "idx_teamrace_map" ON "teamrace" ("Map")`,
2021-10-31 23:43:36 +01:00
`CREATE INDEX IF NOT EXISTS "idx_teamrace_ID" ON "teamrace" ("ID")`,
`CREATE INDEX IF NOT EXISTS "idx_teamrace_mapID" ON "teamrace" ("Map", "ID")`
2021-10-31 23:43:36 +01:00
])
2021-10-30 20:26:37 +02:00
2021-10-31 21:24:55 +01:00
log("Creating teamrankings table...")
2021-10-30 20:26:37 +02:00
sqlite.exec(`
CREATE TABLE IF NOT EXISTS "teamrankings" (
"Map" varchar(128) NOT NULL,
"ID" varbinary(16) NOT NULL,
"Name" varchar(16) NOT NULL,
"Time" float NOT NULL DEFAULT 0,
"Timestamp" timestamp NOT NULL DEFAULT current_timestamp,
"Server" char(4) NOT NULL DEFAULT '',
"teamrank" INTEGER NOT NULL);
`)
2021-10-31 21:24:55 +01:00
log("Calculating teamrankings for each map...")
2021-10-30 20:26:37 +02:00
tasks.processTeamRankings()
2021-10-31 21:24:55 +01:00
log("Generating teamrankings index...")
2021-10-31 23:43:36 +01:00
execMany([
`CREATE INDEX IF NOT EXISTS "idx_teamrankings_map" ON "teamrankings" ("Map")`,
`CREATE INDEX IF NOT EXISTS "idx_teamrankings_rank" ON "teamrankings" ("teamrank")`,
`CREATE INDEX IF NOT EXISTS "idx_teamrankings_player" ON "teamrankings" ("name")`
])
2021-10-30 20:26:37 +02:00
sqlite.exec(`
CREATE TABLE IF NOT EXISTS "points" (
"rank" INTEGER NOT NULL,
"name" varchar(16) NOT NULL,
"points" INTEGER NOT NULL);
`)
log("Generating graphRecordCache...")
sqlite.exec(`
CREATE TABLE IF NOT EXISTS "graphRecordCache" (
"map" varchar(128) NOT NULL,
"player" varchar(16) NOT NULL,
"time" float NOT NULL DEFAULT 0,
"timestamp" timestamp NOT NULL DEFAULT current_timestamp,
"Server" char(4) NOT NULL DEFAULT '');
`)
tasks.processTimeGraph()
execMany([
`CREATE INDEX IF NOT EXISTS "idx_graphCache_player" ON "graphRecordCache" ("player")`,
`CREATE INDEX IF NOT EXISTS "idx_graphCache_map" ON "graphRecordCache" ("map");`
])
2021-10-31 21:24:55 +01:00
log("Inserting points to DB...")
2021-10-30 20:26:37 +02:00
tasks.processAllPoints()
skinDB.exec(`
CREATE TABLE IF NOT EXISTS "skindata" (
"timestamp" INTEGER NOT NULL,
"player" varchar(16) NOT NULL,
"clan" varchar(12) NOT NULL,
"flag" INTEGER NOT NULL,
"skin" varchar(16) NOT NULL,
"useColor" INTEGER NOT NULL,
"colorBodyRaw" INTEGER NOT NULL,
"colorBodyHex" varchar(8) NOT NULL,
"colorFeetRaw" INTEGER NOT NULL,
"colorFeetHex" varchar(8) NOT NULL);
`)
}