diff options
| author | troido <troido@hotmail.com> | 2018-04-14 18:13:19 +0200 |
|---|---|---|
| committer | troido <troido@hotmail.com> | 2018-04-14 18:13:19 +0200 |
| commit | aa5b7c9d753f6f140d7f55d0089e6e19028031b7 (patch) | |
| tree | 6573cbda9383c9f12a625f95bd48bbd20cfd3f22 /asciifarm/client/inputhandler.py | |
| parent | b15f89a3576b98f57ff1ec083e9299c890c1f7fc (diff) | |
Stopped using hy for keybindings
keybindings are now a JSON format.
Somehow hy errored for me, and the cleanest solution seemed not to use hy at all
Thinking about it, the client was basically loading a new interpreter to optimize something that didn't need optimizing.
It didn't make it more readable/writable either, and it always was a lot of hassle.
Diffstat (limited to 'asciifarm/client/inputhandler.py')
| -rw-r--r-- | asciifarm/client/inputhandler.py | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/asciifarm/client/inputhandler.py b/asciifarm/client/inputhandler.py new file mode 100644 index 0000000..79209bd --- /dev/null +++ b/asciifarm/client/inputhandler.py @@ -0,0 +1,92 @@ + +import shlex + +class InvalidCommandException(Exception): + pass + + +class InputHandler: + + def __init__(self, client, display, connection): + self.client = client + self.display = display + self.connection = connection + + self.commands = { + "send": self.send, + "input": self.input, + "move": self.move, + "say": self.say, + "chat": self.chat, + "log": self.log, + "do": self.do, + "runinput": self.runInput, + "select": self.select, + "inputwithselected": self.actWithSelected, + "eval": self.eval, + "exec": self.exec + } + + def execute(self, action): + if isinstance(action[0], str): + command = action[0] + if command in self.commands: + self.commands[command](*action[1:]) + else: + raise InvalidCommandException("Invalid command '{}'".format(command)) + else: + raise Exception("Command should be a string") + + + + def send(self, data): + self.client.send(data) + + def input(self, action): + self.send(["input", action]) + + def move(self, direction): + self.input(["move", direction]) + + def say(self, text): + self.input(["say", text]) + + def chat(self, text): + self.send(["chat", text]) + + def log(self, text): + self.client.log(text) + + def do(self, actions): + for action in actions: + self.execute(action) + + def runInput(self): + message = self.display.getString() + if message: + if message[0] == '/': + if message[1] == '/': + self.chat(message[1:]) + else: + try: + self.execute(shlex.split(message[1:])) + except InvalidCommandException as e: + self.log(", ".join(e.args)) + else: + self.chat(message) + + def select(self, widget, value, relative=False, modular=False): + self.display.getWidget(widget).select(value, relative, modular) + + def actWithSelected(self, action, widget): + self.input([action, self.display.getWidget(widget).getSelected()]) + + def eval(self, *texts): + text = " ".join(texts) + self.log(eval(text, {"self": self, "client": self.client, "connection": self.connection, "display": self.display})) + + def exec(self, *texts): + text = " ".join(texts) + exec(text, {"self": self, "client": self.client, "connection": self.connection, "display": self.display}) + + |
