forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerEval.scala
More file actions
192 lines (172 loc) · 6.59 KB
/
Copy pathServerEval.scala
File metadata and controls
192 lines (172 loc) · 6.59 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
package lila.study
import chess.format.pgn.Glyphs
import chess.format.{ Fen, Uci, UciCharPair, UciPath }
import play.api.libs.json.*
import lila.core.perm.Granter
import lila.core.study.GetRelayCrowd
import lila.db.dsl.bsonWriteOpt
import lila.tree.Node.Comment
import lila.tree.{ Advice, Analysis, Branch, Info, Node, Root }
object ServerEval:
final class Requester(
chapterRepo: ChapterRepo,
userApi: lila.core.user.UserApi
)(using Executor):
private val onceEvery = scalalib.cache.OnceEvery[StudyChapterId](5.minutes)
def apply(study: Study, chapter: Chapter, userId: UserId, official: Boolean = false): Funit =
chapter.serverEval
.forall: eval =>
!eval.done && onceEvery(chapter.id)
.so:
for
isOfficial <- fuccess(official) >>|
fuccess(userId.is(UserId.lichess)) >>|
userApi.me(userId).map(_.soUse(Granter.opt(_.Relay)))
_ <- chapterRepo.startServerEval(chapter)
yield lila.common.Bus.pub(
lila.core.fishnet.Bus.StudyChapterRequest(
studyId = study.id,
chapterId = chapter.id,
initialFen = chapter.root.fen.some,
variant = chapter.setup.variant,
moves = chess.format
.UciDump(
moves = chapter.root.mainline.map(_.move.san),
initialFen = chapter.root.fen.some,
variant = chapter.setup.variant,
force960Notation = true
)
.toOption
.map(_.flatMap(chess.format.Uci.apply)) | List.empty,
userId = userId,
official = isOfficial
)
)
final class Merger(
sequencer: StudySequencer,
socket: StudySocket,
chapterRepo: ChapterRepo,
divider: lila.core.game.Divider,
analysisJson: lila.tree.AnalysisJson
)(using Executor, Scheduler):
def apply(analysis: Analysis, complete: Boolean): Funit = analysis.id match
case Analysis.Id.Study(studyId, chapterId) =>
sequencer.sequenceStudyWithChapter(studyId, chapterId):
case Study.WithChapter(_, chapter) =>
for
_ <- complete.so(chapterRepo.completeServerEval(chapter))
_ <- chapter.root.mainline
.zip(analysis.infoAdvices)
.foldM(UciPath.root):
case (path, (node, (info, advOpt))) =>
saveAnalysis(chapter, node, path, info, advOpt)
.andDo(sendProgress(studyId, chapterId, analysis, complete))
.logFailure(logger)
yield ()
case _ => funit
private def saveAnalysis(
chapter: Chapter,
node: Branch,
path: UciPath,
info: Info,
advOpt: Option[Advice]
): Future[UciPath] =
val nextPath = path + node.id
def saveAnalysisLine() =
chapter.root
.nodeAt(path)
.flatMap: parent =>
analysisLine(parent, chapter.setup.variant, info).map: subTree =>
parent.addChild(subTree) -> subTree
.so: (_, subTree) =>
chapterRepo.addSubTree(chapter, subTree, path, none)
def saveInfoAdvice() =
import BSONHandlers.given
import lila.db.dsl.given
import lila.study.Node.BsonFields as F
((info.eval.score.isDefined && node.eval.isEmpty) || (advOpt.isDefined && !node.comments.hasLichessComment))
.so(
chapterRepo
.setNodeValues(
chapter,
nextPath,
List(
F.score -> info.eval.score
.ifTrue:
node.eval.isEmpty ||
advOpt.isDefined && node.comments.findBy(Comment.Author.Lichess).isEmpty
.flatMap(bsonWriteOpt),
F.comments -> advOpt
.map: adv =>
node.comments + Comment(
Comment.Id.make,
adv.makeComment(withEval = false, withBestMove = true).into(Comment.Text),
Comment.Author.Lichess
)
.flatMap(bsonWriteOpt),
F.glyphs -> advOpt
.map(adv => node.glyphs.merge(Glyphs.fromList(List(adv.judgment.glyph))))
.flatMap(bsonWriteOpt)
)
)
)
saveAnalysisLine()
>> saveInfoAdvice().inject(nextPath)
end saveAnalysis
private def analysisLine(root: Node, variant: chess.variant.Variant, info: Info): Option[Branch] =
val (_, reversedGames, error) =
chess.Replay.gameMoveWhileValidReverse(info.variation.take(20), root.fen, variant)
error.foreach(e => logger.info(e.value))
reversedGames match
case Nil => none
case (g, m) :: rest =>
rest
.foldLeft(makeBranch(g, m)):
case (node, (g, m)) =>
makeBranch(g, m).addChild(node)
.some
private def makeBranch(g: chess.Game, m: Uci.WithSan) =
Branch(
id = UciCharPair(m.uci),
ply = g.ply,
move = m,
fen = Fen.write(g),
check = g.situation.check,
crazyData = g.situation.board.crazyData,
clock = none,
forceVariation = false
)
private def sendProgress(
studyId: StudyId,
chapterId: StudyChapterId,
analysis: Analysis,
complete: Boolean
): Funit =
chapterRepo
.byId(chapterId)
.flatMapz: chapter =>
reallySendToChapter(studyId, chapter, complete).mapz:
socket.onServerEval(
studyId,
ServerEval.Progress(
chapterId = chapter.id,
tree = lila.study.TreeBuilder(chapter.root, chapter.setup.variant),
analysis = analysisJson.bothPlayers(chapter.root.ply, analysis),
division = divisionOf(chapter)
)
)
private def reallySendToChapter(studyId: StudyId, chapter: Chapter, complete: Boolean): Fu[Boolean] =
if complete || chapter.relay.isEmpty
then fuTrue
else
lila.common.Bus
.ask[Int]("getRelayCrowd") { GetRelayCrowd(studyId, _) }
.map(_ < 5000)
def divisionOf(chapter: Chapter) =
divider(
id = chapter.id.into(GameId),
sans = chapter.root.mainline.map(_.move.san).toVector,
variant = chapter.setup.variant,
initialFen = chapter.root.fen.some
)
case class Progress(chapterId: StudyChapterId, tree: Root, analysis: JsObject, division: chess.Division)