diff options
Diffstat (limited to 'asciifarm/client')
| -rw-r--r-- | asciifarm/client/commandhandler.py | 8 | ||||
| -rw-r--r-- | asciifarm/client/gameclient.py | 99 | ||||
| -rw-r--r-- | asciifarm/client/parseargs.py | 2 |
3 files changed, 60 insertions, 49 deletions
diff --git a/asciifarm/client/commandhandler.py b/asciifarm/client/commandhandler.py index f2e94d1..0c3027f 100644 --- a/asciifarm/client/commandhandler.py +++ b/asciifarm/client/commandhandler.py @@ -17,7 +17,6 @@ class CommandHandler: self.client = client self.commands = { - "send": self.send, "input": self.input, "move": self.move, "say": self.say, @@ -65,11 +64,8 @@ class CommandHandler: # Commands - def send(self, data): - self.client.send(data) - def input(self, action): - self.send(["input", action]) + self.client.sendInput(action) def move(self, direction): self.input(["move", direction]) @@ -81,7 +77,7 @@ class CommandHandler: self.input(["pick", option]) def chat(self, text): - self.send(["chat", text]) + self.client.sendChat( text) def log(self, text): diff --git a/asciifarm/client/gameclient.py b/asciifarm/client/gameclient.py index 3cd2050..57503db 100644 --- a/asciifarm/client/gameclient.py +++ b/asciifarm/client/gameclient.py @@ -13,6 +13,7 @@ from queue import Queue import ratuil.inputs from .inputhandler import InputHandler +from asciifarm.common import messages class Client: @@ -33,15 +34,24 @@ class Client: self.queue = Queue() - def send(self, data): - text = json.dumps(data) - self.connection.send(text) + def sendMessage(self, message): + self.connection.send(json.dumps(message.to_json())) + + def sendInput(self, inp): + message = messages.InputMessage(inp) + self.sendMessage(message) + + def sendChat(self, text): + try: + self.sendMessage(messages.ChatMessage(text)) + except messages.InvalidMessageError as e: + self.log(e.description) def start(self): + self.sendMessage(messages.NameMessage(self.name)) threading.Thread(target=self.listen, daemon=True).start() threading.Thread(target=self.getInput, daemon=True).start() - self.connection.send(json.dumps(["name", self.name])) self.command_loop() def listen(self): @@ -79,48 +89,53 @@ class Client: self.close("error: name is already taken") return if error == "invalidname": - self.close("Invalid name error: "+ msg[2:]) + self.close("Invalid name error: "+ str(msg[2:])) return self.log(" ".join(msg[1:])) - if msgType == 'field': - field = msg[1] - fieldWidth = field['width'] - fieldHeight = field['height'] - self.display.resizeField((fieldWidth, fieldHeight)) - fieldCells = field['field'] - mapping = field['mapping'] - self.display.drawFieldCells( - ( - tuple(reversed(divmod(i, fieldWidth))), - mapping[spr] - ) - for i, spr in enumerate(fieldCells)) - - if msgType == 'changecells' and len(msg[1]): - self.display.drawFieldCells(msg[1]) - - if msgType == "playerpos": - self.display.setFieldCenter(msg[1]) - - if msgType == "health": - health, maxHealth = msg[1] - self.display.setHealth(health, maxHealth) - if maxHealth is None: - self.log("You have died. Restart the client to respawn") - if msgType == "inventory": - self.display.setInventory(msg[1]) - if msgType == "equipment": - self.display.setEquipment(msg[1]) - if msgType == "ground": - self.display.setGround(msg[1]) if msgType == "message": self.log(*msg[1:]) - if msgType == "options": - if msg[1] != None: - description, options = msg[1] - self.log(description) - for option in options: - self.log(option) + if msgType == "world": + for msg in msg[1]: + msgType = msg[0] + if msgType == 'field': + field = msg[1] + fieldWidth = field['width'] + fieldHeight = field['height'] + self.display.resizeField((fieldWidth, fieldHeight)) + fieldCells = field['field'] + mapping = field['mapping'] + self.display.drawFieldCells( + ( + tuple(reversed(divmod(i, fieldWidth))), + mapping[spr] + ) + for i, spr in enumerate(fieldCells)) + + if msgType == 'changecells' and len(msg[1]): + self.display.drawFieldCells(msg[1]) + + if msgType == "playerpos": + self.display.setFieldCenter(msg[1]) + + if msgType == "health": + health, maxHealth = msg[1] + self.display.setHealth(health, maxHealth) + if maxHealth is None: + self.log("You have died. Restart the client to respawn") + if msgType == "inventory": + self.display.setInventory(msg[1]) + if msgType == "equipment": + self.display.setEquipment(msg[1]) + if msgType == "ground": + self.display.setGround(msg[1]) + if msgType == "message": + self.log(*msg[1:]) + if msgType == "options": + if msg[1] != None: + description, options = msg[1] + self.log(description) + for option in options: + self.log(option) def log(self, text, type=None): diff --git a/asciifarm/client/parseargs.py b/asciifarm/client/parseargs.py index 9a73adf..a8b393b 100644 --- a/asciifarm/client/parseargs.py +++ b/asciifarm/client/parseargs.py @@ -23,7 +23,7 @@ def parse_args(argv): Kill the goblins and plant the seeds. ~troido""", formatter_class=argparse.RawDescriptionHelpFormatter) - parser.add_argument('-n', '--name', help='Your player name (must be unique!). Defaults to username on inet sockets and tildename on (unix socket (including abstract)', default=None) + parser.add_argument('-n', '--name', help='Your player name (must be unique!). Defaults to username on inet sockets and tildename on unix socket (including abstract). Apart from the tilde in a tildename all characters must be unicode letters, numbers or connection puctuation. The maximum size of a name is 256 bytes when encoded as utf8', default=None) parser.add_argument("-a", "--address", help="The address of the socket. When the socket type is 'abstract' this is just a name. When it is 'unix' this is a filename. When it is 'inet' is should be in the format 'address:port', eg 'localhost:8080'. Defaults depends on the socket type") parser.add_argument("-s", "--socket", help="the socket type. 'unix' is unix domain sockets, 'abstract' is abstract unix domain sockets and 'inet' is inet sockets. ", choices=["abstract", "unix", "inet"], default="abstract") parser.add_argument('-k', '--keybindings', help='The file with the keybinding configuration. This file is a JSON file.', default="default") |
