from evennia.commands.default.muxcommand import MuxCommand from evennia.server.sessionhandler import SESSIONS from evennia.utils import utils, evtable import time class CmdWho(MuxCommand): """ who [prefix] doing [prefix] doing/set [pithy quote] This command shows who is presently online. If a prefix is given, it will only show the accounts whose names start with that prefix. For admins, if you wish to see the player-side list, type 'doing'. For all players, if you'd like to set a pithy quote to show up in the last column, use 'doing/set'. If you don't provide a value to doing/set, it will clear yours. """ key = "who" aliases = ["doing", ] locks = "cmd:all()" account_caller = True def func(self): def get_sessions(prefix=None): """ Given an optional prefix for account names, return a list of currently connected sessions. This is just a convenience function since we might use it several places. :param prefix: A prefix that the account name must start with to be included in the list. :return: """ sessions = SESSIONS.get_sessions() if prefix: sessions = \ filter(lambda sess: sess.get_account().key.startswith( prefix) if sess.get_account() is not None else False, sessions) return sessions if self.cmdstring == "doing" and "set" in self.switches: if self.args == "": self.msg("Doing field cleared.") self.account.db.who_doing = None else: self.msg("Doing field set: {}".format(self.args)) self.account.db.who_doing = self.args return session_list = get_sessions(self.args) session_list = sorted(session_list, key=lambda sess: sess.conn_time) if self.cmdstring == "doing": show_admin_data = False else: show_admin_data= self.account.check_permstring("Developer") or self.account.check_permstring("Admins") if show_admin_data: table = evtable.EvTable("Account Name", "On For", "Idle", "Location", "Client", "Address") else: table = evtable.EvTable("Account Name", "On For", "Idle", "Doing") for session in session_list: if not session.logged_in: continue delta_cmd = time.time() - session.cmd_last_visible delta_conn = time.time() - session.conn_time account = session.get_account() account_name = account.key doing_string = account.db.who_doing if account.db.who_doing else "" character = session.get_puppet() if show_admin_data: table.add_row(utils.crop(account_name, 25), utils.time_format(delta_conn, 0), utils.time_format(delta_conn, 1), "#{}".format(character.location.id) if character else "", "Web Client" if session.protocol_key == "websocket" else session.protocol_flags['CLIENTNAME'], session.address[0] if isinstance(session.address, tuple) else session.address) else: table.add_row(utils.crop(account_name, 25), utils.time_format(delta_conn, 0), utils.time_format(delta_cmd, 1), utils.crop(doing_string, 35)) self.msg(table)