ddstats-server/libs/ddnss/handler.js

107 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-11-06 21:59:49 +01:00
import fs from 'fs'
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
2021-11-06 21:59:49 +01:00
/**
* This function takes a Teeworlds coutry code and
* maps it to a two letter country code instead
*
* @param number Teeworlds country code
*
* @returns Two letter country code
*/
export function mapCountryCode(code) {
const codes = fs.readFileSync('./libs/ddnss/countrycodes.json')
const json = JSON.parse(codes)
for (const country in json) {
if(country == code)
return json[country]
}
/* If none is found, just return default */
return "default"
}
2021-10-30 20:26:37 +02:00
export async function ddnssStart() {
const getServers = await download('https://ddnet.tw/status/index.json', "_RETURN_VALUE_")
2021-11-06 21:59:49 +01:00
const servers = await JSON.parse(getServers)
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
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-11-06 21:59:49 +01:00
if (skinData === null)
return resolve()
const stmt = skinDB.prepare
(`
INSERT INTO "skindata" VALUES
(
$timestamp,
$player,
$clan,
$flag,
$twFlag,
$skin,
$useColor,
$colorBodyRaw,
$colorBodyHex,
$colorFeetRaw,
$colorFeetHex
)
`)
2021-10-31 23:43:36 +01:00
const currentTime = Date.now()
2021-11-06 21:59:49 +01:00
for (const entry of skinData) {
stmt.run({
2021-11-01 01:05:37 +01:00
timestamp: currentTime,
player: entry.player,
clan: entry.clan,
2021-11-06 21:59:49 +01:00
flag: mapCountryCode(entry.flag),
twFlag: entry.flag,
2021-11-01 01:05:37 +01:00
skin: entry.skindata.skin,
useColor: entry.skindata.useColor,
colorBodyRaw: entry.skindata.colorBody.raw,
colorBodyHex: entry.skindata.colorBody.hex,
colorFeetRaw: entry.skindata.colorFeet.raw,
2021-11-06 21:59:49 +01:00
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
}