Skip to content

Commit

Permalink
Replacing *-qr with *-hex - closes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
djwhitt committed Sep 29, 2016
1 parent cf2979d commit fc13c52
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/clj/zetawar/ai.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
(let [db @conn
game (game/game-by-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))]
move (game/closest-move-to-hex db game unit (:terrain/q base) (:terrain/r base))]
(if (game/on-capturable-base? db game unit)
(game/capture! conn game-id (:unit/q unit) (:unit/r unit))
(when (first move) ; hack to deal with [nil nil]
Expand Down
22 changes: 11 additions & 11 deletions src/clj/zetawar/app.cljs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
(ns zetawar.app
(:require
[cognitect.transit :as transit]
[datascript.core :as d]
[goog.crypt.base64 :as base64]
[lzw]
[zetawar.db :refer [e find-by qe qes]]
[zetawar.data :as data]
[zetawar.game :as game]
[zetawar.hex :as hex]
[zetawar.util :refer [oonly spy]]))
[cognitect.transit :as transit]
[datascript.core :as d]
[goog.crypt.base64 :as base64]
[lzw]
[zetawar.db :refer [e find-by qe qes]]
[zetawar.data :as data]
[zetawar.game :as game]
[zetawar.hex :as hex]
[zetawar.util :refer [oonly spy]]))

