 
 (defn build [conn game-id]
   (let [db @conn
-        game (qe '[:find ?g
-                   :in $ ?game-id
-                   :where
-                   [?g :game/id ?game-id]]
-                 db game-id)
+        game (game/game-by-id db game-id)
         current-faction (:game/current-faction game)
         owned-bases (->> (qes '[:find ?t
                                 :in $ ?g

(ns zetawar.ai
  (:require
    [cljs.pprint :refer [pprint]]
    [datascript.core :as d]
    [zetawar.db :refer [e qe qes]]
    [zetawar.game :as game]))

;; for each unit:
;; - repair if < 50% health (optional)
;; - get moves
;; - pick move that gets closest to a capturable base
;;   - only move units that can capture onto bases
;; - capture if on base
;; - get attacks
;; - attack a random enemy unit if possible

;; for each owned base:
;; - build a random unit

(defn move [conn game-id unit]
  (when-not (:unit/capturing unit)
    (let [db @conn
          game (qe '[:find ?g
                     :in $ ?game-id
                     :where
                     [?g :game/id ?game-id]]
                   db game-id)
          base (game/closest-capturable-base db game unit)
          move (game/closest-move-to-qr db game unit (:terrain/q base) (:terrain/r base))]
      (if (game/on-capturable-base? db game unit)

(defn attack [conn game-id unit]
  (when-not (:unit/capturing unit)
    (let [db @conn
          game (game/game-by-id db game-id)
          enemy (-> (game/enemies-in-range db game unit) shuffle first)]
      (when (and enemy (game/can-attack? db game unit))
        (game/attack! conn game-id
