From 98984098c9f230e1caabd70764639c5814b0171f Mon Sep 17 00:00:00 2001 From: Brian Minsk Date: Mon, 17 Feb 2020 16:10:39 +0700 Subject: [PATCH] completed, including a minor fix to the client that wasn't asked for ... --- client.py | 3 +++ server.py | 63 +++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/client.py b/client.py index 786d5d4..b55c601 100644 --- a/client.py +++ b/client.py @@ -19,5 +19,8 @@ print(response) + if response.startswith("OK! Goodbye"): + exit(-1) + my_message = input("> ").encode('utf-8') + b'\n' client_socket.sendall(my_message) diff --git a/server.py b/server.py index d0d46c4..2150a74 100644 --- a/server.py +++ b/server.py @@ -43,7 +43,7 @@ class Server(object): each room have a unique description. """ - game_name = "Realms of Venture" + def __init__(self, port=50000): self.input_buffer = "" @@ -55,6 +55,8 @@ def __init__(self, port=50000): self.room = 0 + self.game_name = "Realms of Venture" + def connect(self): self.socket = socket.socket( socket.AF_INET, @@ -79,9 +81,11 @@ def room_description(self, room_number): :return: str """ - # TODO: YOUR CODE HERE + return ["You are in room 0.", + "You are in room 1.", + "You are in room 2.", + "You are in room 3."][room_number] - pass def greet(self): """ @@ -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() - pass def move(self, argument): """ @@ -133,9 +140,25 @@ def move(self, argument): :return: None """ - # TODO: YOUR CODE HERE + if self.room == 0: + if argument == "west": + self.room = 1 + elif argument == "east": + self.room = 2 + elif argument == "north": + self.room = 3 + elif self.room == 1: + if argument == "east": + self.room = 0 + elif self.room == 2: + if argument == 'west': + self.room = 0 + elif self.room == 3: + if argument == "south": + self.room = 0 + + self.output_buffer = self.room_description(self.room) - pass def say(self, argument): """ @@ -151,10 +174,9 @@ def say(self, argument): :return: None """ - # TODO: YOUR CODE HERE - - pass + self.output_buffer = 'You say, "{}"'.format(argument) + def quit(self, argument): """ Quits the client from the server. @@ -167,9 +189,8 @@ def quit(self, argument): :return: None """ - # TODO: YOUR CODE HERE - - pass + self.done = True + self.output_buffer = 'Goodbye' def route(self): """ @@ -183,9 +204,17 @@ def route(self): :return: None """ - # TODO: YOUR CODE HERE + received = self.input_buffer.split(" ") + + command = received.pop(0).lower().strip() + arguments = " ".join(received).strip() + + { + 'quit': self.quit, + 'move': self.move, + 'say': self.say, + }[command](arguments) - pass def push_output(self): """ @@ -197,9 +226,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()