diff --git a/src/hottub/gs.clj b/src/hottub/gs.clj index ef808f6..afdcb53 100644 --- a/src/hottub/gs.clj +++ b/src/hottub/gs.clj @@ -54,6 +54,11 @@ (assoc index key #{id}) index))) +(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)))) + (defn set-entity [id value] (update-indices id (entity id) value) (let [gsnew @@ -68,9 +73,11 @@ (defn q [indextype query] (q-in @*gamestate* indextype query)) -(defmethod update-index :name [indextype indexold id entityold entitynew] - (-> indexold - (remove-id-from-index id (get entityold :name)) - (add-id-to-index id (get entitynew :name)))) +(defn define-id-index [indextype & [key]] + (let [key (or key indextype)] + (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) #{})))) -(defmethod query-index :name [indextype index query] (or (get index query) #{})) +(define-id-index :name) +(define-id-index :type)