From 614b3b6794ed03adf7614d1d8ece070189f49b5e Mon Sep 17 00:00:00 2001 From: Gunturu Date: Mon, 23 Dec 2019 01:05:42 -0800 Subject: [PATCH] Socket adventure activity --- server.py | 57 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/server.py b/server.py index d0d46c4..c5a81fb 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:", + "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 mauva wallpaper:", + ][room_number] def greet(self): """ @@ -108,9 +111,10 @@ def get_input(self): :return: None """ - # TODO: YOUR CODE HERE - - pass + received = b'' + while b'\n' not in received: + received += self.client_connection.recv(16) + self.input_buffer = received.decode() def move(self, argument): """ @@ -133,9 +137,20 @@ def move(self, argument): :return: None """ - # TODO: YOUR CODE HERE - - pass + if self.room == 0 and argument == "north": + self.room = 3 + elif self.room == 0 and argument == "west": + self.room = 1 + elif self.room == 0 and argument == "east": + self.room = 2 + elif self.room == 1 and argument == "east": + self.room = 0 + elif self.room == 2 and argument == "west": + self.room = 0 + elif self.room == 3 and argument == "south": + self.room = 0 + + self.output_buffer = self.room_description(self.room) def say(self, argument): """ @@ -151,9 +166,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 +180,8 @@ def quit(self, argument): :return: None """ - # TODO: YOUR CODE HERE - - pass + self.done = True + self.output_buffer = "Goodbye!" def route(self): """ @@ -183,9 +195,14 @@ def route(self): :return: None """ - # TODO: YOUR CODE HERE - - pass + received = self.input_buffer.split(" ") + command = received.pop(0) + arguments = " ".join(received) + { + 'quit': self.quit, + 'move': self.move, + 'say': self.say, + }[command](arguments) def push_output(self): """ @@ -197,9 +214,7 @@ 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()