98 lines
3.8 KiB
JavaScript
98 lines
3.8 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 */
|
|
log("Generating race index...")
|
|
|
|
/* Generate race index TODO: Remove useless ones */
|
|
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")`
|
|
])
|
|
|
|
/* Create rankings table */
|
|
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()
|
|
|
|
/* Generate rankings index */
|
|
log("Generating rankings index...")
|
|
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 */
|
|
log("Generating teamrace index...")
|
|
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")`);
|
|
|
|
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...")
|
|
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 */
|
|
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);
|
|
`)
|
|
}
|