Skip to content
Browse files

Updated user ranking, allowing for ties.

  • Loading branch information...
1 parent cae9c94 commit 0d6e51bb568cc1243271743a4a44a193780929f1 @amcnamara committed Sep 28, 2011
Showing with 56 additions and 46 deletions.
  1. +8 −0 resources/public/css/style.css
  2. +16 −16 resources/public/script/foreclojure.js
  3. +27 −30 src/foreclojure/users.clj
  4. +5 −0 src/foreclojure/utils.clj
View
8 resources/public/css/style.css
@@ -368,6 +368,14 @@ table.my-table td {
font-size: 12px;
}
+td.oddrank {
+ background-color: #DDF !important;
+}
+
@amalloy
amalloy added a note Sep 28, 2011

Why do we need !important? This is pretty vile to use except as a last resort.

@amcnamara
Owner
amcnamara added a note Sep 28, 2011

agreed. but otherwise it get overwritten by the jquery datatables stylesheet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+td.evenrank {
+ background-color: #EEF !important;
+}
+
td.title-link {
font-size: 16px;
}
View
32 resources/public/script/foreclojure.js
@@ -43,35 +43,35 @@ function configureDataTables(){
$('#problem-table').dataTable( {
"iDisplayLength": 25,
- "aaSorting": [[5, "desc"], [ 1, "asc" ], [ 4, "desc" ]],
+ "aaSorting": [[5, "desc"], [1, "asc"], [4, "desc"]],
"aoColumns": [
- null,
- { "sType": "difficulty" },
- null,
- null,
- null,
- { "sType": "string" }
+ {"sType" : "string"},
+ {"sType" : "difficulty"},
+ {"sType" : "string"},
+ {"sType" : "string"},
+ {"sType" : "numeric"},
+ {"sType" : "string"}
]
} );
$('#unapproved-problems').dataTable( {
"iDisplayLength": 25,
- "aaSorting": [[ 3, "desc" ]],
+ "aaSorting": [[3, "desc"]],
"aoColumns": [
- null,
- null,
- null,
- null
+ {"sType": "string"},
+ {"sType": "string"},
+ {"sType": "string"},
+ {"sType": "string"}
]
} );
$('#user-table').dataTable( {
"iDisplayLength":100,
- "aaSorting": [[ 0, "asc" ]],
+ "aaSorting": [[0, "asc"]],
"aoColumns": [
- null,
- null,
- null
+ {"sType": "numeric"},
+ {"sType": "string"},
+ {"sType": "numeric"}
]
} );
}
View
57 src/foreclojure/users.clj
@@ -1,7 +1,7 @@
(ns foreclojure.users
(:require [ring.util.response :as response]
[sandbar.stateful-session :as session])
- (:use [foreclojure.utils :only [from-mongo row-class get-user with-user]]
+ (:use [foreclojure.utils :only [from-mongo row-class rank-class get-user with-user]]
[foreclojure.template :only [def-page content-page]]
[foreclojure.config :only [config repo-url]]
[somnium.congomongo :only [fetch-one fetch update!]]
@@ -19,35 +19,32 @@
:where {:user name}
:only [:_id])))
-(def sort-by-solved-and-date (juxt (comp count :solved) :last-login))
-
-(defn users-sort [users]
- (reverse (sort-by sort-by-solved-and-date users)))
-
(defn get-users []
- (let [users (from-mongo
- (fetch :users
- :only [:user :solved :contributor]))
- sortfn (comp - count :solved)]
- (sort-by sortfn users)))
-
-(defn get-user-with-ranking [username, users]
- (when username
- (let [total (count users)
- users-with-rankings (map-indexed
- (fn [idx itm]
- (assoc itm :rank
- (str (inc idx) " out of " total)))
- users)]
- (first
- (filter #(= username (% :user)) users-with-rankings)))))
+ (from-mongo
+ (fetch :users
+ :only [:user :solved :contributor])))
+
+(defn get-ranked-users []
@amcnamara
Owner
amcnamara added a note Sep 28, 2011

@amalloy please review this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ (let [users (get-users)]
+ (reduce into []
+ (map-indexed
+ (fn [rank tied-users]
+ (for [user (sort-by :user tied-users)]
+ (assoc user :rank (inc rank))))
+ (map second
+ (sort-by (comp - first)
+ (group-by (comp count :solved)
+ (filter #(-> % :solved coll?)
+ users))))))))
@amalloy
amalloy added a note Sep 28, 2011

Forcing it into a vector is weird. Rather than (reduce into [] (map-indexed f xs)), I think you probably want (mapcat f (range) xs) or something like that.

@amalloy
amalloy added a note Sep 28, 2011

Does this filter mean that users who have solved no problems are not displayed at all? I guess I can see that this would make some sense, but it seems like an unrelated change.

@amalloy
amalloy added a note Sep 28, 2011

(map second m) is just (vals m) since m is a map.

Edit: I guess it's not a map, since you're doing a sort-by first. Never mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
(defn get-top-100-and-current-user [username]
- (let [users (get-users)
- user-ranking (get-user-with-ranking username users)]
- {:user-ranking user-ranking
- :top-100 (take 100 users)}))
-
+ (let [ranked-users (get-ranked-users)
+ this-user (first (filter (fn [{:keys [user]}]
+ (= username user))
@amalloy
amalloy added a note Sep 28, 2011

(comp #{username} :user)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ ranked-users))
+ this-user-ranking (assoc this-user :rank (str (:rank this-user "?") " out of " (count ranked-users)))]
@amalloy
amalloy added a note Sep 28, 2011

(update-in this-user [:rank] #(str (or % "?") " out of " (count ranked-users)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {:user-ranking this-user-ranking
+ :top-100 (take 100 ranked-users)}))
(defn golfer? [user]
(some user golfer-tags))
@@ -86,9 +83,9 @@
[:th {:style "width: 40px;"} "Rank"]
[:th "Username"]
[:th "Problems Solved"]]]
- (map-indexed (fn [rownum {:keys [user contributor solved]}]
+ (map-indexed (fn [rownum {:keys [rank user contributor solved]}]
[:tr (row-class rownum)
- [:td (inc rownum)]
+ [:td (rank-class rank) rank]
[:td
(when contributor [:span.contributor "* "])
[:a.user-profile-link {:href (str "/user/" user)} user]]
@@ -101,7 +98,7 @@
(content-page
{:heading "All 4Clojure Users"
:sub-heading (list [:span.contributor "*"] " " (link-to repo-url "4clojure contributor"))
- :main (generate-user-list (get-users))})})
+ :main (generate-user-list (get-ranked-users))})})
(def-page top-users-page []
(let [username (session/session-get :user)
View
5 src/foreclojure/utils.clj
@@ -191,3 +191,8 @@
{:class (if (even? x)
"evenrow"
"oddrow")})
+
+(defn rank-class [x]
+ {:class (if (even? x)
+ "evenrank"
+ "oddrank")})

0 comments on commit 0d6e51b

Please sign in to comment.
Something went wrong with that request. Please try again.