ddstats-server/libs/database/generate.js

129 lines
4.7 KiB
JavaScript

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")
/**
* This constructs the DB with indexes and rankings...
* @module db/generateDB
*/
export function generateDB() {
/* TODO: Clean this up as it is a mess */
/* TODO: Remove useless ones */
if (process.env.GENERATE_DB !== "true")
return log("Won't generate the database since 'GENERATE_DB' is not set to \"true\" in '.env'!")
const exists = sqlite.prepare(`SELECT count(*) as a FROM sqlite_master WHERE type='table' AND name='points'`).get()
if(!exists.a)
return log("Database already generated!")
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");`
])
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")`
])
log("Creating rankings table...")
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);
`)
log("Calculating rankings for each map...")
tasks.processRankings()
log("Generating rankings index...")
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")`
])
log("Generating teamrace index...")
execMany([
`CREATE INDEX IF NOT EXISTS "idx_teamrace_map" ON "teamrace" ("Map")`,
`CREATE INDEX IF NOT EXISTS "idx_teamrace_ID" ON "teamrace" ("ID")`,
`CREATE INDEX IF NOT EXISTS "idx_teamrace_mapID" ON "teamrace" ("Map", "ID")`
])
log("Creating teamrankings table...")
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);
`)
log("Calculating teamrankings for each map...")
tasks.processTeamRankings()
log("Generating teamrankings index...")
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")`
])
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");`
])
log("Inserting points to DB...")
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);
`)
}