From aa5b7c9d753f6f140d7f55d0089e6e19028031b7 Mon Sep 17 00:00:00 2001 From: troido Date: Sat, 14 Apr 2018 18:13:19 +0200 Subject: 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. --- asciifarm/client/gameclient.py | 22 ++++------ asciifarm/client/inputhandler.py | 92 +++++++++++++++++++++++++++++++++++++++ asciifarm/client/inputhandling.hy | 48 -------------------- asciifarm/client/keymacros.hy | 25 ----------- asciifarm/client/main.py | 7 +-- 5 files changed, 105 insertions(+), 89 deletions(-) create mode 100644 asciifarm/client/inputhandler.py delete mode 100644 asciifarm/client/inputhandling.hy delete mode 100644 asciifarm/client/keymacros.hy (limited to 'asciifarm/client') diff --git a/asciifarm/client/gameclient.py b/asciifarm/client/gameclient.py index 9903533..69380db 100644 --- a/asciifarm/client/gameclient.py +++ b/asciifarm/client/gameclient.py @@ -12,8 +12,8 @@ from .display.screen import Screen import string from .display.display import Display -import hy -from .inputhandling import InputHandler +from .inputhandler import InputHandler +from .keynames import nameFromKey class Client: @@ -26,9 +26,9 @@ class Client: self.logFile = logFile self.inputHandler = InputHandler(self, self.display, self.connection) - self.inputHandler.readCommands(keybindings) + self.keybindings = keybindings["actions"] - self.controlsString = self.inputHandler.getDocs() + self.controlsString = keybindings.get("help", "") self.display.showInfo(self.controlsString) @@ -106,26 +106,22 @@ class Client: self.display.update() def log(self, text): + if not isinstance(text, str): + text = str(text) self.display.addMessage(text) if self.logFile: with(open(self.logFile, 'a')) as f: f.write(text+'\n') - def nameFromKey(self, keynum): # this probably belongs in inputhandler... - prenamed = { - 10: "KEY_ENTER" - } - if keynum in prenamed: - return prenamed[keynum] - return str(curses.keyname(keynum), "utf-8") - def command_loop(self): while self.keepalive: key = self.stdscr.getch() if key == 27: self.keepalive = False return - self.inputHandler.onKey(key) + keyName = nameFromKey(key) + if keyName in self.keybindings: + self.inputHandler.execute(self.keybindings[keyName]) 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}) + + diff --git a/asciifarm/client/inputhandling.hy b/asciifarm/client/inputhandling.hy deleted file mode 100644 index 9c3e254..0000000 --- a/asciifarm/client/inputhandling.hy +++ /dev/null @@ -1,48 +0,0 @@ - -(require [asciifarm.client.keymacros [*]]) -(import [asciifarm.client.keynames [nameFromKey]]) - -(defclass InputHandler [] - - (defn --init-- [self client display connection] - (setv self.client client) - (setv self.display display) - (setv self.connection connection) - (setv self.commands None)) - - (defn readCommands [self commandsstring] (do - (setv self.commands - (dict-comp - (str key) - ( - (eval `(do - (require [asciifarm.client.keymacros [*]]) - (fn [handler] - (fn [] ~value)))) - self) - [[key value] (.items (read-str commandsstring))])) - ((.get self.commands "init" (fn []))))) - - (defn runCommand [self commandstring] - (try - (eval (read-str (+ "(" commandstring ")"))) - (except [e Exception] - (self.client.log (repr e))))) - - (defn parseMessage [self message] - (if message - (if (= (first message) "/") - (do - (setv msg (.join "" (drop 1 message))) - (if (= (first msg) "/") - (send ["chat" msg]) - (self.runCommand msg))) - (send ["chat" message])))) - - (defn getDocs [self] - (if (in "help" self.commands) ((get self.commands "help")) "")) - - (defn onKey [self key] (do - (setv keyname (nameFromKey key)) - (if (in keyname self.commands) ((get self.commands keyname))))) -) diff --git a/asciifarm/client/keymacros.hy b/asciifarm/client/keymacros.hy deleted file mode 100644 index 2de89e4..0000000 --- a/asciifarm/client/keymacros.hy +++ /dev/null @@ -1,25 +0,0 @@ - - -(defmacro send [data] - `(self.client.send ~data)) - -(defmacro inp [action] - `(send ["input" ~action])) - -(defmacro move [dir] - `(inp ["move" ~dir])) - -(defmacro say [text] - `(inp ["say" ~text])) - -(defmacro log [text] - `(self.client.log ~text)) - -(defmacro chat [text] - `(send ["chat" ~text])) - -(defmacro doall [actions] - `(for [action ~actions] (action))) - -(defmacro log [text] - `(self.client.log ~text)) diff --git a/asciifarm/client/main.py b/asciifarm/client/main.py index 1a4ac51..ece3c5a 100755 --- a/asciifarm/client/main.py +++ b/asciifarm/client/main.py @@ -15,7 +15,7 @@ charMapPath = os.path.join(farmsPath, "charmaps") keybindingsPath = os.path.join(farmsPath, "keybindings") standardCharFiles = [name[:-5] for name in os.listdir(charMapPath) if name[-5:] == ".json"] -standardKeyFiles = [name[:-3] for name in os.listdir(keybindingsPath) if name[-3:] == ".hy"] +standardKeyFiles = [name[:-5] for name in os.listdir(keybindingsPath) if name[-5:] == ".json"] defaultAdresses = { @@ -54,9 +54,10 @@ def main(argv=None): keyFile = args.keybindings if keyFile in standardKeyFiles: - keyFile = os.path.join(keybindingsPath, keyFile + ".hy") + keyFile = os.path.join(keybindingsPath, keyFile + ".json") with open(keyFile, 'r') as kf: - keybindings = kf.read() + # todo: support yaml + keybindings = json.load(kf) address = args.address if address is None: -- cgit