;; TODO: rename to root
(defn app [db]
Expand Down Expand Up @@ -76,14 +76,14 @@
(defn current-game-id [db]
(:game/id (current-game db)))

(defn selected-qr [db]
(defn selected-hex [db]
(first (d/q '[:find ?q ?r
:where
[?a :app/selected-q ?q]
[?a :app/selected-r ?r]]
db)))

(defn targeted-qr [db]
(defn targeted-hex [db]
(first (d/q '[:find ?q ?r
:where
[?a :app/targeted-q ?q]
Expand Down
2 changes: 1 addition & 1 deletion src/clj/zetawar/events/game.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

(defmethod router/handle-event ::repair-unit
[{:as handler-ctx :keys [db]} [_ q r]]
(let [[q r] (app/selected-qr db)]
(let [[q r] (app/selected-hex db)]
{:tx (game/repair-tx db (app/current-game db) q r)}))

(defmethod router/handle-event ::capture-base
Expand Down
22 changes: 11 additions & 11 deletions src/clj/zetawar/events/ui.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
[{:as handler-ctx :keys [db]} [_ ev-q ev-r]]
(let [app (app/root db)
game (app/current-game db)
[selected-q selected-r] (app/selected-qr db)
[targeted-q targeted-r] (app/targeted-qr db)
[selected-q selected-r] (app/selected-hex db)
[targeted-q targeted-r] (app/targeted-hex db)
unit (game/unit-at db game ev-q ev-r)
terrain (game/terrain-at db game ev-q ev-r)
selected-unit (game/unit-at db game selected-q selected-r)
Expand Down Expand Up @@ -121,8 +121,8 @@
(defmethod router/handle-event ::clear-selection
[{:as handler-ctx :keys [db]} _]
(let [app (app/root db)
[selected-q selected-r] (app/selected-qr db)
[targeted-q targeted-r] (app/targeted-qr db)]
[selected-q selected-r] (app/selected-hex db)
[targeted-q targeted-r] (app/targeted-hex db)]
{:tx (cond-> []
(and selected-q selected-r)
(->
Expand All @@ -142,8 +142,8 @@

(defmethod router/handle-event ::move-selected-unit
[{:as handler-ctx :keys [db]} _]
(let [[from-q from-r] (app/selected-qr db)
[to-q to-r] (app/targeted-qr db)]
(let [[from-q from-r] (app/selected-hex db)
[to-q to-r] (app/targeted-hex db)]
{:dispatch [[::e.game/move-unit from-q from-r to-q to-r]
[::move-selection from-q from-r to-q to-r]]}))

Expand All @@ -164,28 +164,28 @@

(defmethod router/handle-event ::attack-targeted
[{:as handler-ctx :keys [db]} _]
(let [[attacker-q attacker-r] (app/selected-qr db)
[target-q target-r] (app/targeted-qr db)]
(let [[attacker-q attacker-r] (app/selected-hex db)
[target-q target-r] (app/targeted-hex db)]
{:dispatch [[::e.game/attack-unit attacker-q attacker-r target-q target-r]
[::clear-selection]
[::alert-if-win]]}))

(defmethod router/handle-event ::repair-selected
[{:as handler-ctx :keys [db]} _]
(let [[q r] (app/selected-qr db)]
(let [[q r] (app/selected-hex db)]
{:dispatch [[::e.game/repair-unit q r]
[::clear-selection]]}))

(defmethod router/handle-event ::capture-selected
[{:as handler-ctx :keys [db]} _]
(let [[q r] (app/selected-qr db)]
(let [[q r] (app/selected-hex db)]
{:dispatch [[::e.game/capture-base q r]
[::clear-selection]
[::alert-if-win]]}))

(defmethod router/handle-event ::build-unit
[{:as handler-ctx :keys [db]} _]
(let [[q r] (app/selected-qr db)]
(let [[q r] (app/selected-hex db)]
{:dispatch [[::e.game/build-unit q r :unit-type.id/infantry]
[::clear-selection]]}))

Expand Down
8 changes: 4 additions & 4 deletions src/clj/zetawar/game.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
(= :terrain-type.id/base
(get-in x [:terrain/type :terrain-type/id])))

(defn terrain-qr [terrain]
(defn terrain-hex [terrain]
[(:terrain/q terrain)
(:terrain/r terrain)])

Expand Down Expand Up @@ -125,7 +125,7 @@
(defn unit? [x]
(contains? x :unit/type))

(defn unit-qr [unit]
(defn unit-hex [unit]
[(:unit/q unit)
(:unit/r unit)])

Expand Down Expand Up @@ -322,7 +322,7 @@
(defn move-tx
([db game unit to-terrain]
(let [new-move-count (inc (get unit :unit/move-count 0))
[to-q to-r] (terrain-qr to-terrain)]
[to-q to-r] (terrain-hex to-terrain)]
(check-can-move db game unit)
;; TODO: check move is valid
[{:db/id (e unit)
Expand Down Expand Up @@ -715,7 +715,7 @@
closest)))
terrains)))

(defn closest-move-to-qr [db game unit q r]
(defn closest-move-to-hex [db game unit q r]
(reduce
(fn [closest move]
(if closest
Expand Down
7 changes: 3 additions & 4 deletions src/clj/zetawar/players/embedded_ai.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

#_(defn handle-event* [{:as player-ctx :keys []} msg]
(let [ev-ctx (assoc player-ctx :db @conn)
{:as ret :keys [tx]} (handle-event ev-ctx msg)
]
(log/tracef "Handler returned: %s" (pr-str ret))
{:as ret :keys [tx]} (handle-event ev-ctx msg)]
(log/tracef "Player handler returned: %s" (pr-str ret))
(when tx
(log/debugf "Transacting: %s" (pr-str tx))
(d/transact! conn tx))
Expand All @@ -19,7 +18,7 @@

(defn new-player [{:as player-ctx :keys [notify-pub]} faction-color]
(let [player-chan (async/chan (async/dropping-buffer 10))]
(async/sub notify-pub :all player-chan)
(async/sub notify-pub :faction.color/all player-chan)
(async/sub notify-pub faction-color player-chan)
(go-loop [msg (<! player-chan)]
(when msg
Expand Down
26 changes: 13 additions & 13 deletions src/clj/zetawar/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -309,46 +309,46 @@
(and (= q (:app/targeted-q app'))
(= r (:app/targeted-r app')))))

(deftrack selected-qr [conn]
(deftrack selected-hex [conn]
(-> @(app conn)
(select-values [:app/selected-q
:app/selected-r])
not-empty))

(deftrack targeted-qr [conn]
(deftrack targeted-hex [conn]
(-> @(app conn)
(select-values [:app/targeted-q
:app/targeted-r])
not-empty))

(deftrack selected-unit [conn]
@(apply unit-at conn @(selected-qr conn)))
@(apply unit-at conn @(selected-hex conn)))

(deftrack unit-selected? [conn]
@(apply unit-at? conn @(selected-qr conn)))
@(apply unit-at? conn @(selected-hex conn)))

(defn selected-can-move? [conn]
(apply can-move? conn @(selected-qr conn)))
(apply can-move? conn @(selected-hex conn)))

(defn selected-can-attack? [conn]
(apply can-attack? conn @(selected-qr conn)))
(apply can-attack? conn @(selected-hex conn)))

(defn selected-can-repair? [conn]
(apply can-repair? conn @(selected-qr conn)))
(apply can-repair? conn @(selected-hex conn)))

(defn selected-can-capture? [conn]
(apply can-capture? conn @(selected-qr conn)))
(apply can-capture? conn @(selected-hex conn)))

(deftrack selected-can-build? [conn q r]
(let [[q r] @(selected-qr conn q r)]
(let [[q r] @(selected-hex conn q r)]
(and (not @(unit-selected? conn q r))
@(current-base? conn q r))))

(deftrack valid-destinations-for-selected [conn]
(if @(selected-can-move? conn)
(let [db @conn
game' (d/entity db @(game-eid conn))
[q r] @(selected-qr conn)
[q r] @(selected-hex conn)
unit (game/unit-at db game' q r)]
(game/valid-destinations db game' unit))
#{}))
Expand All @@ -357,16 +357,16 @@
(contains? @(valid-destinations-for-selected conn) [q r]))

(deftrack selected-can-move-to-targeted? [conn]
(let [[tq tr] @(targeted-qr conn)]
(let [[tq tr] @(targeted-hex conn)]
(and @(selected-can-move? conn)
(contains? @(valid-destinations-for-selected conn) [tq tr]))))

(deftrack enemy-in-range-of-selected? [conn q r]
(let [[selected-q selected-r] @(selected-qr conn)]
(let [[selected-q selected-r] @(selected-hex conn)]
@(in-range-of-enemy-at? conn selected-q selected-r q r)))

(deftrack selected-can-attack-targeted? [conn]
(let [[tq tr] @(targeted-qr conn)]
(let [[tq tr] @(targeted-hex conn)]
(and @(selected-can-attack? conn)
@(enemy-in-range-of-selected? conn tq tr))))

Expand Down
8 changes: 4 additions & 4 deletions test/zetawar/game_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
(testing "a terrain is a terrain"
(is (game/terrain? terrain)))))

(deftest test-terrain-qr
(deftest test-terrain-hex
(let [db @(helper/create-aruba-conn)
game (app/current-game db)
terrain (game/terrain-at db game 1 0)]
(is (= [1 0] (game/terrain-qr terrain)))))
(is (= [1 0] (game/terrain-hex terrain)))))

(deftest test-terrain-at
(let [db @(helper/create-aruba-conn)
Expand Down Expand Up @@ -100,11 +100,11 @@
(is (not (game/unit? terrain)))
(is (game/unit? unit))))

(deftest test-unit-qr
(deftest test-unit-hex
(let [db @(helper/create-aruba-conn)
game (app/current-game db)
unit (game/unit-at db game 2 2)]
(is (= [2 2] (game/unit-qr unit)))))
(is (= [2 2] (game/unit-hex unit)))))

(deftest test-unit-at
(let [db @(helper/create-aruba-conn)
Expand Down
18 changes: 9 additions & 9 deletions test/zetawar/subs_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -287,37 +287,37 @@
(testing "returns false if specified coordinates are targeted"
(is (= false @(subs/targeted? conn 2 2))))))

(deftest test-selected-qr
(deftest test-selected-hex
(let [conn (helper/create-aruba-conn)
db @conn
game (app/current-game db)
app (app/app db)]
(testing "returns selected coordinates"
(is (= nil @(subs/selected-qr conn)))
(is (= nil @(subs/selected-hex conn)))
(d/transact! conn [{:db/id (e app)
:app/selected-q 1
:app/selected-r 2}])
(is (= [1 2] @(subs/selected-qr conn)))
(is (= [1 2] @(subs/selected-hex conn)))
(d/transact! conn [{:db/id (e app)
:app/selected-q 2
:app/selected-r 1}])
(is (= [2 1] @(subs/selected-qr conn))))))
(is (= [2 1] @(subs/selected-hex conn))))))

(deftest test-targeted-qr
(deftest test-targeted-hex
(let [conn (helper/create-aruba-conn)
db @conn
game (app/current-game db)
app (app/app db)]
(testing "returns selected coordinates"
(is (= nil @(subs/targeted-qr conn)))
(is (= nil @(subs/targeted-hex conn)))
(d/transact! conn [{:db/id (e app)
:app/targeted-q 1
:app/targeted-r 2}])
(is (= [1 2] @(subs/targeted-qr conn)))
(is (= [1 2] @(subs/targeted-hex conn)))
(d/transact! conn [{:db/id (e app)
:app/targeted-q 2
:app/targeted-r 1}])
(is (= [2 1] @(subs/targeted-qr conn))))))
(is (= [2 1] @(subs/targeted-hex conn))))))

;; TODO: combine with copy in game-test
(def valid-destinations
Expand Down Expand Up @@ -394,5 +394,5 @@
:app/selected-q 6
:app/selected-r 8
:app/targeted-q 7
:app/targeted-r 7 }])
:app/targeted-r 7}])
(is (= false @(subs/selected-can-attack-targeted? conn))))))

0 comments on commit fc13c52

Please sign in to comment.