forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttempt.scala
78 lines (65 loc) · 1.85 KB
/
Attempt.scala
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
package lila.puzzle
import org.joda.time.DateTime
case class Attempt(
id: String, // userId/puzzleId
puzzleId: PuzzleId,
userId: String,
date: DateTime,
win: Boolean,
time: Int, // millis
puzzleRating: Int,
puzzleRatingDiff: Int,
userRating: Int,
userRatingDiff: Int,
vote: Option[Boolean]) {
def seconds = math.round(time / 1000)
def loss = !win
def userPostRating = userRating + userRatingDiff
def puzzlePostRating = puzzleRating + puzzleRatingDiff
}
object Attempt {
def makeId(puzzleId: PuzzleId, userId: String) = s"$puzzleId/$userId"
object BSONFields {
val id = "_id"
val puzzleId = "p"
val userId = "u"
val date = "d"
val win = "w"
val time = "t"
val puzzleRating = "pr"
val puzzleRatingDiff = "pd"
val userRating = "ur"
val userRatingDiff = "ud"
val vote = "v"
}
import reactivemongo.bson._
import lila.db.BSON
import BSON.BSONJodaDateTimeHandler
implicit val attemptBSONHandler = new BSON[Attempt] {
import BSONFields._
def reads(r: BSON.Reader): Attempt = Attempt(
id = r str id,
puzzleId = r int puzzleId,
userId = r str userId,
date = r.get[DateTime](date),
win = r bool win,
time = r int time,
puzzleRating = r int puzzleRating,
puzzleRatingDiff = r int puzzleRatingDiff,
userRating = r int userRating,
userRatingDiff = r int userRatingDiff,
vote = r boolO vote)
def writes(w: BSON.Writer, o: Attempt) = BSONDocument(
id -> o.id,
puzzleId -> o.puzzleId,
userId -> o.userId,
date -> o.date,
win -> o.win,
time -> w.int(o.time),
puzzleRating -> w.int(o.puzzleRating),
puzzleRatingDiff -> w.int(o.puzzleRatingDiff),
userRating -> w.int(o.userRating),
userRatingDiff -> w.int(o.userRatingDiff),
vote -> o.vote)
}
}