# HG changeset patch # User Steve Losh # Date 1314147099 14400 # Node ID 98dc76fd10322b34a03946d7df6e9de5ae899441 # Parent d8fa70c915850c483f70bded6abd06a7426d4131 Add basic looping methods. diff -r d8fa70c91585 -r 98dc76fd1032 src/clojurecraft/events.clj --- 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))))