Potential final fix for #30

This commit is contained in:
Logan Cusano
2023-06-23 17:48:28 -04:00
parent 47d18c494d
commit 7163df21e9
4 changed files with 48 additions and 34 deletions

View File

@@ -25,7 +25,7 @@ async function joinServerWrapper(presetName, channelId, connections) {
}); });
// Check which nodes have the selected preset // Check which nodes have the selected preset
onlineNodes = onlineNodes.filter(node => node.nearbySystems.includes(presetName)); onlineNodes = onlineNodes.filter(node => node.presets.includes(presetName));
log.DEBUG("Filtered Online Nodes: ", onlineNodes); log.DEBUG("Filtered Online Nodes: ", onlineNodes);
// Check if any nodes with this preset are available // Check if any nodes with this preset are available
@@ -113,10 +113,7 @@ module.exports = {
var presetsAvailable = []; var presetsAvailable = [];
for (const nodeObject of nodeObjects) { for (const nodeObject of nodeObjects) {
log.DEBUG("Node object: ", nodeObject); log.DEBUG("Node object: ", nodeObject);
for (const presetName in nodeObject.nearbySystems) { presetsAvailable.push.apply(presetsAvailable, nodeObject.presets);
if (!isNaN(presetName)) presetsAvailable.push(nodeObject.nearbySystems[presetName]);
else presetsAvailable.push(presetName)
}
} }
log.DEBUG("All Presets available: ", presetsAvailable); log.DEBUG("All Presets available: ", presetsAvailable);
@@ -133,7 +130,7 @@ module.exports = {
try{ try{
const guildId = interaction.guild.id; const guildId = interaction.guild.id;
const presetName = interaction.options.getString('preset'); const presetName = interaction.options.getString('preset');
if (!interaction.member.voice.channel.id) return interaction.editReply(`You need to be in a voice channel, ${interaction.user}`) if (!interaction.member.voice.channel) return interaction.editReply(`You need to be in a voice channel, ${interaction.user}`)
const channelId = interaction.member.voice.channel.id; const channelId = interaction.member.voice.channel.id;
log.DEBUG(`Join requested by: ${interaction.user.username}, to: '${presetName}', in channel: ${channelId} / ${guildId}`); log.DEBUG(`Join requested by: ${interaction.user.username}, to: '${presetName}', in channel: ${channelId} / ${guildId}`);

View File

@@ -35,7 +35,7 @@ exports.newNode = async (req, res) => {
_port: req.body.port ?? null, _port: req.body.port ?? null,
_location: req.body.location ?? null, _location: req.body.location ?? null,
_nearbySystems: req.body.nearbySystems ?? null, _nearbySystems: req.body.nearbySystems ?? null,
_online: req.body.online ?? 0 _online: (req.body.online == "true" || req.body.online == "True") ? true : false
}); });
addNewNode(newNode, (newNodeObject) => { addNewNode(newNode, (newNodeObject) => {
@@ -65,53 +65,62 @@ exports.getNodeInfo = async (req, res) => {
exports.nodeCheckIn = async (req, res) => { exports.nodeCheckIn = async (req, res) => {
if (!req.body.id) return res.status(400).json("No id specified"); if (!req.body.id) return res.status(400).json("No id specified");
getNodeInfoFromId(req.body.id, (nodeInfo) => { getNodeInfoFromId(req.body.id, (nodeInfo) => {
let checkInObject = new nodeObject({}); let checkInObject = {};
// Convert the online status to a boolean to be worked with // Convert the online status to a boolean to be worked with
nodeInfo.online = nodeInfo.online !== 0; log.DEBUG("REQ Body: ", req.body);
var isObjectUpdated = false; var isObjectUpdated = false;
if (req.body.name && req.body.name != nodeInfo.name) { if (req.body.name && req.body.name != nodeInfo.name) {
checkInObject.name = req.body.name; checkInObject._name = req.body.name;
isObjectUpdated = true; isObjectUpdated = true;
} }
if (req.body.ip && req.body.ip != nodeInfo.ip) { if (req.body.ip && req.body.ip != nodeInfo.ip) {
checkInObject.ip = req.body.ip; checkInObject._ip = req.body.ip;
isObjectUpdated = true; isObjectUpdated = true;
} }
if (req.body.port && req.body.port != nodeInfo.port) { if (req.body.port && req.body.port != nodeInfo.port) {
checkInObject.port = req.body.port; checkInObject._port = req.body.port;
isObjectUpdated = true; isObjectUpdated = true;
} }
if (req.body.location && req.body.location != nodeInfo.location) { if (req.body.location && req.body.location != nodeInfo.location) {
checkInObject.location = req.body.location; checkInObject._location = req.body.location;
isObjectUpdated = true; isObjectUpdated = true;
} }
if (req.body.nearbySystems && JSON.stringify(req.body.nearbySystems) !== JSON.stringify(nodeInfo.nearbySystems)) { if (req.body.nearbySystems && JSON.stringify(req.body.nearbySystems) !== JSON.stringify(nodeInfo.nearbySystems)) {
checkInObject.nearbySystems = req.body.nearbySystems; checkInObject._nearbySystems = req.body.nearbySystems;
isObjectUpdated = true; isObjectUpdated = true;
} }
if (req.body.online && (req.body.online === "true") != nodeInfo.online) { if (req.body.online != nodeInfo.online || req.body.online && (req.body.online === "true") != nodeInfo.online) {
checkInObject.online = (req.body.online === "true"); checkInObject._online = req.body.online;
isObjectUpdated = true; isObjectUpdated = true;
} }
// If no changes are made tell the client // If no changes are made tell the client
if (!isObjectUpdated) return res.status(200).json("No keys updated"); if (!isObjectUpdated) return res.status(200).json("No keys updated");
log.INFO("Updating the following keys for ID: ", req.body.id, checkInObject); log.INFO("Updating the following keys for ID: ", req.body.id, checkInObject);
// Adding the ID key to the body so that the client can double-check their ID
checkInObject.id = nodeInfo.id;
updateNodeInfo(checkInObject, () => {
return res.status(202).json({"updatedKeys": checkInObject});
})
})
checkInObject._id = req.body.id;
checkInObject = new nodeObject(checkInObject);
if (!nodeInfo) {
log.WARN("No existing node found with this ID, adding node: ", checkInObject);
addNewNode(checkInObject, (newNode) => {
return res.status(201).json({"updatedKeys": newNode});
});
}
else {
updateNodeInfo(checkInObject, () => {
return res.status(202).json({"updatedKeys": checkInObject});
});
}
});
} }
/** /**
@@ -123,7 +132,7 @@ exports.nodeCheckIn = async (req, res) => {
*/ */
exports.requestNodeJoinServer = async (req, res) => { exports.requestNodeJoinServer = async (req, res) => {
if (!req.body.clientId || !req.body.channelId || !req.body.presetName) return res.status(400).json("Missing information in request, requires clientId, channelId, presetName"); if (!req.body.clientId || !req.body.channelId || !req.body.presetName) return res.status(400).json("Missing information in request, requires clientId, channelId, presetName");
await joinServerWrapper(req.body.presetName, req.body.channelId, req.body.clientId) await joinServerWrapper(req.body.presetName, req.body.channelId, req.body.clientId);
} }
/** /**

View File

@@ -24,6 +24,8 @@ const nodeConnectionsTable = `${process.env.NODE_DB_NAME}.node_connections`;
* @returns {nodeObject} The converted node object to be used downstream * @returns {nodeObject} The converted node object to be used downstream
*/ */
function returnNodeObjectFromRow(row) { function returnNodeObjectFromRow(row) {
if (!isNaN(row.online)) row.online = Boolean(row.online);
else if (row.online == "true" || row.online == "false") row.online = (row.online == "true");
return new nodeObject({ return new nodeObject({
_id: row.id, _id: row.id,
_name: row.name, _name: row.name,
@@ -31,7 +33,7 @@ function returnNodeObjectFromRow(row) {
_port: row.port, _port: row.port,
_location: row.location, _location: row.location,
_nearbySystems: BufferToJson(row.nearbySystems), _nearbySystems: BufferToJson(row.nearbySystems),
_online: (row.online === 1) ? true : false, _online: row.online,
}); });
} }
@@ -122,6 +124,7 @@ exports.getOnlineNodes = (callback) => {
// Call back the first (and theoretically only) row // Call back the first (and theoretically only) row
// Specify 0 so downstream functions don't have to worry about it // Specify 0 so downstream functions don't have to worry about it
if (!sqlResponse.length > 0) return (callback) ? callback(false) : false;
return (callback) ? callback(await returnNodeObjectFromRow(sqlResponse[0])) : await returnNodeObjectFromRow(sqlResponse[0]); return (callback) ? callback(await returnNodeObjectFromRow(sqlResponse[0])) : await returnNodeObjectFromRow(sqlResponse[0]);
} }
exports.getNodeInfoFromId = getNodeInfoFromId exports.getNodeInfoFromId = getNodeInfoFromId
@@ -136,9 +139,15 @@ exports.addNewNode = async (nodeObject, callback) => {
ip = nodeObject.ip, ip = nodeObject.ip,
port = nodeObject.port, port = nodeObject.port,
location = nodeObject.location, location = nodeObject.location,
nearbySystems = utils.JsonToBuffer(nodeObject.nearbySystems ?? {}), nearbySystems = utils.JsonToBuffer(nodeObject.nearbySystems ?? {})
online = nodeObject.online
const sqlQuery = `INSERT INTO ${nodesTable} (name, ip, port, location, nearbySystems, online) VALUES ('${name}', '${ip}', ${port}, '${location}', '${nearbySystems}', ${online})`; var online = nodeObject.online;
if (typeof online === "boolean" || typeof online === "number") {
if (online) online = 1;
else online = 0;
}
const sqlQuery = `INSERT INTO ${nodesTable} (name, ip, port, location, nearbySystems, online) VALUES ('${name}', '${ip}', ${port}, '${location}', '${nearbySystems}', ${online})`;
const sqlResponse = await new Promise((recordResolve, recordReject) => { const sqlResponse = await new Promise((recordResolve, recordReject) => {
runSQL(sqlQuery, (rows) => { runSQL(sqlQuery, (rows) => {

View File

@@ -109,7 +109,7 @@ class nodeObject {
* @param {*} param0._ip The IP that the master can contact the node at * @param {*} param0._ip The IP that the master can contact the node at
* @param {*} param0._port The port that the client is listening on * @param {*} param0._port The port that the client is listening on
* @param {*} param0._location The physical location of the node * @param {*} param0._location The physical location of the node
* @param {*} param0._online True/False if the node is online or offline * @param {*} param0._online True/False if the node is online or offline
* @param {*} param0._nearbySystems An object array of nearby systems * @param {*} param0._nearbySystems An object array of nearby systems
*/ */
constructor({ _id = undefined, _name = undefined, _ip = undefined, _port = undefined, _location = undefined, _nearbySystems = undefined, _online = undefined }) { constructor({ _id = undefined, _name = undefined, _ip = undefined, _port = undefined, _location = undefined, _nearbySystems = undefined, _online = undefined }) {
@@ -117,10 +117,9 @@ class nodeObject {
this.name = _name; this.name = _name;
this.ip = _ip; this.ip = _ip;
this.port = _port; this.port = _port;
this.location = _location; this.location = _location;
if (_nearbySystems && Array.isArray(_nearbySystems)) this.nearbySystems = _nearbySystems; this.nearbySystems = _nearbySystems;
else if (_nearbySystems) this.nearbySystems = Object.keys(_nearbySystems); if (this.nearbySystems) this.presets = Object.keys(_nearbySystems);
else this.nearbySystems = _nearbySystems;
this.online = _online; this.online = _online;
} }
} }