summaryrefslogtreecommitdiff
path: root/asciifarm/client
diff options
context:
space:
mode:
Diffstat (limited to 'asciifarm/client')
-rw-r--r--asciifarm/client/gameclient.py22
-rw-r--r--asciifarm/client/inputhandler.py92
-rw-r--r--asciifarm/client/inputhandling.hy48
-rw-r--r--asciifarm/client/keymacros.hy25
-rwxr-xr-xasciifarm/client/main.py7
5 files changed, 105 insertions, 89 deletions
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: