async def render_appearance(self, entry: "TaskEntry", viewer: "GameObject", internal=False): parser = entry.parser out = fmt.FormatList(viewer) see_dbrefs = viewer.session.admin if viewer.session else True def format_name(obj, cmd=None): name = viewer.get_dub_or_keyphrase_for(obj) display = ansi_fun("hw", name) if cmd: display = send_menu(display, [(f"{cmd} {name}", cmd)]) if see_dbrefs: display += f" ({obj.dbref})" return display if (nameformat := self.attributes.get_value("NAMEFORMAT")): result = await parser.evaluate(nameformat, executor=self, number_args={ 0: self.objid, 1: self.name }) out.add(fmt.Line(result))
def help(cls, entry): """ This is called by the command-help system if help is called on this command. """ enactor = entry.enactor if cls.__doc__: out = fmt.FormatList(enactor) out.add(fmt.Header(f"Help: {cls.name}")) out.add(fmt.Line(cls.__doc__)) out.add(fmt.Footer()) enactor.send(out) else: enactor.msg(text="Help is not implemented for this command.")
async def execute(self): mdict = self.match_obj.groupdict() args = mdict.get("args", None) if not args: raise CommandException("@py requires arguments!") out = fmt.FormatList(self.executor) out.add(fmt.Line(f">>> {args}")) duration = "" ret = None try: # reroute standard output to game client console old_stdout = sys.stdout old_stderr = sys.stderr sys.stdout = self sys.stderr = self try: pycode_compiled = compile(args, "", "eval") except Exception: pycode_compiled = compile(args, "", "exec") measure_time = True if measure_time: t0 = time.time() ret = eval(pycode_compiled, {}, self.available_vars()) t1 = time.time() duration = " (runtime ~ %.4f ms)" % ((t1 - t0) * 1000) else: ret = eval(pycode_compiled, {}, self.available_vars()) except Exception: exc_type, exc_value, tb = sys.exc_info() trace = Traceback.extract(exc_type, exc_value, tb, show_locals=False) out.add(fmt.PyException(trace)) finally: # return to old stdout sys.stdout = old_stdout sys.stderr = old_stderr out.add(fmt.PyDebug(repr(ret))) if duration: out.add(fmt.Line(duration)) self.executor.send(out)
async def execute(self): await self.setup() self.start_timer = time.time() try: await self.do_execute() except CPUTimeExceeded as mxp: pass except BreakTaskException as brk: pass except Exception as e: if self.session and await self.session.see_tracebacks(self): exc_type, exc_value, tb = sys.exc_info() trace = Traceback.extract(exc_type, exc_value, tb, show_locals=False) out = fmt.FormatList(self.session) out.add(fmt.PyException(trace)) self.session.send(out) traceback.print_exc(file=sys.stdout)
def display_help(self, data): cat_sort = sorted(data.keys()) out = fmt.FormatList(self.entry.executor) out.add(fmt.Header("Help: Available Commands")) for cat_key in cat_sort: cat = data[cat_key] out.add(fmt.Subheader(cat_key)) cmds = sorted([cmd for cmd in cat], key=lambda x: x.name) out.add( fmt.TabularTable([ send_menu( cmd.name, commands=[(f"help {cmd.name}", f"help {cmd.name}")], ) for cmd in cmds ])) out.add(fmt.Footer("help <command> for further help")) self.entry.executor.send(out)
options = { "accounts": self.list_accounts, "districts": self.list_districts } if not (choice := options.get( partial_match(mode.clean, options.keys()), None)): raise CommandException( f"That is not a valid choice. Choices are: {options.keys()}") choice() def list_accounts(self): if not (accounts := self.enactor.core.get_tag("account").all()): raise CommandException("There are no accounts to list.") out = fmt.FormatList(self.enactor) out.add(fmt.Header("Accounts")) table = fmt.Table("Objid", "Name", "Characters") for acc in accounts: table.add_row(acc.objid, acc.name, ", ".join(x.name for x in acc.characters.all())) out.add(table) out.add(fmt.Footer()) self.enactor.send(out) def list_districts(self): def line(dist, o, depth=0): l = fmt.Text( " " * depth + f"?? {dist.objid:<15} {dist.name:<30} {len(dist.rooms.all())}") o.add(l)
def msg(self, text, **kwargs): flist = fmt.FormatList(self, **kwargs) flist.add(fmt.Line(text)) self.send(flist)
def write(self, text): out = fmt.FormatList(self.executor) out.add(fmt.PyDebug(text.rsplit("\n", 1)[0])) self.executor.send(out)
async def execute(self): out = fmt.FormatList(self.executor) out.add(fmt.Line("See you again!")) out.reason = "quit" self.executor.send(out) self.executor.terminate()