Merge branch 'sqlite' of https://git.qwik.space/BurnyLlama/ddstats-server into sqlite
This commit is contained in:
commit
213477e212
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -119,7 +119,5 @@ dist
|
||||||
|
|
||||||
package-lock.json
|
package-lock.json
|
||||||
pnpm-lock.yaml
|
pnpm-lock.yaml
|
||||||
*.sql*
|
|
||||||
players.msgpack
|
|
||||||
.env
|
.env
|
||||||
ddnss/
|
data/*
|
|
@ -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
|
|
7
index.js
7
index.js
|
@ -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}.`))
|
||||||
|
|
21
libs/database/decodeMsgpack.js
Normal file
21
libs/database/decodeMsgpack.js
Normal 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
|
||||||
|
}
|
|
@ -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
|
||||||
|
)
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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');
|
||||||
|
|
|
@ -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
22
template.env
Normal 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
|
Loading…
Reference in New Issue
Block a user