Browse Source

Trivial character load/save

master
parent
commit
58538c74f5
4 changed files with 21 additions and 8 deletions
  1. +1
    -0
      .gitignore
  2. +1
    -0
      characters/README
  3. +1
    -1
      src/character_suite/pdf.clj
  4. +18
    -7
      src/character_suite/server.clj

+ 1
- 0
.gitignore View File

@@ -13,3 +13,4 @@ pom.xml
.repl
.nrepl-port
compiled/
characters/*

+ 1
- 0
characters/README View File

@@ -0,0 +1 @@
Directory where characters will be stored for now.

+ 1
- 1
src/character_suite/pdf.clj View File

@@ -59,7 +59,7 @@
[[(:str characteristics) 240 705 18] [(:dex characteristics) 327 705 18] [(:pow characteristics) 420 705 18]
[(:con characteristics) 240 672 18] [(:app characteristics) 327 672 18] [(:edu characteristics) 420 672 18]
[(:siz characteristics) 240 640 18] [(:int characteristics) 327 640 18] [(:move-rate character) 420 640 18]
[(:name character) 85 720 10 Element/ALIGN_LEFT]
[(:name character) 85 720 10 Element/ALIGN_LEFT 95]
[(:player character) 87 704 10 Element/ALIGN_LEFT]
[(:class character) 108 688 10 Element/ALIGN_LEFT]
[(:age character) 79 672 10 Element/ALIGN_LEFT]


+ 18
- 7
src/character_suite/server.clj View File

@@ -6,7 +6,8 @@
java.net.URL
java.net.URLDecoder
java.net.URLEncoder
java.util.Date)
java.util.Date
java.util.UUID)
(:require [character-suite.pdf :as pdf]
[clojure.edn :as edn]
[clojure.java.io :as io]
@@ -18,21 +19,31 @@
[ring.middleware.session :refer [wrap-session]]
[ring.util.response :refer [file-response resource-response]]))

(def ^:dynamic *character-path* "characters")

(defn save-character [uuid character]
(spit (io/file *character-path* uuid)
(pr-str character)))

(defn load-character [uuid]
(edn/read-string (slurp (io/file *character-path* uuid))))

(defn get-character-handler [request]
(let [character (-> request :session :character)
(let [character-uuid (-> request :session :character)
character (load-character character-uuid)
pdf-contents (.toByteArray (pdf/coc-pdf "resources/coc-charactersheet.pdf" character))]
(println (str "Creating PDF for " character))
{:body (ByteArrayInputStream. pdf-contents)
:headers {"Content-Type" "application/pdf"}}))

(defn post-character-handler [request]
(let [raw-body (:body request)
incoming-edn (if raw-body (String. (.bytes raw-body)) "{}")
incoming-character (edn/read-string incoming-edn)]
(println (str "Saving character " incoming-character))
{:body "Character saved"
incoming-character (edn/read-string incoming-edn)
character-uuid (.toString (UUID/randomUUID))]
(save-character character-uuid incoming-character)
{:body (pr-str {:id character-uuid})
:headers {"Content-Type" "text/plain"}
:session (assoc (:session request) :character incoming-character)}))
:session (assoc (:session request) :character character-uuid)}))

(defn index-handler [request]
(file-response "resources/public/index.html"))


Loading…
Cancel
Save