The testRcon
function is an asynchronous module that retrieves a list of server masters, logs the list, sends an RCON command to the first master, waits for a second, and closes the UDP client, before being exported as a module. This function utilizes the serverApi
to interact with the Quake 3 server, including importing and executing specific functions for listing masters and sending RCON commands.
npm run import -- "test quake 3 rcon commands"
var importer = require('../Core')
var serverApi = importer.import("quake 3 server commands")
async function testRcon (command) {
var masters = await serverApi.listMasters(void 0, void 0, false)
console.log(masters)
await serverApi.sendRcon(masters[0].ip, masters[0].port, command)
await new Promise(resolve => setTimeout(resolve, 1000))
serverApi.udpClient.close()
}
module.exports = testRcon
// Import required modules
const { Core } = require('../Core');
const { Quake3ServerCommands } = Core.import('quake 3 server commands');
// Define the testRcon function
async function testRcon(command) {
/**
* Tests an RCON command on the first available master server.
*
* @param {string} command - The RCON command to send.
*
* @throws {Error} If the master server list is empty or the RCON command fails.
*/
try {
// Get the list of master servers
const masters = await Quake3ServerCommands.listMasters();
// If no master servers are available, throw an error
if (!masters.length) {
throw new Error('No master servers available');
}
// Get the first master server
const master = masters[0];
// Log the master server details
console.log(`Master Server: ${master.ip}:${master.port}`);
// Send the RCON command to the master server
await Quake3ServerCommands.sendRcon(master.ip, master.port, command);
// Pause for 1 second to allow the RCON command to be processed
await new Promise(resolve => setTimeout(resolve, 1000));
// Close the UDP client
await Quake3ServerCommands.udpClient.close();
} catch (error) {
// Log any errors that occur
console.error(error);
}
}
// Export the testRcon function
module.exports = testRcon;
Code Breakdown
var importer = require('../Core')
: Imports a module named Core
from the parent directory (../
) and assigns it to the variable importer
.var serverApi = importer.import('quake 3 server commands')
: Imports a specific module or set of functions from the Core
module, identified by the string 'quake 3 server commands'
, and assigns it to the variable serverApi
.testRcon
Functionasync function testRcon (command)
: Defines an asynchronous function named testRcon
that takes a single argument command
.var masters = await serverApi.listMasters(void 0, void 0, false)
: Uses the listMasters
function from the serverApi
to retrieve a list of server masters. The function returns a promise, which is awaited for the result. The void 0
arguments suggest that some parameters may be optional or do not apply in this case.console.log(masters)
: Logs the list of server masters to the console.await serverApi.sendRcon(masters[0].ip, masters[0].port, command)
: Uses the sendRcon
function from the serverApi
to send an RCON (Remote Console) command to the first server master's IP address and port. The command is passed as an argument, which is the function's argument command
.await new Promise(resolve => setTimeout(resolve, 1000))
: Waits for 1 second (1000 milliseconds) before proceeding.serverApi.udpClient.close()
: Closes the UDP client associated with the serverApi
.module.exports = testRcon
: Exports the testRcon
function as a module, making it available for import and use in other parts of the application.