Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 137 lines (124 sloc) 4.47 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
(ns foreclojure.problems
  (:use foreclojure.utils
        [foreclojure.social :only [tweet-link gist!]]
        [clojail core testers]
        somnium.congomongo
        (hiccup form-helpers page-helpers core)
        [amalloy.utils.debug :only [?]]
        compojure.core)
  (:require [sandbar.stateful-session :as session]
            [clojure.string :as s]))

(defn get-solved [user]
  (set
   (:solved (from-mongo
             (fetch-one :users
                        :where {:user user}
                        :only [:solved])))))

(defn get-problem [x]
  (from-mongo
   (fetch-one :problems :where {:_id x})))

(defn get-problem-list []
  (from-mongo
   (fetch :problems
          :only [:_id :title :tags :times-solved]
          :sort {:id 1})))

(defn mark-completed [id code & [user]]
  (let [user (or user (session/session-get :user))
        gist-link (html [:div.share
                         [:a.novisited {:href "/share/code"} "Share"]
                         " this solution with your friends!"])

        message
        (if user
          (do
            (when (not-any? #{id} (get-solved user))
              (update! :users {:user user} {:$addToSet {:solved id}})
              (update! :problems {:_id id} {:$inc {:times-solved 1}}))
            "Congratulations, you've solved the problem!")
          "You've solved the problem! If you log in we can track your progress.")]
    (session/session-put! :code [id code])
    (flash-msg (str message " " gist-link) "/problems")))

(def restricted-list ['use 'require 'in-ns 'future 'agent 'send 'send-off 'pmap 'pcalls])

(defn get-tester [restricted]
  (into secure-tester (concat restricted-list (map symbol restricted))))

(def sb (sandbox*))

(defn run-code [id raw-code]
  (let [code (.trim raw-code)
p (get-problem id)
        tests (concat (:tests p) (:secret-tests p))
        func-name (:function-name p)
        sb-tester (get-tester (:restricted p))]
    (if (empty? code)
      (do
(session/flash-put! :code code)
(flash-error "Empty input is not allowed"
(str "/problem/" id)))
      (try
(loop [[test & more] tests]
(if-not test
(mark-completed id code)
(let [testcase (s/replace test "__" (str code))]
(if (sb sb-tester (read-string testcase))
(recur more)
(do
(session/flash-put! :code code)
(flash-error "You failed the unit tests."
(str "/problem/" id)))))))
(catch Exception e
(do
(session/flash-put! :code code)
(flash-error (.getMessage e) (str "/problem/" id))))))))


(def-page code-box [id]
  (let [problem (get-problem (Integer. id))]
    [:div
     [:span {:id "prob-title"} (problem :title)]
     [:hr]
     [:div {:id "prob-desc"}
      (problem :description)[:br]
      [:div {:id "testcases"}
       (for [test (:tests problem)]
         [:li {:class "testcase"} test])]
      (if-let [restricted (problem :restricted)]
        [:div {:id "restrictions"}
         [:u "Special Restrictions"] [:br]
         (map (partial vector :li) restricted)])]
     [:div
      [:b "Enter your code:" [:br]
       [:span {:class "error"} (session/flash-get :error)]]]
     (form-to [:post "/run-code"]
              (text-area {:id "code-box"
                          :spellcheck "false"}
                         :code (session/flash-get :code))
              (hidden-field :id id)
              [:br]
              [:button.large {:type "submit"} "Run"])]))

(def-page problem-page []
  [:div.congrats (session/flash-get :message)]
  [:table#problem-table.my-table
   [:thead
    [:tr
     [:th "Title"]
     [:th "Tags"]
     [:th "Count"]
     [:th "Solved?"]]]
   (let [solved (get-solved (session/session-get :user))
         problems (get-problem-list)]
     (map-indexed
      (fn [x {:keys [title times-solved tags], id :_id}]
        [:tr (row-class x)
         [:td.titlelink
          [:a {:href (str "/problem/" id)}
           title]]
         [:td.centered
          (s/join " " (map #(str "<span class='tag'>" % "</span>")
                           tags))]
         [:td.centered (int times-solved)]
         [:td.centered
          [:img {:src (if (contains? solved id)
                        "/images/checkmark.png"
                        "/images/empty-sq.png")}]]])
      problems))])

(defroutes problems-routes
  (GET "/problems" [] (problem-page))
  (GET "/problem/:id" [id] (code-box id))
  (POST "/run-code" {{:strs [id code]} :form-params}
        (run-code (Integer. id) code)))
Something went wrong with that request. Please try again.