-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
CoordinateApi.scala
48 lines (41 loc) · 1.3 KB
/
CoordinateApi.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
package lila.coordinate
import chess.{ ByColor, Color }
import reactivemongo.api.bson.*
import lila.db.dsl.{ *, given }
final class CoordinateApi(scoreColl: Coll)(using Executor):
private given BSONDocumentHandler[Score] = Macros.handler[Score]
def getScore(userId: UserId): Fu[Score] =
scoreColl.byId[Score](userId).dmap(_ | Score(userId))
def addScore(mode: CoordMode, color: Color, hits: Int)(using me: MyId): Funit =
scoreColl.update
.one(
$id(me),
$push(
$doc(
s"${color.name}${(mode == CoordMode.nameSquare).so("NameSquare")}" -> $doc(
"$each" -> $arr(hits),
"$slice" -> -20
)
)
),
upsert = true
)
.void
def bestScores(userIds: List[UserId]): Fu[Map[UserId, ByColor[Int]]] =
scoreColl
.aggregateList(maxDocs = Int.MaxValue, _.sec): framework =>
import framework.*
Match($doc("_id".$in(userIds))) -> List(
Project(
$doc(
"white" -> $doc("$max" -> "$white"),
"black" -> $doc("$max" -> "$black")
)
)
)
.map:
_.flatMap: doc =>
doc.getAsOpt[UserId]("_id").map {
_ -> ByColor(~doc.int("white"), ~doc.int("black"))
}
.toMap