remove client and websocket
This commit is contained in:
@@ -1,134 +0,0 @@
|
||||
import { Router } from 'express'
|
||||
import { param } from 'express-validator'
|
||||
import { validateErrors } from '../common'
|
||||
import db, { getHostList } from '../db/db'
|
||||
import { sql } from 'kysely'
|
||||
import { allData } from '../process/process'
|
||||
|
||||
const { max, sum } = db.fn
|
||||
const router = Router()
|
||||
// timeout middleware ?
|
||||
router.get('/*', (req, res, next) => {
|
||||
next()
|
||||
})
|
||||
|
||||
const queryFilters = {
|
||||
player: 'attacker_id',
|
||||
server: 'servername',
|
||||
map: 'map',
|
||||
weapon: 'cause_of_death',
|
||||
gamemode: 'game_mode',
|
||||
host: 'host'
|
||||
} as const
|
||||
|
||||
const path = {
|
||||
player: 'attacker_id',
|
||||
servers: 'servername',
|
||||
maps: 'map',
|
||||
weapons: 'cause_of_death',
|
||||
gamemodes: 'game_mode',
|
||||
hosts: 'host'
|
||||
} as const
|
||||
|
||||
interface KillRecord {
|
||||
kills: number
|
||||
deaths: number
|
||||
deaths_while_equipped: number
|
||||
username?: string
|
||||
max_distance: number
|
||||
total_distance: number
|
||||
}
|
||||
|
||||
router.get('/hosts', (_req, res) => {
|
||||
void (async () => {
|
||||
const result = await getHostList()
|
||||
const data: Record<number, string> = {}
|
||||
result.forEach((e) => (data[Number(e.host_id)] = e.host_name))
|
||||
|
||||
const dataString = JSON.stringify(data)
|
||||
const buffer = Buffer.from(dataString)
|
||||
const size = buffer.length
|
||||
res.status(200).setHeader('X-File-Size', size).setHeader('Content-Type', 'application/json').send(buffer)
|
||||
})()
|
||||
})
|
||||
|
||||
function processQueryArgs (data: ReturnType<typeof db.selectFrom<'kill_view'>>, queryArgs: string | string[]): ReturnType<typeof db.selectFrom<'kill_view'>> {
|
||||
Object.entries(queryArgs).forEach(([one, two]) => {
|
||||
if (!two) return
|
||||
if (!(one in queryFilters)) return
|
||||
const key = one as keyof typeof queryFilters
|
||||
if (Array.isArray(two)) {
|
||||
data = data.where((qb) => {
|
||||
(two as string[]).forEach((e) => { qb = qb.orWhere(queryFilters[key], '=', e) })
|
||||
return qb
|
||||
})
|
||||
} else {
|
||||
data = data.where(queryFilters[key], '=', two)
|
||||
}
|
||||
})
|
||||
return data
|
||||
}
|
||||
|
||||
router.get('/players',
|
||||
(req, res) => {
|
||||
void (async () => {
|
||||
const result = db.selectFrom('kill_view')
|
||||
let data
|
||||
const resultFiltered = processQueryArgs(result, req.query as unknown as string | string[])
|
||||
// Cache stuff when no route is present so request doesn't takes 4secs to complete
|
||||
if (result !== resultFiltered) {
|
||||
const selection = resultFiltered.select([sum<number>('kills').as('kills'), sum<number>('deaths').as('deaths'), sum<number>('deaths_with_weapon').as('deaths_while_equipped'), 'attacker_id', sql<string>`last(attacker_name)`.as('username'), sum<number>('total_distance').as('total_distance'), max('max_distance').as('max_distance')]).groupBy('attacker_id')
|
||||
data = (await selection.execute()).reduce<Record<string, KillRecord>>((acc, curr) => {
|
||||
acc[curr.attacker_id] = {
|
||||
kills: Number(curr.kills),
|
||||
deaths: Number(curr.deaths),
|
||||
max_distance: Number(curr.max_distance),
|
||||
total_distance: Number(curr.total_distance),
|
||||
deaths_while_equipped: Number(curr.deaths_while_equipped),
|
||||
username: curr.username
|
||||
}
|
||||
return acc
|
||||
}, {})
|
||||
} else {
|
||||
data = allData
|
||||
}
|
||||
|
||||
const dataString = JSON.stringify(data)
|
||||
const buffer = Buffer.from(dataString)
|
||||
const size = buffer.length
|
||||
res.status(200).setHeader('X-File-Size', size).setHeader('Content-Type', 'application/json').send(buffer)
|
||||
})()
|
||||
})
|
||||
|
||||
router.get('/:dataType',
|
||||
param('dataType')
|
||||
.custom((e) => e in path)
|
||||
.withMessage('Only weapons, players, maps, gamemodes or servers are valid paths'),
|
||||
validateErrors,
|
||||
(req, res) => {
|
||||
void (async () => {
|
||||
let result = db.selectFrom('kill_view')
|
||||
if (req.query) {
|
||||
result = processQueryArgs(result, req.query as unknown as string | string[])
|
||||
}
|
||||
const dataType = req.params.dataType as keyof typeof path
|
||||
|
||||
const selection = result.select([sum<number>('kills').as('kills'), sum<number>('deaths').as('deaths'), sum<number>('deaths_with_weapon').as('deaths_while_equipped'), path[dataType], sum<number>('total_distance').as('total_distance'), max('max_distance').as('max_distance')]).groupBy(path[dataType])
|
||||
const data = (await selection.execute()).reduce<Record<string, KillRecord>>((acc, curr) => {
|
||||
acc[curr[path[dataType]]] = {
|
||||
kills: Number(curr.kills),
|
||||
deaths: Number(curr.deaths),
|
||||
max_distance: Number(curr.max_distance),
|
||||
total_distance: Number(curr.total_distance),
|
||||
deaths_while_equipped: Number(curr.deaths_while_equipped)
|
||||
}
|
||||
return acc
|
||||
}, {})
|
||||
const dataString = JSON.stringify(data)
|
||||
const buffer = Buffer.from(dataString)
|
||||
const size = buffer.length
|
||||
res.status(200).setHeader('X-File-Size', size).setHeader('Content-Type', 'application/json').send(buffer)
|
||||
})()
|
||||
})
|
||||
|
||||
export default router
|
||||
@@ -1,51 +0,0 @@
|
||||
import * as dotenv from 'dotenv'
|
||||
import cluster from 'cluster'
|
||||
import os from 'os'
|
||||
import express from 'express'
|
||||
import cors from 'cors'
|
||||
import client from './client/client'
|
||||
import { dbReady } from './db/db'
|
||||
import processAll from './process/process'
|
||||
import listenKills from './process/onKill'
|
||||
dotenv.config()
|
||||
|
||||
const cCPUs = os.cpus().length
|
||||
|
||||
const port = 3000
|
||||
|
||||
if (cluster.isPrimary && process.env.ENVIRONMENT !== 'dev') {
|
||||
// Create a worker for each CPU
|
||||
for (let i = 0; i < cCPUs; i++) {
|
||||
cluster.fork()
|
||||
}
|
||||
cluster.on('online', function (worker) {
|
||||
console.log(`Worker ${worker.process.pid ?? ''} is online`)
|
||||
})
|
||||
cluster.on('exit', function (worker, code, signal) {
|
||||
console.log(`Worker ${worker.process.pid ?? ''} died`)
|
||||
})
|
||||
}
|
||||
|
||||
export default
|
||||
new Promise((resolve, reject) => {
|
||||
void (async () => {
|
||||
if (!cluster.isPrimary || process.env.ENVIRONMENT === 'dev') {
|
||||
const app = express()
|
||||
app.use(express.json())
|
||||
app.use(cors({ exposedHeaders: ['X-File-Size', 'Content-Encoding'] }))
|
||||
app.get('/', (req, res) => {
|
||||
res.send('Tone client API online')
|
||||
})
|
||||
|
||||
app.use('/', client)
|
||||
|
||||
await dbReady()
|
||||
await processAll()
|
||||
await listenKills()
|
||||
const listenServer = app.listen(port, '0.0.0.0', () => {
|
||||
console.log(`Tone client api listening on port ${port}`)
|
||||
resolve(listenServer)
|
||||
})
|
||||
}
|
||||
})()
|
||||
})
|
||||
@@ -1,56 +0,0 @@
|
||||
import { Client } from 'pg'
|
||||
import { allData } from './process'
|
||||
|
||||
export const pgClient = new Client({
|
||||
host: process.env.POSTGRES_HOST,
|
||||
database: process.env.POSTGRES_DATABASE,
|
||||
user: process.env.POSTGRES_USER,
|
||||
password: process.env.POSTGRES_PASSWORD
|
||||
})
|
||||
|
||||
export default async function listenKills () {
|
||||
await pgClient.connect()
|
||||
await pgClient.query('LISTEN new_kill')
|
||||
|
||||
pgClient.on('notification', (data) => {
|
||||
if (!data.payload) return
|
||||
const payload = JSON.parse(data.payload)
|
||||
let killEntry = allData[payload.attacker_id]
|
||||
let deathEntry = allData[payload.victim_id]
|
||||
|
||||
if (!killEntry) {
|
||||
killEntry = {
|
||||
kills: 0,
|
||||
deaths: 0,
|
||||
max_distance: 0,
|
||||
total_distance: 0,
|
||||
deaths_while_equipped: 0,
|
||||
username: payload.attacker_name
|
||||
}
|
||||
allData[payload.attacker_id] = killEntry
|
||||
}
|
||||
|
||||
if (!deathEntry) {
|
||||
deathEntry = {
|
||||
kills: 0,
|
||||
deaths: 0,
|
||||
max_distance: 0,
|
||||
total_distance: 0,
|
||||
deaths_while_equipped: 0,
|
||||
username: payload.attacker_name
|
||||
}
|
||||
allData[payload.victim_id] = deathEntry
|
||||
}
|
||||
|
||||
if (payload.victim_id !== payload.attacker_id) {
|
||||
killEntry.kills++
|
||||
killEntry.total_distance = Number(killEntry.total_distance) + Number(payload.distance)
|
||||
killEntry.max_distance = Math.max(killEntry.max_distance || 0, payload.distance)
|
||||
}
|
||||
deathEntry.deaths++
|
||||
deathEntry.deaths_while_equipped++
|
||||
deathEntry.username = payload.victim_name
|
||||
killEntry.username = payload.attacker_name
|
||||
})
|
||||
return pgClient
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
import db from '../db/db'
|
||||
import { sql } from 'kysely'
|
||||
const { max, sum } = db.fn
|
||||
|
||||
export let allData: Awaited<ReturnType<typeof processGlobalStats>>
|
||||
|
||||
/**
|
||||
* populates allData
|
||||
* @returns
|
||||
*/
|
||||
async function processAll (): Promise<void> {
|
||||
console.log('Starting data calculation...')
|
||||
const timeStart = new Date()
|
||||
|
||||
allData = await processGlobalStats()
|
||||
|
||||
console.log(`Data calculation finished. Took + ${Math.abs(new Date().getTime() - timeStart.getTime()) / 1000} seconds`)
|
||||
}
|
||||
|
||||
interface KillRecord {
|
||||
kills: number
|
||||
deaths: number
|
||||
deaths_while_equipped: number
|
||||
username?: string
|
||||
max_distance: number
|
||||
total_distance: number
|
||||
}
|
||||
|
||||
async function processGlobalStats () {
|
||||
const data = await db.selectFrom('kill_view').select(['attacker_id', sql<string>`last(attacker_name)`.as('username'), sum('kills').as('kills'), sum('deaths').as('deaths'), sum('deaths_with_weapon').as('deaths_while_equipped'), sum('total_distance').as('total_distance'), max('max_distance').as('max_distance')]).groupBy('attacker_id').execute()
|
||||
return data.reduce<Record<string, KillRecord>>((acc, curr) => {
|
||||
acc[curr.attacker_id] = {
|
||||
kills: Number(curr.kills),
|
||||
deaths: Number(curr.deaths),
|
||||
max_distance: Number(curr.max_distance),
|
||||
total_distance: Number(curr.total_distance),
|
||||
deaths_while_equipped: Number(curr.deaths_while_equipped),
|
||||
username: curr.username
|
||||
}
|
||||
return acc
|
||||
}, {})
|
||||
}
|
||||
export default processAll
|
||||
@@ -1,78 +0,0 @@
|
||||
import * as dotenv from 'dotenv'
|
||||
import { Client } from 'pg'
|
||||
import { type WebSocket, WebSocketServer } from 'ws'
|
||||
import { type KillTable } from './db/model'
|
||||
dotenv.config()
|
||||
|
||||
const port = 3002
|
||||
const wss = new WebSocketServer({ port })
|
||||
|
||||
export const pgClient = new Client({
|
||||
host: process.env.POSTGRES_HOST,
|
||||
database: process.env.POSTGRES_DATABASE,
|
||||
user: process.env.POSTGRES_USER,
|
||||
password: process.env.POSTGRES_PASSWORD
|
||||
})
|
||||
|
||||
wss.on('connection', function connection (ws: WebSocket & { isAlive: boolean }, req) {
|
||||
const ip = req?.headers['x-forwarded-for']?.toString().split(',')[0].trim() ??
|
||||
req.socket.remoteAddress ?? 'ip_unknown'
|
||||
console.log(`${new Date().toLocaleString()},${ip},connect,${wss.clients.size}`)
|
||||
ws.onclose = function () {
|
||||
console.log(`${new Date().toLocaleString()},${ip},connect,${wss.clients.size}`)
|
||||
}
|
||||
ws.onmessage = function (msg) {
|
||||
if (msg.data === 'pong') {
|
||||
ws.isAlive = true
|
||||
return
|
||||
}
|
||||
if (msg.data === 'ping') {
|
||||
ws.send('pong')
|
||||
}
|
||||
}
|
||||
ws.send('ping')
|
||||
})
|
||||
|
||||
const interval = setInterval(function ping () {
|
||||
wss.clients.forEach(function each (ws) {
|
||||
if (!(ws as WebSocket & { isAlive: boolean }).isAlive) { ws.terminate(); return }
|
||||
|
||||
(ws as WebSocket & { isAlive: boolean }).isAlive = false
|
||||
ws.send('ping')
|
||||
})
|
||||
}, 30000)
|
||||
|
||||
wss.on('close', function close () {
|
||||
clearInterval(interval)
|
||||
})
|
||||
|
||||
export default async () => {
|
||||
await pgClient.connect()
|
||||
await pgClient.query('LISTEN new_kill')
|
||||
|
||||
pgClient.on('notification', (data) => {
|
||||
if (!data.payload) return
|
||||
const payload = JSON.parse(data.payload) as KillTable
|
||||
wss.clients.forEach(function (client) {
|
||||
if (client.readyState === client.OPEN) {
|
||||
client.send(
|
||||
JSON.stringify({
|
||||
match_id: payload.match_id,
|
||||
attacker_id: payload.attacker_id
|
||||
// attacker_name: payload.attacker_name,
|
||||
// cause_of_death: payload.cause_of_death,
|
||||
// victim_id: payload.victim_id,
|
||||
// victim_name: payload.victim_name,
|
||||
// attacker_current_weapon: payload.attacker_current_weapon,
|
||||
// victim_current_weapon: payload.victim_current_weapon,
|
||||
// distance: payload.distance,
|
||||
// game_mode: payload.game_mode,
|
||||
// servername: payload.servername,
|
||||
// map: payload.map,
|
||||
// host: payload.host
|
||||
})
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user