add assoc-in-entity / update-in-entity

This commit is contained in:
Jeremy Penner 2013-05-04 00:18:42 -04:00
parent 69c533ceec
commit b0bf0746c0

View file

@ -43,7 +43,7 @@
(assoc-in @*gamestate* [:indices indextype] index)))))
(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)]
(if (empty? idsafter)
(dissoc index key)
@ -54,10 +54,10 @@
(if-let [idsbefore (get index key)]
(assoc index key (conj idsbefore id))
(if key
(assoc index key #{id})
(assoc index key #{id})
index)))
(defn update-id-index-by-key [key indexold id entityold entitynew]
(defn update-id-index-by-key [key indexold id entityold entitynew]
(-> indexold
(remove-id-from-index id (get entityold key))
(add-id-to-index id (get entitynew key))))
@ -65,7 +65,7 @@
(defn set-entity [id value]
(let [value (if value (assoc value ::id id) nil)]
(update-indices id (entity id) value)
(let [gsnew
(let [gsnew
(if (= value nil)
(assoc @*gamestate* :entities (dissoc (get @*gamestate* :entities) id))
(assoc-in @*gamestate* [:entities id] value))]
@ -75,6 +75,12 @@
(defn update-entity [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]
(set-entity (::id value) nil))
@ -92,7 +98,7 @@
(defn define-id-index [indextype & [key]]
(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))
(defmethod query-index indextype [indextype index query] (or (get index query) #{}))))