This commit is contained in:
furo 2021-11-01 22:52:26 +01:00
commit 213477e212
8 changed files with 60 additions and 47 deletions

4
.gitignore vendored
View File

@ -119,7 +119,5 @@ dist
package-lock.json package-lock.json
pnpm-lock.yaml pnpm-lock.yaml
*.sql*
players.msgpack
.env .env
ddnss/ data/*

View File

@ -1,11 +0,0 @@
# The database connect string
MONGO_URI = "mongodb://user:passwd@host/db"
# The port on which the server starts...
PORT = 12345
# Should the server try to generate the database?
GENERATE_DB = "false"
# The API paginates. How many entries per page?
ENTRIES_PER_PAGE = 50

View File

@ -1,15 +1,14 @@
import express from 'express' import express from 'express'
import dotenv from 'dotenv' import { config as loadEnv } from 'dotenv'
import api from './api/api.js' import api from './api/api.js'
import { generateDB } from './libs/database/generate.js' import { generateDB } from './libs/database/generate.js'
import { dbInit } from './libs/database/init.js' import { dbInit } from './libs/database/init.js'
dotenv.config() loadEnv()
dbInit() dbInit()
generateDB() generateDB()
const Server = express() const Server = express()
Server.use('/api', api) Server.use('/api', api)
Server.listen(process.env.PORT, () => console.log(`Server started and listening on port ${process.env.PORT}.`)) Server.listen(process.env.PORT ?? 12345, () => console.log(`Server started and listening on port ${process.env.PORT ?? 12345}.`))

View File

@ -0,0 +1,21 @@
import msgpack from '@msgpack/msgpack'
import fs from 'fs'
/**
* This module parses the msgpack provided by DDNet...
* @module db/decodeMsgpack
*/
export default function decodeMsgpack() {
const data = fs.readFileSync(process.env.MSGPACK_PATH ?? 'data/players.msgpack')
const decoded = msgpack.decodeMulti(data, { wrap: true })
const order = ['categories', 'maps', 'totalPoints', 'pointsRanks', 'pointsThisWeek', 'pointsThisMonth', 'teamRankPoints', 'rankPoints', 'serverRanks']
let final = {}
let i = 0
for (const part of decoded) {
final[order[i]] = part
++i
}
return final
}

View File

@ -67,7 +67,8 @@ export function generateDB() {
"time" float NOT NULL DEFAULT 0, "time" float NOT NULL DEFAULT 0,
"date" timestamp NOT NULL DEFAULT current_timestamp, "date" timestamp NOT NULL DEFAULT current_timestamp,
"server" char(4) NOT NULL DEFAULT '', "server" char(4) NOT NULL DEFAULT '',
"rank" INTEGER NOT NULL); "rank" INTEGER NOT NULL
)
`) `)
log("Calculating rankings for each map...") log("Calculating rankings for each map...")
@ -96,7 +97,8 @@ export function generateDB() {
"time" float NOT NULL DEFAULT 0, "time" float NOT NULL DEFAULT 0,
"date" timestamp NOT NULL DEFAULT current_timestamp, "date" timestamp NOT NULL DEFAULT current_timestamp,
"server" char(4) NOT NULL DEFAULT '', "server" char(4) NOT NULL DEFAULT '',
"teamrank" INTEGER NOT NULL); "teamrank" INTEGER NOT NULL
)
`) `)
log("Calculating teamrankings for each map...") log("Calculating teamrankings for each map...")
@ -116,13 +118,14 @@ export function generateDB() {
"player" varchar(16) NOT NULL, "player" varchar(16) NOT NULL,
"time" float NOT NULL DEFAULT 0, "time" float NOT NULL DEFAULT 0,
"date" timestamp NOT NULL DEFAULT current_timestamp, "date" timestamp NOT NULL DEFAULT current_timestamp,
"server" char(4) NOT NULL DEFAULT ''); "server" char(4) NOT NULL DEFAULT ''
)
`) `)
tasks.processTimeGraph() tasks.processTimeGraph()
execMany([ execMany([
`CREATE INDEX IF NOT EXISTS "idx_graphCache_player" ON "graphRecordCache" ("player")`, `CREATE INDEX IF NOT EXISTS "idx_graphCache_player" ON "graphRecordCache" ("player")`,
`CREATE INDEX IF NOT EXISTS "idx_graphCache_map" ON "graphRecordCache" ("map");` `CREATE INDEX IF NOT EXISTS "idx_graphCache_map" ON "graphRecordCache" ("map")`
]) ])
log("Inserting points to DB...") log("Inserting points to DB...")
@ -139,6 +142,7 @@ export function generateDB() {
"colorBodyRaw" INTEGER NOT NULL, "colorBodyRaw" INTEGER NOT NULL,
"colorBodyHex" varchar(8) NOT NULL, "colorBodyHex" varchar(8) NOT NULL,
"colorFeetRaw" INTEGER NOT NULL, "colorFeetRaw" INTEGER NOT NULL,
"colorFeetHex" varchar(8) NOT NULL); "colorFeetHex" varchar(8) NOT NULL
)
`) `)
} }

