172 lines
8.5 KiB
Python
172 lines
8.5 KiB
Python
import npyscreen
|
|
|
|
from yaml_parser import parse_datafile as parse
|
|
|
|
|
|
class Handler(npyscreen.ButtonPress):
|
|
"""
|
|
Very important, called when the player hits send, there are several things we need to do here:
|
|
1: handle the player's input, and run logic, this is done in handler.py
|
|
2: prepare new items to display on the screen
|
|
3: re-render the screen
|
|
"""
|
|
|
|
def localize_room(self, roomlocation):
|
|
"""
|
|
This method can re-localize the room.
|
|
"""
|
|
try:
|
|
room = parse(self.parent.parentApp.mainPath / 'gamedata/world' / roomlocation)['room']
|
|
|
|
# If the file could not be found
|
|
except FileNotFoundError:
|
|
# Log a critical error!
|
|
self.parent.parentApp.log.critical(
|
|
'Handler could not load the current room! Is the player file corrupt or was there a typo? Path was {0}'.format(
|
|
self.parent.parentApp.mainPath / 'gamedata/world' / roomlocation))
|
|
# Put the player in a blank room i forgot to finish
|
|
room = parse(self.parent.parentApp.mainPath / 'gamedata/world/blank_room.yaml')
|
|
return room
|
|
|
|
def whenPressed(self):
|
|
self.parent.parentApp.log.debug('Send button pressed!')
|
|
# This is the raw command from the user
|
|
raw_command = self.parent.dialogueBox.value.upper()
|
|
|
|
# Clear the dialogue box, TODO: This may become unneeded if issue #8 is fixed
|
|
self.parent.dialogueBox.value = ''
|
|
|
|
# This is the raw command from the user
|
|
parsed_command = raw_command.split()
|
|
|
|
try:
|
|
command = parsed_command.pop(0)
|
|
except IndexError:
|
|
self.parent.parentApp.log.warn(
|
|
'Command "{0}" could not be split, was it malformed or incomplete?'.format(raw_command))
|
|
command = ''
|
|
arguments = parsed_command # Whatever is left in the list, are arguments.
|
|
|
|
# Handle an empty command
|
|
if len(command) <= 2:
|
|
self.parent.update_log('Command was too short, try something like "MOVE", "LOOK AROUND" or "USE".')
|
|
|
|
else:
|
|
# Concatenate everything back together (just to show the user the program understood them correctly
|
|
self.parent.update_log(command + ' ' + ' '.join(str(s) for s in arguments))
|
|
|
|
"""
|
|
This is where real logic can happen!
|
|
"""
|
|
|
|
# Localize the player
|
|
player = self.parent.parentApp.player
|
|
|
|
room = self.localize_room(player.playerData['player']['location']) # Localize the room
|
|
|
|
# By now we should be situated in our room, and with our player.
|
|
# self.parent.parentApp.log.debug(room) # We dont need to log this, its too verbose!
|
|
|
|
# TODO: Should upgrade these to use fuzzy words library! and not direct comparisons!
|
|
if command == 'LOOK':
|
|
try:
|
|
if arguments[0] == 'AROUND':
|
|
try:
|
|
self.parent.update_log(room['look_around'])
|
|
except KeyError:
|
|
self.parent.update_log('There is nothing to look at?.. This might be a bug.')
|
|
if arguments[0] == 'AT':
|
|
try:
|
|
# Argument[1] is the "thing" you want to look at, yaml is lowercase so we lowercase it.
|
|
self.parent.update_log(room[arguments[1].lower()]['look_at'])
|
|
except KeyError:
|
|
self.parent.update_log("Im not sure what you're trying to look at.")
|
|
except IndexError:
|
|
self.parent.parentApp.log.error('Could not handle {0}, {1}'.format(command, arguments))
|
|
self.parent.update_log("You must specify something to look at.")
|
|
|
|
elif command in ['PICK', 'TAKE']:
|
|
try:
|
|
if arguments[0] == 'UP' or command in ['TAKE']:
|
|
if command in ['TAKE']:
|
|
arguments = ['UP'] + arguments # This is a messy way to fix take logic vs pick up logic but it works.
|
|
if len(arguments) <= 2: # If there are only 2 args ex:up, item then we dont need to merge that last arg
|
|
try:
|
|
# Argument[1] is the "thing" you want to pick up, yaml is lowercase so we lowercase it.
|
|
self.parent.parentApp.log.info('Player tried to pick up {0}'.format(arguments[1]))
|
|
self.parent.update_log(room[arguments[1].lower()]['pick_up'])
|
|
player.add_inventory(room[arguments[1].lower()]['item_name'])
|
|
except KeyError:
|
|
self.parent.update_log("You cant pick that up.")
|
|
else: # if its a longer list of args, the player prolly wants to pick up an item with multiple words, like hand_axe, or log_viewer
|
|
try:
|
|
long_arg = '_'.join(
|
|
arguments[1:]) # Joins whatever comes after 1 in our args with '_' between
|
|
self.parent.parentApp.log.info(
|
|
'Player tried to pick up long object {0}'.format(long_arg))
|
|
self.parent.update_log(room[long_arg.lower()]['pick_up'])
|
|
player.add_inventory(room[long_arg.lower()]['item_name'])
|
|
except KeyError:
|
|
self.parent.update_log("You cant pick that up.")
|
|
except IndexError:
|
|
self.parent.parentApp.log.error('Could not handle {0}, {1}'.format(command, arguments))
|
|
self.parent.update_log("You must specify something to pick up.")
|
|
|
|
elif command == 'OPEN':
|
|
try:
|
|
self.parent.parentApp.log.info('Player tried to open door: {0}'.format(arguments[0]))
|
|
new_room = room[arguments[0].lower()]['leads_to']
|
|
self.parent.parentApp.log.debug('New room is: {0}'.format(new_room))
|
|
upon_enter = player.change_room(new_room) # Change the player to that new room.
|
|
self.parent.update_log(upon_enter) # Print the new room upon enter text.
|
|
|
|
room = self.localize_room(player.playerData['player']['location']) # Re-Localize!
|
|
except KeyError:
|
|
self.parent.update_log("You cant open that.")
|
|
except IndexError:
|
|
self.parent.update_log("You must specify something to open")
|
|
|
|
elif command == 'INSPECT':
|
|
try:
|
|
self.parent.parentApp.log.info('Player inspecting: {0}'.format(arguments[0]))
|
|
self.parent.update_log(
|
|
room[arguments[0].lower()]['inspect']) # Prints the inspect text, if it exists
|
|
except KeyError:
|
|
self.parent.update_log("Nothing more to inspect here.")
|
|
except IndexError:
|
|
self.parent.update_log("You must specify something to inspect.")
|
|
|
|
elif command == 'START':
|
|
self.parent.update_log('Welcome to the game! Try to |LOOK AROUND|.')
|
|
|
|
else:
|
|
self.parent.parentApp.log.info('Player\'s command was not understood: {0}'.format(command))
|
|
self.parent.update_log('I didn\'t understand {0}'.format(command))
|
|
|
|
if command == 'WHERE':
|
|
# TODO: this should take the human readable room name, not the code-name
|
|
self.parent.update_log('You are in {0}.'.format(room))
|
|
|
|
# Log the command that we parsed
|
|
self.parent.parentApp.log.info('Parsed command "{0}" with arguments "{1}"'.format(command, arguments))
|
|
|
|
"""
|
|
Do a little bit of final setup, change the art if we
|
|
need to, change the text at the bottom and update the inventory.
|
|
"""
|
|
try:
|
|
self.parent.update_location(
|
|
room['name'],
|
|
room['art'])
|
|
except KeyError:
|
|
self.parent.update_location('Unknown Location')
|
|
|
|
self.parent.update_inventory(player.playerData['player']['inventory'])
|
|
|
|
# Make sure to re-draw the art box when we're all done (in case we updated it in logic above)
|
|
self.parent.artContent.display()
|
|
|
|
# Switch back to the game menu.
|
|
# TODO: possibly deprecate this statement?
|
|
self.parent.parentApp.switchForm('GAME')
|