Started some refactoring...

This commit is contained in:
BurnyLlama 2021-10-31 21:24:55 +01:00
parent dfb54ba50e
commit 62dd9a4db0
11 changed files with 55 additions and 29 deletions

View File

@ -1,5 +1,5 @@
import { Router } from 'express'
import { sqlite } from '../db/init.js'
import { sqlite } from '../libs/db/init.js'
const finishApi = Router()

View File

@ -1,5 +1,5 @@
import { Router } from 'express'
import { sqlite } from '../db/init.js'
import { sqlite } from '../libs/db/init.js'
const graphApi = Router()

View File

@ -1,5 +1,5 @@
import { Router } from 'express'
import { sqlite } from '../db/init.js'
import { sqlite } from '../libs/db/init.js'
const mapApi = Router()

View File

@ -1,5 +1,5 @@
import { Router } from 'express'
import { sqlite } from '../db/init.js'
import { sqlite } from '../libs/db/init.js'
const playerApi = Router()

View File

@ -1,25 +1,20 @@
import express from 'express'
import dotenv from 'dotenv'
import api from './api/api.js'
import { generateDB } from "./db/generate.js"
import { sqlite, dbInit } from "./db/init.js"
import { ddnssStart, scrapeServer } from './ddnss/handler.js'
import { generateDB } from "./libs/db/generate.js"
import { sqlite, dbInit } from "./libs/db/init.js"
import { ddnssStart, scrapeServer } from './libs/ddnss/handler.js'
//import tasks from './db/tasks.js'
/* Read the .env file */
dotenv.config()
/* Init db */
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()
/* Generate the DB if false */
if(!exists.a)
generateDB()
/* Init express */
const Server = express()
Server.use('/api', api)

View File

@ -1,5 +1,9 @@
import { sqlite, skinDB } from "./init.js"
import tasks from "./tasks.js"
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...
@ -7,18 +11,20 @@ import tasks from "./tasks.js"
*/
export function generateDB() {
/* 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 */
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_Map_2" ON "race" ("Map","Name")`)
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_Name" ON "race" ("Name","Timestamp")`)
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_Server" ON "race" ("Server")`)
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_MapTimestamp" ON "race" ("Map","Timestamp")`)
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_Timestamp" ON "race" ("Timestamp")`)
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_race_MapNameTime" ON "race" ("Map", "Name", "Time")`)
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 */
console.log("Creating rankings table")
log("Creating rankings table...")
sqlite.exec(`
CREATE TABLE IF NOT EXISTS "rankings" (
"Map" varchar(128) NOT NULL,
@ -29,22 +35,22 @@ export function generateDB() {
"rank" INTEGER NOT NULL);
`)
console.log("Calculating rankings for each map")
log("Calculating rankings for each map...")
tasks.processRankings()
/* 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_rank" ON "rankings" ("rank")`)
sqlite.exec(`CREATE INDEX IF NOT EXISTS "idx_rankings_player" ON "rankings" ("Name")`)
/* 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_ID" ON "teamrace" ("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(`
CREATE TABLE IF NOT EXISTS "teamrankings" (
"Map" varchar(128) NOT NULL,
@ -56,10 +62,10 @@ export function generateDB() {
"teamrank" INTEGER NOT NULL);
`)
console.log("Calculating teamrankings for each map")
log("Calculating teamrankings for each map...")
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_rank" ON "teamrankings" ("teamrank")`)
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 */
console.log("Inserting points to DB")
log("Inserting points to DB...")
tasks.processAllPoints()
skinDB.exec(`

12
libs/db/helper.js Normal file
View 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
View 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}`)
}