Files
DRB-CnC/Server/controllers/adminController.js

93 lines
3.4 KiB
JavaScript

// Config
require('dotenv').config();
// Debug
const { DebugBuilder } = require("../utilities/debugBuilder.js");
const log = new DebugBuilder("server", "adminController");
// Utilities
const mysqlHandler = require("../utilities/mysqlHandler");
const utils = require("../utilities/utils");
const requests = require("../utilities/httpRequests");
const { leaveServerWrapper } = require("../commands/leave");
const { joinServerWrapper } = require("../commands/join");
/** Get the presets of all online nodes, can be used for functions
*
* @param callback Callback function
* @returns {*} A list of the systems online
*/
async function getPresetsOfOnlineNodes(callback) {
mysqlHandler.getOnlineNodes((onlineNodes) => {
return callback(onlineNodes);
});
}
async function getNodeBotStatus(nodeId, callback) {
mysqlHandler.getNodeInfoFromId(nodeId, (nodeObject) =>{
reqOptions = new requests.requestOptions("/bot/status", "GET", nodeObject.ip, nodeObject.port, undefined, 5);
requests.sendHttpRequest(reqOptions, JSON.stringify({}), (responseObject) => {
if (responseObject === false) {
// Bot is joined
}
else {
// Bot is free
}
return callback(responseObject);
});
});
}
/** Return to requests for the presets of all online nodes, cannot be used in functions
*
* @param {*} req Express request parameter
* @param {*} res Express response parameter
*/
exports.getAvailablePresets = async (req, res) => {
await getPresetsOfOnlineNodes((systems) => {
res.status(200).json({
"systemsOnline": systems
});
})
}
/** Request a node to join the server listening to a specific preset
*
* @param {*} req Express request parameter
* @var {*} req.body.preset The preset to join (REQ)
* @var {*} req.body.nodeId The specific node to join (OPT/REQ if more than one node has the preset)
* @var {*} req.body.clientId The ID of the client that we want to join with
* @var {*} req.body.channelId The channel Id of the discord channel to join
* @param {*} res Express response parameter
*/
exports.joinPreset = async (req, res) => {
if (!req.body.preset) return res.status(400).json("No preset specified");
if (!req.body.nodeId) return res.status(400).json("No node ID specified");
if (!req.body.clientId) return res.status(400).json("No client ID specified");
if (!req.body.channelId) return res.status(400).json("No channel ID specified");
const preset = req.body.preset;
const nodeId = req.body.nodeId;
const clientId = req.body.clientId;
const channelId = req.body.channelId;
const joinedClient = await joinServerWrapper(preset, channelId, clientId, nodeId);
if (!joinedClient) return res.send(400).json("No joined client");
return res.send(200).json(joinedClient);
}
/** Request a node to join the server listening to a specific preset
*
* @param {*} req Express request parameter
* @param {*} res Express response parameter
* @var {*} req.body.clientId The ID of the client to disconnect
*/
exports.leaveServer = async (req, res) => {
if (!req.body.clientId) return res.status(400).json("No clientID specified");
const clientId = req.body.clientId;
await leaveServerWrapper(clientId)
return res.send(200);
}