Skip to content

PA3 #2

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

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
104 changes: 66 additions & 38 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
class Server(object):
"""
An adventure game socket server

An instance's methods share the following variables:

* self.socket: a "bound" server socket, as produced by socket.bind()
* self.client_connection: a "connection" socket as produced by socket.accept()
* self.input_buffer: a string that has been read from the connected client and
Expand All @@ -17,14 +17,14 @@ class Server(object):
* self.done: A boolean, False until the client is ready to disconnect
* self.room: one of 0, 1, 2, 3. This signifies which "room" the client is in,
according to the following map:

3 N
| ^
1 - 0 - 2 |

When a client connects, they are greeted with a welcome message. And then they can
move through the connected rooms. For example, on connection:

OK! Welcome to Realms of Venture! This room has brown wall paper! (S)
move north (C)
OK! This room has white wallpaper. (S)
Expand All @@ -36,7 +36,7 @@ class Server(object):
OK! This room has a green floor! (S)
quit (C)
OK! Goodbye! (S)

Note that we've annotated server and client messages with *(S)* and *(C)*, but
these won't actually appear in server/client communication. Also, you'll be
free to develop any room descriptions you like: the only requirement is that
Expand Down Expand Up @@ -79,18 +79,24 @@ def room_description(self, room_number):
:return: str
"""

# TODO: YOUR CODE HERE

pass
room_descriptions = {0:"You are in the room with the white wallpaper.",
1:"You are in the room with the green wallpaper.",
2:"You are in the room with the brown wallpaper.",
3:"You are in the room with the mauve wallpaper."}
try:
return room_descriptions[room_number]
except KeyError:
self.room = 0
return "You dont know where you are and you pass out"

def greet(self):
"""
Welcome a client to the game.

Puts a welcome message and the description of the client's current room into
the output buffer.
:return: None

:return: None
"""
self.output_buffer = "Welcome to {}! {}".format(
self.game_name,
Expand All @@ -101,103 +107,125 @@ def get_input(self):
"""
Retrieve input from the client_connection. All messages from the client
should end in a newline character: '\n'.

This is a BLOCKING call. It should not return until there is some input from
the client to receive.
:return: None

:return: None
"""

# TODO: YOUR CODE HERE
client_input = b""
while b'\n' not in client_input:
client_input += self.client_connection.recv(16)

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

pass

def move(self, argument):
"""
Moves the client from one room to another.

Examines the argument, which should be one of:

* "north"
* "south"
* "east"
* "west"

"Moves" the client into a new room by adjusting self.room to reflect the
number of the room that the client has moved into.

Puts the room description (see `self.room_description`) for the new room
into "self.output_buffer".

:param argument: str
:return: None
"""

# TODO: YOUR CODE HERE
room_movement = {0:{"north": 3, "east": 2, "west": 1},
1:{"east": 0},
2:{"west": 0},
3:{"south": 0}}

if argument in room_movement[self.room]:
self.room = room_movement[self.room][argument]
self.output_buffer = self.room_description(self.room)
else:
self.output_buffer = "You can't go that way\n"
self.output_buffer += self.room_description(self.room)

pass

def say(self, argument):
"""
Lets the client speak by putting their utterance into the output buffer.

For example:
`self.say("Is there anybody here?")`
would put
`You say, "Is there anybody here?"`
into the output buffer.

:param argument: str
:return: None
"""

# TODO: YOUR CODE HERE
self.output_buffer = f"You say, {argument}."


pass

def quit(self, argument):
"""
Quits the client from the server.

Turns `self.done` to True and puts "Goodbye!" onto the output buffer.

Ignore the argument.

:param argument: str
:return: None
"""

# TODO: YOUR CODE HERE
self.done = True
self.output_buffer = "Goodbye!"

pass

def route(self):
"""
Examines `self.input_buffer` to perform the correct action (move, quit, or
say) on behalf of the client.

For example, if the input buffer contains "say Is anybody here?" then `route`
should invoke `self.say("Is anybody here?")`. If the input buffer contains
"move north", then `route` should invoke `self.move("north")`.

:return: None
"""

# TODO: YOUR CODE HERE
received = self.input_buffer.split(" ")

pass
command = received.pop(0)
arguments = " ".join(received)
try:
{'quit': self.quit,
'move': self.move,
'say': self.say,}[command](arguments)
except KeyError:
self.output_buffer= "I don't know what that means\n"

def push_output(self):
"""
Sends the contents of the output buffer to the client.

This method should prepend "OK! " to the output and append "\n" before
sending it.
:return: None

:return: None
"""

# TODO: YOUR CODE HERE
self.client_connection.sendall(b"OK! " + self.output_buffer.encode() + b"\n")

pass

Expand Down