From 07bf1023ae288f6ca283a4a5583af53d019390ac Mon Sep 17 00:00:00 2001 From: Dissidente Date: Thu, 7 May 2020 17:52:49 +0100 Subject: [PATCH 1/2] ADD: .spod command CHANGE: moved friendlyTime() to comand_utility() to be used by other commands CHANGE: added loginCount and totalTime to the output of getUsersList() --- TalkerNode.js | 103 ++++++++++++++++++++++++++++++-------------- commands/examine.js | 40 ++--------------- commands/spod.js | 52 ++++++++++++++++++++++ 3 files changed, 125 insertions(+), 70 deletions(-) create mode 100644 commands/spod.js diff --git a/TalkerNode.js b/TalkerNode.js index 496ab92..ea54342 100644 --- a/TalkerNode.js +++ b/TalkerNode.js @@ -610,6 +610,43 @@ function command_utility() { findCommand: findCommand, sendData: sendData, + /* + * Returns a friendly time format + */ + friendlyTime: function friendlyTime(ms) { + let msec, sec, min, hour, day, month, year; + msec = Math.floor(ms % 1000); + sec = Math.floor((ms / 1000) % 60); + min = Math.floor((ms / 1000 / 60) % 60); + hour = Math.floor((ms / 1000 / 60 / 60) % 24); + day = Math.floor((ms / 1000 / 60 / 60 / 24) % 30); + month = Math.floor((ms / 1000 / 60 / 60 / 24 / 30) % 12); + year = Math.floor((ms / 1000 / 60 / 60 / 24 / 30 / 12)); + let f_time = ""; + if (msec) { + f_time = msec + " milliseconds"; + } + if (sec) { + f_time = sec + " seconds"; + } + if (min) { + f_time = min + " minutes"; + } + if (hour) { + f_time = hour + " hours, " + f_time; + } + if (day) { + f_time = day + " days, " + f_time; + } + if (month) { + f_time = month + " months, " + f_time; + } + if (year) { + f_time = year + " years, " + f_time; + } + return f_time; + }, + /* * Execute function to all connected users *but* the triggering one. * It stops at the first connected user to which the function returns true, returning true. @@ -675,39 +712,39 @@ function command_utility() { return usersdb.get(name).value(); }, - // returns the username of an "aproximate" user - // read 'getAproxOnlineUser' to understand the difference between - // 'getOnlineUser' and it, same happens here between 'getUser' and - // 'getAproxUser'. - getAproxUser: function getAproxUser(name) { - if (this.getUser(name) !== undefined) return [name]; - var possibilities = []; - for (var key in usersdb.getState()) { - if (name.toLowerCase() === key.toLowerCase().substr(0,name.length) && (name.length < key.length)) { - possibilities.push(key); - } - } - if (possibilities.length === 0) return []; - return possibilities; - }, - - // updates a user in the database - // TODO: argh, we surely don't want this! harden it! - updateUser: function updateUser(username, userObj) { - username = username.toLowerCase().charAt(0).toUpperCase() + username.toLowerCase().slice(1); - usersdb.set(username,userObj).write(); - }, - - // get users list, only insensitive information - getUsersList: function getUsersList() { - var list = []; - for (var key in usersdb.getState()) { - // retrieving username, rank and loginTime. If needed, we can always add stuff later - var val = usersdb.get(key).value(); - list.push({username:key, rank:val.rank, loginTime:val.loginTime}); - } - return list; - }, + // returns the username of an "aproximate" user + // read 'getAproxOnlineUser' to understand the difference between + // 'getOnlineUser' and it, same happens here between 'getUser' and + // 'getAproxUser'. + getAproxUser: function getAproxUser(name) { + if (this.getUser(name) !== undefined) return [name]; + var possibilities = []; + for (var key in usersdb.getState()) { + if (name.toLowerCase() === key.toLowerCase().substr(0,name.length) && (name.length < key.length)) { + possibilities.push(key); + } + } + if (possibilities.length === 0) return []; + return possibilities; + }, + + // updates a user in the database + // TODO: argh, we surely don't want this! harden it! + updateUser: function updateUser(username, userObj) { + username = username.toLowerCase().charAt(0).toUpperCase() + username.toLowerCase().slice(1); + usersdb.set(username,userObj).write(); + }, + + // get users list, only insensitive information + getUsersList: function getUsersList() { + var list = []; + for (var key in usersdb.getState()) { + // retrieving username, rank and loginTime. If needed, we can always add stuff later + var val = usersdb.get(key).value(); + list.push({username:key, rank:val.rank, loginTime:val.loginTime, totalTime:val.totalTime, loginCount:val.loginCount}); + } + return list; + }, // gives a full view of the universe; TODO: we surely don't want this // TODO: in the meantime, we don't need to define a function for this! diff --git a/commands/examine.js b/commands/examine.js index 5c3d4d8..40821e7 100644 --- a/commands/examine.js +++ b/commands/examine.js @@ -10,40 +10,6 @@ exports.command = { "instead.", usage: ".examine []", - friendlyTime: function(ms) { - let msec, sec, min, hour, day, month, year; - msec = Math.floor(ms % 1000); - sec = Math.floor((ms / 1000) % 60); - min = Math.floor((ms / 1000 / 60) % 60); - hour = Math.floor((ms / 1000 / 60 / 60) % 24); - day = Math.floor((ms / 1000 / 60 / 60 / 24) % 30); - month = Math.floor((ms / 1000 / 60 / 60 / 24 / 30) % 12); - year = Math.floor((ms / 1000 / 60 / 60 / 24 / 30 / 12)); - let f_time = ""; - if (msec) { - f_time = msec + " milliseconds"; - } - if (sec) { - f_time = sec + " seconds"; - } - if (min) { - f_time = min + " minutes"; - } - if (hour) { - f_time = hour + " hours, " + f_time; - } - if (day) { - f_time = day + " days, " + f_time; - } - if (month) { - f_time = month + " months, " + f_time; - } - if (year) { - f_time = year + " years, " + f_time; - } - return f_time; - }, - // Function to execute the command execute: function(socket, command, command_access) { var chalk = require('chalk'); @@ -77,16 +43,16 @@ exports.command = { } else { // we should always be going into this if, but let's double check anyway if (typeof w.loginTime !== 'undefined') { - command_access.sendData(socket, chalk.bold(":: ") + chalk.cyan(whom) + " has spent " + chalk.bold(this.friendlyTime(Date.now() - w.loginTime)) + chalk.green(" online") + ".\r\n"); + command_access.sendData(socket, chalk.bold(":: ") + chalk.cyan(whom) + " has spent " + chalk.bold(command_access.friendlyTime(Date.now() - w.loginTime)) + chalk.green(" online") + ".\r\n"); } } } else { if (command_access.getOnlineUser(whom) !== false) { // the user is currently online - command_access.sendData(socket, chalk.bold(":: ") + chalk.cyan(whom) + " has spent " + chalk.bold(this.friendlyTime(w.totalTime + (Date.now() - w.loginTime))) + chalk.green(" online") + ".\r\n"); + command_access.sendData(socket, chalk.bold(":: ") + chalk.cyan(whom) + " has spent " + chalk.bold(command_access.friendlyTime(w.totalTime + (Date.now() - w.loginTime))) + chalk.green(" online") + ".\r\n"); } else { // the user isn't online - command_access.sendData(socket, chalk.bold(":: ") + chalk.cyan(whom) + " has spent " + chalk.bold(this.friendlyTime(w.totalTime)) + chalk.green(" online") + ".\r\n"); + command_access.sendData(socket, chalk.bold(":: ") + chalk.cyan(whom) + " has spent " + chalk.bold(command_access.friendlyTime(w.totalTime)) + chalk.green(" online") + ".\r\n"); } } command_access.sendData(socket, diff --git a/commands/spod.js b/commands/spod.js new file mode 100644 index 0000000..8a7f04e --- /dev/null +++ b/commands/spod.js @@ -0,0 +1,52 @@ +exports.command = { + name: "spod", // Name of command to be executed (Max 10 chars) + autoload: true, // Should the command be autoloaded at startup + unloadable: true, // Can the command be unloaded dynamically + min_rank: 0, // Minimum rank to use to execute the command + display: "Shows top users per total time or login count", // Summary help text to show in the .help command (Max 60 chars) + help: "Shows top users per total time. Using -l will show the top login counts", // Full help text when .help is used + usage: ".spod [-l]", // usage of the command + weigth: 0, // if two commands are elegible to be invoked, + // the heavier wins. If not present, weigth = 0. + + // Function to execute the command + execute: function(socket, command, command_access) { + var chalk = require('chalk'); + if(command === "") { // sort by total time + var userList = command_access.getUsersList().sort(function(a,b){return b.totalTime - a.totalTime;}); + var lengthMaxFriendlyTotalTime = command_access.friendlyTime(userList[0].totalTime).length; + command_access.sendData(socket, "\r\n" + chalk.cyan("+-- Top Users by login time -------------------------------------------------+\r\n\r\n")); + } else { + if(command === '-l') { // sort by login count + var userList = command_access.getUsersList().sort(function(a,b){return b.loginCount - a.loginCount;}); + var lengthMaxLoginCount = userList[0].loginCount.toString().length; + command_access.sendData(socket, "\r\n" + chalk.cyan("+-- Top Users by login count ------------------------------------------------+\r\n\r\n")); + } else { // invalid argument + command_access.sendData(socket, "Invalid argument. Check .help " + this.name + " for usage instructions.\r\n"); + return; + } + } + for (let index = 0; index < userList.length; index++) { + const userObject = userList[index]; + + if(socket.username == userObject.username) { // hey, it's me! + var userRow = "> "; + } else { + var userRow = " "; + } + if(command === '-l') { + userRow = userRow + userObject.loginCount.toString().padStart(lengthMaxLoginCount) + " logins"; + } else { + userRow = userRow + command_access.friendlyTime(userObject.totalTime).padStart(lengthMaxFriendlyTotalTime); + } + userRow = userRow + " : " + userObject.username + "\r\n"; + if(userRow[0] === '>') + { + command_access.sendData(socket, chalk.cyan(userRow)); + } else { + command_access.sendData(socket, userRow); + } + } + command_access.sendData(socket, chalk.cyan("\r\n+----------------------------------------------------------------------------+\r\n")); + } +} From 769e0ea9c37360ba4f09815232e5099d341c5b3e Mon Sep 17 00:00:00 2001 From: Marcos Marado Date: Thu, 22 Apr 2021 20:32:06 +0100 Subject: [PATCH 2/2] .spod: show current login time, for online users --- commands/spod.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/commands/spod.js b/commands/spod.js index 086bad2..a3020d7 100644 --- a/commands/spod.js +++ b/commands/spod.js @@ -13,9 +13,16 @@ exports.command = { var chalk = require('chalk'); var formatters = require('../utils/formatters.js'); var userlist; + + // the usertime is found in a different way, depending on if they're online or not + // TODO: consider moving this into an helper function that can be used on .exa and elsewhere + var usertime = function(user) { + return user.loggedin ? (Date.now() - user.loginTime) : user.totalTime; + } + if(command === "") { // sort by total time - userList = command_access.getUsersList().sort(function(a,b){return b.totalTime - a.totalTime;}); - var lengthMaxFriendlyTotalTime = formatters.friendly_time(userList[0].totalTime).length; + userList = command_access.getUsersList().sort(function(a,b){return usertime(b) - usertime(a);}); + var lengthMaxFriendlyTotalTime = formatters.friendly_time(usertime(userList[0])).length; command_access.sendData(socket, "\r\n" + chalk.cyan("+-- Top Users by login time -------------------------------------------------+\r\n\r\n")); } else if(command === '-l') { // sort by login count userList = command_access.getUsersList().sort(function(a,b){return b.loginCount - a.loginCount;}); @@ -36,7 +43,7 @@ exports.command = { if(command === '-l') { userRow = userRow + userObject.loginCount.toString().padStart(lengthMaxLoginCount) + " logins"; } else { - userRow = userRow + formatters.friendly_time(userObject.totalTime).padStart(lengthMaxFriendlyTotalTime); + userRow = userRow + formatters.friendly_time(usertime(userObject)).padStart(lengthMaxFriendlyTotalTime); } userRow = userRow + " : " + userObject.username + "\r\n"; if(userRow[0] === '>') {