diff --git a/server.py b/server.py index d0d46c4..2fbf3e2 100644 --- a/server.py +++ b/server.py @@ -79,9 +79,12 @@ def room_description(self, room_number): :return: str """ - # TODO: YOUR CODE HERE - - pass + return [ + "You are in the room with the white wallpaper.", # room_number = 0 + "You are in the room with the green wallpaper.", # room_number = 1 + "You are in the room with the brown wallpaper.", # room_number = 2 + "You are in the room with the mauve wallpaper.", # room_number = 3 + ][room_number] def greet(self): """ @@ -108,9 +111,11 @@ def get_input(self): :return: None """ - # TODO: YOUR CODE HERE + recieved = b'' + while b'\n' not in recieved: # continue until \n + recieved += self.client_connection.recv(16) - pass + self.input_buffer = recieved.decode().strip() # strips the \n def move(self, argument): """ @@ -133,9 +138,24 @@ 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 +171,7 @@ def say(self, argument): :return: None """ - # TODO: YOUR CODE HERE - - pass + self.output_buffer = 'You say, "{}"'.format(argument) def quit(self, argument): """ @@ -167,9 +185,8 @@ def quit(self, argument): :return: None """ - # TODO: YOUR CODE HERE - - pass + self.done = True + self.output_buffer = "Goodbye!" def route(self): """ @@ -183,9 +200,22 @@ def route(self): :return: None """ - # TODO: YOUR CODE HERE + recieved = self.input_buffer.split(" ") + + command = recieved.pop(0) + arguments = " ".join(recieved) + + # If 'self.input_buffer' was "say Is anybody here?", then: + # 'command' should be "say" and arguments should be "Is anybody here?". + # + # If 'self.input_buffer' was "move north", then: + # 'command' should be "move" and arguments should be "north". - pass + { + 'quit': self.quit, + 'move': self.move, + 'say': self.say, + }[command](arguments) def push_output(self): """ @@ -196,10 +226,8 @@ def push_output(self): :return: None """ - - # TODO: YOUR CODE HERE - - pass + # can't send strings, have to send bytes (Ex. bytes = b"") + self.client_connection.sendall(b"OK! " + self.output_buffer.encode() + b"\n") def serve(self): self.connect()