Patched wiki2html to give correct source code snippet output.

This commit is contained in:
Griatch 2011-09-11 12:17:33 +02:00
parent 22b23be095
commit eae89eabc0
31 changed files with 1028 additions and 175 deletions

View file

@ -124,17 +124,42 @@ to take one example.
pre-parsed input to actually do whatever the command is supposed to do.
This is the main body of the command.
Finally, you should always make an informative ```__doc__``
Finally, you should always make an informative `doc
string <http://www.python.org/dev/peps/pep-0257/#what-is-a-docstring>`_
at the top of your class. This string is dynamically read by the `Help
system <HelpSystem.html>`_ to create the help entry for this command.
You should decide on a way to format your help and stick to that.
(``__doc__``) at the top of your class. This string is dynamically read
by the `Help system <HelpSystem.html>`_ to create the help entry for
this command. You should decide on a way to format your help and stick
to that.
Below is how you define a simple alternative "``look at``" command:
::
from game.gamesrc.commands.basecommand import Commandclass CmdLookAt(Command): """ An alternative (and silly) look command Usage: look at <what> Where <what> may only be 'here' in this example. This initial string (the __doc__ string) is also used to auto-generate the help for this command ... """ key = "look at" # this is the command name to use aliases = ["la", "look a"] # aliases to the command name locks = "cmd:all()" help_category = "General" def parse(self): "Very trivial parser" self.what = self.args.strip() def func(self): "This actually does things" caller = self.caller if not self.what: caller.msg("Look at what?") elif self.what == 'here': # look at the current location description = caller.location.db.desc caller.msg(description) else: # we don't add any more functionality in this example caller.msg("Sorry, you can only look 'here'...")
from game.gamesrc.commands.basecommand import Commandclass CmdLookAt(Command):
"""
An alternative (and silly) look command Usage:
look at <what> Where <what> may only be 'here' in this example. This initial string (the __doc__ string)
is also used to auto-generate the help
for this command ...
"""
key = "look at" # this is the command name to use
aliases = ["la", "look a"] # aliases to the command name
locks = "cmd:all()"
help_category = "General" def parse(self):
"Very trivial parser"
self.what = self.args.strip() def func(self):
"This actually does things"
caller = self.caller
if not self.what:
caller.msg("Look at what?")
elif self.what == 'here':
# look at the current location
description = caller.location.db.desc
caller.msg(description)
else:
# we don't add any more functionality in this example
caller.msg("Sorry, you can only look 'here'...")
The power of having commands as classes and to separate ``parse()`` and
``func()`` lies in the ability to inherit functionality without having
@ -191,7 +216,17 @@ rules <Commands#Merge_rules.html>`_ section).
::
from src.commands.cmdset import CmdSet from game.gamesrc.commands import mycommandsclass MyCmdSet(CmdSet): def at_cmdset_creation(self): """ The only thing this method should need to do is to add commands to the set. """ self.add(mycommands.MyCommand1()) self.add(mycommands.MyCommand2()) self.add(mycommands.MyCommand3())
from src.commands.cmdset import CmdSet
from game.gamesrc.commands import mycommandsclass MyCmdSet(CmdSet):
def at_cmdset_creation(self):
"""
The only thing this method should need
to do is to add commands to the set.
"""
self.add(mycommands.MyCommand1())
self.add(mycommands.MyCommand2())
self.add(mycommands.MyCommand3())
The !CmdSet's ``add()`` method can also take another CmdSet as input. In
this case all the commands from that CmdSet will be appended to this one
@ -199,7 +234,10 @@ as if you added them line by line:
::
at_cmdset_creation(): ... self.add(AdditionalCmdSet) # adds all command from this set ...
at_cmdset_creation():
...
self.add(AdditionalCmdSet) # adds all command from this set
...
If you added your command to an existing cmdset (like to the default
cmdset), that set is already loaded into memory. You need to make the
@ -261,7 +299,19 @@ look:
::
from game.gamesrc.commands.basecommand import MuxCommandclass MyCommand(MuxCommand): """ Simple command example Usage: mycommand <text> This command simply echoes text back to the caller. (this string is also the help text for the command) """ key = "mycommand" locks = "cmd:all()" def func(self): "This actually does things" if not self.args: self.caller.msg("You didn't enter anything!") else: self.caller.msg("You gave the string: '%s'" % self.args)
from game.gamesrc.commands.basecommand import MuxCommandclass MyCommand(MuxCommand):
"""
Simple command example Usage:
mycommand <text> This command simply echoes text back to the caller.
(this string is also the help text for the command)
""" key = "mycommand"
locks = "cmd:all()" def func(self):
"This actually does things"
if not self.args:
self.caller.msg("You didn't enter anything!")
else:
self.caller.msg("You gave the string: '%s'" % self.args)
Next we want to make this command available to us. There are many ways
to do this, but all of them involves putting this command in a *Command
@ -278,7 +328,10 @@ This is what we have now:
::
from game.gamesrc.commands.basecmdset import CmdSet
from game.gamesrc.commands import mycommandclass MyCmdSet(CmdSet): key = "MyCmdSet" def at_cmdset_creation(self): self.add(mycommand.MyCommand())
from game.gamesrc.commands import mycommandclass MyCmdSet(CmdSet):
key = "MyCmdSet" def at_cmdset_creation(self):
self.add(mycommand.MyCommand())
This new command set could of course contain any number of commands. We
will now temporarily *merge* this command set to your current set. This
@ -342,7 +395,15 @@ class and you will in fact append it to the existing command set.
::
# file gamesrc/commands/basecmdset.py ... from game.gamesrc.commands import mycommandclass DefaultSet(BaseDefaultSet): key = DefaultMUX def at_cmdset_creation(self): # this first adds all default commands super(DefaultSet, self).at_cmdset_creation() # all commands added after this point will extend or # overwrite the default commands. self.add(mycommand.MyCommand())
# file gamesrc/commands/basecmdset.py
...
from game.gamesrc.commands import mycommandclass DefaultSet(BaseDefaultSet):
key = DefaultMUX def at_cmdset_creation(self): # this first adds all default commands
super(DefaultSet, self).at_cmdset_creation() # all commands added after this point will extend or
# overwrite the default commands.
self.add(mycommand.MyCommand())
Again, you need to run the ``@reload`` command to make these changes
available.
@ -431,7 +492,8 @@ Same-key commands are merged by priority.
::
# Union A1,A2 + B1,B2,B3,B4 = A1,A2,B3,B4
# Union
A1,A2 + B1,B2,B3,B4 = A1,A2,B3,B4
**Intersect** - Only commands found in *both* cmdsets (i.e. which have
the same keys) end up in the merged cmdset, with the higher-priority
@ -439,7 +501,8 @@ cmdset replacing the lower one's commands.
::
# Intersect A1,A3,A5 + B1,B2,B4,B5 = A1,A5
# Intersect
A1,A3,A5 + B1,B2,B4,B5 = A1,A5
**Replace** - The commands of the higher-prio cmdset completely replaces
the lower-priority cmdset's commands, regardless of if same-key commands
@ -447,7 +510,8 @@ exist or not.
::
# Replace A1,A3 + B1,B2,B4,B5 = A1,A3
# Replace
A1,A3 + B1,B2,B4,B5 = A1,A3
**Remove** - The high-priority command sets removes same-key commands
from the lower-priority cmdset. They are not replaced with anything, so
@ -456,7 +520,8 @@ high-prio one as a template.
::
# Remove A1,A3 + B1,B2,B3,B4,B5 = B2,B4,B5
# Remove
A1,A3 + B1,B2,B3,B4,B5 = B2,B4,B5
Besides ``priority`` and ``mergetype``, a command set also takes a few
other variables to control how they merge:
@ -483,7 +548,17 @@ More advanced cmdset example:
::
class MyCmdSet(CmdSet): key = "MyCmdSet" priority = 4 mergetype = "Replace" key_mergetype = 'MyOtherCmdSet':'Union' def at_cmdset_creation(self): """ The only thing this method should need to do is to add commands to the set. """ self.add(mycommands.MyCommand1()) self.add(mycommands.MyCommand2()) self.add(mycommands.MyCommand3())
class MyCmdSet(CmdSet): key = "MyCmdSet"
priority = 4
mergetype = "Replace"
key_mergetype = 'MyOtherCmdSet':'Union' def at_cmdset_creation(self):
"""
The only thing this method should need
to do is to add commands to the set.
"""
self.add(mycommands.MyCommand1())
self.add(mycommands.MyCommand2())
self.add(mycommands.MyCommand3())
System commands
---------------
@ -534,7 +609,12 @@ command must be added to a cmdset as well before it will work.
::
from src.commands import cmdhandler from game.gamesrc.commands.basecommand import Commandclass MyNoInputCommand(Command): "Usage: Just press return, I dare you" key = cmdhandler.CMD_NOINPUT def func(self): self.caller.msg("Don't just press return like that, talk to me!")
from src.commands import cmdhandler
from game.gamesrc.commands.basecommand import Commandclass MyNoInputCommand(Command):
"Usage: Just press return, I dare you"
key = cmdhandler.CMD_NOINPUT
def func(self):
self.caller.msg("Don't just press return like that, talk to me!")
How commands actually work
--------------------------