Skip to content

Instantly share code, notes, and snippets.

@Quiark
Created February 22, 2011 23:50
Show Gist options
  • Save Quiark/839687 to your computer and use it in GitHub Desktop.
Save Quiark/839687 to your computer and use it in GitHub Desktop.
Ukázka kódu tresky plesky
(defn plesk
([request item] (plesk request item {}))
([request item kwargs]
(let [rnd-box (fn [word]
(let [rnd-gen (new Random (.hashCode word))
gen-color (fn [] (+ 155 (.nextInt rnd-gen 100)))]
[:span {:style
(str "padding: "
(.nextFloat rnd-gen) "ex "
(.nextFloat rnd-gen) "ex "
(.nextFloat rnd-gen) "ex "
(.nextFloat rnd-gen) "ex;"
"background-color: rgb(" (gen-color) ", " (gen-color) ", " (gen-color) ");")}
word]))
voted (plesk/voted-for request item)]
[:div
(when (:err kwargs)
[:div.error (:err kwargs)])
[:div {:class (if (:box kwargs)
"plesk box plesk_standalone"
"plesk")}
[:a {:title (if voted "Zrušit, že se mi líbí." "Líbí se mi")
:rel "nofollow"
:pleskid (ds/ent->url item)
:class "plesk_score button_invis "
:href (str "/plesk/" (ds/ent->url item)
(if voted "/unvote" "/upvote"))}
[:span.plesk_score_num
(:upvotes item) "x"]
[:img {:src (if voted "/design/down_btn1.png" "/design/up_btn1.png")}]]
[:div {:class "plesk_tools"}
(mklink-to {:rel "nofollow" :class "block button_invis"}
(str "/plesk/" (ds/ent->url item) "/report")
[:img {:src "/design/flag1.png"}] " nevhodné")
(mklink-to {:rel "nofollow" :class "block button_invis"
:onclick (str "facebook_share('" (plesk-link request (ds/ent->url item)) "')")}
"#"
[:img {:src "/images/fb.ico"}] " sdílet")]
[:div.plesk_data
[:div.plesk_text
(seq (join (map rnd-box (:words item)) " ")) "."]]
(if (:main kwargs)
(list
[:p "Tento plesk je již hotový. Neváhejte a " (mklink-to (mvc-url "plesk" "random") "Zapojte se do jiného")]
[:p "Na " (mklink-to "/" "hlavní stránce") " se můžete podívat na další výtvory." ]))]])))
(ns tresky.plesk
(:use
tresky.tools
compojure.core
hiccup.core)
(:require
[clojure.set :as sets]
[tresky.datastore :as ds]
[tresky.users :as usr])
(:import
(com.google.appengine.api.datastore Query Entity DatastoreServiceFactory FetchOptions$Builder)))
(def DONEPLESK_COOKIE "doneplesk_")
(defn voted-for [request item]
(> (ds/count-entities
(ds/query "upvote" [:where [= "author" (usr/user-id request)]] (:key item)))
0))
(defn unvote [request item]
(ds/with-txn [txn]
(let [vote (ds/find-one (ds/query "upvote" [:where [= "author" (usr/user-id request)]] (:key item)))]
(ds/delete txn (:key vote))
(ds/put txn (dec-prop item :upvotes) nil))))
(defn upvote [request item]
(ds/with-txn [txnn]
(ds/put txnn {:kind "upvote" :author (usr/user-id request)} (:key item))
(ds/put txnn (inc-prop item :upvotes) nil)))
(def sentence-elements
["Kdo" "Kde" "Jak" "Co" "Koho"])
(def elements-help
["Kdo to udělal (podmět)?" "Kde to dělal?" "Jak se to stalo?" "Co udělal (sloveso)?" "Koho, co (předmět)?"])
(defn done [item]
"Returns true if the plesk has all the words. Does not use the :finished property."
(>= (count (:words item)) (count sentence-elements)))
(defn update [request id addword]
"Adds a word to the plesk."
(ds/with-txn [txn]
(let [item (ds/get txn (ds/url->key id))]
(cond
(done item) (throw (new Exception "Bohužel tento Plesk už byl dokončen."))
true (let [new-item (assoc item
:words (conj (:words item) addword)
:last-update (new java.util.Date))
old-author (:author new-item)
with-author (assoc new-item
:author (conj (if (coll? old-author)
old-author
[old-author])
(usr/user-id request))) ]
(ds/put txn (if (done with-author)
(assoc with-author :finished true)
with-author)
nil))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment