ddstats-server/libs/database/generate.js

187 lines
7.6 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() {
2021-11-04 20:06:51 +01:00
if (process.env.GENERATE_DB !== "enabled")
return log("Won't generate the database since 'GENERATE_DB' is not set to \"enabled\" in '.env'!")
const exists = sqlite.prepare(`SELECT count(*) as a FROM sqlite_master WHERE type='table' AND name='points'`).get()
2021-11-04 20:31:01 +01:00
if(exists.a === 1)
return log("Database already generated!")
2021-11-01 21:52:48 +01:00
/* Check if columns are already renamed */
2021-11-01 22:49:52 +01:00
const renamed = sqlite.prepare(`SELECT COUNT(*) AS a FROM pragma_table_info('race') WHERE name='date'`).get()
2021-11-01 21:52:48 +01:00
2021-11-02 00:19:38 +01:00
if(renamed.a === 0) {
2021-11-01 21:52:48 +01:00
log("Renaming columns...")
execMany([
`ALTER TABLE race RENAME COLUMN Map TO map`,
`ALTER TABLE race RENAME COLUMN Name TO player`,
`ALTER TABLE race RENAME COLUMN Time TO time`,
`ALTER TABLE race RENAME COLUMN Timestamp TO date`,
`ALTER TABLE race RENAME COLUMN Server TO server`,
`ALTER TABLE teamrace RENAME COLUMN Map TO map`,
`ALTER TABLE teamrace RENAME COLUMN Name TO player`,
`ALTER TABLE teamrace RENAME COLUMN Time TO time`,
`ALTER TABLE teamrace RENAME COLUMN ID TO id`,
`ALTER TABLE teamrace RENAME COLUMN Timestamp TO date`,
`ALTER TABLE maps RENAME COLUMN Map TO map`,
`ALTER TABLE maps RENAME COLUMN Server TO category`,
`ALTER TABLE maps RENAME COLUMN Points TO points`,
`ALTER TABLE maps RENAME COLUMN Stars TO stars`,
`ALTER TABLE maps RENAME COLUMN Mapper TO mapper`,
`ALTER TABLE maps RENAME COLUMN Timestamp TO release`
])
}
2021-10-31 21:24:55 +01:00
log("Generating race index...")
execMany([
2021-11-01 21:52:48 +01:00
`CREATE INDEX IF NOT EXISTS "idx_race_player" ON "race" ("player")`,
`CREATE INDEX IF NOT EXISTS "idx_race_name" ON "race" ("player", "date")`,
`CREATE INDEX IF NOT EXISTS "idx_race_server" ON "race" ("server")`,
`CREATE INDEX IF NOT EXISTS "idx_race_mapTimestamp" ON "race" ("map", "date")`,
`CREATE INDEX IF NOT EXISTS "idx_race_timestamp" ON "race" ("date")`,
`CREATE INDEX IF NOT EXISTS "idx_race_mapNameTime" ON "race" ("map", "player", "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" (
"category" varchar(32) NOT NULL,
"points" integer NOT NULL DEFAULT 0,
2021-11-01 21:52:48 +01:00
"map" varchar(128) NOT NULL,
"player" varchar(16) NOT NULL,
"time" float NOT NULL DEFAULT 0,
"date" timestamp NOT NULL DEFAULT current_timestamp,
"server" char(4) NOT NULL DEFAULT '',
"rank" INTEGER NOT NULL,
"finishes" INTEGER NOT NULL DEFAULT 0
2021-11-01 21:58:19 +01:00
)
2021-10-30 20:26:37 +02:00
`)
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([
2021-11-01 21:52:48 +01:00
`CREATE INDEX IF NOT EXISTS "idx_rankings_map" ON "rankings" ("map")`,
2021-10-31 23:43:36 +01:00
`CREATE INDEX IF NOT EXISTS "idx_rankings_rank" ON "rankings" ("rank")`,
`CREATE INDEX IF NOT EXISTS "idx_rankings_player" ON "rankings" ("player")`,
`CREATE INDEX IF NOT EXISTS "idx_rankings_finishes" ON "rankings" ("finishes")`,
2021-11-07 20:10:21 +01:00
`CREATE INDEX IF NOT EXISTS "idx_rankings_mapRank" ON "rankings" ("map", "rank")`,
`CREATE INDEX IF NOT EXISTS "idx_rankings_playerServer" ON "rankings" ("player", "server")`
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("Generating teamrace index...")
2021-10-31 23:43:36 +01:00
execMany([
2021-11-01 21:52:48 +01:00
`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")`
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" (
"category" varchar(32) NOT NULL,
"points" integer NOT NULL DEFAULT 0,
2021-11-01 21:52:48 +01:00
"map" varchar(128) NOT NULL,
"id" varbinary(16) NOT NULL,
"player" varchar(16) NOT NULL,
"time" float NOT NULL DEFAULT 0,
"date" timestamp NOT NULL DEFAULT current_timestamp,
"server" char(4) NOT NULL DEFAULT '',
2021-11-01 21:58:19 +01:00
"teamrank" INTEGER NOT NULL
)
2021-10-30 20:26:37 +02:00
`)
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([
2021-11-01 21:52:48 +01:00
`CREATE INDEX IF NOT EXISTS "idx_teamrankings_map" ON "teamrankings" ("map")`,
2021-10-31 23:43:36 +01:00
`CREATE INDEX IF NOT EXISTS "idx_teamrankings_rank" ON "teamrankings" ("teamrank")`,
`CREATE INDEX IF NOT EXISTS "idx_teamrankings_player" ON "teamrankings" ("player")`,
`CREATE INDEX IF NOT EXISTS "idx_teamrankings_playerCategoryMap" ON "teamrankings" ("player", "category", "map")`,
`CREATE INDEX IF NOT EXISTS "idx_teamrankings_mapTeamrank" ON "teamrankings" ("map", "teamrank")`
2021-10-31 23:43:36 +01:00
])
2021-10-30 20:26:37 +02:00
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,
2021-11-01 21:52:48 +01:00
"date" timestamp NOT NULL DEFAULT current_timestamp,
2021-11-01 21:58:19 +01:00
"server" char(4) NOT NULL DEFAULT ''
)
`)
tasks.processTimeGraph()
execMany([
`CREATE INDEX IF NOT EXISTS "idx_graphCache_player" ON "graphRecordCache" ("player")`,
2021-11-01 21:58:19 +01:00
`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()
log("Generating a new maps table...")
/* Rename the old one as we wanna use that name for the new one*/
sqlite.exec(`ALTER TABLE maps RENAME TO oldmaps`)
sqlite.exec(`
CREATE TABLE IF NOT EXISTS "maps" (
"map" varchar(128) NOT NULL,
"category" varchar(32) NOT NULL,
"points" integer NOT NULL DEFAULT 0,
"stars" integer NOT NULL DEFAULT 0,
"mapper" char(128) NOT NULL,
"release" timestamp NOT NULL DEFAULT current_timestamp,
"avgTime" FLOAT NOT NULL DEFAULT 0,
"medianTime" FLOAT NOT NULL DEFAULT 0,
"topTime" FLOAT NOT NULL DEFAULT 0,
"topTeamTime" FLOAT NOT NULL DEFAULT 0,
"finishesUnique" INTEGER NOT NULL DEFAULT 0,
"finishesTotal" INTEGER NOT NULL DEFAULT 0,
"finishesTeam" INTEGER NOT NULL DEFAULT 0
)
`)
tasks.processMaps()
log("Generating map index...")
execMany([
`CREATE INDEX IF NOT EXISTS "idx_maps_map" ON "maps" ("map")`,
2021-11-04 19:47:40 +01:00
`CREATE INDEX IF NOT EXISTS "idx_maps_category" ON "maps" ("category")`,
`CREATE INDEX IF NOT EXISTS "idx_maps_categoryMap" ON "maps" ("category", "map")`
])
2021-10-30 20:26:37 +02:00
skinDB.exec(`
CREATE TABLE IF NOT EXISTS "skindata" (
"timestamp" INTEGER NOT NULL,
"player" varchar(16) NOT NULL,
"clan" varchar(12) NOT NULL,
2021-11-06 21:59:49 +01:00
"flag" varchar(16) NOT NULL DEFAULT 'default',
"twFlag" INTEGER NOT NULL,
2021-10-30 20:26:37 +02:00
"skin" varchar(16) NOT NULL,
"useColor" INTEGER NOT NULL,
"colorBodyRaw" INTEGER NOT NULL,
"colorBodyHex" varchar(8) NOT NULL,
"colorFeetRaw" INTEGER NOT NULL,
2021-11-01 21:58:19 +01:00
"colorFeetHex" varchar(8) NOT NULL
)
2021-10-30 20:26:37 +02:00
`)
2021-11-07 20:10:21 +01:00
skinDB.exec(`CREATE INDEX IF NOT EXISTS "idx_player" ON "skindata" ("player")`)
2021-10-30 20:26:37 +02:00
}