· 3 years ago · Mar 20, 2022, 01:20 PM
1"""
2Commands
3
4Commands describe the input the account can do to the game.
5
6"""
7
8from evennia.commands.command import Command as BaseCommand
9
10
11from evennia import CmdSet
12
13
14from evennia import default_cmds
15
16
17from evennia import utils
18
19
20class Command(BaseCommand):
21 """
22 Base command (you may see this if a child command had no help text defined)
23
24 Note that the class's `__doc__` string is used by Evennia to create the
25 automatic help entry for the command, so make sure to document consistently
26 here. Without setting one, the parent's docstring will show (like now).
27
28 """
29
30 # Each Command class implements the following methods, called in this order
31 # (only func() is actually required):
32 #
33 # - at_pre_cmd(): If this returns anything truthy, execution is aborted.
34 # - parse(): Should perform any extra parsing needed on self.args
35 # and store the result on self.
36 # - func(): Performs the actual work.
37 # - at_post_cmd(): Extra actions, often things done after
38 # every command, like prompts.
39 #
40 pass
41
42
43class CmdEcho(Command):
44 """
45 A simple echo command.
46
47 Usage: echo <something>
48 """
49 key = "echo"
50
51 def parse(self):
52 self.args = self.args.strip()
53
54 def func(self):
55 if not self.args:
56 self.caller.msg("Vous devez introduire un argument!")
57 return
58 self.caller.msg(f"Echo:'{self.args}'")
59
60
61class MyCmdInventory(default_cmds.CmdInventory):
62 """
63 Voir son Inventaire
64
65 Usage:
66 inventaire
67 inv
68 i
69
70 Affiche l'inventaire du personnage.
71 """
72 key = "inventaire"
73 aliases = ["inv", "i"]
74 locks = "cmd:all()"
75 arg_regex = r"$"
76
77 def func(self):
78 """check inventory"""
79 items = self.caller.contents
80 if not items:
81 string = "\nVous ne transportez rien."
82 else:
83 from evennia.utils.ansi import raw as raw_ansi
84
85 table = self.styled_table(border="header")
86 for item in items:
87 table.add_row(
88 f"|C{item.name}|n",
89 "{}|n".format(utils.crop(raw_ansi(item.db.desc or ""), width=50) or ""),
90 )
91 string = f"\n|wVous transportez:\n{table}"
92 self.caller.msg(string)
93
94
95class MyCmdSet(CmdSet):
96
97 def at_cmdset_creation(self):
98 self.add(CmdEcho)
99 self.add(MyCmdInventory)
100# -------------------------------------------------------------
101#
102# The default commands inherit from
103#
104# evennia.commands.default.muxcommand.MuxCommand.
105#
106# If you want to make sweeping changes to default commands you can
107# uncomment this copy of the MuxCommand parent and add
108#
109# COMMAND_DEFAULT_CLASS = "commands.command.MuxCommand"
110#
111# to your settings file. Be warned that the default commands expect
112# the functionality implemented in the parse() method, so be
113# careful with what you change.
114#
115# -------------------------------------------------------------
116
117# from evennia.utils import utils
118#
119#
120# class MuxCommand(Command):
121# """
122# This sets up the basis for a MUX command. The idea
123# is that most other Mux-related commands should just
124# inherit from this and don't have to implement much
125# parsing of their own unless they do something particularly
126# advanced.
127#
128# Note that the class's __doc__ string (this text) is
129# used by Evennia to create the automatic help entry for
130# the command, so make sure to document consistently here.
131# """
132# def has_perm(self, srcobj):
133# """
134# This is called by the cmdhandler to determine
135# if srcobj is allowed to execute this command.
136# We just show it here for completeness - we
137# are satisfied using the default check in Command.
138# """
139# return super().has_perm(srcobj)
140#
141# def at_pre_cmd(self):
142# """
143# This hook is called before self.parse() on all commands
144# """
145# pass
146#
147# def at_post_cmd(self):
148# """
149# This hook is called after the command has finished executing
150# (after self.func()).
151# """
152# pass
153#
154# def parse(self):
155# """
156# This method is called by the cmdhandler once the command name
157# has been identified. It creates a new set of member variables
158# that can be later accessed from self.func() (see below)
159#
160# The following variables are available for our use when entering this
161# method (from the command definition, and assigned on the fly by the
162# cmdhandler):
163# self.key - the name of this command ('look')
164# self.aliases - the aliases of this cmd ('l')
165# self.permissions - permission string for this command
166# self.help_category - overall category of command
167#
168# self.caller - the object calling this command
169# self.cmdstring - the actual command name used to call this
170# (this allows you to know which alias was used,
171# for example)
172# self.args - the raw input; everything following self.cmdstring.
173# self.cmdset - the cmdset from which this command was picked. Not
174# often used (useful for commands like 'help' or to
175# list all available commands etc)
176# self.obj - the object on which this command was defined. It is often
177# the same as self.caller.
178#
179# A MUX command has the following possible syntax:
180#
181# name[ with several words][/switch[/switch..]] arg1[,arg2,...] [[=|,] arg[,..]]
182#
183# The 'name[ with several words]' part is already dealt with by the
184# cmdhandler at this point, and stored in self.cmdname (we don't use
185# it here). The rest of the command is stored in self.args, which can
186# start with the switch indicator /.
187#
188# This parser breaks self.args into its constituents and stores them in the
189# following variables:
190# self.switches = [list of /switches (without the /)]
191# self.raw = This is the raw argument input, including switches
192# self.args = This is re-defined to be everything *except* the switches
193# self.lhs = Everything to the left of = (lhs:'left-hand side'). If
194# no = is found, this is identical to self.args.
195# self.rhs: Everything to the right of = (rhs:'right-hand side').
196# If no '=' is found, this is None.
197# self.lhslist - [self.lhs split into a list by comma]
198# self.rhslist - [list of self.rhs split into a list by comma]
199# self.arglist = [list of space-separated args (stripped, including '=' if it exists)]
200#
201# All args and list members are stripped of excess whitespace around the
202# strings, but case is preserved.
203# """
204# raw = self.args
205# args = raw.strip()
206#
207# # split out switches
208# switches = []
209# if args and len(args) > 1 and args[0] == "/":
210# # we have a switch, or a set of switches. These end with a space.
211# switches = args[1:].split(None, 1)
212# if len(switches) > 1:
213# switches, args = switches
214# switches = switches.split('/')
215# else:
216# args = ""
217# switches = switches[0].split('/')
218# arglist = [arg.strip() for arg in args.split()]
219#
220# # check for arg1, arg2, ... = argA, argB, ... constructs
221# lhs, rhs = args, None
222# lhslist, rhslist = [arg.strip() for arg in args.split(',')], []
223# if args and '=' in args:
224# lhs, rhs = [arg.strip() for arg in args.split('=', 1)]
225# lhslist = [arg.strip() for arg in lhs.split(',')]
226# rhslist = [arg.strip() for arg in rhs.split(',')]
227#
228# # save to object properties:
229# self.raw = raw
230# self.switches = switches
231# self.args = args.strip()
232# self.arglist = arglist
233# self.lhs = lhs
234# self.lhslist = lhslist
235# self.rhs = rhs
236# self.rhslist = rhslist
237#
238# # if the class has the account_caller property set on itself, we make
239# # sure that self.caller is always the account if possible. We also create
240# # a special property "character" for the puppeted object, if any. This
241# # is convenient for commands defined on the Account only.
242# if hasattr(self, "account_caller") and self.account_caller:
243# if utils.inherits_from(self.caller, "evennia.objects.objects.DefaultObject"):
244# # caller is an Object/Character
245# self.character = self.caller
246# self.caller = self.caller.account
247# elif utils.inherits_from(self.caller, "evennia.accounts.accounts.DefaultAccount"):
248# # caller was already an Account
249# self.character = self.caller.get_puppet(self.session)
250# else:
251# self.character = None
252