src/clojurecraft/events.clj @ 43f02bd6b197

Make handlers return lists.
author Steve Losh <steve@stevelosh.com>
date Wed, 10 Aug 2011 01:25:24 -0400
parents 53bc495283f5
children dfb084418ef9
(ns clojurecraft.events)

(defn add-handler [bot event-type handler]
  (dosync
    (let [current-handlers (event-type @(:event-handlers bot))
          updated-handlers (conj current-handlers handler)]
      (alter (:event-handlers bot) assoc event-type updated-handlers))))

(defn clear-handlers [bot event-type]
  (dosync (alter (:event-handlers bot) dissoc event-type)))


(defn- fire-handler [bot event-type & args]
  (let [action-groups (map #(apply (eval %) (into [bot] args))
                           (event-type @(:event-handlers bot)))
        queue-action #(.put (:actionqueue bot) %)
        queue-action-group #(map queue-action %)]
    (println action-groups)
    (dorun (map queue-action-group action-groups))))

(defn fire-chat [bot message]
  (fire-handler bot :chat message))