Skip to content

Instantly share code, notes, and snippets.

@avidal
Created February 14, 2011 22:21
Show Gist options
  • Save avidal/826710 to your computer and use it in GitHub Desktop.
Save avidal/826710 to your computer and use it in GitHub Desktop.
import commands as cmd
"""
cmd.register(syntax=None, command=None)
syntax can be a list/tuple of strings, or a string. if it's a list of strings, each string is registered as a valid syntax. if no syntax is specified, it's assumed to not have a syntax (eg: exits has nothing but the actual command name)
command can be a list/tuple of strings, or a string. if it's a list of strings, each string is registered as a valid name for the command. if no commands are specified, it's inferred from the name of the function (eg: def exits registers as 'exits' the command)
"""
@cmd.validators.register('container')
def container_parser(env, arg):
# env is the environment
# args is the actual string value
# we should return the actual object/value, or None
ch = env.ch
arg = arg.lower()
items = ch.equipment + ch.inventory + ch.room.contents
items = filter(lambda obj: ch.can_see(obj), items)
containers = filter(lambda obj: obj.is_container(), items)
for item in containers:
if item.name.lower() == arg:
return item
return None
@cmd.validators.register('visible')
def visible_parser(env, arg):
# the visible parser will check all visible objects in the room/about the character
# highest priority is items on the person
ch = env.ch
items = ch.equipment + ch.inventory + ch.room.contents
items = filter(lambda obj: ch.can_see(obj), items)
for item in items:
if item.name.lower() == arg:
return item
return None
@cmd.register(['', '(at)? <visible:obj>', '((at)? <visible:obj>)? (in)? <container:container>'], command=['l', 'look'])
def look(ch, *args, **kwargs):
if 'container' in kwargs:
container = kwargs['container']
# look in the container
# ... print container contents to player ...
elif 'obj' in kwargs:
# ... print obj properties to player ...
else:
# ... print room to player ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment