summaryrefslogtreecommitdiff
path: root/asciifarm/client/commandhandler.py
blob: 9a0ff4435d0b18df234c2e3afa129cdd3cedb57e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

import shlex
import json

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,
            "json": self.json,
            "ijson": self.ijson
            }
    
    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, startText=""):
        self.client.inputHandler.startTyping(startText)
    
    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)
    
    def json(self, text):
        self.send(json.loads(text))
    
    def ijson(self, text):
        self.input(json.loads(text))