Exemplo n.º 1
0
	def drop(self, command_list):
		item_names = command_list[1::2]

		for item_name in item_names:
			item = self.game.get_from_inventory(item_name[0])
			if item:
				self.game.curloc.add_item(item_name[0], item)
				wrap("You drop the {0}.".format(item.name))
			else:
				wrap("The {0} isn't in your inventory.".format(item_name[0]))
Exemplo n.º 2
0
	def go(self, command_list):
		direction = command_list[1][0]
		if direction:
			if direction in self.game.curloc.connections:
				if self.game.curloc.is_blocked(direction, self.game):
					wrap(self.game.curloc.get_block_description(direction))
				else:
					print(self.game.curloc.connections[direction].name)
					self.game.curloc = self.game.curloc.connections[direction]
					if self.game.curloc.end_game:
						self.game.describe_current_location()
					else:
						self.game.describe()
			else:
				print("You can't go %s from here." % direction.upper())
		return self.game.curloc.end_game
Exemplo n.º 3
0
	def parse_command(self, command):
		self.command_history.append(command)
		end_game = False

		tokens = self.lexicon.tokenize(command) # Still need to split commands by ','
		command = self.check_structure(tokens) # Check if syntax is a proper command
		if command:
			# First word of command should match the command function below.
			action = getattr(self, command[0][0], None)
			if action:
				action(command)
			else:
				wrap("You can't {0}.".format(self.command_history[-1]))
		else:
			responses = ["Huh?", "What?", "Say again?", "Come again?",
				"Can you say that differently?", "Does. Not. Compute."]
			i = randrange(len(responses))
			wrap(responses[i])
Exemplo n.º 4
0
	def take(self, command_list):
		end_game = False
		item_names = command_list[1::2]
		
		for item_name in item_names:
			# check to see if already in inventory
			if self.game.has_item(item_name[0]):
				wrap("You already have the {0}.".format(item_name[0]))
			else:
				item = self.game.curloc.get_item(item_name[0])
				if item:
					if item.gettable:
						matched_item = True
						self.game.add_to_inventory(item)
						wrap(item.take_text)
					else:
						print(item.take_fail)
				else:
					print("The {0} isn't here.".format(item_name[0]))
Exemplo n.º 5
0
def p_apply_function(t):
    """
    sexpr : LPAREN function sexprs RPAREN
    sexpr : LPAREN function sexpr RPAREN
    """
    f = t[2]
    if hasattr(f, "__name__"):
        fname = f.__name__
    else:
        fname = f
    if callable(f):
        if f.__name__ in ["add", "div", "mul", "sub"]:
            t[0] = "$(({} {} {}))".format(t[3][0], INVERSE_OPERATOR_MAP[f], t[3][1])
        else:
            t[0] = f(*t[3])
        if fname == "range":
            t[0] = create_array(t[0])
    elif isinstance(f, basestring):
        if f == MACRO_FOR:
            loop_variable, iterable, body = t[3]
            t[
                0
            ] = """for %s in $( %s ); do
                %s
            done""" % (
                loop_variable,
                iterable,
                body,
            )
        elif f == MACRO_ARRAY:
            t[0] = create_array([t[3]])
        elif f == MACRO_BACKTICKS:
            t[0] = "`%s`" % t[3].replace("'", "")
        elif f == MACRO_VAR:
            t.is_variable = True
            t[0] = wrap("${}".format(t[3]), '"')
        elif f == MACRO_LET:
            t[3][1] = shell_quote(t[3][1])
            t[0] = "%s=%s" % tuple(t[3])
        elif f == MACRO_EXPORT:
            t[3][1] = shell_quote(t[3][1])
            t[0] = "export %s=%s" % tuple(t[3])
        elif f == MACRO_IF_ELSE:
            condition, body, else_body = t[3]
            t[
                0
            ] = """if [ {} ]; then
                {}
            else
                {}
            fi""".format(
                condition, body, else_body
            )
        elif f.strip("?") in ("ne", "eq", "gt", "ge", "lt", "le"):
            left_operand, right_operand = t[3]
            t[0] = " ".join([shell_quote(left_operand), "-{}".format(f.strip("?")), shell_quote(right_operand)])
        elif f in (MACRO_COMMENT, MACRO_RAW):
            if t[3].startswith("'"):
                strip_char = "'"
            else:
                strip_char = '"'
            t[0] = t[3].strip(strip_char)
            if f == MACRO_COMMENT:
                t[0] = "# %s" % t[0]
        elif f == MACRO_PIPE:
            t[0] = " | ".join(t[3])
        else:
            if isinstance(t[3], list):

                def _quote_tokens(token):
                    if hasattr(t, "is_variable"):
                        if getattr(t, "is_variable"):
                            return '"{}"'.format(unicode(token))
                    else:
                        return shell_quote(unicode(token))

                t[0] = "{} {}".format(f, " ".join(map(_quote_tokens, chain(t[3]))))
            else:
                t[0] = "%s %s" % (f, t[3])
    if isinstance(t[0], bool):
        t[0] = unicode(t[0]).lower()
