diff --git a/.gitignore b/.gitignore index 3336d87..53529e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,130 @@ -# USER SPECIFIC/IDE FILES -.idea/ - -# GENERAL PYTHON FILES +# Byte-compiled / optimized / DLL files +**/.idea/* __pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ diff --git a/server.py b/server.py index d0d46c4..e3702e8 100644 --- a/server.py +++ b/server.py @@ -45,6 +45,7 @@ class Server(object): game_name = "Realms of Venture" + def __init__(self, port=50000): self.input_buffer = "" self.output_buffer = "" @@ -55,6 +56,7 @@ def __init__(self, port=50000): self.room = 0 + def connect(self): self.socket = socket.socket( socket.AF_INET, @@ -67,6 +69,7 @@ def connect(self): self.client_connection, address = self.socket.accept() + def room_description(self, room_number): """ For any room_number in 0, 1, 2, 3, return a string that "describes" that @@ -79,9 +82,15 @@ def room_description(self, room_number): :return: str """ - # TODO: YOUR CODE HERE + room = { + 0: "white", + 1: "green", + 2: "brown", + 3: "mauve" + } + + return f"You are in the room with the {room[room_number]} wallpaper." - pass def greet(self): """ @@ -97,6 +106,7 @@ def greet(self): self.room_description(self.room) ) + def get_input(self): """ Retrieve input from the client_connection. All messages from the client @@ -108,9 +118,12 @@ def get_input(self): :return: None """ - # TODO: YOUR CODE HERE + received = b'' + while b'\n' not in received: + received += self.client_connection.read(16) + + self.input_buffer = received.decode() - pass def move(self, argument): """ @@ -132,10 +145,26 @@ def move(self, argument): :param argument: str :return: None """ + 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 - # TODO: YOUR CODE HERE + 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) - pass def say(self, argument): """ @@ -151,9 +180,8 @@ def say(self, argument): :return: None """ - # TODO: YOUR CODE HERE + return f"You say, \"{argument}\"" - pass def quit(self, argument): """ @@ -167,9 +195,9 @@ def quit(self, argument): :return: None """ - # TODO: YOUR CODE HERE + self.done = True + self.output_buffer = "Goodbye!" - pass def route(self): """ @@ -183,9 +211,24 @@ def route(self): :return: None """ - # TODO: YOUR CODE HERE + received = self.input_buffer.split(" ") + + command = received.pop(0) + arguments = " ".join(received) + + # If `self.input_buffer` was "say Is anybody here?", then: + # `command` should now be "say" and `arguments` should now be "Is anybody here?". + # + # If `self.input_buffer` was "move north", then: + # `command` should now be "move" and `arguments` should now be "north". + + { + 'quit': self.quit, + 'move': self.move, + 'say': self.say, + }[command](arguments) + - pass def push_output(self): """ @@ -197,9 +240,8 @@ def push_output(self): :return: None """ - # TODO: YOUR CODE HERE + self.client_connection.sendall(b"OK! " + self.output_buffer.encode() + b"\n") - pass def serve(self): self.connect()