forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RatingChartApi.scala
59 lines (49 loc) · 1.62 KB
/
RatingChartApi.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
package lila.history
import scala.concurrent.duration._
import scala.math.round
import org.joda.time.DateTime
import play.api.libs.json._
import lila.rating.{ Glicko, PerfType }
import lila.user.{ User, Perfs }
final class RatingChartApi(
historyApi: HistoryApi,
mongoCache: lila.memo.MongoCache.Builder,
cacheTtl: FiniteDuration) {
def apply(user: User): Fu[Option[String]] = cache(user) map { chart =>
chart.nonEmpty option chart
}
private val cache = mongoCache[User, String](
prefix = "history:rating",
f = (user: User) => build(user) map (~_),
maxCapacity = 64,
timeToLive = cacheTtl,
keyToString = _.id)
private val columns = Json stringify {
Json.arr(
Json.arr("string", "Date"),
Json.arr("number", "Standard"),
Json.arr("number", "Opponent Rating"),
Json.arr("number", "Average")
)
}
private def build(user: User): Fu[Option[String]] = {
def ratingsMapToJson(perfType: PerfType, ratingsMap: RatingsMap) = Json obj (
"name" -> perfType.name,
"points" -> ratingsMap.map {
case (days, rating) =>
val date = user.createdAt plusDays days
Json.arr(date.getYear, date.getMonthOfYear - 1, date.getDayOfMonth, rating)
}
)
historyApi get user.id map2 { (history: History) =>
Json stringify {
Json.toJson {
import lila.rating.PerfType._
List(Bullet, Blitz, Classical, Correspondence, Chess960, KingOfTheHill, ThreeCheck, Antichess, Atomic, Horde, RacingKings, Crazyhouse, Puzzle) map { pt =>
ratingsMapToJson(pt, history(pt))
}
}
}
}
}
}