summaryrefslogtreecommitdiff
path: root/asciifarm/client/inputhandling.hy
blob: 8d19e4ac86df10ebc6a0e5121e7b133382f13298 (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

(require [asciifarm.client.keymacros [*]])

(defmacro eval-in-context [code] 
    `(
        (eval `(do
            (require [asciifarm.client.keymacros [*]])
            (fn [client display connection]
                ~~code)))
        self.client
        self.display
        self.connection))

(defmacro sendinput [message] `(
    self.client.send ["input" ~message]))

(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]
        (setv self.commands
            (dict-comp 
                (eval key)
                (
                    (eval `(do
                        (require [asciifarm.client.keymacros [*]])
                        (fn [handler]
                            (fn [] ~value))))
                    self)
                [[key value] (.items (read-str commandsstring))])))
    
    (defn runCommand [self commandstring]
          (eval (read-str (+ "(" commandstring ")"))))
    
    (defn parseMessage [self message]
        (if message
            (if (= (first message) "/")
                (do
                    (setv msg (.join "" (drop 1 message)))
                    (if (= (first msg) "/")
                        (inp ["say" msg])
                        (self.runCommand msg)))
                (inp ["say" message]))))
    
    (defn getDocs [self]
          (if (in "help" self.commands) ((get self.commands "help")) ""))
    
    (defn onKey [self key]
          (if (in key self.commands) ((get self.commands key))))
)