summaryrefslogtreecommitdiff
path: root/asciifarm/client/commandhandler.py
diff options
context:
space:
mode:
authortroido <troido@hotmail.com>2018-04-18 23:27:34 +0200
committertroido <troido@hotmail.com>2018-04-18 23:27:34 +0200
commit757ad925b47d24b1d0714671b8f04147a5748480 (patch)
treebc67553d76ac3301db7d2c20881448afc411221d /asciifarm/client/commandhandler.py
parentc0eb8ed32d53030de5b75b49a8e0f64e7c4a05ff (diff)
improved input handling
renamed InputHandler to CommandHandler. made InputHandler for input code that used to be in gameclient also started my own getstr() replacement.
Diffstat (limited to 'asciifarm/client/commandhandler.py')
-rw-r--r--asciifarm/client/commandhandler.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/asciifarm/client/commandhandler.py b/asciifarm/client/commandhandler.py
new file mode 100644
index 0000000..7eefe5b
--- /dev/null
+++ b/asciifarm/client/commandhandler.py
@@ -0,0 +1,84 @@
+
+import shlex
+
+class InvalidCommandException(Exception):
+ pass
+
+
+class CommandHandler:
+
+ def __init__(self, client):
+ self.client = client
+
+ 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,
+ "scrollchat": self.scrollChat
+ }
+
+ 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")
+
+
+ # Commands
+
+ 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):
+ self.client.inputHandler.typing = True
+
+ def select(self, widget, value, relative=False, modular=False):
+ self.client.display.getWidget(widget).select(value, relative, modular)
+
+ def actWithSelected(self, action, widget):
+ self.input([action, self.client.display.getWidget(widget).getSelected()])
+
+ def eval(self, *texts):
+ text = " ".join(texts)
+ self.log(eval(text, {"self": self, "client": self.client, "connection": self.client.connection, "display": self.client.display}))
+
+ def exec(self, *texts):
+ text = " ".join(texts)
+ exec(text, {"self": self, "client": self.client, "connection": self.client.connection, "display": self.client.display})
+
+ def scrollChat(self, lines):
+ self.client.display.scrollBack(lines)
+
+