ddstats-server/libs/db/generate.js

98 lines
3.8 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 21:24:55 +01:00
log("Generating race index...")
2021-10-30 20:26:37 +02:00
/* Generate race index TODO: Remove useless ones */
2021-10-31 21:24:55 +01:00
execMany([
`CREATE INDEX IF NOT EXISTS "idx_race_Map_2" ON "race" ("Map","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-30 20:26:37 +02:00
/* Create rankings table */
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()
/* Generate rankings index */
2021-10-31 21:24:55 +01:00
log("Generating rankings index...")
2021-10-30 20:26:37 +02:00
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_rankings_map" ON "rankings" ("Map")`)
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_rankings_rank" ON "rankings" ("rank")`)
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_rankings_player" ON "rankings" ("Name")`)
/* Generate teamrace index */
2021-10-31 21:24:55 +01:00
log("Generating teamrace index...")
2021-10-30 20:26:37 +02:00
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrace_Map" ON "teamrace" ("Map")`);
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrace_ID" ON "teamrace" ("ID")`);
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrace_MapID" ON "teamrace" ("Map", "ID")`);
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-30 20:26:37 +02:00
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrankings_map" ON "teamrankings" ("Map")`)
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrankings_rank" ON "teamrankings" ("teamrank")`)
sqlite.exec(`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);
`)
/* Process all types of points */
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);
`)
}