Skip to content

Commit

Permalink
working on storing session state within clojure session.
Browse files Browse the repository at this point in the history
  • Loading branch information
ekoontz committed Feb 7, 2011
1 parent 83dec90 commit 9862ba6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
21 changes: 15 additions & 6 deletions src/index.clj
Expand Up @@ -11,7 +11,7 @@

(defs
;; strings
company-name* "Verbi Italiani by Eugene Koontz"
company-name* "foofers.org presents Verbi Italiani"
app-name* "Verbi Italiani"

;; colors
Expand Down Expand Up @@ -60,13 +60,14 @@
(button subscribe-id* "Subscribe!" important-color*))

(slice header [text & [id]]
(html [:h1 {:id (wo# id)} text]))
(title "Verbi Italiani")
(html [:h1 {:id (wo# id)} text]))

(defn navigation []
(html
[:div [:a {:href "/"} "top"]]
[:div [:a {:href "/quiz/"} "take a quiz..."]]
[:div [:a {:href "http://github.com/ekoontz/italianverbs"} "source code"]]
[:div {:style "float:left;width:auto;padding:10px;"} [:a {:href "/"} "top"]]
[:div {:style "float:left;width:auto;padding:10px;"} [:a {:href "/quiz/"} "take a quiz..."]]
[:div {:style "float:left;width:auto;padding:10px;"} [:a {:href "http://github.com/ekoontz/italianverbs"} "source code"]]
))

(slice site-header
Expand Down Expand Up @@ -94,10 +95,18 @@

(load-file "src/quiz.clj")

(def sessions (hash-map))

(def sessions (assoc sessions 'reqid 0))

(slice update-state
(let [update (def sessions (assoc sessions 'reqid (+ 1 (get sessions 'reqid))))]
(html [:div {:style "float:left;border:1px dashed green"} (str "request_id: " (get sessions 'reqid))])))

(defroutes app
(GET "/" _ (main-page))
(GET "/subscribe" _ (slices site-header subscribe-button))
(GET "/quiz*" _ (slices site-header quiz))
(GET "/quiz*" _ (slices site-header quiz update-state))
(GET "/test" r (slices jquery
(dom (alert ~(:remote-addr r)))
(html [:h1 "Hi"])
Expand Down
10 changes: 5 additions & 5 deletions src/quiz.clj
@@ -1,33 +1,33 @@
(ns italianverbs)

(defn wrapchoice [word]
;; FIXME: url-encode word.
(let [href_prefix "/quiz?"]
(html [:h3 [:a {:href (str href_prefix "guess=" word)} word]])))

(defn guess ^{:impure true} [lexicon remaining answer show-true-at]
(html
(if (= remaining show-true-at)
(wrapchoice answer))
(wrapchoice (str answer "(true)")))
(if (> remaining 0)
(let [choice (rand-int (count lexicon))]
(html (wrapchoice (get lexicon (nth (keys lexicon) choice)))
(html (wrapchoice (str (get lexicon (nth (keys lexicon) choice))))
(guess (dissoc lexicon (nth (keys lexicon) choice)) (- remaining 1) answer show-true-at))))))

(defn guesses ^{:impure true} [question answer lexicon true-index]
(let [multiple-choices 3]
(html
;; 4 choices; show true answer at a random position amongst the false ones.
;; TODO: remove true answer from lexicon in (guess) call.
(guess (dissoc lexicon question) multiple-choices answer (rand-int multiple-choices)))))
(guess (dissoc lexicon question) multiple-choices answer (rand-int (+ 1 multiple-choices))))))

(slice next-question [index]
(html [:div [:h2 [:i (nth (keys lexicon) index)]]
(html [:div {:style "width:100%;float:left;border:0px dashed blue"} [:h2 [:i (nth (keys lexicon) index)]]
[:div {:style "padding-left:1em"}
(guesses (nth (keys lexicon) index)
(get lexicon (nth (keys lexicon) index))
lexicon index)]]))


(slice quiz ^{:impure true} []
(next-question (rand-int (count lexicon))))

Expand Down
2 changes: 2 additions & 0 deletions src/table.clj
Expand Up @@ -17,7 +17,9 @@
(add-verb "scrivere" "to write")
(add-verb "correggere" "to correct")
(add-verb "leggere" "to read")
(add-verb "mangiere" "to eat")
(add-verb "parlere" "to speak")
(add-verb "pranzare" "to eat lunch")

(slice verb-table
(html [:table
Expand Down

0 comments on commit 9862ba6

Please sign in to comment.