Skip to content

Commit

Permalink
Fixes #11 and #13
Browse files Browse the repository at this point in the history
  • Loading branch information
Sigurhjörtur Snorrason committed Dec 14, 2015
1 parent 7c4c662 commit 243bd52
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/ratings/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
:headers {"Content-Type" "application/json"}})
(POST "/addgame" {{:strs [white-id black-id result] :as params} :form-params session :session headers :headers}
(try
(str (glicko/score-game white-id black-id (Integer. result)))
(json/generate-string (glicko/score-game white-id black-id (Integer. result)))
(redirect (get headers "referer"))
(catch com.fasterxml.jackson.core.JsonParseException e
(.printStackTrace e)
Expand All @@ -55,7 +55,7 @@

(POST "/addplayer" {{:strs [name] :as params} :form-params session :session headers :headers}
(try
(str (glicko/add-new-player name))
(json/generate-string (glicko/add-new-player name))
(redirect (get headers "referer"))
(catch com.fasterxml.jackson.core.JsonParseException e
(.printStackTrace e)
Expand Down Expand Up @@ -101,7 +101,7 @@
{:status 500
:headers {"Content-Type" "application/json"}
:body (json/generate-string {:error (str "An unexpected error occurred! ")})})))
(DELETE "/delete-player" {{:strs [_id] :as params} :form-params session :seesion headers :headers}
(DELETE "/delete-game" {{:strs [_id] :as params} :form-params session :seesion headers :headers}
(try
(str (glicko/delete-game _id))
(redirect (get headers "referer"))
Expand Down
30 changes: 17 additions & 13 deletions src/ratings/glicko.clj
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,22 @@
(mc/find-map-by-id (get-db) "players" (ObjectId. id)))

(defn add-game [{rating1 :rating rd1 :rating-rd id1 :_id} {rating2 :rating rd2 :rating-rd id2 :_id} result]
(mc/insert (get-db) "games" (assoc nil
:_id (ObjectId.)
:white (str id1)
:black (str id2)
:result result
:white-old-rating rating1
:white-old-rd rd1
:black-old-rating rating2
:black-old-rd rd2
:added (c/to-string (t/now)))))
(let [game (assoc nil
:_id (ObjectId.)
:white (str id1)
:black (str id2)
:result result
:white-old-rating rating1
:white-old-rd rd1
:black-old-rating rating2
:black-old-rd rd2
:added (c/to-string (t/now)))]
(mc/insert (get-db) "games" game)
game))

(defn score-game [white-id black-id result]
(let [player1 (get-player-from-id white-id)
player2 (get-player-from-id black-id)]
(add-game player1 player2 result)
(cond (= 1 result)
(do (update-rating player1 player2 1)
(update-rating player2 player1 0))
Expand All @@ -102,10 +103,13 @@
(update-rating player1 player2 0))
:else
(do (update-rating player1 player2 0.5)
(update-rating player2 player1 0.5)))))
(update-rating player2 player1 0.5)))
(add-game player1 player2 result)))

(defn add-new-player [name]
(mc/insert (get-db) "players" (assoc nil :_id (ObjectId.) :name name :rating 1200 :rating-rd 350)))
(let [player (assoc nil :_id (ObjectId.) :name name :rating 1200 :rating-rd 350)]
(mc/insert (get-db) "players" player)
player))


(defn get-latest-game-between-players [white black games latest]
Expand Down

0 comments on commit 243bd52

Please sign in to comment.