forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClasProgress.scala
159 lines (145 loc) · 4.44 KB
/
ClasProgress.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package lila.clas
import java.time.Duration
import reactivemongo.api.*
import reactivemongo.api.bson.*
import lila.db.dsl.{ *, given }
import lila.game.{ Game, GameRepo }
import lila.puzzle.PuzzleRound
import lila.rating.PerfType
import lila.user.User
case class ClasProgress(
perfType: PerfType,
days: Int,
students: Map[UserId, StudentProgress]
):
def apply(user: User.WithPerf) =
students.getOrElse(
user.id,
StudentProgress(
nb = 0,
rating = (user.perf.intRating, user.perf.intRating),
wins = 0,
millis = 0
)
)
def isPuzzle = perfType == PerfType.Puzzle
case class StudentProgress(
nb: Int,
wins: Int,
millis: Long,
rating: (IntRating, IntRating)
):
def ratingProgress = (rating._2 - rating._1) into IntRatingDiff
def winRate = if nb > 0 then wins * 100 / nb else 0
def duration = Duration.ofMillis(millis)
final class ClasProgressApi(
gameRepo: GameRepo,
perfsRepo: lila.user.UserPerfsRepo,
historyApi: lila.history.HistoryApi,
puzzleColls: lila.puzzle.PuzzleColls,
studentCache: ClasStudentCache
)(using Executor):
case class PlayStats(nb: Int, wins: Int, millis: Long)
def apply(perfType: PerfType, days: Int, students: List[Student.WithUser]): Fu[ClasProgress] =
val users = students.map(_.user)
val userIds = users.map(_.id)
val playStatsFu =
if perfType == PerfType.Puzzle
then getPuzzleStats(userIds, days)
else getGameStats(perfType, userIds, days)
val progressesFu = for
usersWithPerf <- perfsRepo.withPerf(users, perfType)
progresses <- historyApi.progresses(usersWithPerf, perfType, days)
yield progresses
playStatsFu zip progressesFu map { case (playStats, progresses) =>
ClasProgress(
perfType,
days,
users zip progresses map { (u, rating) =>
val playStat = playStats get u.id
u.id -> StudentProgress(
nb = playStat.so(_.nb),
rating = rating,
wins = playStat.so(_.wins),
millis = playStat.so(_.millis)
)
} toMap
)
}
private def getPuzzleStats(userIds: List[UserId], days: Int): Fu[Map[UserId, PlayStats]] =
puzzleColls.round:
_.aggregateList(Int.MaxValue, _.sec): framework =>
import framework.*
Match(
$doc(
PuzzleRound.BSONFields.user $in userIds,
PuzzleRound.BSONFields.date $gt nowInstant.minusDays(days)
)
) -> List:
GroupField("u")(
"nb" -> SumAll,
"win" -> Sum(
$doc(
"$cond" -> $arr("$w", 1, 0)
)
)
)
.map:
_.flatMap: obj =>
obj.getAsOpt[UserId]("_id") map { id =>
id -> PlayStats(
nb = ~obj.int("nb"),
wins = ~obj.int("win"),
millis = 0
)
}
.toMap
private def getGameStats(
perfType: PerfType,
userIds: List[UserId],
days: Int
): Fu[Map[UserId, PlayStats]] =
import Game.{ BSONFields as F }
import lila.game.Query
gameRepo.coll
.aggregateList(maxDocs = Int.MaxValue, _.sec): framework =>
import framework.*
Match(
$doc(
F.playerUids $in userIds,
Query.createdSince(nowInstant minusDays days),
F.perfType -> perfType.id
)
) -> List(
Project(
$doc(
F.playerUids -> true,
F.winnerId -> true,
"ms" -> $doc("$subtract" -> $arr(s"$$${F.movedAt}", s"$$${F.createdAt}")),
F.id -> false
)
),
UnwindField(F.playerUids),
Match($doc(F.playerUids $in userIds)),
GroupField(F.playerUids)(
"nb" -> SumAll,
"win" -> Sum(
$doc(
"$cond" -> $arr($doc("$eq" -> $arr("$us", "$wid")), 1, 0)
)
),
"ms" -> SumField("ms")
)
)
.map:
_.flatMap: obj =>
obj.getAsOpt[UserId](F.id) map { id =>
id -> PlayStats(
nb = ~obj.int("nb"),
wins = ~obj.int("win"),
millis = ~obj.long("ms")
)
}
.toMap
private[clas] def onFinishGame(game: lila.game.Game): Unit =
if game.userIds.exists(studentCache.isStudent) then gameRepo.denormalizePerfType(game)