diff --git a/server.py b/server.py index d0d46c4..6fc4597 100644 --- a/server.py +++ b/server.py @@ -80,17 +80,23 @@ def room_description(self, room_number): """ # TODO: YOUR CODE HERE + return [ + "You are in the room with the white wallpaper.", + "You are in the room with the green wallpaper.", + "You are in the room with the brown wallpaper.", + "You are in the room with the mauve wallpaper.", + ][room_number] - pass + # pass def greet(self): """ Welcome a client to the game. - + Puts a welcome message and the description of the client's current room into the output buffer. - - :return: None + + :return: None """ self.output_buffer = "Welcome to {}! {}".format( self.game_name, @@ -101,105 +107,172 @@ def get_input(self): """ Retrieve input from the client_connection. All messages from the client should end in a newline character: '\n'. - + This is a BLOCKING call. It should not return until there is some input from the client to receive. - - :return: None + + :return: None """ # TODO: YOUR CODE HERE + received = b'' + while b'\n' not in received: + received += self.client_connection.recv(16) + + self.input_buffer = received.decode().strip() - pass + # pass def move(self, argument): """ Moves the client from one room to another. - + Examines the argument, which should be one of: - + * "north" * "south" * "east" * "west" - + "Moves" the client into a new room by adjusting self.room to reflect the number of the room that the client has moved into. - + Puts the room description (see `self.room_description`) for the new room into "self.output_buffer". - + :param argument: str :return: None """ # TODO: YOUR CODE HERE + # if self.room == 0 and argument == "north": + # self.room = 3 + + # if self.room == 0 and argument == "west": + # self.room = 1 + + # if self.room == 0 and argument == "east": + # self.room = 2 + + # if self.room == 1 and argument == "east": + # self.room = 0 + + # if self.room == 2 and argument == "west": + # self.room = 0 + + # if self.room == 3 and argument == "south": + # self.room = 0 - pass + # self.output_buffer = self.room_description(self.room) + if self.room == 0: + if argument == 'north': + self.room = 3 + if argument == 'west': + self.room = 1 + if argument == 'east': + self.room = 2 + + if self.room == 1: + if argument == 'east': + self.room = 0 + + if self.room == 2: + if argument == 'west': + self.room = 0 + + if self.room == 3: + if argument == 'south': + self.room = 0 + + self.output_buffer = self.room_description(self.room) def say(self, argument): """ Lets the client speak by putting their utterance into the output buffer. - + For example: `self.say("Is there anybody here?")` would put `You say, "Is there anybody here?"` into the output buffer. - + :param argument: str :return: None """ # TODO: YOUR CODE HERE + self.output_buffer = 'You say, "{}"'.format(argument) - pass + # pass def quit(self, argument): """ Quits the client from the server. - + Turns `self.done` to True and puts "Goodbye!" onto the output buffer. - + Ignore the argument. - + :param argument: str :return: None """ # TODO: YOUR CODE HERE + self.done = True + self.output_buffer = "Goodbye!" - pass + # pass def route(self): """ Examines `self.input_buffer` to perform the correct action (move, quit, or say) on behalf of the client. - + For example, if the input buffer contains "say Is anybody here?" then `route` should invoke `self.say("Is anybody here?")`. If the input buffer contains "move north", then `route` should invoke `self.move("north")`. - + :return: None """ # TODO: YOUR CODE HERE - - pass + received = self.input_buffer.split(" ") + + command = received.pop(0) + arguments = " ".join(received) + + if (command.lower() == 'say') or (command.lower() == 'move' and arguments.lower() + in ['north', 'south', 'east', 'west']) or\ + (command.lower() == 'quit'): + { + 'quit': self.quit, + 'move': self.move, + 'say': self.say, + }[command](arguments) + else: + self.output_buffer = "First word in command must be either 'move' or 'say'." +\ + "\nIf 'say', then following can be anything." +\ + "\nIf 'move', then following must be either 'north', " +\ + "'south', 'east', 'west'" + + # If `self.input_buffer` was "say Is anybody here?", then: + # `command` should now be "say" and `arguments` should now be "Is anybody here?". + # + # If `self.input_buffer` was "move north", then: + # `command` should now be "move" and `arguments` should now be "north". def push_output(self): """ Sends the contents of the output buffer to the client. - + This method should prepend "OK! " to the output and append "\n" before sending it. - - :return: None + + :return: None """ # TODO: YOUR CODE HERE - - pass + self.client_connection.sendall(b"OK! " + self.output_buffer.encode() + b"\n") def serve(self): self.connect()