Permalink
Browse files

Merge branch '10-problems' into 11-idioms.

  • Loading branch information...
2 parents 2b06919 + 725ae46 commit a278d27cecff99a3a648210eceeec4b4cd68673a @technomancy committed Apr 17, 2009
Showing with 28 additions and 24 deletions.
  1. +2 −2 src/mire.clj
  2. +13 −8 src/mire/rooms.clj
  3. +1 −2 src/mire/test/commands.clj
  4. +10 −10 src/mire/test/rooms.clj
  5. +2 −2 src/mire/util.clj
View
@@ -38,5 +38,5 @@
(recur (read-line))))
(finally (cleanup))))))
-(load-rooms)
-(defonce server (create-server port mire-handle-client))
+(set-rooms)
+(defonce server (create-server port mire-handle-client))
View
@@ -1,9 +1,6 @@
(ns mire.rooms)
-(def rooms)
-(def default-rooms-dir
- (str (.getParent (.getParentFile (.getAbsoluteFile (java.io.File. *file*))))
- "/data/rooms/"))
+(declare rooms)
(defn load-room [rooms file]
(let [room (read-string (slurp (.getAbsolutePath file)))]
@@ -15,11 +12,19 @@
:items (ref (or (:items room) #{}))
:inhabitants (ref #{})}})))
-(defn load-rooms
+(defn load-rooms [dir]
+ "Given a dir, return a map with an entry corresponding to each file
+in it. Files should be maps containing room data."
+ (reduce load-room {} (.listFiles (java.io.File. dir))))
+
+(defn set-rooms
+ "Set mire.rooms/rooms to a map of rooms corresponding to each file
+ in dir. This function should be used only once at mire startup, so
+ having a def inside the function body should be OK. Defaults to
+ looking in data/rooms/."
([dir]
- (def rooms (reduce load-room {}
- (.listFiles (java.io.File. dir)))))
- ([] (load-rooms default-rooms-dir)))
+ (def rooms (load-rooms dir)))
+ ([] (set-rooms "data/rooms/")))
(defn room-contains?
[room thing]
@@ -2,11 +2,10 @@
(:use [mire commands player rooms] :reload-all)
(:use [clojure.contrib test-is seq-utils duck-streams]))
-(def rooms-dir "data/rooms/")
+(set-rooms "data/rooms/")
(defmacro def-command-test [name & body]
`(deftest ~name
- (load-rooms rooms-dir)
(binding [*current-room* (ref (:start rooms))
*inventory* (ref #{})
*player-name* "Tester"]
View
@@ -2,18 +2,18 @@
(:use [mire player rooms] :reload-all)
(:use [clojure.contrib test-is seq-utils]))
-(deftest test-load-rooms
- (load-rooms "data/rooms/")
- (doseq [name [:start :closet :hallway :promenade]]
- (is (contains? rooms name)))
- (is (re-find #"promenade" (:desc (:promenade rooms))))
- (is (= :hallway (:west @(:exits (:promenade rooms)))))
- (is (includes? @(:items (:promenade rooms)) :bunny))
- (is (empty? @(:inhabitants (:promenade rooms)))))
+(deftest test-set-rooms
+ (let [rooms (load-rooms "data/rooms/")]
+ (doseq [name [:start :closet :hallway :promenade]]
+ (is (contains? rooms name)))
+ (is (re-find #"promenade" (:desc (:promenade rooms))))
+ (is (= :hallway (:west @(:exits (:promenade rooms)))))
+ (is (includes? @(:items (:promenade rooms)) :bunny))
+ (is (empty? @(:inhabitants (:promenade rooms))))))
(deftest test-room-contains?
- (load-rooms "data/rooms/")
- (let [closet (:closet rooms)]
+ (let [rooms (load-rooms "data/rooms/")
+ closet (:closet rooms)]
(is (not (empty? (filter #(= % :keys) @(:items closet)))))
(is (room-contains? closet "keys"))
(is (not (room-contains? closet "monkey")))))
View
@@ -3,5 +3,5 @@
(defn move-between-refs
"Move one instance of obj between from and to. Must be called in a transaction."
[obj from to]
- (commute from disj obj)
- (commute to conj obj))
+ (alter from disj obj)
+ (alter to conj obj))

0 comments on commit a278d27

Please sign in to comment.