Exemplo n.º 6
0
def p_apply_function(t):
    """
    sexpr : LPAREN function sexprs RPAREN
    sexpr : LPAREN function sexpr RPAREN
    """
    f = t[2]
    if hasattr(f, '__name__'):
        fname = f.__name__
    else:
        fname = f
    if callable(f):
        if f.__name__ in ['add', 'div', 'mul', 'sub']:
            t[0] = '$(({} {} {}))'.format(
                t[3][0],
                INVERSE_OPERATOR_MAP[f],
                t[3][1]
            )
        else:
            t[0] = f(*t[3])
        if fname == "range":
            t[0] = create_array(t[0])
    elif isinstance(f, basestring):
        if f == MACRO_FOR:
            loop_variable, iterable, body = t[3]
            t[0] = """for %s in $( %s ); do
                %s
            done""" % (loop_variable, iterable, body,)
        elif f == MACRO_ARRAY:
            t[0] = create_array([t[3]])
        elif f == MACRO_BACKTICKS:
            t[0] = '`%s`' % t[3].replace("'", '')
        elif f == MACRO_VAR:
            t.is_variable = True
            t[0] = wrap('${}'.format(t[3]), '"')
        elif f == MACRO_LET:
            t[3][1] = shell_quote(t[3][1])
            t[0] = "%s=%s" % tuple(t[3])
        elif f == MACRO_EXPORT:
            t[3][1] = shell_quote(t[3][1])
            t[0] = "export %s=%s" % tuple(t[3])
        elif f == MACRO_IF_ELSE:
            condition, body, else_body = t[3]
            t[0] = """if [ {} ]; then
                {}
            else
                {}
            fi""".format(
                condition,
                body,
                else_body
            )
        elif f.strip('?') in ('ne', 'eq', 'gt', 'ge', 'lt', 'le',):
            left_operand, right_operand = t[3]
            t[0] = ' '.join([
                shell_quote(left_operand),
                '-{}'.format(f.strip('?')),
                shell_quote(right_operand)
            ])
        elif f in (MACRO_COMMENT, MACRO_RAW):
            if t[3].startswith("'"):
                strip_char = "'"
            else:
                strip_char = '"'
            t[0] = t[3].strip(strip_char)
            if f == MACRO_COMMENT:
                t[0] = '# %s' % t[0]
        elif f == MACRO_PIPE:
            t[0] = ' | '.join(t[3])
        else:
            if isinstance(t[3], list):
                def _quote_tokens(token):
                    if hasattr(t, 'is_variable'):
                        if getattr(t, 'is_variable'):
                            return '"{}"'.format(
                                unicode(token)
                            )
                    else:
                        return shell_quote(
                            unicode(token)
                        )
                t[0] = "{} {}".format(
                    f,
                    ' '.join(
                        map(
                            _quote_tokens,
                            chain(t[3])
                        )
                    )
                )
            else:
                t[0] = "%s %s" % (f, t[3])
    if isinstance(t[0], bool):
        t[0] = unicode(t[0]).lower()
Exemplo n.º 7
0
 def describe_current_location(self):
     """Describe the current location by printing its description field."""
     wrap(self.curloc.description)