Skip to content
This repository has been archived by the owner on Oct 21, 2021. It is now read-only.

Commit

Permalink
Step 2: commands
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Jan 31, 2009
1 parent f526ade commit 40e592b
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 233 deletions.
2 changes: 1 addition & 1 deletion mire.sh
@@ -1,3 +1,3 @@
#!/bin/sh

java -cp jars/clojure.jar:jars/clojure-contrib.jar clojure.main src/mire.clj
java -cp jars/clojure.jar:jars/clojure-contrib.jar:src/ clojure.main src/mire.clj
41 changes: 9 additions & 32 deletions src/mire.clj
@@ -1,43 +1,20 @@
#!/usr/bin/env clj

(add-classpath (str "file://" (.getParent (java.io.File. *file*)) "/"))

(ns mire
(:use [mire commands player rooms])
(:use [mire commands])
(:use [clojure.contrib server-socket duck-streams]))

(def port 3333)

(defn welcome []
(dosync (commute *players* conj {*name* *out*})
(commute (:inhabitants @*current-room*) conj *name*))
(println "Welcome to Mire, " *name* "\n")
(println (look))
(print prompt) (flush))

(defn cleanup []
"Drop all inventory and remove player from room and player list."
(doseq [thing @*inventory*] (discard thing))
(dosync (commute *players* dissoc *name*)
(commute (:inhabitants @*current-room*)
(partial remove #(= % *name*)))))
(def prompt "> ")

(defn- mire-handle-client [in out]
(binding [*in* (reader in)
*out* (writer out)]
;; bindings doesn't work sequentially, so we need to nest them
;; otherwise the call to read-name uses the old value of *in*/*out*
(binding [*name* (read-name)
*inventory* (ref [])
*current-room* (ref (@mire.rooms/*rooms* :start))]
(welcome)
(try
(loop [input (read-line)]
(when input
(println (execute input))
(print prompt) (flush)
(recur (read-line))))
(finally (cleanup))))))
(print prompt) (flush)
(loop [input (read-line)]
(println (execute input))
(print prompt)
(flush)
(recur (read-line)))))

(load-rooms (str (.getParent (java.io.File. *file*)) "/../data/rooms/"))
(defonce server (create-server port mire-handle-client))
(def server (create-server port mire-handle-client))
122 changes: 9 additions & 113 deletions src/mire/commands.clj
@@ -1,120 +1,16 @@
(ns mire.commands
(:use [mire player rooms util])
(:use [clojure.contrib str-utils seq-utils]))
(:use [clojure.contrib str-utils]))

(declare commands)
(defn current-time []
(str "It is now "(java.util.Date.)))

;; Command functions

(defn look "Get a description of the surrounding environs and its contents."
[]
(let [room @*current-room*]
(str (:desc room) "\n"
(look-exits room)
(look-items room)
(look-inhabitants room))))

(defn inventory
"What are we carrying? Let's take a look."
[]
(if (empty? @*inventory*)
"You are not carrying anything."
(str-join "\n" (map #(:desc (*items* %)) @*inventory*))))

(defn move
"\"♬ We gotta get out of this place... ♪\" Give a direction."
[direction]
(let [target-name (@(:exits @*current-room*) (keyword direction))
target (@*rooms* target-name)]
(if target-name
(dosync
(move-between-refs *name*
(:inhabitants @*current-room*)
(:inhabitants target))
(ref-set *current-room* target)
(look))
"You can't go that way.")))

(defn grab
"Pick something up."
[thing]
(dosync
(if (room-contains? @*current-room* thing)
(do (move-between-refs (keyword thing)
(:items @*current-room*)
*inventory*)
(str "You picked up the " thing "."))
(str "There isn't any " thing " here."))))

(defn discard
"Put something down."
[thing]
(dosync
(if (inventory-contains? thing)
(do (move-between-refs (keyword thing)
*inventory*
(:items @*current-room*))
(str "You dropped up the " thing "."))
(str "You don't have a " thing "."))))

(defn say
"Speak some words so that they can be heard by everyone within earshot."
[& words]
(let [string (str "\"" (str-join " " words) "\"")]
(doseq [player @(:inhabitants @*current-room*)]
(if (not (= player *name*))
(binding [*out* (*players* player)]
(println *name* "says:" string)
(print prompt) (flush))))
(str "You say: " string)))

(defn help
"Print an explaination of available commands."
[]
;; TODO: remove non-commands from output
(str-join "\n" (map #(str (key %) ": " (:doc (meta (val %))))
(ns-publics 'mire.commands))))

;; Command data

(def commands {"move" move
"north" (fn [] (move :north))
"south" (fn [] (move :south))
"east" (fn [] (move :east))
"west" (fn [] (move :west))

"look" look
"inventory" inventory
"grab" grab
"discard" discard
"say" say
"help" help

;; aliases
"speak" say
"go" move
"get" grab
"take" grab
"drop" discard

;; for debugging
"who" (fn [& args] *name*)

"l" look
"i" inventory})

(def unknown-responses ["What you say?" "Speak up!" "I don't get it."
"Please rephrase that." "Your words confuse me."])

;; Command handling
(def commands {"time" current-time,
"look" (fn [] "You see an empty room, waiting to be filled.")})

(defn execute
"Execute a command that is passed to us."
[input]
(let [[command & args] (re-split #"\s+" input)]
(try
(apply (commands (.toLowerCase command)) args)
(catch java.lang.NullPointerException _
(pick-rand unknown-responses))
(catch java.lang.IllegalArgumentException _
"You can't do that."))))
(let [input-words (re-split #"\s+" input)
command (first input-words)
args (rest input-words)]
(apply (commands command) args)))
32 changes: 0 additions & 32 deletions src/mire/player.clj

This file was deleted.

33 changes: 0 additions & 33 deletions src/mire/rooms.clj

This file was deleted.

22 changes: 0 additions & 22 deletions src/mire/util.clj

This file was deleted.

0 comments on commit 40e592b

Please sign in to comment.