Skip to content

Instantly share code, notes, and snippets.

@prnk28
Created February 25, 2020 23:29
Show Gist options
  • Save prnk28/b4c0139e913d08fd93816b68135e9624 to your computer and use it in GitHub Desktop.
Save prnk28/b4c0139e913d08fd93816b68135e9624 to your computer and use it in GitHub Desktop.
Data Structure that manages client connections in lobby - Useful for WebSockets
// Pradyumn Nukala
// Safely manages connections
class Channel {
constructor() {
this.receivers = {};
this.senders = {};
this.default = {};
}
// ***********************************
// ****** Channel Checks *************
// ***********************************
// Find Item in Array,
// options = exists(bool): check exists or not,
// client: client to check,
// noLog: bool for logging on/off
// target: receivers/senders/default
clientCheck(options = {}, callback) {
// Check if item exists
if (options.exists) {
// Check Target -- Receivers
if (options.target === "Receivers") {
if (this.receivers.hasOwnProperty(options.client.id)) {
// Validate Callback
if (typeof callback == "function") {
return callback();
}
} else {
// Check if log
if (options.logging) {
console.log("Item not found in array when checking if it exists.");
}
return false;
}
}
// Check Target -- Senders
else if (options.target === "Senders") {
if (this.senders.hasOwnProperty(options.client.id)) {
// Validate Callback
if (typeof callback == "function") {
return callback();
}
} else {
// Check if log
if (options.logging) {
console.log("Item not found in array when checking if it exists.");
}
return false;
}
}
// Check Target -- Default
else if (options.target === "Default") {
if (this.default.hasOwnProperty(options.client.id)) {
// Validate Callback
if (typeof callback == "function") {
return callback();
}
} else {
// Check if log
if (options.logging) {
console.log("Item not found in array when checking if it exists.");
}
return false;
}
}
// Check if item doesnt exist
} else {
// Check Target -- Receivers
if (options.target === "Receivers") {
if (!this.receivers.hasOwnProperty(options.client.id)) {
// Validate Callback
if (typeof callback == "function") {
return callback();
}
} else {
// Check if log
if (options.logging) {
console.log("Item not found in array when checking if it exists.");
}
return false;
}
}
// Check Target -- Senders
else if (options.target === "Senders") {
if (!this.senders.hasOwnProperty(options.client.id)) {
// Validate Callback
if (typeof callback == "function") {
return callback();
}
} else {
// Check if log
if (options.logging) {
console.log("Item not found in array when checking if it exists.");
}
return false;
}
}
// Check Target -- Default
else if (options.target == "Default") {
if (!this.default.hasOwnProperty(options.client.id)) {
// Validate Callback
if (typeof callback == "function") {
return callback();
}
} else {
// Check if log
if (options.logging) {
console.log("Item not found in array when checking if it exists.");
}
return false;
}
}
}
}
toMap() {
return {
default: this.default,
senders: this.senders,
receivers: this.receivers,
};
}
// ***********************************
// ****** Channel Management *********
// ***********************************
// Add Item if it Doesnt Exist
safeAdd(client, target, logging = true) {
// Check Options
var options = {
exists: false,
client: client,
target: target,
logging: logging
};
// Run Check
this.clientCheck(options, () => {
// Check Target -- Receivers
if (target === "Receivers") {
this.receivers[client.id] = client;
}
// Check Target -- Senders
else if (target === "Senders") {
this.senders[client.id] = client;
}
// Check Target -- Default
else if (target == "Default") {
this.default[client.id] = client;
}
// Check if log
if (logging) {
console.log("Item safely added");
}
return true;
});
return false;
}
// Copy Map to Target
safeCopy(map, client) {
// Check Target -- Receivers
this.receivers = map["receivers"];
// Check Target -- Senders
this.senders = map["senders"];
// Check Target -- Default
this.default = map["default"];
// Check Client Status - Initialize
if (client["status"]["code"] === 1) {
// Update Lobby
this.shift(client, "Default");
}
// Check Client Status - Sending
else if (client["status"]["code"] === 10) {
// Add to senders, and check
this.shift(client, "Senders");
}
// Check Client Status - Receiving
else if (client["status"]["code"] === 20) {
// Add to receivers, and check
this.shift(client, "Receivers");
}
}
// Delete Item in Array
safeRemove(client, target, logging = true) {
// Check Options
var options = {
exists: true,
client: client,
target: target,
logging: logging
};
// Run Check
this.clientCheck(options, () => {
// Check Target -- Receivers
if (target === "Receivers") {
delete this.receivers[client.id];
}
// Check Target -- Senders
else if (target === "Senders") {
delete this.senders[client.id];
}
// Check Target -- Default
else if (target == "Default") {
delete this.default[client.id];
}
// Check if log
if (logging) {
console.log("Item safely deleted");
}
return true;
});
return false;
}
// Delete All provided arrays
leave(client) {
this.safeRemove(client, "Senders");
this.safeRemove(client, "Default");
this.safeRemove(client, "Receivers");
}
// Move Items from another ClientArray
shift(client, target) {
// Check Target -- Receivers
if (target === "Receivers") {
this.safeAdd(client, "Receivers");
this.safeRemove(client, "Senders");
this.safeRemove(client, "Default");
}
// Check Target -- Senders
else if (target === "Senders") {
this.safeAdd(client, "Senders");
this.safeRemove(client, "Receivers");
this.safeRemove(client, "Default");
}
// Check Target -- Default
else if (target == "Default") {
this.safeAdd(client, "Default");
this.safeRemove(client, "Senders");
this.safeRemove(client, "Receivers");
}
}
// Returns Combined Count of Lobby
size() {
return Object.keys(this.default).length +
Object.keys(this.senders).length +
Object.keys(this.receivers).length;
}
}
module.exports = Channel;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment