diff options
| author | troido <troido@hotmail.com> | 2017-10-28 12:31:42 +0200 |
|---|---|---|
| committer | troido <troido@hotmail.com> | 2017-10-28 12:31:42 +0200 |
| commit | a8fd6fc77a3c5af06727a68684e61c63553679a8 (patch) | |
| tree | cde31f96a13907b3b5889d630667084e8dfdbcec /asciifarm/client/gameclient.py | |
| parent | 4fe76206a7b183eaf46268e9dc03916de093f3b9 (diff) | |
better use of __main__ and __init__
Diffstat (limited to 'asciifarm/client/gameclient.py')
| -rw-r--r-- | asciifarm/client/gameclient.py | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/asciifarm/client/gameclient.py b/asciifarm/client/gameclient.py new file mode 100644 index 0000000..d6e2595 --- /dev/null +++ b/asciifarm/client/gameclient.py @@ -0,0 +1,110 @@ +#! /usr/bin/python3 + +import os +import sys + +import curses +import threading +#import logging +import json +import getpass +import argparse +from .display.screen import Screen +import string +from .display import Display + +#logging.basicConfig(filename="client.log", filemode='w', level=logging.DEBUG) + +class Client: + + def __init__(self, stdscr, display, name, connection, keybindings): + self.stdscr = stdscr + self.display = display + self.name = name + self.keepalive = True + self.connection = connection + + self.commands = {} + for key, commands in keybindings["input"].items(): + if isinstance(commands[0], str): + commands = [commands] + self.commands[ord(key)] = [["input", command] for command in commands] + + self.controlsString = "Controls:\n"+'\n'.join( + chr(key) + ": " + ', '.join(' '.join(action[1]) for action in actions) + for key, actions in self.commands.items() + if chr(key) in string.printable) + + self.display.showInfo(self.controlsString) + + + + def start(self): + threading.Thread(target=self.listen, daemon=True).start() + self.connection.send(json.dumps(["name", self.name])) + self.command_loop() + + + def listen(self): + self.connection.listen(self.update, self.close) + + def close(self, err=None): + self.keepalive = False + sys.exit() + + + def update(self, databytes): + if not self.keepalive: + sys.exit() + datastr = databytes.decode('utf-8') + data = json.loads(datastr) + if len(data) and isinstance(data[0], str): + data = [data] + for msg in data: + msgType = msg[0] + if msgType == 'error': + error = msg[1] + if error == "nametaken": + print("error: name is already taken", file=sys.stderr) + self.close() + 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 = msg[1] + if health: + self.display.setHealth(*health) + if msgType == "inventory": + self.display.setInventory(msg[1]) + if msgType == "ground": + self.display.setGround(msg[1]) + + + self.display.update() + + def command_loop(self): + while self.keepalive: + key = self.stdscr.getch() + if key == 27: + self.keepalive = False + if key in self.commands: + self.connection.send(json.dumps(self.commands[key])) + + + + |
