Mainly, fix linting errors and add TODOs

This commit is contained in:
Simon Brooke 2021-12-09 21:05:55 +00:00
parent 741c857a7a
commit 534d2d54a5
No known key found for this signature in database
GPG key ID: A7A4F18D1D4DF987
9 changed files with 42 additions and 45 deletions

View file

@ -46,6 +46,7 @@
an app server such as Tomcat an app server such as Tomcat
put any initialization code here" put any initialization code here"
[] []
;; TODO fix timbre config!
;; (timbre/set-config! ;; (timbre/set-config!
;; [:appenders :rotor] ;; [:appenders :rotor]
;; {:min-level :info ;; {:min-level :info

View file

@ -35,12 +35,12 @@
(deftype RenderableTemplate [template params] (deftype RenderableTemplate [template params]
Renderable Renderable
(render [this request] (render [_ request]
(content-type (content-type
(->> (assoc (merge params {:version (System/getProperty "mw-ui.version")}) (->> (assoc (merge params {:version (System/getProperty "mw-ui.version")})
(keyword (s/replace template #".html" "-selected")) "active" (keyword (s/replace template #".html" "-selected")) "active"
:servlet-context :servlet-context
(if-let [context (:servlet-context request)] (when-let [context (:servlet-context request)]
(.getContextPath context))) (.getContextPath context)))
(parser/render-file (str template-path template)) (parser/render-file (str template-path template))
response) response)

View file

@ -34,9 +34,10 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn format-css-class [statekey] (defn format-css-class
"Format this statekey, assumed to be a keyword indicating a state in the "Format this statekey, assumed to be a keyword indicating a state in the
world, into a CSS class" world, into a CSS class"
[statekey]
(subs (str statekey) 1)) (subs (str statekey) 1))
@ -92,7 +93,8 @@
(defn render-inspector (defn render-inspector
"Render in Hiccup format the HTML content of an inspector on this cell." "Render in Hiccup format the HTML content of an inspector on this cell."
[cell table] ([cell _] (render-inspector cell))
([cell]
[:table {:class "music-ruled"} [:table {:class "music-ruled"}
[:tr [:tr
[:td {:colspan 2 :style "text-align: center;"} [:td {:colspan 2 :style "text-align: center;"}
@ -100,6 +102,6 @@
:width 64 :width 64
:height 64}]]] :height 64}]]]
[:tr [:th "Key"][:th "Value"]] [:tr [:th "Key"][:th "Value"]]
(map #(vector :tr (vector :th %)(vector :td (cell %))) (keys cell))]) (map #(vector :tr (vector :th %)(vector :td (cell %))) (keys cell))]))

View file

@ -1,9 +1,9 @@
(ns ^{:doc "Routes which serve the main pages of the application." (ns ^{:doc "Routes which serve the main pages of the application."
:author "Simon Brooke"} :author "Simon Brooke"}
mw-ui.routes.home mw-ui.routes.home
(:use clojure.walk (:require [clojure.java/io :refer [file]]
compojure.core) [clojure.walk :refer [keywordize-keys]]
(:require [clojure.pprint :only [pprint]] [compojure.core :refer [defroutes GET POST]]
[hiccup.core :refer [html]] [hiccup.core :refer [html]]
[mw-engine.utils :as engine-utils] [mw-engine.utils :as engine-utils]
[mw-ui.layout :as layout] [mw-ui.layout :as layout]
@ -13,7 +13,6 @@
[mw-ui.routes.params :as params] [mw-ui.routes.params :as params]
[mw-ui.routes.save :as save] [mw-ui.routes.save :as save]
[mw-ui.util :as util] [mw-ui.util :as util]
[noir.io :as io]
[noir.session :as session] [noir.session :as session]
[ring.util.response :as response])) [ring.util.response :as response]))
@ -45,7 +44,8 @@
(sort (sort
(filter #(not (nil? %)) (filter #(not (nil? %))
(map #(first (rest (re-matches #"([0-9a-z-]+).png" (.getName %)))) (map #(first (rest (re-matches #"([0-9a-z-]+).png" (.getName %))))
(file-seq (clojure.java.io/file "resources/public/img/tiles")))))) ;; TODO: this will not work when running from jar; see utils.clj
(file-seq (file "resources/public/img/tiles"))))))
(defn about-page [] (defn about-page []
@ -63,14 +63,16 @@
:components ["mw-engine" "mw-parser" "mw-ui"] :components ["mw-engine" "mw-parser" "mw-ui"]
:version (System/getProperty "mw-ui.version")})) :version (System/getProperty "mw-ui.version")}))
(defn home-page [] (defn home-page
"Render the home page." "Render the home page."
[]
(layout/render "trusted-content.html" {:title "Welcome to MicroWorld" (layout/render "trusted-content.html" {:title "Welcome to MicroWorld"
:content (util/md->html "/md/mw-ui.md") :content (util/md->html "/md/mw-ui.md")
:version (System/getProperty "mw-ui.version")})) :version (System/getProperty "mw-ui.version")}))
(defn inspect-page [request] (defn inspect-page
"Open an inspector on the cell at the co-ordinates specified in this request" "Open an inspector on the cell at the co-ordinates specified in this request"
[request]
(let [params (keywordize-keys (:params request)) (let [params (keywordize-keys (:params request))
xs (:x params) xs (:x params)
ys (:y params) ys (:y params)
@ -86,7 +88,7 @@
true true
(layout/render "inspector.html" (layout/render "inspector.html"
{:title (format "Inspect cell at %d, %d" x y) {:title (format "Inspect cell at %d, %d" x y)
:content (html (world/render-inspector cell world)) :content (html (world/render-inspector cell))
:cell cell :cell cell
:x (:x cell) :x (:x cell)
:y (:y cell) :y (:y cell)

View file

@ -1,14 +1,10 @@
(ns ^{:doc "Route which handles the upload of worlds/rules from the client." (ns ^{:doc "Route which handles the upload of worlds/rules from the client."
:author "Simon Brooke"} :author "Simon Brooke"}
mw-ui.routes.load mw-ui.routes.load
(:use clojure.walk (:require [clojure.walk :refer [keywordize-keys]]
compojure.core)
(:require [hiccup.core :refer [html]]
[noir.io :as io] [noir.io :as io]
[noir.session :as session] [noir.session :as session]
[ring.util.response :as response] [mw-ui.layout :as layout]))
[mw-ui.layout :as layout]
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
@ -38,7 +34,7 @@
(io/upload-file "/tmp/" file) (io/upload-file "/tmp/" file)
(cond (cond
(session/put! :world (session/put! :world
(with-open [eddi (java.io.FileReader. (:tempfile file))] (read))) (with-open [_ (java.io.FileReader. (:tempfile file))] (read)))
(str "Successfully loaded your world from " (:filename file)))) (str "Successfully loaded your world from " (:filename file))))
@ -52,8 +48,7 @@
([] ([]
(load-page nil)) (load-page nil))
([request] ([request]
(let [params (keywordize-keys (:params request)) (let [file (:file request)]
file (:file request)]
(try (try
(layout/render "load.html" (layout/render "load.html"
{:title "Load World" {:title "Load World"

View file

@ -1,15 +1,11 @@
(ns ^{:doc "Route which serves and handles the parameters page." (ns ^{:doc "Route which serves and handles the parameters page."
:author "Simon Brooke"} :author "Simon Brooke"}
mw-ui.routes.params mw-ui.routes.params
(:use clojure.walk (:require [clojure.walk :refer [keywordize-keys]]
clojure.java.io
compojure.core)
(:require [hiccup.core :refer [html]]
[mw-engine.heightmap :as heightmap] [mw-engine.heightmap :as heightmap]
[mw-parser.bulk :as compiler] [mw-parser.bulk :as compiler]
[mw-ui.layout :as layout] [mw-ui.layout :as layout]
[mw-ui.util :as util] [mw-ui.util :as util]
[mw-ui.render-world :as world]
[noir.io :as io] [noir.io :as io]
[noir.session :as session])) [noir.session :as session]))
@ -58,14 +54,14 @@
pause (:pause params) pause (:pause params)
rulefile (:ruleset params) rulefile (:ruleset params)
rulepath (str "/rulesets/" rulefile ".txt")] rulepath (str "/rulesets/" rulefile ".txt")]
(if (not= map "") (when (not= map "")
(session/put! :world (session/put! :world
(heightmap/apply-heightmap (heightmap/apply-heightmap
(io/get-resource (str "/img/heightmaps/" map ".png"))))) (io/get-resource (str "/img/heightmaps/" map ".png")))))
(when (not= rulefile "") (when (not= rulefile "")
(session/put! :rule-text (io/slurp-resource rulepath)) (session/put! :rule-text (io/slurp-resource rulepath))
(session/put! :rules (compiler/compile-file (io/get-resource rulepath)))) (session/put! :rules (compiler/compile-file (io/get-resource rulepath))))
(if (not= pause "") (when (not= pause "")
(session/put! :pause pause)) (session/put! :pause pause))
(layout/render "params.html" (layout/render "params.html"
(merge (send-params) (merge (send-params)

View file

@ -1,16 +1,11 @@
(ns ^{:doc "Route which serves and handles the rules page." (ns ^{:doc "Route which serves and handles the rules page."
:author "Simon Brooke"} :author "Simon Brooke"}
mw-ui.routes.rules mw-ui.routes.rules
(:use clojure.walk (:require [clojure.walk :refer [keywordize-keys]]
compojure.core)
(:require [hiccup.core :refer [html]]
[mw-parser.bulk :as compiler] [mw-parser.bulk :as compiler]
[mw-ui.layout :as layout] [mw-ui.layout :as layout]
[mw-ui.util :as util]
[mw-ui.render-world :as world]
[noir.io :as io] [noir.io :as io]
[noir.session :as session] [noir.session :as session]))
[ring.util.response :as response]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; ;;;;
@ -47,7 +42,7 @@
:message (str "Successfully compiled " :message (str "Successfully compiled "
(count rules) (count rules)
" rules") }) " rules") })
true {:rule-text (or :else {:rule-text (or
(session/get :rule-text) (session/get :rule-text)
(io/slurp-resource "/rulesets/basic.txt")) (io/slurp-resource "/rulesets/basic.txt"))
:message "No rules found in request; loading defaults"}) :message "No rules found in request; loading defaults"})
@ -67,9 +62,9 @@
the session or from `resources/rulesets/basic.txt` and pass that back." the session or from `resources/rulesets/basic.txt` and pass that back."
([request] ([request]
(let [processed (process-rules-request request)] (let [processed (process-rules-request request)]
(if (:rules processed) (when (:rules processed)
(session/put! :rules (:rules processed))) (session/put! :rules (:rules processed)))
(if (:rule-text processed) (when (:rule-text processed)
(session/put! :rule-text (:rule-text processed))) (session/put! :rule-text (:rule-text processed)))
(layout/render "rules.html" (layout/render "rules.html"
(merge {:title "Edit Rules"} processed)))) (merge {:title "Edit Rules"} processed))))

View file

@ -1,7 +1,7 @@
(ns ^{:doc "Route which handles the saving of world state the client." (ns ^{:doc "Route which handles the saving of world state the client."
:author "Simon Brooke"} :author "Simon Brooke"}
mw-ui.routes.save mw-ui.routes.save
(:require [clojure.pprint :as pretty :only [pprint]] (:require [clojure.pprint :refer [pprint]]
[noir.session :as session] [noir.session :as session]
[noir.response :as response])) [noir.response :as response]))
@ -29,12 +29,13 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn save-page [] (defn save-page
"Save the current world to the browser, using our own custom mime-type in "Save the current world to the browser, using our own custom mime-type in
an attempt to prevent the browser trying to do anything clever with it. an attempt to prevent the browser trying to do anything clever with it.
Note that it is saved as a raw Clojure data structure, not as XML or Note that it is saved as a raw Clojure data structure, not as XML or
any proprietary format." any proprietary format."
[]
(response/content-type (response/content-type
"application/journeyman-mwm; charset=utf-8" "application/journeyman-mwm; charset=utf-8"
(with-out-str (pretty/pprint (session/get :world))))) (with-out-str (pprint (session/get :world)))))

View file

@ -1,7 +1,8 @@
(ns ^{:doc "Utility functions used by other namespaces in this package." (ns ^{:doc "Utility functions used by other namespaces in this package."
:author "Simon Brooke"} :author "Simon Brooke"}
mw-ui.util mw-ui.util
(:require [noir.io :as io] (:require [clojure.java.io :refer [file]]
[noir.io :as io]
[noir.session :as session] [noir.session :as session]
[markdown.core :as md])) [markdown.core :as md]))
@ -36,13 +37,17 @@
(io/slurp-resource filename) (io/slurp-resource filename)
(md/md-to-html-string))) (md/md-to-html-string)))
;; TODO: The reason we can't list files in a jar file, and what to do about it,
;; is here. Too tired to fix this tonight.
;; https://stackoverflow.com/questions/46488466/clojure-list-subfolders-in-resources-in-uberjar
(defn list-resources [directory pattern] (defn list-resources
"List resource files matching `pattern` in `directory`." "List resource files matching `pattern` in `directory`."
[directory pattern]
(let (let
[path (str (io/resource-path) directory)] [path (str (io/resource-path) directory)]
(session/put! :list-resources-path path) (session/put! :list-resources-path path)
(sort (sort
(remove nil? (remove nil?
(map #(first (rest (re-matches pattern (.getName %)))) (map #(first (rest (re-matches pattern (.getName %))))
(file-seq (clojure.java.io/file path))))))) (file-seq (file path)))))))