98dc76fd1032

Add basic looping methods.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Tue, 23 Aug 2011 20:51:39 -0400 (2011-08-24)
parents d8fa70c91585
children 1e03d5cc372b
branches/tags (none)
files src/clojurecraft/events.clj

Changes

--- a/src/clojurecraft/events.clj	Thu Aug 18 23:15:13 2011 -0400
+++ b/src/clojurecraft/events.clj	Tue Aug 23 20:51:39 2011 -0400
@@ -10,12 +10,16 @@
   (dosync (alter (:event-handlers bot) dissoc event-type)))
 
 
+(defn- handle-action-group [bot action-group]
+  (println action-group)
+  (let [queue-action #(.put (:actionqueue bot) %)
+        queue-action-group #(dorun (map queue-action %))]
+    (queue-action-group action-group)))
+
 (defn- fire-handler [bot event-type & args]
   (let [action-groups (map #(apply (deref %) (into [bot] args))
-                           (event-type @(:event-handlers bot)))
-        queue-action #(.put (:actionqueue bot) %)
-        queue-action-group #(dorun (map queue-action %))]
-    (dorun (map queue-action-group action-groups))))
+                           (event-type @(:event-handlers bot)))]
+    (dorun (map handle-action-group (cycle [bot]) action-groups))))
 
 
 (defn fire-chat [bot message]
@@ -24,3 +28,11 @@
 (defn fire-dead [bot]
   (fire-handler bot :dead))
 
+
+(defn- run-loop [bot function sleep-ms]
+  (while (nil? (:exit @(:connection bot)))
+    (handle-action-group bot (function bot))
+    (Thread/sleep sleep-ms)))
+
+(defn add-loop [bot function sleep-ms]
+  (.start (Thread. #(run-loop bot function sleep-ms))))