forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfStat.scala
More file actions
203 lines (186 loc) · 6.22 KB
/
Copy pathPerfStat.scala
File metadata and controls
203 lines (186 loc) · 6.22 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package lila.perfStat
import chess.IntRating
import reactivemongo.api.bson.Macros.Annotations.Key
import java.time.Duration
import scalalib.HeapSort
import lila.rating.PerfType
import lila.rating.PerfType.GamePerf
extension (p: Pov) def loss = p.game.winner.map(_.color != p.color)
case class PerfStat(
@Key("_id") id: String, // userId/perfId
userId: UserId,
perfType: PerfType,
highest: Option[RatingAt],
lowest: Option[RatingAt],
bestWins: Results,
worstLosses: Results,
count: Count,
resultStreak: ResultStreak,
playStreak: PlayStreak
):
def perfKey = perfType.key
def agg(pov: Pov) =
if !pov.game.finished then this
else
val thisYear = pov.game.createdAt.isAfter(nowInstant.minusYears(1))
copy(
highest = RatingAt.agg(highest, pov, 1),
lowest = if thisYear then RatingAt.agg(lowest, pov, -1) else lowest,
bestWins = if ~pov.win then bestWins.agg(pov, 1) else bestWins,
worstLosses = if thisYear && ~pov.loss then worstLosses.agg(pov, -1) else worstLosses,
count = count(pov),
resultStreak = resultStreak.agg(pov),
playStreak = playStreak.agg(pov)
)
def userIds = bestWins.userIds ::: worstLosses.userIds
object PerfStat:
type Getter = (User, PerfType) => Fu[PerfStat]
def makeId(userId: UserId, perf: GamePerf) = s"$userId/${perf.id}"
def init(userId: UserId, perf: GamePerf) =
PerfStat(
id = makeId(userId, perf),
userId = userId,
perfType = perf,
highest = none,
lowest = none,
bestWins = Results(Nil),
worstLosses = Results(Nil),
count = Count.init,
resultStreak = ResultStreak(win = Streaks.init, loss = Streaks.init),
playStreak = PlayStreak(nb = Streaks.init, time = Streaks.init, lastDate = none)
)
case class ResultStreak(win: Streaks, loss: Streaks):
def agg(pov: Pov) =
copy(
win = win.continueOrReset(~pov.win, pov)(1),
loss = loss.continueOrReset(~pov.loss, pov)(1)
)
case class PlayStreak(nb: Streaks, time: Streaks, lastDate: Option[Instant]):
def agg(pov: Pov) =
pov.game.durationSeconds.fold(this) { seconds =>
val cont = seconds < 3 * 60 * 60 && isContinued(pov.game.createdAt)
copy(
nb = nb.continueOrStart(cont, pov)(1),
time = time.continueOrStart(cont, pov)(seconds),
lastDate = pov.game.movedAt.some
)
}
def checkCurrent =
if isContinued(nowInstant) then this
else copy(nb = nb.reset, time = time.reset)
private def isContinued(at: Instant) =
lastDate.forall: ld =>
at.isBefore(ld.plusMinutes(PlayStreak.expirationMinutes))
object PlayStreak:
val expirationMinutes = 60
case class Streaks(cur: Streak, max: Streak):
def continueOrReset(cont: Boolean, pov: Pov)(v: Int) =
copy(cur = cur.continueOrReset(cont, pov)(v)).setMax
def continueOrStart(cont: Boolean, pov: Pov)(v: Int) =
copy(cur = cur.continueOrStart(cont, pov)(v)).setMax
def reset = copy(cur = Streak.init)
private def setMax = copy(max = if cur.v >= max.v then cur else max)
object Streaks:
val init = Streaks(Streak.init, Streak.init)
case class Streak(v: Int, from: Option[GameAt], to: Option[GameAt]):
def continueOrReset(cont: Boolean, pov: Pov)(v: Int) =
if cont then inc(pov, v) else Streak.init
def continueOrStart(cont: Boolean, pov: Pov)(v: Int) =
if cont then inc(pov, v)
else
val at = GameAt(pov.game.createdAt, pov.gameId).some
val end = GameAt(pov.game.movedAt, pov.gameId).some
Streak(v, at, end)
private def inc(pov: Pov, by: Int) =
val at = GameAt(pov.game.createdAt, pov.gameId).some
val end = GameAt(pov.game.movedAt, pov.gameId).some
Streak(v + by, from.orElse(at), end)
def duration = Duration.ofSeconds(v)
object Streak:
val init = Streak(0, none, none)
case class Count(
all: Int,
rated: Int,
win: Int,
loss: Int,
draw: Int,
tour: Int,
berserk: Int,
opAvg: Avg,
seconds: Int,
disconnects: Int
):
def apply(pov: Pov) =
copy(
all = all + 1,
rated = rated + (if pov.game.rated then 1 else 0),
win = win + (if pov.win.contains(true) then 1 else 0),
loss = loss + (if pov.win.contains(false) then 1 else 0),
draw = draw + (if pov.win.isEmpty then 1 else 0),
tour = tour + (if pov.game.isTournament then 1 else 0),
berserk = berserk + (if pov.player.berserk then 1 else 0),
opAvg = pov.opponent.stableRating.fold(opAvg)(r => opAvg.agg(r.value)),
seconds = seconds + (pov.game.durationSeconds match
case Some(s) if s <= 3 * 60 * 60 => s
case _ => 0),
disconnects = disconnects + {
if ~pov.loss && pov.game.status == chess.Status.Timeout then 1 else 0
}
)
def duration = Duration.ofSeconds(seconds)
object Count:
val init = Count(
all = 0,
rated = 0,
win = 0,
loss = 0,
draw = 0,
tour = 0,
berserk = 0,
opAvg = Avg(0, 0),
seconds = 0,
disconnects = 0
)
case class Avg(avg: Double, pop: Int):
def agg(v: Int) =
copy(
avg = ((avg * pop) + v) / (pop + 1),
pop = pop + 1
)
case class GameAt(at: Instant, gameId: GameId)
object GameAt:
def agg(pov: Pov) = GameAt(pov.game.movedAt, pov.gameId)
case class RatingAt(int: IntRating, at: Instant, gameId: GameId)
object RatingAt:
def agg(cur: Option[RatingAt], pov: Pov, comp: Int) =
pov.player.stableRatingAfter
.filter: r =>
cur.forall: c =>
r.value.compare(c.int.value) == comp
.map:
RatingAt(_, pov.game.movedAt, pov.gameId)
.orElse(cur)
import reactivemongo.api.bson.Macros.Annotations.Key
case class Result(@Key("opInt") opRating: IntRating, opId: UserId, at: Instant, gameId: GameId)
case class Results(results: List[Result]):
def agg(pov: Pov, comp: Int) = {
for
opId <- pov.opponent.userId
opInt <- pov.opponent.stableRating
if pov.game.rated
if pov.game.bothPlayersHaveMoved
yield Results(
HeapSort.topN(
Result(
opInt,
opId,
pov.game.movedAt,
pov.gameId
) :: results,
Results.nb
)(using Ordering.by[Result, Int](_.opRating.value * comp))
)
} | this
def userIds = results.map(_.opId)
object Results:
val nb = 5