apply game termination to relay studies

pull/3627/head
Thibault Duplessis 2017-10-05 20:13:29 -05:00
parent 793637eb6b
commit c6ce54c02c
4 changed files with 49 additions and 17 deletions

View File

@ -167,6 +167,7 @@ private object RelayFetch {
} yield RelayGame(
tags = res.tags,
root = res.root,
end = res.end,
whiteName = RelayGame.PlayerName(white),
blackName = RelayGame.PlayerName(black)
)

View File

@ -1,19 +1,22 @@
package lila.relay
import chess.format.pgn.Tags
import lila.study.{ Chapter, Node }
import lila.study.{ Chapter, Node, PgnImport }
case class RelayGame(
tags: Tags,
root: Node.Root,
whiteName: RelayGame.PlayerName,
blackName: RelayGame.PlayerName
blackName: RelayGame.PlayerName,
end: Option[PgnImport.End]
) {
lazy val id = RelayGame.makeId(whiteName.value, blackName.value, tags(_.Event))
def is(c: Chapter) = id == RelayGame.makeId(~c.tags(_.White), ~c.tags(_.Black), c.tags(_.Event))
def finished = end.isDefined
override def toString = id
}

View File

@ -33,7 +33,21 @@ private final class RelaySync(
}
}
private def updateChapter(study: Study, chapter: Chapter, game: RelayGame): Fu[NbMoves] = {
private def updateChapter(study: Study, chapter: Chapter, game: RelayGame): Fu[NbMoves] =
updateChapterStatus(study, chapter, game) >>
updateChapterTree(study, chapter, game)
private def updateChapterStatus(study: Study, chapter: Chapter, game: RelayGame): Funit =
game.end ?? { end =>
(chapter.tags(_.Result).fold(true)(end.resultText !=)) ?? studyApi.setTag(
userId = chapter.ownerId,
studyId = study.id,
lila.study.actorApi.SetTag(chapter.id, "result", end.resultText),
uid = socketUid
)
}
private def updateChapterTree(study: Study, chapter: Chapter, game: RelayGame): Fu[NbMoves] = {
game.root.mainline.foldLeft(Path.root -> none[Node]) {
case ((parentPath, None), gameNode) =>
val path = parentPath + gameNode

View File

@ -16,7 +16,15 @@ object PgnImport {
case class Result(
root: Node.Root,
variant: chess.variant.Variant,
tags: Tags
tags: Tags,
end: Option[End]
)
case class End(
status: chess.Status,
winner: Option[chess.Color],
resultText: String,
statusText: String
)
def apply(pgn: String, contributors: List[LightUser]): Valid[Result] =
@ -50,15 +58,28 @@ object PgnImport {
}
)
)
val end: Option[End] = {
(if (game.finished) game.status.some else result.map(_.status))
.filter(chess.Status.Aborted <=).map { status =>
val winner = game.winnerColor orElse result.flatMap(_.winner)
End(
status = status,
winner = winner,
resultText = chess.Color.showResult(winner),
statusText = lila.game.StatusText(status, winner, game.variant)
)
}
}
val commented =
if (root.mainline.lastOption.??(_.isCommented)) root
else endComment(prep).fold(root) { comment =>
else end.map(endComment(prep)).fold(root) { comment =>
root updateMainlineLast { _.setComment(comment) }
}
Result(
root = commented,
variant = game.variant,
tags = PgnTags(parsedPgn.tags)
tags = PgnTags(parsedPgn.tags),
end = end
)
}
}
@ -73,18 +94,11 @@ object PgnImport {
} getOrElse Comment.Author.External(a)
}
private def endComment(prep: Preprocessed): Option[Comment] = {
private def endComment(prep: Preprocessed)(end: End): Comment = {
import lila.tree.Node.Comment
import prep._
val winner = game.winnerColor orElse result.flatMap(_.winner)
val resultText = chess.Color.showResult(winner)
(if (game.finished) game.status.some else result.map(_.status)) flatMap { status =>
(status >= chess.Status.Aborted) option {
val statusText = lila.game.StatusText(status, winner, game.variant)
val text = s"$resultText $statusText"
Comment(Comment.Id.make, Comment.Text(text), Comment.Author.Lichess)
}
}
import end._
val text = s"$resultText $statusText"
Comment(Comment.Id.make, Comment.Text(text), Comment.Author.Lichess)
}
private def makeVariations(sans: List[San], game: chess.Game, annotator: Option[Comment.Author]) =