Improved command execution on server

This commit is contained in:
Shobhit Pathak
2023-10-11 23:14:29 +05:30
parent e9c9ca8d34
commit d277b8106c
8 changed files with 209 additions and 81 deletions

View File

@@ -15,16 +15,25 @@ router.post('/api/setup-game', is_authenticated, async (req, res) => {
const team2 = req.body.team2;
const selected_map = req.body.selectedMap;
const game_mode = req.body.game_mode.toString();
rcon.rcons[server_id].execute(`mp_teamname_1 "${team1}"`);
rcon.rcons[server_id].execute(`mp_teamname_2 "${team2}"`);
rcon.rcons[server_id].execute(`game_mode ${game_mode}`);
// rcon.rcons[server_id].execute(`mp_teamname_1 "${team1}"`);
// rcon.rcons[server_id].execute(`mp_teamname_2 "${team2}"`);
// rcon.rcons[server_id].execute(`game_mode ${game_mode}`);
if (team1.trim() != "") {
await rcon.execute_command(server_id, `mp_teamname_1 "${team1}"`);
}
if (team2.trim() != "") {
await rcon.execute_command(server_id, `mp_teamname_2 "${team2}"`);
}
await rcon.execute_command(server_id, `game_mode ${game_mode}`);
if (game_mode == "1") {
execute_cfg_on_server(server_id, './cfg/live.cfg');
} else if (game_mode == "2") {
execute_cfg_on_server(server_id, './cfg/live_wingman.cfg');
}
rcon.rcons[server_id].execute(`mp_warmup_pausetimer 1`);
rcon.rcons[server_id].execute(`changelevel ${selected_map}`);
// rcon.rcons[server_id].execute(`mp_warmup_pausetimer 1`);
// rcon.rcons[server_id].execute(`changelevel ${selected_map}`);
await rcon.execute_command(server_id, `mp_warmup_pausetimer 1`);
await rcon.execute_command(server_id, `changelevel ${selected_map}`);
// Adding 1 second delay in executing warmup.cfg to make it effective after map has been changed.
setTimeout(() => {
@@ -41,7 +50,8 @@ router.post('/api/setup-game', is_authenticated, async (req, res) => {
router.post('/api/restart', is_authenticated, async (req, res) => {
try {
const server_id = req.body.server_id;
rcon.rcons[server_id].execute('mp_restartgame 1');
// rcon.rcons[server_id].execute('mp_restartgame 1');
await rcon.execute_command(server_id, `mp_restartgame 1`);
return res.status(200).json({ message: 'Game restarted' });
} catch (error) {
console.log(error);
@@ -49,10 +59,11 @@ router.post('/api/restart', is_authenticated, async (req, res) => {
}
});
router.post('/api/start-warmup', is_authenticated, (req, res) => {
router.post('/api/start-warmup', is_authenticated, async (req, res) => {
try {
const server_id = req.body.server_id;
rcon.rcons[server_id].execute('mp_restartgame 1');
// rcon.rcons[server_id].execute('mp_restartgame 1');
await rcon.execute_command(server_id, `mp_restartgame 1`);
execute_cfg_on_server(server_id, './cfg/warmup.cfg');
return res.status(200).json({ message: 'Warmup started!' });
@@ -65,8 +76,10 @@ router.post('/api/start-warmup', is_authenticated, (req, res) => {
router.post('/api/start-knife', is_authenticated, async (req, res) => {
try {
const server_id = req.body.server_id;
rcon.rcons[server_id].execute('mp_warmup_end');
rcon.rcons[server_id].execute('mp_restartgame 1');
// rcon.rcons[server_id].execute('mp_warmup_end');
// rcon.rcons[server_id].execute('mp_restartgame 1');
await rcon.execute_command(server_id, `mp_warmup_end`);
await rcon.execute_command(server_id, `mp_restartgame 1`);
execute_cfg_on_server(server_id, './cfg/knife.cfg');
return res.status(200).json({ message: 'Knife started!' });
@@ -79,7 +92,8 @@ router.post('/api/start-knife', is_authenticated, async (req, res) => {
router.post('/api/swap-team', is_authenticated, async (req, res) => {
try {
const server_id = req.body.server_id;
rcon.rcons[server_id].execute('mp_swapteams');
// rcon.rcons[server_id].execute('mp_swapteams');
await rcon.execute_command(server_id, `mp_swapteams`);
return res.status(200).json({ message: 'Teams Swapped!' });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
@@ -89,8 +103,10 @@ router.post('/api/swap-team', is_authenticated, async (req, res) => {
router.post('/api/go-live', is_authenticated, async (req, res) => {
try {
const server_id = req.body.server_id;
rcon.rcons[server_id].execute('mp_warmup_end');
const response = await rcon.rcons[server_id].execute('game_mode');
// rcon.rcons[server_id].execute('mp_warmup_end');
await rcon.execute_command(server_id, `mp_warmup_end`);
// const response = await rcon.rcons[server_id].execute('game_mode');
const response = await rcon.execute_command(server_id, `game_mode`);
const game_mode = response.split("=")[1].trim().toString();
if (game_mode == "1") {
console.log("Executing live.cfg")
@@ -99,7 +115,8 @@ router.post('/api/go-live', is_authenticated, async (req, res) => {
console.log("Executing live_wingman.cfg")
execute_cfg_on_server(server_id, './cfg/live_wingman.cfg');
}
rcon.rcons[server_id].execute('mp_restartgame 1');
// rcon.rcons[server_id].execute('mp_restartgame 1');
await rcon.execute_command(server_id, `mp_restartgame 1`);
return res.status(200).json({ message: 'Match is live!!' });
} catch (error) {
@@ -112,7 +129,8 @@ router.post('/api/go-live', is_authenticated, async (req, res) => {
router.post('/api/list-backups', is_authenticated, async (req, res) => {
try {
const server_id = req.body.server_id;
const response = await rcon.rcons[server_id].execute('mp_backup_restore_list_files');
// const response = await rcon.rcons[server_id].execute('mp_backup_restore_list_files');
const response = await rcon.execute_command(server_id, "mp_backup_restore_list_files");
console.log('Server response:', response);
return res.status(200).json({ message: response });
} catch (error) {
@@ -130,8 +148,10 @@ router.post('/api/restore-round', is_authenticated, async (req, res) => {
round_number = "0" + round_number;
}
console.log(`SENDING mp_backup_restore_load_file backup_round${round_number}.txt`)
rcon.rcons[server_id].execute(`mp_backup_restore_load_file backup_round${round_number}.txt`);
rcon.rcons[server_id].execute('mp_pause_match');
// rcon.rcons[server_id].execute(`mp_backup_restore_load_file backup_round${round_number}.txt`);
// rcon.rcons[server_id].execute('mp_pause_match');
await rcon.execute_command(server_id, `mp_backup_restore_load_file backup_round${round_number}.txt`);
await rcon.execute_command(server_id, `mp_pause_match`);
return res.status(200).json({ message: 'Round Restored!' });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
@@ -141,11 +161,14 @@ router.post('/api/restore-round', is_authenticated, async (req, res) => {
router.post('/api/restore-latest-backup', is_authenticated, async (req, res) => {
try {
const server_id = req.body.server_id;
const response = await rcon.rcons[server_id].execute('mp_backup_round_file_last');
// const response = await rcon.rcons[server_id].execute('mp_backup_round_file_last');
const response = await rcon.execute_command(server_id, `mp_backup_round_file_last`);
const last_round_file = response.split("=")[1].trim().toString();
if (last_round_file.includes('.txt')) {
rcon.rcons[server_id].execute(`mp_backup_restore_load_file ${last_round_file}`);
rcon.rcons[server_id].execute('mp_pause_match');
// rcon.rcons[server_id].execute(`mp_backup_restore_load_file ${last_round_file}`);
// rcon.rcons[server_id].execute('mp_pause_match');
await rcon.execute_command(server_id, `mp_backup_restore_load_file ${last_round_file}`);
await rcon.execute_command(server_id, `mp_pause_match`);
return res.status(200).json({ message: `Latest Round Restored! (${last_round_file})` });
} else {
return res.status(200).json({ message: 'No latest backup found!' });
@@ -161,7 +184,8 @@ router.post('/api/restore-latest-backup', is_authenticated, async (req, res) =>
router.post('/api/pause', is_authenticated, async (req, res) => {
try {
const server_id = req.body.server_id;
rcon.rcons[server_id].execute('mp_pause_match');
// rcon.rcons[server_id].execute('mp_pause_match');
const response = await rcon.execute_command(server_id, 'mp_pause_match');
return res.status(200).json({ message: 'Game paused' });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
@@ -172,7 +196,8 @@ router.post('/api/pause', is_authenticated, async (req, res) => {
router.post('/api/unpause', is_authenticated, async (req, res) => {
try {
const server_id = req.body.server_id;
rcon.rcons[server_id].execute('mp_unpause_match');
// rcon.rcons[server_id].execute('mp_unpause_match');
const response = await rcon.execute_command(server_id, 'mp_unpause_match');
return res.status(200).json({ message: 'Game unpaused' });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
@@ -184,28 +209,9 @@ router.post('/api/rcon', is_authenticated, async (req, res) => {
const server_id = req.body.server_id;
const command = req.body.command;
// Wrap the await call in a Promise and add a timeout
const executePromise = new Promise(async (resolve, reject) => {
try {
const response = await Promise.race([
rcon.rcons[server_id].execute(command),
new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ error: 'Command execution timed out' });
}, 1000); // 1 seconds timeout
}),
]);
resolve(response);
} catch (error) {
reject(error);
}
});
const response = await rcon.execute_command(server_id, command);
// Wait for the wrapped Promise to resolve
const response = await executePromise;
console.log(response)
// Check if the result is an error or the actual response
if (response.error) {
if (response == 200) {
return res.status(200).json({ message: 'Command sent!' });
}
@@ -215,6 +221,18 @@ router.post('/api/rcon', is_authenticated, async (req, res) => {
}
});
router.post('/api/say-admin', is_authenticated, async (req, res) => {
try {
const server_id = req.body.server_id;
const message = req.body.message;
const message_to_send = "say " + message;
await rcon.execute_command(server_id, message_to_send);
return res.status(200).json({ message: 'Message sent!' });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
function check_whitelisted_players() {
rcon.rcons[server_id].execute('status_json')
.then((response) => {
@@ -259,7 +277,7 @@ function splitByByteLength(data, length) {
return exportedLines;
}
function execute_cfg_on_server(server_id, cfg_path) {
async function execute_cfg_on_server(server_id, cfg_path) {
fs.readFile(cfg_path, 'utf8', (err, data) => {
if (err) {
@@ -281,16 +299,17 @@ function execute_cfg_on_server(server_id, cfg_path) {
}
const exported_lines = splitByByteLength(data, 512)
function execute_next_item(item) {
async function execute_next_item(item) {
try {
if (item < exported_lines.length) {
console.log(exported_lines[item]);
rcon.rcons[server_id].execute(exported_lines[item]);
console.log("Executing on server:", exported_lines[item]);
// rcon.rcons[server_id].execute(exported_lines[item]);
await rcon.execute_command(server_id, exported_lines[item]);
// Wait for 200ms before moving to the next iteration
// Wait for 100ms before moving to the next iteration
setTimeout(() => {
execute_next_item(item + 1);
}, 200);
}, 100);
}
} catch (error) {
console.log("[execute_next_item] Error:", error)