View File

@ -15,8 +15,8 @@ export function dbInit() {
log("Starting up databases...") log("Starting up databases...")
/* load in db using better-sqlite3 */ /* load in db using better-sqlite3 */
sqlite = new Database('ddnet.sqlite', { verbose: console.log }); sqlite = new Database(process.env.DDNET_SQLITE_PATH ?? 'data/ddnet.sqlite', { verbose: console.log });
skinDB = new Database('skindata.sqlite', { }); skinDB = new Database(process.env.DDNSS_SQLITE_PATH ?? 'data/skindata.sqlite', { });
/* WAL mode */ /* WAL mode */
sqlite.pragma('journal_mode = WAL'); sqlite.pragma('journal_mode = WAL');

View File

@ -1,27 +1,7 @@
import msgpack from '@msgpack/msgpack' import decodeMsgpack from './decodeMsgpack.js'
import fs from 'fs'
import { execMany } from './helper.js' import { execMany } from './helper.js'
import { sqlite } from './init.js' import { sqlite } from './init.js'
/**
* This module parses the msgpack provided by DDNet...
* @module libs/database/decodeMsgpack
*/
export function decodeMsgpack() {
const data = fs.readFileSync('players.msgpack')
const decoded = msgpack.decodeMulti(data, { wrap: true })
const order = ['categories', 'maps', 'totalPoints', 'pointsRanks', 'pointsThisWeek', 'pointsThisMonth', 'teamRankPoints', 'rankPoints', 'serverRanks']
let final = {}
let i = 0
for (const part of decoded) {
final[order[i]] = part
++i
}
return final
}
/** /**
* This generates rankings for each map... * This generates rankings for each map...
* @module libs/database/processRankings * @module libs/database/processRankings
@ -142,7 +122,7 @@ export function processAllPoints() {
"rank" INTEGER NOT NULL, "rank" INTEGER NOT NULL,
"player" varchar(16) NOT NULL, "player" varchar(16) NOT NULL,
"points" INTEGER NOT NULL "points" INTEGER NOT NULL
); )
`) `)
/* Insert data */ /* Insert data */

22
template.env Normal file
View File

@ -0,0 +1,22 @@
#
# You should copy this file to '.env'
# and set all settings there.
#
# MongoDB connection URI
MONGO_URI = "mongodb://user:passwd@host/db"
# Paths to SQLite databases...
DDNET_SQLITE_PATH = "data/ddnet.sqlite"
DDNSS_SQLITE_PATH = "data/skindata.sqlite"
MSGPACK_PATH = "data/players.msgpack"
# Should the server try to generate the database?
GENERATE_DB = "true"
# The port on which the server listens...
PORT = 12345
# The API paginates. How many entries per page?
ENTRIES_PER_PAGE = 50