diff --git a/__pycache__/server.cpython-37.pyc b/__pycache__/server.cpython-37.pyc new file mode 100644 index 0000000..8587cd1 Binary files /dev/null and b/__pycache__/server.cpython-37.pyc differ diff --git a/server.py b/server.py index d0d46c4..171c767 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 red wallpaper.", + "You are in the room with the white wallpaper.", + "You are in the room with the blue wallpaper.", + "You are in the room with the star wallpaper.", + ][room_number] + def greet(self): """ Welcome a client to the game. @@ -108,9 +112,12 @@ def get_input(self): :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 def move(self, argument): """ @@ -133,9 +140,27 @@ def move(self, argument): :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 - pass + 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 + + self.output_buffer = self.room_description(self.room) + + def say(self, argument): """ @@ -151,9 +176,7 @@ def say(self, argument): :return: None """ - # TODO: YOUR CODE HERE - - pass + self.output_buffer = 'You say, "{}"'.format(argument) def quit(self, argument): """ @@ -167,10 +190,9 @@ def quit(self, argument): :return: None """ - # TODO: YOUR CODE HERE - - pass - + self.done = True + self.output_buffer = "Goodbye!" + def route(self): """ Examines `self.input_buffer` to perform the correct action (move, quit, or @@ -183,9 +205,19 @@ def route(self): :return: None """ - # TODO: YOUR CODE HERE + + received = self.input_buffer.split(" ") + + command = received.pop(0) + arguments = " ".join(received) - pass + { + 'quit': self.quit, + 'move': self.move, + 'say': self.say, + }[command](arguments) + + def push_output(self): """ @@ -197,10 +229,8 @@ def push_output(self): :return: None """ - # TODO: YOUR CODE HERE - - pass - + self.client_connection.sendall(b"OK! " + self.output_buffer.encode() + b"\n") + def serve(self): self.connect() self.greet()