ddstats-server/libs/ddnss/handler.js

87 lines
3.0 KiB
JavaScript

import { exec } from 'child_process'
import { skinDB } from '../database/init.js'
import initLog from '../utils/log.js'
import { download } from '../download/dowload.js'
const log = initLog("DDNSS")
export async function ddnssStart() {
const getServers = await download('https://ddnet.tw/status/index.json', "_RETURN_VALUE_")
const servers = await getServers.json()
log(`Found ${servers.length} online servers!`)
for (const server of servers) {
const connection = `${server.ip}:${server.port}`
if (!(server.num_clients > 0 && server.num_clients < (server.max_clients - 2))) {
log(`Server (essentially) full! >> ${connection} -> ${server.num_clients}/${server.max_clients} clients`)
continue
}
if(server.password) {
log(`Server is locked >> ${connection}`)
continue
}
log(`Connecting to server >> ${connection}`)
await scrapeServer(`${connection}`)
}
// PLEASE!!
exec(`pkill -9 -f ddnss`)
}
function scrapeServer(server) {
// TODO: Maybe fix the paths to be dynamic? Or have some sort of buildscript...
// -- BurnyLlama
const command = `./ddnss/build/DDNet "ui_server_address ${server}" -f ddnss/build/config.conf`
return new Promise((resolve, reject) => {
exec(command, { encoding: 'utf8' }, (err, stdout, stderr) => {
try {
const skinData = JSON.parse(stdout)
if (skinData === null) {
resolve()
return
}
const currentTime = Date.now()
// TODO: Store statement once and reuse same statment. (whatever that means)
for (const entry of skinData)
skinDB.prepare(`
INSERT INTO "skindata"
(
$timestamp,
$player,
$clan,
$flag,
$skin,
$useColor,
$colorBodyRaw,
$colorBodyHex,
$colorFeetRaw,
$ColorFeetHex
)
`)
.run({
timestamp: currentTime,
player: entry.player,
clan: entry.clan,
flag: entry.flag,
skin: entry.skindata.skin,
useColor: entry.skindata.useColor,
colorBodyRaw: entry.skindata.colorBody.raw,
colorBodyHex: entry.skindata.colorBody.hex,
colorFeetRaw: entry.skindata.colorFeet.raw,
colorFeetHex: entry.skindata.colorFeet.hex,
})
} catch (e) {
log(`Failed to handle ${server}!`)
}
resolve()
})
})
}