Created
January 7, 2013 09:47
-
-
Save anonymous/4473757 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## C O M M A N D S ## | |
''' | |
This could be neat, because you could store the command list as a constant somewhere that is populated during run-time with all the commands that are contained inside a particular module. ie. Put each command inside its own file with a list of synonyms as one of the variables, then dynamically import it and populate the COMMAND_LIST dictionary with its synonyms and function. Just a fun thought. :D | |
''' | |
COMMAND_LIST = { | |
'give': give, | |
'provide': give, | |
'yield': give, | |
} | |
def error_cmd(*args): | |
raise ValueError('Not a valid command. Valid commands include: {0}'.format(COMMAND_LIST.keys())) | |
def give(*args): # just take in anything and manually process it | |
# Example: | |
# something = 'money', to = 'to', someone = 'vendor' | |
something = None | |
someone = None | |
try: | |
something, to, someone = args | |
if to != 'to': | |
raise ValueError | |
except ValueError: | |
raise ValueError('Invalid Syntax: give <something> to <someone>') | |
# perform the action based on the arguments | |
player.remove_from_inventory(something) | |
vendor.place_in_inventory(something) | |
def take(item, *args): # Mix it up when the aruments are optional | |
# Example: | |
# take scroll; item = 'scroll', from = 'from', someone = 'vagrant' | |
# take scroll from vagrant | |
# do a thing... | |
pass | |
def eat(item): # Be strict | |
# Example: | |
# item = 'apple' | |
# do a thing... | |
pass | |
## / C O M M A N D S ## | |
## R O O M S ## | |
''' | |
Perhaps store the functions of the game in a yaml file to (a) reduce the amount of classes with the same name [Tile] and (b) make maintainability a bit easier. Different user's additions could be stored in different yaml files that described the dungeon in a human-readable way. | |
''' | |
rooms: | |
- coords: 0,0 | |
desc: 'You are surrounded by blah blah blah.' | |
leave: 'You exit the blah blah blah.' | |
object: money | |
action: take | |
object: fountain | |
action: drink, bathe | |
mob: vendor | |
action: list, buy, blah... | |
- coords: 0,1 | |
desc: ... | |
leave: ... | |
... | |
## / R O O M S ## | |
## H A N D L E R ## | |
''' | |
This would more or less be the same as what you've already got in place. :D | |
''' | |
class Handler(do): | |
# Examples: | |
# give money to vendor | |
# take scroll | |
# eat apple | |
# construct the action, subject, preposition, and object | |
args = do.split() # ['give', 'money', 'to', 'vendor'] | |
action = do.pop(0) # action = 'give', args = ['money', 'to', 'vendor'] | |
# process action | |
try: | |
COMMAND_LIST.get(action, error_cmd)(*args) | |
except InGameException as e: # something expected, like not having the item | |
print e | |
except ValueError as e: | |
print e | |
## / H A N D L E R ## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment