-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
JsonView.scala
65 lines (50 loc) · 1.91 KB
/
JsonView.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
package lila.perfStat
import play.api.libs.json.*
import lila.common.Json.given
import lila.core.LightUser
import lila.rating.{ Glicko, Perf }
import lila.core.i18n.Translate
import lila.rating.PerfType
final class JsonView(getLightUser: LightUser.GetterSync):
import JsonView.given
private given userIdWriter: OWrites[UserId] = OWrites { u =>
val light = getLightUser(u)
Json.obj(
"id" -> u.value,
"name" -> light.fold(u.into(UserName))(_.name),
"title" -> light.flatMap(_.title)
)
}
given Writes[RatingAt] = Json.writes
given Writes[GameAt] = Json.writes
given Writes[Result] = Json.writes
given Writes[Results] = Json.writes
given Writes[Streak] = Json.writes
given Writes[Streaks] = Json.writes
given Writes[PlayStreak] = Json.writes
given Writes[ResultStreak] = Json.writes
given Writes[Count] = Json.writes
given (using Translate): Writes[PerfStat] = Json.writes
def apply(data: PerfStatData)(using Translate) =
Json.obj(
"user" -> data.user.user,
"perf" -> data.user.perfs(data.stat.perfType),
"rank" -> data.rank,
"percentile" -> data.percentile,
"stat" -> data.stat
)
object JsonView:
import lila.rating.Glicko.glickoWrites
private given Writes[Instant] = Writes: d =>
JsString(isoDateTimeFormatter.print(d))
given OWrites[User] = OWrites: u =>
Json.obj("name" -> u.username)
given OWrites[Perf] = OWrites: p =>
Json.obj("glicko" -> p.glicko, "nb" -> p.nb, "progress" -> p.progress)
private given Writes[Avg] = Writes: a =>
JsNumber(scalalib.Maths.roundDownAt(a.avg, 2))
given (using Translate): OWrites[PerfType] = OWrites: pt =>
Json.obj(
"key" -> pt.key,
"name" -> pt.trans
)