SNHU-IT-140/Adventure Game/adventure_game/Handler.py

66 lines
2.8 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 whenPressed(self):
self.parent.parentApp.log.debug('Send button pressed!')
# This is the raw command from the user
raw_command = self.parent.dialogueBox.value
# 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", "PICK UP" 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
roomlocation = player.playerData['player']['location'] + '.yaml'
try:
room = parse(self.parent.parentApp.mainPath / 'gamedata/world' / roomlocation)
# 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')
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))
# 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')