ddstats-server/libs/ddnss/handler.js

87 lines
3.0 KiB
JavaScript
Raw Normal View History

2021-10-30 20:26:37 +02:00
import { exec } from 'child_process'
2021-10-31 23:43:36 +01:00
import { skinDB } from '../database/init.js'
import initLog from '../utils/log.js'
import { download } from '../download/dowload.js'
2021-10-31 23:43:36 +01:00
const log = initLog("DDNSS")
2021-10-30 20:26:37 +02:00
export async function ddnssStart() {
const getServers = await download('https://ddnet.tw/status/index.json', "_RETURN_VALUE_")
const servers = await getServers.json()
2021-10-31 23:43:36 +01:00
2021-11-01 18:12:46 +01:00
log(`Found ${servers.length} online servers!`)
2021-10-31 23:43:36 +01:00
for (const server of servers) {
const connection = `${server.ip}:${server.port}`
2021-10-30 20:26:37 +02:00
2021-11-01 01:05:37 +01:00
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
}
2021-10-30 20:26:37 +02:00
2021-10-31 23:43:36 +01:00
log(`Connecting to server >> ${connection}`)
await scrapeServer(`${connection}`)
2021-10-30 20:26:37 +02:00
}
2021-10-31 23:43:36 +01:00
2021-11-01 18:12:46 +01:00
// PLEASE!!
2021-10-31 20:46:43 +01:00
exec(`pkill -9 -f ddnss`)
2021-10-30 20:26:37 +02:00
}
2021-10-31 23:43:36 +01:00
function scrapeServer(server) {
2021-11-01 18:12:46 +01:00
// TODO: Maybe fix the paths to be dynamic? Or have some sort of buildscript...
// -- BurnyLlama
2021-10-31 23:43:36 +01:00
const command = `./ddnss/build/DDNet "ui_server_address ${server}" -f ddnss/build/config.conf`
2021-10-30 20:26:37 +02:00
2021-10-31 23:43:36 +01:00
return new Promise((resolve, reject) => {
exec(command, { encoding: 'utf8' }, (err, stdout, stderr) => {
2021-10-30 20:26:37 +02:00
try {
2021-10-31 23:43:36 +01:00
const skinData = JSON.parse(stdout)
2021-10-30 20:26:37 +02:00
2021-10-31 23:43:36 +01:00
if (skinData === null) {
resolve()
return
}
const currentTime = Date.now()
2021-11-01 01:05:37 +01:00
// TODO: Store statement once and reuse same statment. (whatever that means)
2021-10-31 23:43:36 +01:00
for (const entry of skinData)
2021-11-01 01:05:37 +01:00
skinDB.prepare(`
2021-10-31 23:43:36 +01:00
INSERT INTO "skindata"
(
2021-11-01 01:05:37 +01:00
$timestamp,
$player,
$clan,
$flag,
$skin,
$useColor,
$colorBodyRaw,
$colorBodyHex,
$colorFeetRaw,
$ColorFeetHex
)
2021-10-31 23:43:36 +01:00
`)
2021-11-01 01:05:37 +01:00
.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,
})
2021-10-31 23:43:36 +01:00
} catch (e) {
log(`Failed to handle ${server}!`)
2021-10-30 20:26:37 +02:00
}
2021-10-31 23:43:36 +01:00
resolve()
2021-10-30 20:26:37 +02:00
})
})
2021-10-31 20:46:43 +01:00
}