add assoc-in-entity / update-in-entity
This commit is contained in:
parent
69c533ceec
commit
b0bf0746c0
|
@ -43,7 +43,7 @@
|
||||||
(assoc-in @*gamestate* [:indices indextype] index)))))
|
(assoc-in @*gamestate* [:indices indextype] index)))))
|
||||||
|
|
||||||
(defn remove-id-from-index [index id key]
|
(defn remove-id-from-index [index id key]
|
||||||
(if-let [idsbefore (get index key)]
|
(if-let [idsbefore (get index key)]
|
||||||
(let [idsafter (disj idsbefore id)]
|
(let [idsafter (disj idsbefore id)]
|
||||||
(if (empty? idsafter)
|
(if (empty? idsafter)
|
||||||
(dissoc index key)
|
(dissoc index key)
|
||||||
|
@ -54,10 +54,10 @@
|
||||||
(if-let [idsbefore (get index key)]
|
(if-let [idsbefore (get index key)]
|
||||||
(assoc index key (conj idsbefore id))
|
(assoc index key (conj idsbefore id))
|
||||||
(if key
|
(if key
|
||||||
(assoc index key #{id})
|
(assoc index key #{id})
|
||||||
index)))
|
index)))
|
||||||
|
|
||||||
(defn update-id-index-by-key [key indexold id entityold entitynew]
|
(defn update-id-index-by-key [key indexold id entityold entitynew]
|
||||||
(-> indexold
|
(-> indexold
|
||||||
(remove-id-from-index id (get entityold key))
|
(remove-id-from-index id (get entityold key))
|
||||||
(add-id-to-index id (get entitynew key))))
|
(add-id-to-index id (get entitynew key))))
|
||||||
|
@ -65,7 +65,7 @@
|
||||||
(defn set-entity [id value]
|
(defn set-entity [id value]
|
||||||
(let [value (if value (assoc value ::id id) nil)]
|
(let [value (if value (assoc value ::id id) nil)]
|
||||||
(update-indices id (entity id) value)
|
(update-indices id (entity id) value)
|
||||||
(let [gsnew
|
(let [gsnew
|
||||||
(if (= value nil)
|
(if (= value nil)
|
||||||
(assoc @*gamestate* :entities (dissoc (get @*gamestate* :entities) id))
|
(assoc @*gamestate* :entities (dissoc (get @*gamestate* :entities) id))
|
||||||
(assoc-in @*gamestate* [:entities id] value))]
|
(assoc-in @*gamestate* [:entities id] value))]
|
||||||
|
@ -75,6 +75,12 @@
|
||||||
(defn update-entity [value]
|
(defn update-entity [value]
|
||||||
(set-entity (::id value) value))
|
(set-entity (::id value) value))
|
||||||
|
|
||||||
|
(defn assoc-in-entity [id keys value]
|
||||||
|
(set-entity id (assoc-in (entity id) keys value)))
|
||||||
|
|
||||||
|
(defn update-in-entity [id keys fn]
|
||||||
|
(set-entity id (update-in (entity id) keys fn)))
|
||||||
|
|
||||||
(defn remove-entity [value]
|
(defn remove-entity [value]
|
||||||
(set-entity (::id value) nil))
|
(set-entity (::id value) nil))
|
||||||
|
|
||||||
|
@ -92,7 +98,7 @@
|
||||||
|
|
||||||
(defn define-id-index [indextype & [key]]
|
(defn define-id-index [indextype & [key]]
|
||||||
(let [key (or key indextype)]
|
(let [key (or key indextype)]
|
||||||
(defmethod update-index indextype [indextype indexold id entityold entitynew]
|
(defmethod update-index indextype [indextype indexold id entityold entitynew]
|
||||||
(update-id-index-by-key key indexold id entityold entitynew))
|
(update-id-index-by-key key indexold id entityold entitynew))
|
||||||
(defmethod query-index indextype [indextype index query] (or (get index query) #{}))))
|
(defmethod query-index indextype [indextype index query] (or (get index query) #{}))))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue