001  (ns ^{:doc "Route which serves and handles the parameters page."
002        :author "Simon Brooke"}
003    mw-ui.routes.params
004    (:require [clojure.walk :refer [keywordize-keys]]
005              [mw-engine.heightmap :as heightmap]
006              [mw-parser.bulk :as compiler]
007              [mw-ui.layout :as layout]
008              [mw-ui.util :as util]
009              [noir.io :as io]
010              [noir.session :as session]))
011  
012  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
013  ;;;;
014  ;;;; mw-ui: a servlet user/visualisation interface for MicroWorld.
015  ;;;;
016  ;;;; This program is free software; you can redistribute it and/or
017  ;;;; modify it under the terms of the GNU General Public License
018  ;;;; as published by the Free Software Foundation; either version 2
019  ;;;; of the License, or (at your option) any later version.
020  ;;;;
021  ;;;; This program is distributed in the hope that it will be useful,
022  ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
023  ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024  ;;;; GNU General Public License for more details.
025  ;;;;
026  ;;;; You should have received a copy of the GNU General Public License
027  ;;;; along with this program; if not, write to the Free Software
028  ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
029  ;;;; USA.
030  ;;;;
031  ;;;; Copyright (C) 2014 Simon Brooke
032  ;;;;
033  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
034  
035  
036  (defn- send-params []
037    {:title "Choose your world"
038     :heightmaps (util/list-resources "/img/heightmaps" #"/?([0-9a-z-_]+).png")
039     :pause (or (session/get :pause) 5)
040     :rulesets (util/list-resources "/rulesets" #"/?([0-9a-z-_]+).txt")
041     })
042  
043  
044  (defn params-page
045    "Handler for params request. If no `request` passed, show empty params form.
046     If `request` is passed, put parameters from request into session and show
047     the world page."
048    ([]
049      (layout/render "params.html" (send-params)))
050    ([request]
051      (try
052        (let [params (keywordize-keys (:form-params request))
053              map (:heightmap params)
054              pause (:pause params)
055              rulefile (:ruleset params)
056              rulepath (str "/rulesets/" rulefile ".txt")]
057          (when (not= map "")
058            (session/put! :world
059                          (heightmap/apply-heightmap
060                            (io/get-resource (str "/img/heightmaps/" map ".png")))))
061          (when (not= rulefile "")
062            (session/put! :rule-text (io/slurp-resource rulepath))
063            (session/put! :rules (compiler/compile-file (io/get-resource rulepath))))
064          (when (not= pause "")
065            (session/put! :pause pause))
066          (layout/render "params.html"
067                         (merge (send-params)
068                                {:r rulefile
069                                 :h map
070                                 :message "Your parameters are saved, now look at your world"})))
071        (catch Exception e
072          (let [params (keywordize-keys (:form-params request))]
073            (layout/render "params.html"
074                           (merge (send-params)
075                                  {:title "Choose your world"
076                                   :r (:ruleset params)
077                                   :h (:heightmap params)
078                                   :message "Your paramters are not saved"
079                                   :error (str (.getName (.getClass e)) ": " (.getMessage e) "; " params)}
080                                  )))))))