· 9 years ago · Jan 30, 2017, 04:10 PM
1"""
2Commands
3
4Commands describe the input the player can do to the game.
5
6"""
7from evennia.commands.default.muxcommand import MuxCommand
8from evennia import Command as BaseCommand
9from evennia import default_cmds
10from django.conf import settings
11from evennia import Command, create_object, utils
12from evennia.utils import create, utils
13from evennia.commands.default import unloggedin
14from evennia.utils import evform
15
16
17# from evennia import default_cmds
18
19
20
21class Command(BaseCommand):
22 """
23 Inherit from this if you want to create your own command styles
24 from scratch. Note that Evennia's default commands inherits from
25 MuxCommand instead.
26
27 Note that the class's `__doc__` string (this text) is
28 used by Evennia to create the automatic help entry for
29 the command, so make sure to document consistently here.
30
31 Each Command implements the following methods, called
32 in this order (only func() is actually required):
33 - at_pre_command(): If this returns True, 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_command(): Extra actions, often things done after
38 every command, like prompts.
39
40 """
41 pass
42
43#------------------------------------------------------------
44#
45# The default commands inherit from
46#
47# evennia.commands.default.muxcommand.MuxCommand.
48#
49# If you want to make sweeping changes to default commands you can
50# uncomment this copy of the MuxCommand parent and add
51#
52# COMMAND_DEFAULT_CLASS = "commands.command.MuxCommand"
53#
54# to your settings file. Be warned that the default commands expect
55# the functionality implemented in the parse() method, so be
56# careful with what you change.
57#
58#------------------------------------------------------------
59
60#from evennia.utils import utils
61#class MuxCommand(Command):
62# """
63# This sets up the basis for a MUX command. The idea
64# is that most other Mux-related commands should just
65# inherit from this and don't have to implement much
66# parsing of their own unless they do something particularly
67# advanced.
68#
69# Note that the class's __doc__ string (this text) is
70# used by Evennia to create the automatic help entry for
71# the command, so make sure to document consistently here.
72# """
73# def has_perm(self, srcobj):
74# """
75# This is called by the cmdhandler to determine
76# if srcobj is allowed to execute this command.
77# We just show it here for completeness - we
78# are satisfied using the default check in Command.
79# """
80# return super(MuxCommand, self).has_perm(srcobj)
81#
82# def at_pre_cmd(self):
83# """
84# This hook is called before self.parse() on all commands
85# """
86# pass
87#
88# def at_post_cmd(self):
89# """
90# This hook is called after the command has finished executing
91# (after self.func()).
92# """
93# pass
94#
95# def parse(self):
96# """
97# This method is called by the cmdhandler once the command name
98# has been identified. It creates a new set of member variables
99# that can be later accessed from self.func() (see below)
100#
101# The following variables are available for our use when entering this
102# method (from the command definition, and assigned on the fly by the
103# cmdhandler):
104# self.key - the name of this command ('look')
105# self.aliases - the aliases of this cmd ('l')
106# self.permissions - permission string for this command
107# self.help_category - overall category of command
108#
109# self.caller - the object calling this command
110# self.cmdstring - the actual command name used to call this
111# (this allows you to know which alias was used,
112# for example)
113# self.args - the raw input; everything following self.cmdstring.
114# self.cmdset - the cmdset from which this command was picked. Not
115# often used (useful for commands like 'help' or to
116# list all available commands etc)
117# self.obj - the object on which this command was defined. It is often
118# the same as self.caller.
119#
120# A MUX command has the following possible syntax:
121#
122# name[ with several words][/switch[/switch..]] arg1[,arg2,...] [[=|,] arg[,..]]
123#
124# The 'name[ with several words]' part is already dealt with by the
125# cmdhandler at this point, and stored in self.cmdname (we don't use
126# it here). The rest of the command is stored in self.args, which can
127# start with the switch indicator /.
128#
129# This parser breaks self.args into its constituents and stores them in the
130# following variables:
131# self.switches = [list of /switches (without the /)]
132# self.raw = This is the raw argument input, including switches
133# self.args = This is re-defined to be everything *except* the switches
134# self.lhs = Everything to the left of = (lhs:'left-hand side'). If
135# no = is found, this is identical to self.args.
136# self.rhs: Everything to the right of = (rhs:'right-hand side').
137# If no '=' is found, this is None.
138# self.lhslist - [self.lhs split into a list by comma]
139# self.rhslist - [list of self.rhs split into a list by comma]
140# self.arglist = [list of space-separated args (stripped, including '=' if it exists)]
141#
142# All args and list members are stripped of excess whitespace around the
143# strings, but case is preserved.
144# """
145# raw = self.args
146# args = raw.strip()
147#
148# # split out switches
149# switches = []
150# if args and len(args) > 1 and args[0] == "/":
151# # we have a switch, or a set of switches. These end with a space.
152# switches = args[1:].split(None, 1)
153# if len(switches) > 1:
154# switches, args = switches
155# switches = switches.split('/')
156# else:
157# args = ""
158# switches = switches[0].split('/')
159# arglist = [arg.strip() for arg in args.split()]
160#
161# # check for arg1, arg2, ... = argA, argB, ... constructs
162# lhs, rhs = args, None
163# lhslist, rhslist = [arg.strip() for arg in args.split(',')], []
164# if args and '=' in args:
165# lhs, rhs = [arg.strip() for arg in args.split('=', 1)]
166# lhslist = [arg.strip() for arg in lhs.split(',')]
167# rhslist = [arg.strip() for arg in rhs.split(',')]
168#
169# # save to object properties:
170# self.raw = raw
171# self.switches = switches
172# self.args = args.strip()
173# self.arglist = arglist
174# self.lhs = lhs
175# self.lhslist = lhslist
176# self.rhs = rhs
177# self.rhslist = rhslist
178#
179# # if the class has the player_caller property set on itself, we make
180# # sure that self.caller is always the player if possible. We also create
181# # a special property "character" for the puppeted object, if any. This
182# # is convenient for commands defined on the Player only.
183# if hasattr(self, "player_caller") and self.player_caller:
184# if utils.inherits_from(self.caller, "evennia.objects.objects.DefaultObject"):
185# # caller is an Object/Character
186# self.character = self.caller
187# self.caller = self.caller.player
188# elif utils.inherits_from(self.caller, "evennia.players.players.DefaultPlayer"):
189# # caller was already a Player
190# self.character = self.caller.get_puppet(self.session)
191# else:
192# self.character = None
193#
194
195
196class FooBar(Command):
197
198
199 key = "foobar"
200
201 def func(self):
202 foobar = create.create_player("TestPlayer4", email="test@test.com", password="testpassword", typeclass=settings.BASE_PLAYER_TYPECLASS)
203 foobar.db.FIRST_LOGIN = True
204 self.msg("%s" % foobar)
205 unloggedin._create_character(self, foobar, settings.BASE_CHARACTER_TYPECLASS, settings.DEFAULT_HOME, settings.PERMISSION_PLAYER_DEFAULT)
206
207class Kill(MuxCommand):
208 key = "kill"
209 lock = "cmd:all()"
210
211 def func(self):
212 if self.lhs:
213 obj = self.caller.search(self.lhs)
214 obj.db.alive = 0
215 self.caller.msg("%s is dead." % (obj.key))
216
217class Heal(MuxCommand):
218 key = "heal"
219 lock = "cmd:all()"
220
221 def func(self):
222 if self.lhs:
223 obj = self.caller.search(self.lhs)
224 obj.db.alive = 1
225 self.caller.msg("%s is alive." % (obj.key))
226
227
228class Sheet(MuxCommand):
229
230 key = "sheet"
231 lock = "cmd:all()"
232
233 def func(self):
234 # create a new form from the template
235 form = evform.EvForm("world/charsheetform.py" )
236
237 # add data to each tagged form cell
238 form.map(cells={1: "Tom the Bouncer",
239 2: "Griatch",
240 3: "A sturdy fellow",
241 4: 12,
242 5: 10,
243 6: 5,
244 7: 18,
245 8: 10,
246 9: 3})
247 # create the EvTables
248 tableA = evform.EvTable("HP", "MV", "MP",
249 table=[["**"], ["*****"], ["***"]],
250 border="incols")
251 tableB = evform.EvTable("Skill", "Value", "Exp",
252 table=[["Shooting", "Herbalism", "Smithing"],
253 [12, 14, 9], ["550/1200", "990/1400", "205/900"]],
254 border="incols")
255 # add the tables to the proper ids in the form
256 form.map(tables={"A": tableA,
257 "B": tableB}
258
259 # unicode is required since the example contains non-ascii characters
260 self.caller.msg(unicode(form))