Started some refactoring...
This commit is contained in:
parent
dfb54ba50e
commit
62dd9a4db0
|
@ -1,5 +1,5 @@
|
||||||
import { Router } from 'express'
|
import { Router } from 'express'
|
||||||
import { sqlite } from '../db/init.js'
|
import { sqlite } from '../libs/db/init.js'
|
||||||
|
|
||||||
const finishApi = Router()
|
const finishApi = Router()
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Router } from 'express'
|
import { Router } from 'express'
|
||||||
import { sqlite } from '../db/init.js'
|
import { sqlite } from '../libs/db/init.js'
|
||||||
|
|
||||||
const graphApi = Router()
|
const graphApi = Router()
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Router } from 'express'
|
import { Router } from 'express'
|
||||||
import { sqlite } from '../db/init.js'
|
import { sqlite } from '../libs/db/init.js'
|
||||||
|
|
||||||
const mapApi = Router()
|
const mapApi = Router()
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Router } from 'express'
|
import { Router } from 'express'
|
||||||
import { sqlite } from '../db/init.js'
|
import { sqlite } from '../libs/db/init.js'
|
||||||
|
|
||||||
const playerApi = Router()
|
const playerApi = Router()
|
||||||
|
|
||||||
|
|
11
index.js
11
index.js
|
@ -1,25 +1,20 @@
|
||||||
import express from 'express'
|
import express from 'express'
|
||||||
import dotenv from 'dotenv'
|
import dotenv from 'dotenv'
|
||||||
import api from './api/api.js'
|
import api from './api/api.js'
|
||||||
import { generateDB } from "./db/generate.js"
|
import { generateDB } from "./libs/db/generate.js"
|
||||||
import { sqlite, dbInit } from "./db/init.js"
|
import { sqlite, dbInit } from "./libs/db/init.js"
|
||||||
import { ddnssStart, scrapeServer } from './ddnss/handler.js'
|
import { ddnssStart, scrapeServer } from './libs/ddnss/handler.js'
|
||||||
//import tasks from './db/tasks.js'
|
//import tasks from './db/tasks.js'
|
||||||
|
|
||||||
/* Read the .env file */
|
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
|
|
||||||
/* Init db */
|
|
||||||
dbInit()
|
dbInit()
|
||||||
|
|
||||||
/* check if the table points exists */
|
|
||||||
const exists = sqlite.prepare(`SELECT count(*) as a FROM sqlite_master WHERE type='table' AND name='points'`).get()
|
const exists = sqlite.prepare(`SELECT count(*) as a FROM sqlite_master WHERE type='table' AND name='points'`).get()
|
||||||
|
|
||||||
/* Generate the DB if false */
|
|
||||||
if(!exists.a)
|
if(!exists.a)
|
||||||
generateDB()
|
generateDB()
|
||||||
|
|
||||||
/* Init express */
|
|
||||||
const Server = express()
|
const Server = express()
|
||||||
Server.use('/api', api)
|
Server.use('/api', api)
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
import { sqlite, skinDB } from "./init.js"
|
import { sqlite, skinDB } from './init.js'
|
||||||
import tasks from "./tasks.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...
|
* This constructs the DB with indexes and rankings...
|
||||||
|
@ -7,18 +11,20 @@ import tasks from "./tasks.js"
|
||||||
*/
|
*/
|
||||||
export function generateDB() {
|
export function generateDB() {
|
||||||
/* TODO: Clean this up as it is a mess */
|
/* TODO: Clean this up as it is a mess */
|
||||||
console.log("Generating race index")
|
log("Generating race index...")
|
||||||
|
|
||||||
/* Generate race index TODO: Remove useless ones */
|
/* Generate race index TODO: Remove useless ones */
|
||||||
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_Map_2" ON "race" ("Map","Name")`)
|
execMany([
|
||||||
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_Name" ON "race" ("Name","Timestamp")`)
|
`CREATE INDEX IF NOT EXISTS "idx_race_Map_2" ON "race" ("Map","Name")`,
|
||||||
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_Server" ON "race" ("Server")`)
|
`CREATE INDEX IF NOT EXISTS "idx_race_Name" ON "race" ("Name","Timestamp")`,
|
||||||
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_MapTimestamp" ON "race" ("Map","Timestamp")`)
|
`CREATE INDEX IF NOT EXISTS "idx_race_Server" ON "race" ("Server")`,
|
||||||
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_Timestamp" ON "race" ("Timestamp")`)
|
`CREATE INDEX IF NOT EXISTS "idx_race_MapTimestamp" ON "race" ("Map","Timestamp")`,
|
||||||
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_MapNameTime" ON "race" ("Map", "Name", "Time")`)
|
`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 */
|
/* Create rankings table */
|
||||||
console.log("Creating rankings table")
|
log("Creating rankings table...")
|
||||||
sqlite.exec(`
|
sqlite.exec(`
|
||||||
CREATE TABLE IF NOT EXISTS "rankings" (
|
CREATE TABLE IF NOT EXISTS "rankings" (
|
||||||
"Map" varchar(128) NOT NULL,
|
"Map" varchar(128) NOT NULL,
|
||||||
|
@ -29,22 +35,22 @@ export function generateDB() {
|
||||||
"rank" INTEGER NOT NULL);
|
"rank" INTEGER NOT NULL);
|
||||||
`)
|
`)
|
||||||
|
|
||||||
console.log("Calculating rankings for each map")
|
log("Calculating rankings for each map...")
|
||||||
tasks.processRankings()
|
tasks.processRankings()
|
||||||
|
|
||||||
/* Generate rankings index */
|
/* Generate rankings index */
|
||||||
console.log("Generating 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_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_rank" ON "rankings" ("rank")`)
|
||||||
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_rankings_player" ON "rankings" ("Name")`)
|
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_rankings_player" ON "rankings" ("Name")`)
|
||||||
|
|
||||||
/* Generate teamrace index */
|
/* Generate teamrace index */
|
||||||
console.log("Generating 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_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_ID" ON "teamrace" ("ID")`);
|
||||||
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrace_MapID" ON "teamrace" ("Map", "ID")`);
|
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrace_MapID" ON "teamrace" ("Map", "ID")`);
|
||||||
|
|
||||||
console.log("Creating teamrankings table")
|
log("Creating teamrankings table...")
|
||||||
sqlite.exec(`
|
sqlite.exec(`
|
||||||
CREATE TABLE IF NOT EXISTS "teamrankings" (
|
CREATE TABLE IF NOT EXISTS "teamrankings" (
|
||||||
"Map" varchar(128) NOT NULL,
|
"Map" varchar(128) NOT NULL,
|
||||||
|
@ -56,10 +62,10 @@ export function generateDB() {
|
||||||
"teamrank" INTEGER NOT NULL);
|
"teamrank" INTEGER NOT NULL);
|
||||||
`)
|
`)
|
||||||
|
|
||||||
console.log("Calculating teamrankings for each map")
|
log("Calculating teamrankings for each map...")
|
||||||
tasks.processTeamRankings()
|
tasks.processTeamRankings()
|
||||||
|
|
||||||
console.log("Generating teamrankings index")
|
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_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_rank" ON "teamrankings" ("teamrank")`)
|
||||||
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrankings_player" ON "teamrankings" ("name")`)
|
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_teamrankings_player" ON "teamrankings" ("name")`)
|
||||||
|
@ -72,7 +78,7 @@ export function generateDB() {
|
||||||
`)
|
`)
|
||||||
|
|
||||||
/* Process all types of points */
|
/* Process all types of points */
|
||||||
console.log("Inserting points to DB")
|
log("Inserting points to DB...")
|
||||||
tasks.processAllPoints()
|
tasks.processAllPoints()
|
||||||
|
|
||||||
skinDB.exec(`
|
skinDB.exec(`
|
12
libs/db/helper.js
Normal file
12
libs/db/helper.js
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import { sqlite } from './init.js'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function takes an array of strings to be ran on the DB.
|
||||||
|
*
|
||||||
|
* @param {array} instructions Array of instructions to be ran.
|
||||||
|
* @author BurnyLlama
|
||||||
|
*/
|
||||||
|
export function execMany(instructions) {
|
||||||
|
for (const instruction of instructions)
|
||||||
|
sqlite.exec(instruction)
|
||||||
|
}
|
13
libs/utils/log.js
Normal file
13
libs/utils/log.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
/**
|
||||||
|
* This function creates a custom logging method that adds a prefix evrytime used.
|
||||||
|
* This is so that you can see what component has done what.
|
||||||
|
* Example:
|
||||||
|
* The database-component would log with the prefix 'database'
|
||||||
|
*
|
||||||
|
* @param {string} prefix The prefix for the logging-function.
|
||||||
|
* @returns {function} The created log function.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export default function initLog(prefix) {
|
||||||
|
return string => console.log(`${prefix} >>> ${string}`)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user