 
 (def connections (atom #{}))
 
-
+(defn estream-handler [k r os ns]
+  (println k))
+  
 (defn ws-handler [req]
   (with-channel req channel
     (on-close channel (fn [status]

(defmethod transition :default [etype state & _]
  state)

(defn calc-state []
  (let [cstream @estream]
    (reduce 
      (fn [state [etype & eargs]]
        (let [transitioned (transition etype state eargs)
              checked-turn
              (if (and (:activerole state) (= (:actionturns state) (dec (:nplayers state))))
                (-> transitioned
                    (assoc :actionturns 0 :activerole nil :actionpicker nil)
                    (update-in [:rolepicker] next-player transitioned))
                transitioned)
              checked-fields
              (if (and (= :startgame etype) (empty? (:fieldtiles checked-turn)))
                (let [random-fields (common/randomize-fields checked-turn)]
                  (doseq [ftype random-fields]
                    (swap! estream conj [:fieldtile ftype] [:field :bank ftype -1]))
                  checked-turn)
                checked-turn)
              ]
          checked-fields))
      {:order []
       :nplayers 0
       :actionturns 0
       :activerole nil
       :fieldtiles []
       :bank {:vp 0
              :field-count 0
              :building nil}}
        cstream)))

(defn get-players [state]
  (into {}
        (remove (fn [[k v]] (keyword? k)) state)))

(defn create-game
  ([p1 p2 p3]

                        (println "channel closed")))
    (swap! connections conj channel)
    
    (add-watch estream :estream estream-handler)
    
    (send! channel (pr-str (calc-state @estream)))
    
    (on-receive channel (fn [data]
                          (let [parsed (edn/read-string data)
                                new-state (calc-state @estream)
                                cf (check-fields new-state)
                                new-state (calc-state @estream)]
                            (if (= :reset parsed)
                              (reset-game)
                              (swap! estream conj parsed))
