diff --git a/server.py b/server.py index d0d46c4..bf6276c 100644 --- a/server.py +++ b/server.py @@ -79,10 +79,14 @@ def room_description(self, room_number): :return: str """ - # TODO: YOUR CODE HERE - - pass + 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] + def greet(self): """ Welcome a client to the game. @@ -108,9 +112,14 @@ def get_input(self): :return: None """ - # TODO: YOUR CODE HERE + received = b'' + + while b'\n' not in received: + received += self.client_connection.recv(16) + + # had to use the replace method to drop the '\n'--wondering if the socket object read method handles this better; for some reason I couldn't get the read method (that was used in the activity help) working + self.input_buffer = received.decode().replace('\n', '') - pass def move(self, argument): """ @@ -133,9 +142,25 @@ def move(self, argument): :return: None """ - # TODO: YOUR CODE HERE + allowed_argument = {0: ["north", "east", "west"], + 1: ["east"], + 2: ["west"], + 3: ["south"]} + + new_move_room = {0: [3, 2, 1], + 1: [0], + 2: [0], + 3: [0]} + + if argument in allowed_argument[self.room]: + new_room_idx = allowed_argument[self.room].index(argument) + new_room = new_move_room[self.room][new_room_idx] + self.room = new_room + + self.output_buffer = self.room_description(self.room) + else: + self.output_buffer = "Invalid movement." - pass def say(self, argument): """ @@ -151,9 +176,8 @@ def say(self, argument): :return: None """ - # TODO: YOUR CODE HERE + self.output_buffer = 'You say, "{}"'.format(argument) - pass def quit(self, argument): """ @@ -167,9 +191,9 @@ def quit(self, argument): :return: None """ - # TODO: YOUR CODE HERE + self.done = True + self.output_buffer = "Goodbye!" - pass def route(self): """ @@ -183,9 +207,17 @@ def route(self): :return: None """ - # TODO: YOUR CODE HERE + received = self.input_buffer.split(" ") + + command = received.pop(0) + arguments = ' '.join(received) + + { + 'quit': self.quit, + 'say': self.say, + 'move': self.move + }[command](arguments) - pass def push_output(self): """ @@ -197,9 +229,8 @@ def push_output(self): :return: None """ - # TODO: YOUR CODE HERE + self.client_connection.sendall(b"OK! " + self.output_buffer.encode() + b"\n") - pass def serve(self): self.connect()