    [zetawar.db :refer [e find-by qe qes qess]]
    [zetawar.game :as game]
    [zetawar.players :as players]
-   [zetawar.util :refer [breakpoint inspect]]))
+   [zetawar.util :as util :refer [breakpoint inspect]]))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;; DB Accessors
 
 (defn root [db]
   (qe '[:find ?a

        [_ :app/game ?g]]
      db))

(defn create-players! [{:as app-ctx :keys [conn players]}]
  (let [factions (qess '[:find ?f
                         :where
                         [_  :app/game ?g]
                         [?g :game/factions ?f]]
                       @conn)]
    (doseq [{:keys [faction/ai faction/color]} factions]
      (let [player-type (if ai ::players/reference-ai ::players/human)
            player (players/new-player app-ctx player-type color)]
        (players/start player)
        (swap! players assoc color player)))))

(defn start-new-game! [{:as app-ctx :keys [conn players]} scenario-id]
  (let [game (current-game @conn)]
    (when-not game
      (game/load-specs! conn))
    (let [scenario-def (data/scenario-definitions scenario-id)
          game-id (game/load-scenario! conn data/map-definitions scenario-def)
          app-eid (or (some-> (root @conn) e) -101)]
      (d/transact! conn (cond-> [{:db/id app-eid
                                  :app/game [:game/id game-id]}]
                          game (conj [:db.fn/retractEntity (e game)])))
      (if players
        (create-players! app-ctx)
        (log/warnf "Skipping player creation for tests")))))

(defn encode-game-state [game-state]
  (let [writer (transit/writer :json)]

                                (game/get-game-state db)
                                encode-game-state)]
    (set! js/window.location.hash encoded-game-state)))
