Files
kickkingsapi/helpers.ts
T
2026-05-02 20:22:35 +00:00

30 lines
919 B
TypeScript

import { spawn } from 'child_process';
import { logGameExit, logGameStderr } from './server_log';
export function GetRandomPort(min: number, max: number): number {
return randomIntFromInterval(min, max);
}
export function OpenGameInstance(path: string, port: number, matchId?: number): void {
const args = ["-port", port.toString()];
if (matchId != null && Number.isFinite(matchId)) {
args.push("-matchId", String(Math.trunc(matchId)));
}
const child = spawn(path, args);
child.stdout.on('data', (data: Buffer) => {
// console.log('stdout: ' + data);
});
child.stderr.on('data', (data: Buffer) => {
logGameStderr(port, data);
});
child.on('close', (code: number | null) => {
logGameExit(port, code);
});
}
function randomIntFromInterval(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1) + min);
}