My Monster! It lives!

This commit is contained in:
Simon Brooke 2014-07-14 18:35:35 +01:00
parent 77cfb32bb2
commit ad0e992000
10 changed files with 262 additions and 68 deletions

View file

@ -1,17 +1,15 @@
(ns mw-ui.routes.home
(:use compojure.core)
(:use compojure.core
[mw-ui.routes.rules :as rules])
(:require [hiccup.core :refer [html]]
[mw-parser.bulk :as compiler]
[mw-ui.layout :as layout]
[mw-ui.util :as util]
[mw-ui.render-world :as world]
[noir.session :as session]))
(defn home-page []
(layout/render "world.html" {:title "Watch your world grow"
:content (html (world/render-world-table))
:seconds (or (session/get :seconds) 5)
:maybe-refresh "refresh"}))
(layout/render "home.html" {:title "Welcome to MicroWorld"
:content (util/md->html "/md/mw-ui.md")}))
(defn world-page []
(layout/render "world.html" {:title "Watch your world grow"
@ -34,27 +32,10 @@
:states (list-states)
:components ["mw-engine" "mw-parser" "mw-ui"]}))
(defn rules-page
([request]
(let [rule-text (:src request)
error
(try
(do
(if rule-text
(session/put! :rules (compiler/compile-string rule-text)))
(session/put! :rule-text rule-text)
nil)
(catch Exception e (.getMessage e)))]
(layout/render "rules.html" {:title "Edit Rules"
:rule-text (or (session/get :rule-text) (slurp "resources/public/rulesets/basic.txt"))
:error error})))
([]
(rules-page nil)))
(defroutes home-routes
(GET "/" [] (home-page))
(GET "/about" [] (about-page))
(GET "/docs" [] (docs-page))
(GET "/world" [] (world-page))
(GET "/rules" request (rules-page request))
(POST "/rules" request (rules-page request)))
(GET "/rules" request (rules/rules-page request))
(POST "/rules" request (rules/rules-page request)))

View file

@ -0,0 +1,48 @@
(ns mw-ui.routes.rules
(:use clojure.walk
compojure.core)
(:require [hiccup.core :refer [html]]
[mw-parser.bulk :as compiler]
[mw-ui.layout :as layout]
[mw-ui.util :as util]
[mw-ui.render-world :as world]
[noir.session :as session]))
(defn process-rules-request
[request]
(let [src (:src (keywordize-keys (:form-params request)))]
(try
(cond src
(let [rules (compiler/compile-string src)]
{:rule-text src
:rules rules
:message (str "Successfully compiled "
(count rules)
" rules") })
true {:rule-text (or
(session/get :rule-text)
(slurp "resources/rulesets/basic.txt"))
:message "No rules found in request; loading defaults"})
(catch Exception e
{:rule-text src
:message "An error occurred during compilation"
:error (str (.getName (.getClass e)) ": " (.getMessage e))}))))
(defn rules-page
"Request handler for the `rules` request. If the `request` contains a value
for `:src`, treat that as rule source and try to compile it. If compilation
succeeds, stash the compiled rules and the rule text on the session, and
provide feedback; if not, provide feedback.
If `request` doesn't contain a value for `:src`, load basic rule source from
the session or from `resources/rulesets/basic.txt` and pass that back."
([request]
(let [processed (process-rules-request request)]
(if (:rules processed)
(session/put! :rules (:rules processed)))
(if (:rule-text processed)
(session/put! :rule-text (:rule-text processed)))
(layout/render "rules.html"
(merge {:title "Edit Rules"} processed))))
([]
(rules-page nil)))