Skip to content

Commit

Permalink
Replace unhelpful utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
samcf committed Oct 3, 2024
1 parent c9ede46 commit 70bc453
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 26 deletions.
3 changes: 1 addition & 2 deletions src/main/ogres/app/component/panel_lobby.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
[ogres.app.const :refer [VERSION]]
[ogres.app.hooks :refer [use-dispatch use-query]]
[ogres.app.provider.release :as release]
[ogres.app.util :refer [comp-fn]]
[uix.core :refer [defui $ use-context]]))

(def ^:private query-footer
Expand Down Expand Up @@ -108,7 +107,7 @@
($ :legend (str "Players"))
($ :.session-players
(if (seq conns)
(let [xf (filter (comp-fn = :user/type :conn))]
(let [xf (filter (comp #{:conn} :user/type))]
(for [conn (->> conns (sequence xf) (sort-by :db/id))]
($ :.session-player {:key (:db/id conn)}
($ :.session-player-color {:data-color (:user/color conn)})
Expand Down
4 changes: 2 additions & 2 deletions src/main/ogres/app/component/panel_tokens.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require [goog.object :as object :refer [getValueByKeys]]
[ogres.app.component :refer [icon image pagination modal-fullscreen]]
[ogres.app.hooks :refer [use-publish use-dispatch use-image use-image-uploader use-query use-shortcut]]
[ogres.app.util :refer [separate comp-fn]]
[ogres.app.util :refer [separate]]
[uix.core :as uix :refer [defui $ use-callback use-ref use-state use-effect use-memo]]
[uix.dom :refer [create-portal]]
["@dnd-kit/core"
Expand Down Expand Up @@ -124,7 +124,7 @@
(let [result (use-query query-form [:db/ident :root])
{data :root/token-images
{type :user/type} :root/user} result
[pub prv] (separate (comp-fn = :image/scope :public) data)
[pub prv] (separate (comp #{:public} :image/scope) data)
data-pub (into [:default] (reverse pub))
data-prv (vec (reverse prv))
drop-pub (use-droppable #js {"id" "scope-pub"})
Expand Down
15 changes: 9 additions & 6 deletions src/main/ogres/app/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[clojure.string :refer [trim]]
[ogres.app.const :refer [grid-size]]
[ogres.app.geom :as geom]
[ogres.app.util :refer [comp-fn round round-grid]]))
[ogres.app.util :refer [round]]))

(def ^:private suffix-max-xf
(map (fn [[label tokens]] [label (apply max (map :initiative/suffix tokens))])))
Expand Down Expand Up @@ -272,14 +272,14 @@
:camera/point [0 0]
:camera/scale 1}}))
(= id (:db/id (:user/camera user)))
(let [host-cam (first (filter (comp-fn not= :db/id id) (:user/cameras user)))
(let [host-cam (first (filter (comp (complement #{id}) :db/id) (:user/cameras user)))
host-scn (:db/id (:camera/scene host-cam))]
(into [[:db/retractEntity (:db/id camera)]
[:db/retractEntity (:db/id (:camera/scene camera))]
{:db/ident :user :user/camera {:db/id (:db/id host-cam)}}]
(for [[tmp conn] (sequence (indexed) (:session/conns session))
:let [cam (->> (:user/cameras conn)
(filter (comp-fn = (comp :db/id :camera/scene) host-scn))
(filter (comp #{host-scn} :db/id :camera/scene))
(first))
idx (or (:db/id cam) tmp)]]
{:db/id (:db/id conn)
Expand Down Expand Up @@ -475,13 +475,16 @@
[ox oy] :scene/grid-origin}
:camera/scene} :user/camera} user
tx (+ (/ sx (or scale 1)) (or cx 0))
ty (+ (/ sy (or scale 1)) (or cy 0))]
ty (+ (/ sy (or scale 1)) (or cy 0))
rd (/ grid-size 2)
mx (mod ox grid-size)
my (mod oy grid-size)]
[(cond-> {:db/id -1 :object/type :token/token}
(some? hash) (assoc :token/image {:image/hash hash})
(not align?) (assoc :object/point [(round tx) (round ty)])
align? (assoc :object/point
[(round-grid tx (/ grid-size 2) (mod ox grid-size))
(round-grid ty (/ grid-size 2) (mod oy grid-size))]))
[(+ (round (- tx rd mx) grid-size) rd mx)
(+ (round (- ty rd my) grid-size) rd my)]))
{:db/id (:db/id (:user/camera user))
:camera/selected -1
:camera/draw-mode :select
Expand Down
17 changes: 1 addition & 16 deletions src/main/ogres/app/util.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
(ns ogres.app.util
(:require [ogres.app.const :refer [grid-size]]))
(ns ogres.app.util)

(defn key-by
"Returns a map of the given `coll` whose keys are the result of calling `f`
Expand All @@ -15,25 +14,11 @@
(vec (for [f [filter remove]]
(map first (f second pcoll))))))

(defn comp-fn
"Returns a function which applies f with the result of calling key-fn on the
given value and xs. Useful for passing to higher order functions like
filter and map."
[f key-fn & xs]
(fn [x]
(apply f (key-fn x) xs)))

(defn round
"Round the scalar `x` to nearest `n` (default 1)."
([x] (round x 1))
([x n] (* (js/Math.round (/ x n)) n)))

(defn round-grid
"Returns the given scalar `x` rounded to the nearest `grid-size`, accounting
for the radius `r` of the object and scalar offset `o`."
[x r o]
(+ (round (- x r o) grid-size) r o))

(defn display-size
"Returns the given size in bytes as a string using the most appropriate
unit affix.
Expand Down

0 comments on commit 70bc453

Please sign in to comment.