Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

submitting activity #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 48 additions & 31 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,19 @@ def __init__(self, port=50000):
self.port = port

self.room = 0
self.room_color = ["green", "brown", "yellow", "mauve"]

def connect(self):
self.socket = socket.socket(
socket.AF_INET,
socket.SOCK_STREAM,
socket.IPPROTO_TCP)

self.socket.setsockopt(
socket.SOL_SOCKET,
socket.SO_REUSEADDR,
1)

address = ('127.0.0.1', self.port)
self.socket.bind(address)
self.socket.listen(1)
Expand All @@ -78,10 +84,12 @@ def room_description(self, room_number):
:param room_number: int
:return: str
"""
if type(room_number) is int:
return f"{self.room_color[room_number].capitalize()} wallpaper covers the walls,\n" \
f"bathing the room in warm light reflected from the half-drawn curtains."
else:
return "Room number not found."

# TODO: YOUR CODE HERE

pass

def greet(self):
"""
Expand All @@ -92,11 +100,9 @@ def greet(self):

:return: None
"""
self.output_buffer = "Welcome to {}! {}".format(
self.game_name,
self.room_description(self.room)
)

self.output_buffer = f"Welcome to {self.game_name}! \n\n" \
f"{self.room_description(self.room)}"

def get_input(self):
"""
Retrieve input from the client_connection. All messages from the client
Expand All @@ -107,11 +113,12 @@ def get_input(self):

:return: None
"""
received = b''
while b'\n' not in received:
received+=self.client_connection.recv(16)

# TODO: YOUR CODE HERE

pass

self.input_buffer = received.decode().strip()

def move(self, argument):
"""
Moves the client from one room to another.
Expand All @@ -132,10 +139,20 @@ def move(self, argument):
:param argument: str
:return: None
"""

# TODO: YOUR CODE HERE

pass
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
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):
"""
Expand All @@ -150,10 +167,8 @@ def say(self, argument):
:param argument: str
:return: None
"""

# TODO: YOUR CODE HERE

pass
self.output_buffer = f'You say, "{argument}"'


def quit(self, argument):
"""
Expand All @@ -167,9 +182,8 @@ def quit(self, argument):
:return: None
"""

# TODO: YOUR CODE HERE

pass
self.done = True
self.output_buffer = "Goodbye!"

def route(self):
"""
Expand All @@ -182,10 +196,15 @@ def route(self):

:return: None
"""
received = self.input_buffer.split(" ")
command = received.pop(0)
arguments = " ".join(received)

# TODO: YOUR CODE HERE

pass
{
'quit': self.quit,
'move': self.move,
'say': self.say
}[command](arguments)

def push_output(self):
"""
Expand All @@ -196,10 +215,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()
Expand All @@ -210,6 +227,6 @@ def serve(self):
self.get_input()
self.route()
self.push_output()

self.client_connection.close()
self.socket.close()